C语言实现计算器的简单四则运算

如图

img


这道程序的内容输入我就不是特别清楚,难道是一个死循环遇见'='再结束输入吗?但输入完后又该怎样?麻烦了。
多输几个字,卡正文bug

你题目的解答代码如下:

#include<stdio.h>

int main()
{
    int a,b;
    char op;
    scanf("%d", &a);
    while (1)
    {
        scanf("%1s", &op);
        if (op=='=')
            break;
        scanf("%d", &b);
        switch(op)
        {
            case '+': a = a+b;break;
            case '-': a = a-b;break;
            case '*': a = a*b;break;
            case '/':
                if (b==0){
                    printf("ERROR");
                    return 0;
                }
                a = a/b;
                break;
            default:
                printf("ERROR");
                return 0;
        }
    }
    printf("%d\n", a);
    return 0;
}

img

如有帮助,望采纳!谢谢!

参考:

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define OKAY 1
#define ERROR 0 
char ops[7]={'+','-','*','/','(',')','#'};
int cmp[7][7]={
    {2,2,1,1,1,2,2},
    {2,2,1,1,1,2,2},
    {2,2,2,2,1,2,2},
    {2,2,2,2,1,2,2},
    {1,1,1,1,1,3,0},
    {2,2,2,2,0,2,2},
    {1,1,1,1,1,0,3},
};
 
typedef struct {
    char op[101];
    int top;
}Stack1;
typedef struct {
    int num[100];
    int top;
}Stack2;
Stack1 *s1;
Stack2 *s2;
 
int InitStack1(Stack1 *s){
    if(!s){
        return ERROR;
    }
        s->top=-1;
        return OKAY;
}
int InitStack2(Stack2 *s){
    if(!s){
        return ERROR;
    }
    s->top=-1;
    return OKAY;
}
int pushStack1(Stack1 *s ,char c){
    if(s->top==MAXSIZE-1){
        return ERROR;
    }
    s->top++;
    s->op[s->top]=c;
    return OKAY;     
    
}
int pushStack2(Stack2 *s,double n){
    if(s->top==MAXSIZE-1){
        return ERROR;
    }
    s->top++;
    s->num[s->top]=n;
    return OKAY; 
}
int isEmpty1(Stack1 *s){
    return ((s->top==-1)?ERROR:OKAY);
}
int isEmpty2(Stack2 *s){
    return((s->top==-1)?ERROR:OKAY);
    
}
char popStack1(Stack1 *s,char c){
    if(s->top==-1){
        printf("运算符栈为空!\n");
        return ERROR;
    }
    
        c=s->op[s->top];
        s->top--;
        return c;
 
    
}
int popStack2(Stack2 *s,int n){
    if(s->top==-1){
        printf("操作数栈为空!\n");
        return ERROR;
    }
    
        n=s->num[s->top];
        s->top--;
        return n;
 
    
}
char getTop1(Stack1 *s){
    if(s->top==-1){
        printf("运算符栈为空!\n");
        return ERROR;
    }
    return (s->op[s->top]);
}
int getTop2(Stack2 *s){
    if(s->top==-1){
        printf("运算符栈为空!\n");
        return ERROR;
    }
    return (s->num[s->top]);
}
char compare(char op1,char op2){
    int i,m,n;
    int priority;
    char pri;
    for(i=0;i<7;i++){
        if(op1==ops[i]){
            m=i;
        }
        if(op2==ops[i]){
            n=i;
        }
    }
    priority=cmp[m][n];
    switch(priority){
        case 1:
            pri='<';
            break;
        case 2:
            pri='>';
            break;
        case 3:
            pri='=';
            break;
        case 0:
            pri='$';
            printf("表达式错误!");
            break; 
    }
    return pri;
}
/*
计算函数 
*/
int compute(int a,char op,int b){
    int result;
    switch(op){
        case '+':
            result=a+b;
            break;
        case '-':
            result=a-b;
            break;
        case '*':
            result=a*b;
            break;
        case '/':
            result=a/b;
            break;
    }
    return result;
} 
int main(){
    int a=0;
    int b=0;
    int temp=0,sum=0;
    char op,ch;
    Stack1 *s1;
    Stack2 *s2;
    s1=(Stack1*)malloc(sizeof(Stack1));
    s2=(Stack2*)malloc(sizeof(Stack2));
    InitStack1(s1);
    //printf("1");
    InitStack2(s2);
    //printf("2")
    char *str=(char*)malloc(100);//使用指针字符串
    pushStack1(s1,'#');
    printf("Please input a str:\n");
    gets(str);
    ch=*str++;
    //printf("%c",ch);
    while(ch!='#' || getTop1(s1)!='#'){
        if(ch>='0' && ch<='9'){
            temp=ch-'0';
            ch=*str++;
            while(ch>='0' && ch<='9'){
                temp=temp*10+ch-'0';
                ch=*str++;
            }
            printf("temp:%d\n",temp);
            pushStack2(s2,temp);
        }
        
        else{
            switch(compare(getTop1(s1),ch)){
                //printf("pop %d\n",popStack2(s2,))
                case'<':
                     pushStack1(s1,ch);
                     ch=*str++;
                     break;
                case'=':
                     popStack1(s1,op);
                     ch=*str++;
                     break; 
                case'>':
                      op=popStack1(s1,op);
                      b=popStack2(s2,b);
                      a=popStack2(s2,a);
                      sum=compute(a,op,b);
                      pushStack2(s2,sum);
                      
                      break;
                          
            }
        }
    }
    printf("%d",getTop2(s2));
     
    
}