c++使用stack制作的简易四则运算计算器

上一章我们学习了stack的基本用法,接下来,我们将运用stack制作一个加减乘除计算器!

其中,要求支持加、减、乘、除四种运算符;支持括号,计算顺序与数学定义相同;考虑输入合法性;

使用STL 的 stack 容器实现计算过程。

代码如下:

#include<iostream>
#include<stack>
#include<string>
#include<cmath>
using namespace std;
//定义优先级函数
int priority(char op){
    switch(op){
        case '+':
        case '-':
          return 1;
        case '*':
        case '/':
          return 2;
        default:
          return 0;
    }
}
//定义计算函数
double calculate(stack<double>&s,char op){
    double b=s.top();
    s.pop();
    double a=s.top();
    s.pop();
    switch(op){
        case '+':
        return a+b;
        case '-':
        return a-b;
        case '*':
        return a*b;
        case '/':
        return a/b;
        default:
        return 0;
    }
}
int main(){
    string line;
    while(getline(cin,line)){//输入字符串
    stack<double>nums;//操作数
    stack<char>ops;   //操作符号
    string num;
    int i=0;
    while(i<line.size()){
        if(isdigit(line[i])){//是数字读取整个数字
        while(i<line.size()&&isdigit(line[i])){
            num+=line[i];//字符串拼接
            i++;
        }
        nums.push(stod(num));//将数字num压入nums操作栈中
        num.clear();
        i--;//循环内++了
        }
        else if(line[i] == '+' || line[i] == '-' ||
         line[i] == '*' || line[i] =='/'){
            //运算符
            while(!ops.empty()&&priority(line[i])<=priority(ops.top())){
                //栈顶元素优先级大于等于当前运算符,弹出栈顶元素并计算
                nums.push(calculate(nums,ops.top()));
                ops.pop();
            }
            ops.push(line[i]);//将运算符压入运算符操作栈中
         }
         else if(line[i]=='('){
            //左括号压入操作符栈
            ops.push(line[i]);
         }
         //右括号,弹出元素并计算,直到遇到)为止
         else if(line[i]==')'){
            while(!ops.empty()&&ops.top()!='('){
                nums.push(calculate(nums,ops.top()));
                ops.pop();

            }
            ops.pop();
         }
         i++;
        }
        while(!ops.empty()){
            nums.push(calculate(nums,ops.top()));
            ops.pop();
        }
        cout<<"result:"<<nums.top()<<endl;

}
return 0;
}

先码到这里,太晚了,以后有时间再解释解释(

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是栈实现非负整数四则运算计算器的 C 代码: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define STACK_SIZE 100 // 定义栈的大小 // 定义栈结构体 typedef struct { int items[STACK_SIZE]; // 栈元素 int top; // 栈顶指针 } Stack; // 初始化栈 void initStack(Stack *s) { s->top = -1; } // 判断栈是否为空 int isEmpty(Stack *s) { return s->top == -1; } // 判断栈是否已满 int isFull(Stack *s) { return s->top == STACK_SIZE - 1; } // 入栈操作 void push(Stack *s, int num) { if (isFull(s)) { printf("栈已满,无法入栈。\n"); exit(1); } s->items[++(s->top)] = num; } // 出栈操作 int pop(Stack *s) { if (isEmpty(s)) { printf("栈为空,无法出栈。\n"); exit(1); } return s->items[(s->top)--]; } // 获取栈顶元素 int peek(Stack *s) { return s->items[s->top]; } // 计算表达式的值 int calculate(char *expr) { int i = 0, num = 0; Stack numStack, opStack; char c, op; initStack(&numStack); // 初始化数字栈 initStack(&opStack); // 初始化操作符栈 while (expr[i] != '\0') { c = expr[i]; if (isdigit(c)) { // 如果是数字 num = num * 10 + (c - '0'); if (!isdigit(expr[i+1])) { // 下一个字符不是数字,则表示数字已经结束 push(&numStack, num); // 将数字入栈 num = 0; // 将 num 置为零,用于下一个数字的计算 } } else if (c == '+' || c == '-') { // 如果是加或减 if (isEmpty(&opStack)) { // 操作符栈为空,直接入栈 push(&opStack, c); } else { while (!isEmpty(&opStack) && peek(&opStack) != '(') { // 将所有高优先级的操作符出栈 op = pop(&opStack); num = pop(&numStack); if (op == '+') { // 执行加法操作 push(&numStack, pop(&numStack) + num); } else { // 执行减法操作 push(&numStack, pop(&numStack) - num); } } push(&opStack, c); // 将当前操作符入栈 } } else if (c == '*' || c == '/') { // 如果是乘或除 if (isEmpty(&opStack) || peek(&opStack) == '+' || peek(&opStack) == '-') { // 操作符栈为空或栈顶为低优先级操作符,直接入栈 push(&opStack, c); } else { // 将所有高优先级的操作符出栈 while (!isEmpty(&opStack) && (peek(&opStack) == '*' || peek(&opStack) == '/')) { op = pop(&opStack); num = pop(&numStack); if (op == '*') { // 执行乘法操作 push(&numStack, pop(&numStack) * num); } else { // 执行除法操作 push(&numStack, pop(&numStack) / num); } } push(&opStack, c); // 将当前操作符入栈 } } else if (c == '(') { // 如果是左括号,直接入栈 push(&opStack, c); } else if (c == ')') { // 如果是右括号 while (!isEmpty(&opStack) && peek(&opStack) != '(') { // 将所有括号内的操作符出栈 op = pop(&opStack); num = pop(&numStack); if (op == '+') { // 执行加法操作 push(&numStack, pop(&numStack) + num); } else { // 执行减法操作 push(&numStack, pop(&numStack) - num); } } pop(&opStack); // 将左括号出栈 } i++; } // 将剩余的操作符出栈 while (!isEmpty(&opStack)) { op = pop(&opStack); num = pop(&numStack); if (op == '+') { // 执行加法操作 push(&numStack, pop(&numStack) + num); } else if (op == '-') { // 执行减法操作 push(&numStack, pop(&numStack) - num); } else if (op == '*') { // 执行乘法操作 push(&numStack, pop(&numStack) * num); } else if (op == '/') { // 执行除法操作 push(&numStack, pop(&numStack) / num); } } return pop(&numStack); // 返回最终的结果 } int main() { char expr[100]; printf("请输入表达式:\n"); scanf("%s", expr); printf("结果为:%d\n", calculate(expr)); return 0; } ``` 以上是栈实现非负整数四则运算计算器的 C 代码,使用方法为输入一个表达式并回车即可得到计算结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值