简单计算器(使用C++的栈)

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<stack>
  5 using namespace std;
  6 int isop(char c)
  7 {
  8     if (c == ‘+‘ || c == ‘-‘ || c == ‘*‘ || c == ‘/‘ || c == ‘(‘ || c == ‘)‘||c==‘#‘)
  9         return 1;
 10     else return 0;
 11 }
 12 char pro(char s,char c)
 13 {
 14     switch (s){
 15     case(‘+‘):
 16     case(‘-‘):
 17         if (c == ‘+‘ || c == ‘-‘)return ‘>‘;
 18         else if (c == ‘*‘ || c == ‘/‘)return ‘<‘;
 19         else if (c == ‘(‘)return ‘<‘;
 20         else if (c == ‘)‘)return ‘>‘;
 21         else return ‘>‘;
 22         break;
 23
 24     case(‘*‘):
 25     case(‘/‘):
 26         if (c == ‘+‘ || c == ‘-‘)return ‘>‘;
 27         else if (c == ‘*‘ || c == ‘/‘)return ‘>‘;
 28         else if (c == ‘(‘)return ‘<‘;
 29         else if (c == ‘)‘)return ‘>‘;
 30         else return ‘>‘;
 31         break;
 32     case(‘(‘):
 33         if (c == ‘)‘)return ‘=‘;
 34         else return ‘<‘;
 35     case(‘)‘) :
 36         return ‘>‘;
 37     case(‘#‘):
 38         if (c == ‘#‘)return ‘=‘;
 39         else return ‘<‘;
 40     }
 41 }
 42 int oper(int a, char op, int b)
 43 {
 44     if (op == ‘+‘)return a + b;
 45     else if (op == ‘-‘)return a - b;
 46     else if (op == ‘*‘)return a*b;
 47     else if (op == ‘/‘)return a / b;
 48 }
 49 int main()
 50 {
 51     char ch,op;
 52     int i,a,b,res;
 53     stack<int>opn;
 54     stack<char>opt;
 55     ch = getchar();
 56     opt.push(‘#‘);
 57     while (ch != ‘#‘||opt.top() != ‘#‘)
 58     {
 59         if (!isop(ch))
 60         {
 61             i = atoi(&ch);
 62             ch = getchar();
 63             while (!isop(ch))
 64             {
 65                 i = i * 10 + atoi(&ch);
 66                 ch = getchar();
 67             }
 68             opn.push(i);
 69         }
 70         else
 71         {
 72             switch (pro(opt.top(), ch))
 73             {
 74             case(‘<‘) :
 75                 opt.push(ch);
 76                 ch = getchar();
 77                 break;
 78
 79             case(‘=‘) :
 80                 opt.pop();
 81                 ch = getchar();
 82                 break;
 83
 84             case(‘>‘) :
 85                 op = opt.top();
 86                 opt.pop();
 87                 b = opn.top();
 88                 opn.pop();
 89                 a = opn.top();
 90                 opn.pop();
 91                 res=oper(a, op, b);
 92                 opn.push(res);
 93                 break;
 94             }
 95         }
 96     }
 97     printf("%d\n", opn.top());
 98     system("pause");
 99     return 0;
100 }
时间: 2024-10-10 10:56:15

简单计算器(使用C++的栈)的相关文章

HDU 1237 简单计算器(后缀式+栈)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16351    Accepted Submission(s): 5604 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,

HDU1237 简单计算器 【栈】+【逆波兰式】

版本:1.0 日期:2014.5.17 2014.6.1 版权:© 2014 kince 转载注明出处 在介绍SwitchButton之前,先来看一下系统Button是如何实现的.源码如下: @RemoteView public class Button extends TextView { public Button(Context context) { this(context, null); } public Button(Context context, AttributeSet att

hdu1237 简单计算器 (模拟+栈)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15075    Accepted Submission(s): 5132 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,

简单计算器(杭电1237)(栈的运用)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12594    Accepted Submission(s): 4139 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,

hdu 1237 简单计算器(栈处理)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26553    Accepted Submission(s): 9626 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

题目1019:简单计算器(栈的使用)

题目链接:http://ac.jobdu.com/problem.php?pid=1019 题目具体分析见:https://github.com/zpfbuaa/JobduInCPlusPlus   参考代码: // // 1019 简单计算器.cpp // oj // // Created by PengFei_Zheng on 04/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <st

HDU 1237:简单计算器【栈】

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6429    Accepted Submission(s): 2044 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

简单计算器(栈)

欢迎参加——每周六晚的BestCoder(有米!) 简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14873    Accepted Submission(s): 5061 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试

hdoj 1237 简单计算器

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14512    Accepted Submission(s): 4920 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

重读The C programming Lanuage 笔记三:简单计算器程序

1 //简单计算器 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <ctype.h> 6 #include <string.h> 7 #include <math.h> 8 9 #define MAXOP 100 //max size of operand or operator 10 #define NUMBER '0' //sign of a number was found