九度OJ-1101-計算表達式

題目描述:
對於一個不存在括號的表達式進行計算
輸入:
存在多種數據,每組數據一行,表達式不存在空格
輸出:
輸出結果
樣例輸入:
6/2+3+3*4
樣例輸出:
18

這個題目一行輸入可能有多個表達式
方法1:

#include <stack>
#include <stdio.h>
using namespace std;
char str[220]; //保存表達式字符串
/*
優先級矩陣,記人爲添加在表達式首位的標記運算符爲0號,
+爲1號,-爲2號,*爲3號,/爲4號,若mat[i][j]==1
則表示i號運算符優先級大於j號。
*/
int mat[][5]={
    1,0,0,0,0,
    1,0,0,0,0,
    1,0,0,0,0,
    1,1,1,0,0,
    1,1,1,0,0,
};

stack<int> op;  //運算符棧
stack<int> in;//數字棧
/*
獲得表達式中下一個元素的函數,若函數結束運行時,引用變量reto爲true,則表示該元素
爲一個運算符,其編號保存在飲用變量retn中;否則,表示該元素爲一個數字,其值保存在
引用變量retn中,引用變量i表示遍歷到的字符串的下標。
*/
void getOp(bool &reto,int &retn,int &i){
    if (i==0&&op.empty()==true)//初始時將0號運算符壓棧
    {
        reto=true;
        retn=0;
        return;
    }
    if (str[i]==0)//字符串掃描結束時,將0號運算符壓棧
    {
        reto=true;
        retn=0;
        return;
    }

    if (str[i]>='0'&&str[i]<='9')//當爲數字時,將reto置爲false
    {
        reto = false;
    }
    else{           //其他情況均爲運算符
        reto=true;
        if (str[i]=='+')
        {
            retn=1;
        }else if (str[i]=='-')
        {
            retn=2;
        }else if (str[i]=='*')
        {
            retn=3;
        }else if (str[i]=='/')
        {
            retn=4;
        }
        i++;
        return;
    }
    retn=0;
    for (; str[i] !='+'&&str[i] !='-'&&str[i] !='*'&&str[i] !='/'&&str[i]!=0; ++i)
    {
        retn*=10;
        retn+=str[i]-'0';
    }

    return ;
}
int main(){
    //char str[200];
    freopen("input.txt","r",stdin);
    while(scanf("%s",str)!=EOF){
        //puts(str);
        bool retop;
        int retnum;
        int idx=0;
        while(!op.empty()) op.pop();
        while(!in.empty()) in.pop();
        while(true){
            getOp(retop,retnum,idx);
            //printf("idx:%d,retop:%d,retnum:%d\n",idx,retop,retnum);
            if (retop==false) //操作數進棧
            {
                in.push(retnum);
            }else{
                double tmp;
                if (op.empty()==true||mat[retnum][op.top()]==1)//運算符優先級高的要進棧
                {
                    op.push(retnum);
                }else{
                    while(mat[retnum][op.top()]==0){
                        int ret=op.top();
                        op.pop();
                        int  b=in.top();
                        in.pop();
                        int  a = in.top();
                        in.pop();
                        if (ret==1)
                        {
                            tmp=a+b;
                        }
                        else if(ret ==2) tmp=a-b;
                        else if(ret ==3) tmp=a*b;
                        else tmp=a/b;
                        in.push(tmp);
                    }
                    op.push(retnum);
                }
            }
            if (op.size()==2&&op.top()==0)
            {
                break;
            }

        }
        printf("%d\n",in.top());

    }
    return 0;
}

方法2:利用數組來做

#include <stdio.h>
/*
這種思路是從左至右掃描表達式:將加數 減數 乘式的結果 除式的結果一次保存在
數組中,最後將數組求和。 
*/
int a[100];
int k;
char op;
int i;
int main(){
    //freopen("input.txt","r",stdin);
    while(scanf("%d",&k)!=EOF){
        a[0]=0;
        i=0;
        a[++i]=k;
        while(scanf("%c",&op)!=EOF&&(op=='+'||op=='-'||op=='*'||op=='/')){
            scanf("%d",&k);
            if (op=='+')
            {
                a[++i]=k;
            }else if(op=='-'){
                a[++i]=0-k;
            }else if(op=='*'){
                a[i]*=k;
            }else
                a[i]/=k;
        }
        for (int k = 1; k <=i ; ++k)
            a[0]+=a[k];
        printf("%d\n",a[0]);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章