C語言 計算機

用C實作簡易計算機:

要求使用者輸入一infix運算式,例如1+2*3。

印出此運算式的postfix結果,例如123*+。

再印出由postfix運算式的運算結果,例如7。

題目範圍

 運算子:+,-,*,/

 數字皆為正整數(大於0)

Update:

to:wu

我用c++執行了你的程式 出現以下的錯誤

D:\Documents and Settings\Administrator\桌面\Untitled1.cpp In function `void ErrorExit(char*)':

Update 2:

14 D:\Documents and Settings\Administrator\桌面\Untitled1.cpp `system' undeclared (first use this function)

(Each undeclared identifier is reported only once for each function it appears in.)

Update 3:

15 D:\Documents and Settings\Administrator\桌面\Untitled1.cpp `exit' undeclared (first use this function)

D:\Documents and Settings\Administrator\桌面\Untitled1.cpp In function `int GetNextNum(char**)':

Update 4:

36 D:\Documents and Settings\Administrator\桌面\Untitled1.cpp `atoi' undeclared (first use this function)

D:\Documents and Settings\Administrator\桌面\Untitled1.cpp In function `int main()':

Update 5:

94 D:\Documents and Settings\Administrator\桌面\Untitled1.cpp `abs' undeclared (first use this function)

114 D:\Documents and Settings\Administrator\桌面\Untitled1.cpp `atoi' undeclared (first use this function)

Update 6:

122 D:\Documents and Settings\Administrator\桌面\Untitled1.cpp `system' undeclared (first use this function)

~.~ 也幫我解錯嗎?

Update 7:

不懂 大大意思

要怎麼修改? 可以更清楚一點嗎~

謝謝唷!

1 Answer

Rating
  • Wu
    Lv 5
    10 years ago
    Favorite Answer

    雖然答了多次,我也不客氣了:

    #include <stdio.h>

    #include <string.h>

    #define SIZE 50

    #define PLUS -'+'

    #define MINUS -'-'

    #define MULTIPLY -'*'

    #define DIVISION -'/'

    int stack[SIZE];

    int stackptr=0;

    void ErrorExit(char *msg)

    { printf("錯誤:%s\n",msg);

    system("pause");

    exit(1);

    }

    void Push(int data)

    { if (stackptr==SIZE)

    ErrorExit("推疊已滿!");

    stack[stackptr++]=data;

    }

    int Pop()

    { if (!stackptr)

    ErrorExit("推疊已空!");

    return stack[--stackptr];

    }

    int GetNextNum(char **ptr)

    { char num[10],i=0;

    while (**ptr == ' ' || **ptr == '\t') ++(*ptr);

    while (**ptr && **ptr >='0' && **ptr<='9') num[i++] = *((*ptr)++);

    num[i]=0;

    if (!i) ErrorExit("句式錯誤!");

    return atoi(num);

    }

    void Insert(int num, int op)

    { int tnum;

    tnum = Pop();

    if (!stackptr)

    { Push(tnum);

    Push(num);

    Push(op);

    return;

    }

    if (tnum == MINUS || tnum == PLUS)

    { Push(num);

    Push(op);

    }

    else Insert(num, op);

    Push(tnum);

    }

    int main()

    { char buf[255],*ptr, postfix[255]={0,};

    int i,num;

    while (1)

    { printf("輸入算式(空返回結束):");

    fgets(buf,255,stdin);

    if (buf[0]=='\n') break;

    ptr = buf;

    while (*ptr && *ptr != '\n')

    { switch (ptr[0])

    { case '+':

    ++ptr;

    Push(GetNextNum(&ptr));

    Push(PLUS);

    break;

    case '-':

    ++ptr;

    Push(GetNextNum(&ptr));

    Push(MINUS);

    break;

    case '*':

    ++ptr;

    Insert(GetNextNum(&ptr),MULTIPLY);

    break;

    case '/':

    ++ptr;

    Insert(GetNextNum(&ptr),DIVISION);

    break;

    case ' ':

    case '\t':

    ++ptr;

    break;

    default:

    Push(GetNextNum(&ptr));

    break;

    }

    }

    for (i=0,postfix[0]=0; i<stackptr; ++i,strcat(postfix,buf))

    stack[i]<0 ? sprintf(buf,"%c ",abs(stack[i])) : sprintf(buf,"%d ",stack[i]);

    printf("postfix=%s\n",postfix);

    stackptr=0;

    num=0;

    ptr = strtok(postfix," ");

    while (ptr)

    { switch (*ptr)

    { case '+':

    Push(Pop()+Pop());

    break;

    case '-':

    Push(-1*Pop()+Pop());

    break;

    case '*':

    Push(Pop()*Pop());

    break;

    case '/':

    Push((int)(1.0/Pop() * Pop()));

    break;

    default:

    Push(atoi(ptr));

    break;

    }

    ptr = strtok(NULL," ");

    }

    if (stackptr==1) printf("結果=%d\n",Pop());

    else ErrorExit("句式錯誤!");

    }

    system("pause");

    return 0;

    }

    2010-06-10 12:59:20 補充:

    #include 以下就可以 :

    stdlib.h

    math.h

    2010-06-11 01:03:08 補充:

    #include < stdlib.h>

    #include < math.h>

Still have questions? Get your answers by asking now.