2016-11-15 76 views
-5

我的计算器(见代码打击)只计算整数值。但是我想用真实数字来操作(例如12.24 + 11.06 =)通过使用堆栈的C计算器。我想这个计算器计算实际的数字

您能帮我修改一下我的计算器代码吗?我编程不是很好,我刚开始学习,所以你的帮助非常感谢。

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#define MAX 50 
#define SUM(X,Y) (X)+(Y) 
#define MULTI(X,Y) (X)*(Y) 
#define DIV(X,Y) (X)/(Y) 

typedef struct _Stack 
{ 
char Operator[MAX]; 
int Operand[MAX]; 
int Opt_top; 
int Opd_top; 
}Stack; 
Stack stack; 

void init() 
{ 
stack.Opd_top = stack.Opt_top = 0; 
} 

void optPush(char opt) 
{ 
stack.Operator[stack.Opt_top++] = opt; 
} 
void opdPush(int opd) 
{ 
stack.Operand[stack.Opd_top++] = opd; 
} 

char optPop() 
{ 
return stack.Operator[--stack.Opt_top]; 
} 
int opdPop() 
{ 
return stack.Operand[--stack.Opd_top]; 
} 
void resetExpression(char Exp[], int len) 
{ 
for (int i = 0; i < len; i++) 
    Exp[i] = '\0'; 
} 
int optCheck(char opt1, char opt2) 
{ 
if (opt1 == '*' || opt1 == '/') 
{ 
    if (opt2 == '+' || opt2 == '-') 
     return 1; 
    else 
     return 0; 
} 
else 
    return 0; 
    } 

    int calFunc(int opd1, int opd2, char opt) 
    { 
int result; 
switch (opt) 
{ 
case '+': 
    result = SUM(opd1, opd2); 
    break; 
case'*': 
    result = MULTI(opd1, opd2); 
    break; 
case'/': 
    result = DIV(opd1, opd2); 
    break; 

} 
return result; 
    } 

void calEngine(char Exp[], int len) { 
char tmpExp[MAX] = { 0, }; 
char ch, opt; 
int tmpCnt = 0, opd1, opd2, res, flag = 0; 

for (int i = 0; i < len; i++) { 
    ch = Exp[i]; 
    if (ch == ' ') 
     continue; 
    else if (ch >= '0' && ch <= '9') { 
     tmpExp[tmpCnt++] = ch; 

     if (Exp[i + 1] == '(' || Exp[i + 1] == ')' || Exp[i + 1] == '+' || Exp[i + 1] == '-' || Exp[i + 1] == '*' || Exp[i + 1] == '/' || Exp[i + 1] == '=') 
     { 
      if (flag) 
      { 
       flag = 0; 
       opdPush(-atoi(tmpExp)); 
      } 
      else 
       opdPush(atoi(tmpExp)); 
      resetExpression(tmpExp, tmpCnt); 
      tmpCnt = 0; 
     } 
    } 
    else 
     if (ch == '+' || ch == '-' || ch == '*' || ch == '/') 
     { 


      if (ch == '-') { 
       flag = 1; 
       ch = '+'; 
      } 
      if (stack.Opt_top == 0) 
       optPush(ch); 
      else 
      { 
       opt = optPop(); 
       if (optCheck(opt, ch)) 
       { 
        opd2 = opdPop(); 
        opd1 = opdPop(); 
        res = calFunc(opd1, opd2, opt); 
        opdPush(res); 
        optPush(ch); 
       } 
       else { 
        optPush(opt); 
        optPush(ch); 
       } 
      } 
     } 

     else if (ch == '(') 
      optPush(ch); 
     else if (ch == ')') { 
      while (opt != '(') { 
       opt = optPop(); 
       if (opt != '(') { 
        opd2 = opdPop(); 
        opd1 = opdPop(); 
        res = calFunc(opd1, opd2, opt); 
        opdPush(res); 

       } 
      } 
     } 
     else if (ch == '=') 
      break; 
} 
while (stack.Opt_top != 0) 
{ 
    opd2 = opdPop(); 
    opd1 = opdPop(); 
    printf("opd1:%d opd2:%d\n", opd1, opd2); 
    opt = optPop(); 
    printf("opt: %c\n", opt); 
    res = calFunc(opd1, opd2, opt); 
    printf("result:%d\n", res); 
    opdPush(res); 
} 

    } 

int main(void) 
{ 
int j = 1; 

while (j <= 100) 
{ 
    printf("%d 번째 계산\n",j); 
    char Expression[MAX] = { 0, }; 
    printf("식 입력: "); 
    gets(Expression); 
    calEngine(Expression, strlen(Expression)); 
    printf("%d 번째 계산결과=%d\n\n", j,opdPop()); 

    j++; 

} 
return 0; 
} 
+0

1.正确缩进代码。 2.在你的宏周围放置圆括号:'#define SUM(X,Y)((X)+(Y))',但为什么使用宏而不是函数? –

+0

[C中宏需要括号](http://stackoverflow.com/q/10820340/995714) –

回答

1

那么,你只需要改变操作数的数据类型。使其可以是floatdouble或任何可以处理浮点精度的类似数据类型。

你可以知道它here

+0

使其成为'double'。 – pmg

+0

避免“浮动”,除非你真的想要它,并知道你要做什么 –