标题:c语言做计算器问题
只看楼主
cccv223
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-10-21
 问题点数:0 回复次数:0 
c语言做计算器问题
我做的小计算器程序,有些例子总不输出,求大神帮我看看问题在哪?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
     double *top;
     double *base;                            //定义一个数据栈
} datastack;
typedef struct{
      char *top;
      char *base;                               //定义一个符号栈
} opstack;
void initiatedatastack(datastack *data){
      data->top=data->base=(double*)malloc(100*sizeof(double));
        *(data->top)=*(data->base)=EOF;
}
void initiateopstack(opstack *op){              //栈初始化
         op->top=op->base=(char*)malloc(100*sizeof(char));
        *(op->base)=EOF;
}
void pushdata(datastack *data,double c)                                  //数字压入栈函数
{     data->top++;
    *data->top=c;
 
}
 void pushop(opstack *op,char c)   {                 //符号压入函数
     op->top++;
     *op->top=c;

}

int main(void)
{char a[200],k;
 double r=0.0,d=0.0,c=0.0,m=0.0,n=0.0;int i=0;
 datastack *data;
 opstack *op;
data=(datastack*)malloc(100*sizeof(datastack));
op=(opstack*)malloc(100*sizeof(opstack));
initiateopstack(op);
initiatedatastack(data);
printf("请输入:");
fgets(a,1000,stdin);
for(i=0;a[i]!=EOF;i++){
if(a[0]=='-'){
 i=i+1;
while(a[i]<='9'&&a[i]>='0'){              //第一位为负号
    c=a[i]-48;
    d=d*10+c;
     i++;}printf("%d",i);
 if(a[i]=='.'){
     i++;                         //小数情况
     while(a[i]<='9'&&a[i]>='0'){
      
      r=10.0;
      d=d+((a[i]-48)/r);
      r=r*10.0;
      i++;}}
   d=0.0-d;
   pushdata(data,d);i--;
   d=0.0;
  a[0]='?';
   i--;printf("%d",i);
   continue;
   }
if(a[i]!='\n'){
while(a[i]<='9'&&a[i]>='0'){              //取数字
    c=a[i]-48;
    d=d*10+c;
     i++;        
}
if(a[i]=='.'){
     i++;                         //小数情况
     while(a[i]<='9'&&a[i]>='0'){
      
      r=10.0;
      d=d+((a[i]-48)/r);
      r=r*10.0;
      i++;}
}
pushdata(data,d);printf("x");
d=0.0;}
if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/')
{        
          if(*op->top=='*'){
           m=*data->top;
           data->top--;   // 乘除运算
           n=*data->top;
           m=m*n;
           *data->top=m;
            op->top--;
            pushop(op,a[i]);
           }
        else if(*op->top=='/'){
           m=*data->top;
           data->top--;   // 乘除运算
           n=*data->top;
           m=n/m;
           *data->top=m;
            op->top--;
            pushop(op,a[i]);}
   else if(a[i+1]=='-'&&(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'))
{  pushop(op,a[i]) ;
   i=i+2;
  while(a[i]<='9'&&a[i]>='0'){              //负数情况
    c=a[i]-48;
    d=d*10+c;
     i++;}  
   d=0-d;
   pushdata(data,d);
      i--;
   d=0.0;  
}
  else
{
 pushop(op,a[i]);
 }
}
if(a[i]=='\n'){
m=*data->top;
(data->top)--;
while(*(data->top)!=*(data->base)){
     n=*data->top;
    if(*op->top=='/'){m=n/m;*data->top=m;data->top--;op->top--;}
    else if(*op->top=='*'){m=n*m;*data->top=m;data->top--;op->top--;}
    else if(*op->top=='+'){m=n+m;*data->top=m;data->top--;op->top--;}
    else if(*op->top=='-'){m=n-m;*data->top=m;data->top--;op->top--;}      
}i--;
     a[i+1]=EOF; } }                      //加减运算
printf("  ANSWER:%lf  ",m);
return 0;
}不考虑括号情况
搜索更多相关主题的帖子: top base data i++ while 
2017-10-21 01:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481615-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018412 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved