c語言用棧實現帶括號運算的簡單計算器
阿新 • • 發佈:2019-01-10
c語言用棧實現帶括號的簡單計算器
寫的簡單,棧部分實現比較粗略,可能有沒考慮到的地方
目錄
整體步驟分為三步:
1.棧的簡單實現
2.中綴轉換為字尾
3.字尾運算
1.棧的簡單實現
typedef struct stack{
int value;
char is_op;//這裡新增一個操作符判定主要是有操作符ascii可能與輸入資料重合
stack_t* next;
}STA;
stack_t *stack_init(){
stack_t *p = NULL;
return p;
}
void print_stack(stack_t *stack ){
if(NULL == stack ){
// printf("stack is null! error.\n");
printf("\n\n");
return;
}
if(stack->is_op == 1){
printf("%c ",stack->value);
}else{
printf("%d ",stack->value);
}
print_stack(stack->next);
}
void stack_push(int value, stack_t **stack ,char is_op){
stack_t *p = NULL;
p = (stack_t *)malloc(sizeof(stack_t) * 1);
memset(p,0,sizeof(stack_t));
p->value = value;
p->is_op = is_op;
p->next = *stack;
*stack = p;
}
int stack_pop(stack_t **stack){
if(stack == NULL){
printf("stack is empty!\n");
return 0;
}
stack_t *p = *stack;
int value;
value = (*stack)->value;
*stack = p->next;
free(p);
p=NULL;
return value;
}
int stack_top(stack_t *stack){
return stack->value;
}
2.中綴轉換為字尾
static char op_c[op_row][op_col]={
{'+','-'},
{'*','/','%'},
{'^'},
{'(',')'}
};
int is_operator(char ch){
for(int i = 0;i<op_row;i++){
for(int j = 0;j<op_col;j++){
if(ch == op_c[i][j]){
//printf("%c is op\n",ch);
return true;
}
}
}
// printf("%c is not op\n",ch);
return false;
}
int is_pri(char cur,char befor){
int cur_row = 0;
int befor_row = 0;
for(int i = 0;i<op_row;i++){
for(int j = 0;j<op_col;j++){
if(cur == op_c[i][j]){
cur_row = i;
}
if(befor == op_c[i][j]){
befor_row = i;
}
}
}
return cur_row-befor_row;
}
void suffix(stack_t *input,stack_t **output1){
stack_t *stack_op = stack_init();
stack_t *output = stack_init();
// print_stack(input);
while((input != NULL) && ('=' != (input->value))){
// printf("%d %d\n",input->value,input->is_op);
if(1 == input->is_op){
while(stack_op != NULL){
if((is_pri(input->value,stack_top(stack_op)) <= 0 || ')' == input->value)&& '(' != stack_op->value){
stack_push(stack_pop(&stack_op),&output,1);
}else if(')' == input->value && '(' == stack_op->value){
stack_pop(&stack_op);
}else{
break;
}
}
if(input->value == ')'){
stack_pop(&input);
}else{
// printf("%d into op_stack\n",input->value);
stack_push(stack_pop(&input),&stack_op,1);
}
}else{
stack_push(stack_pop(&input),&output,0);
}
}
while(stack_op != NULL){
stack_push(stack_pop(&stack_op),&output,1);
}
printf("suffix_output:");
print_stack(output);
printf("\n");
while(output != NULL){
stack_push(stack_pop(&output),output1,output->is_op);
}
printf("suffix_output1:");
print_stack(*output1);
printf("\n");
clear_stack(&output);
return;
}
3.字尾運算
int calculate(stack_t *in_s){
stack_t *cal_s = stack_init();
int num1 = 0,num2 = 0;
while(NULL != in_s){
if(1 == in_s->is_op){
num1 = stack_pop(&cal_s);
num2 = stack_pop(&cal_s);
// printf("%d %c %d\n",num2,in_s->value,num1);
switch(in_s->value){
case '+':
stack_push(num2+num1,&cal_s,0);
break;
case '-':
stack_push(num2-num1,&cal_s,0);
break;
case '*':
stack_push(num2*num1,&cal_s,0);
break;
case '/':
stack_push(num2/num1,&cal_s,0);
break;
}
stack_pop(&in_s);
}else{
stack_push(stack_pop(&in_s),&cal_s,in_s->is_op);
}
}
return stack_pop(&cal_s);
}
最後在main()函式裡把字串輸入後轉換入棧,就可以了,記得把輸入的字串入棧處理~
計算器c語言完整程式碼下載