[华为机试真题]68.简单四则运算

题目

输入一个只包含个位数字的简单四则运算表达式字符串,计算该表达式的值
注:
3.1、表达式只含 +, -, *, / 四则运算符,不含括号
3.2、表达式数值只包含个位整数(0-9),且不会出现0作为除数的情况
3.3、要考虑加减乘除按通常四则运算规定的计算优先级
3.4、除法用整数除法,即仅保留除法运算结果的整数部分。比如8/3=2。输入表达式保证无0作为除数情况发生
3.5、输入字符串一定是符合题意合法的表达式,其中只包括数字字符和四则运算符字符,除此之外不含其它任何字符,不会出现计算溢出情况

要求实现函数

int calculate(int len,char *expStr)
【输入】 int len: 字符串长度;char *expStr: 表达式字符串;
【输出】 无
【返回】 计算结果

示例

1)输入:char *expStr = “1+4*5-8/3”
函数返回:19
2)输入:char *expStr = “8/3*3”
函数返回:6

思路

题目要求很简单,只是计算个位数的四则运算,且没有括号。主要考察栈的运用,实现时采用两个辅助栈,一个用来存放操作数,一个用来存放运算符。根据操作符的优先级进行计算。
两个数组分别存储操作数和操作符,扫描输入的字符串,当遇到*和/的时候,边计算边存储,将计算结果存到操作数的数组中,当遇到+和-号时只存储不计算。当所有数据存储完毕后,因为运算就只剩下+-运算了.

代码

/*---------------------------------------
*   日期:2015-07-07
*   作者:SJF0115
*   题目:简单四则运算
*   来源:华为机试真题
-----------------------------------------*/
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

// 简单四则运算
int calculate(string str){
    int size = str.size();
    if(size == 0){
        return 0;
    }//if
    // 数字栈
    stack<int> num;
    // 运算符栈
    stack<char> op;
    for(int i = 0;i < size;++i){
        // 数字
        if(str[i] >= '0' && str[i] <= '9'){
            num.push(str[i] - '0');
        }//if
        // 运算符 加减
        else if(str[i] == '+' || str[i] == '-'){
            op.push(str[i]);
        }//else
        // 运算符 乘除
        else{
            char o = str[i];
            int a = num.top();
            num.pop();
            int b = str[++i] - '0';
            if(o == '*'){
                num.push(a * b);
            }//if
            else if(o == '/'){
                num.push(a / b);
            }//else
        }//else
    }//for
    // 加减操作
    while(!op.empty()){
        char o = op.top();
        op.pop();
        int a = num.top();
        num.pop();
        int b = num.top();
        num.pop();
        if(o == '+'){
            num.push(a + b);
        }//if
        else if(o == '-'){
            num.push(b-a);
        }//else
    }//while
    return num.top();
}

int main(){
    string str;
    //freopen("C:\\Users\\Administrator\\Desktop\\acm.txt","r",stdin);
    while(cin>>str){
        cout<<calculate(str)<<endl;
    }//while
    return 0;
}
#include<stdio.h> #include<stdlib.h> struct four { double a; struct four *next; //定义结构体,作为链表的节点. }; void main() { double sum(void); //函数声明. 该函数返回等式的计算结果. 有优先级的运算符号在函数内部先进行计算。 double sum1; printf("请输入等式,以 '=' 结束, 例如“ 2*2*3-2/2= ” 结果将自动保留六位有效数字\n"); sum1=sum(); printf("该等式的结果为:\t%f\n\n",sum1); } double sum(void) { struct four *head,*pnew,*ptail,*p,*q; //结构体成员. char ah; double s=0,last; //last作为 pnew->a 的前一个数值. int j=1; q=(struct four *)malloc(sizeof(struct four)); scanf("%lf%c",&q->a,&ah); last=q->a; while(j==1 && ah!='=') //头节点的建立. { switch(ah) //对运算符号的优先级进行选择,优先级高的先进行计算. { case '+': j=0; continue; break; case '-': j=0; continue; break; case '*': q=(struct four *)malloc(sizeof(struct four)); scanf("%lf",&q->a); q->a=last*q->a; break; case '/': q=(struct four *)malloc(sizeof(struct four)); scanf("%lf",&q->a); q->a=last/q->a; break; default: printf("Error!\n"); //当运算符号出错时的处理. exit(0); } last=q->a; scanf("%c",&ah); } pnew=(struct four *)malloc(sizeof(struct four)); pnew->a=q->a; //将头节点的信息传递给 head 和 ptail. head=ptail=pnew; while(ah!='=') //接下来节点的建立. { pnew=(struct four *)malloc(sizeof(struct four)); scanf("%lf",&pnew->a); switch(ah) { case '*': pnew->a=last*pnew->a; break; case '/': pnew->a=last/pnew->a; break; case '+': break; case '-': pnew->a=-pnew->a;break; default: printf("Error!\n"); //当运算符号出错时的处理. exit(0); } scanf("%c",&ah); if(ah=='-' || ah=='+'|| ah=='=') //将值进行传递 ptail->next=pnew. { ptail->next=pnew; ptail=pnew; } last=pnew->a; } ptail->next=NULL; p=head; while(p!=NULL) //各个节点数值相加的结果,有优先级符号的已经先计算了. { s=s+(p->a); p=p->next; } return s; //返回运算结果. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值