c语言怎么实现字节的计算器,C语言实现一个简单的计算器

#include

#include

#define N 100

char stack1[N];

char stack2[N];

int top = -1;

char pop(char *stack)

{

char tmp;

if(top == -1)

{

printf("该栈为空!\n");

}

else

{

tmp = stack[top];

top--;

}

return tmp;

}

void push(char *stack,char data)

{

if(top == N-1)

{

printf("该栈已满!\n");

}

else

{

top++;

stack[top] = data;

}

}

char *topostfix(char *expstr) {     char *postfix = (char *)malloc(sizeof(char));     int i = 0;     int j = 0;     int tmp;     while(expstr[i] != '\0')     {         switch(expstr[i])  {      case '+':      case '-':          while(top != -1 && stack1[top] != '(')    {              postfix[j++] = pop(stack1);   }   push(stack1,expstr[i++]);   break;      case '*':      case '/':          while(top != -1 && (stack1[top] == '*' || stack1[top] == '/'))   {       postfix[j++] = pop(stack1);   }   push(stack1,expstr[i++]);   break;      case '(':                                           push(stack1,expstr[i++]);   break;      case ')':          tmp = pop(stack1);   while(top != -1 && tmp != '(')        {       postfix[j++] = tmp;       tmp = pop(stack1);   }   i++;          break;      default :                                       while(expstr[i] >= '0' && expstr[i] <= '9'                                 && expstr[i] != '\0')          {              postfix[j++] = expstr[i++];          }   postfix[j++] = ' ';   break;  }     }     while(top != -1)     {         postfix[j++] = pop(stack1);     }     postfix[j] = '\0';     return postfix; } int operation(char *postfix) {     int i = 0;     int result;     while(postfix[i] != '\0')     {         if(postfix[i] >= '0' && postfix[i] <= '9')    {      result = 0;      while(postfix[i] != ' ')                    {          result = result * 10 + (postfix[i] - '0');   i++;      }      i++;                                          push(stack2,result);  }  else                                         {      if(postfix[i] != ' ')      {          int y = pop(stack2);                  int x = pop(stack2);   switch(postfix[i])                  {       case '+':           result = x + y;           break;       case '-':           result = x - y;    break;       case '*':           result = x * y;    break;       case '/':           result = x / y;    break;   }   push(stack2,result);                     }      i++;  }     }     return result; } int main() {     int result;     char expstr[N];     char *postfix = (char *)malloc(sizeof(char));     printf("请输入表达式:");     gets(expstr);     printf("你输入的表达式为:");     puts(expstr);     postfix = topostfix(expstr);     printf("你输入的表达式的后缀表达式为:");     puts(postfix);     result = operation(postfix);     printf("%s=%d\n",expstr,result);     return 0; } 本来想实现浮点型的,myatof是写好了,但还有很多困难~~~~遂不大想写了~~ 这个计算器只有+,-,*,/,()~~~~ 给点点你们改的空间,将结果能输出为浮点型~~~~~ 提示:强制类型转换~~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值