博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【9108】模拟数学计算器
阅读量:4693 次
发布时间:2019-06-09

本文共 1163 字,大约阅读时间需要 3 分钟。

Time Limit: 10 second

Memory Limit: 2 MB

问题描述

输入一个数学计算表达式,输出结果。(除法运算结果取整)

Input

文件输入仅一行,输入数学表达式

Output

输出数学表达式以及结果 

Sample Input1

1001+98

Sample Output1

1001+98=1099

Sample Input2

1001-98

Sample Output2

1001-98=903

Sample Input3

1001*98

Sample Output3

1001*98=98098

Sample Input4

1001/98

Sample Output4

1001/98=10

【题解】

这道题的测试点就是样例输入和输出。

就是说只有一个运算符。而且还没负数。

所以直接找到运算符的位置。

然后获取运算符左边的数字。获取运算符右边的数字。就可以了。

根据运算符做相应的运算就好了。

要用到string类的函数。

【代码】

#include 
#include
#include
#include
using namespace std;string s;int main(){ getline(cin,s);//直接整行读取。 int i = 1; while (s[i]>='0' && s[i] <='9') //找到操作符的位置 i++; string s1 = s.substr(0,i); //获取操作符左边的数字 char key = s[i];//获取操作符 s = s.erase(0,i+1);//连同操作符和操作符左边的数字全都删掉。 int a = atoi(s1.c_str());//把左边的字符转换成数字 int b = atoi(s.c_str());//右边的也转换成数字(因为左边被删掉了,所以就只剩下右边了) int c; switch (key)//根据运算符,做相应的运算就可以了。 { case '+': c = a+b; break; case '-': c =a-b; break; case '*': c = a*b; break; case '/': c = a/b; break; } printf("%d%c%d=%d",a,key,b,c); //a和b的信息也都要输出。 return 0; }

转载于:https://www.cnblogs.com/AWCXV/p/7632339.html

你可能感兴趣的文章
自己写的网页放在github里面
查看>>
关于Git的学习
查看>>
nginx proxy文件编写总结
查看>>
决策树应用
查看>>
LightOJ_1248 Dice (III)
查看>>
C#后台正则表达式截取字符
查看>>
数据库服务器监控取不到值
查看>>
Major Performance Impacts
查看>>
C primer Plus 作业第四章
查看>>
mysql函数大全
查看>>
Rectangle
查看>>
刷题向》关于搜索+tarjan的奇怪组合题 BZOJ1194 (normal+)
查看>>
AC自动机模板
查看>>
排序二叉树的基本操作
查看>>
Wechat login authorization(OAuth2.0)
查看>>
安装virtualbox须知
查看>>
mui集成百度ECharts的统计图表以及清空释放图表
查看>>
Duplicate entry '' for key 'PRIMARY'
查看>>
传奇脚本中 SendMsg 编号说明
查看>>
Javascript 严格模式详解
查看>>