★简单计算器(STL—vector)

ou_fan

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

Input

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

Output

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

Sample

InputcopyOutputcopy
 
1 + 2 
4 + 2 * 5 - 7 / 11
0 
 
3.00 
13.36 
//ou_fan
#include<bits/stdc++.h>
using namespace std;
//cin不读入空格;回车;
int main() {
	double n;
	//读入
	while (cin>>n&&n!=0) {
		double temp2 = 0;
		vector<double>arr1;
		vector<char>arr2;
		double temp1 = 0;
		double sum = 0;
		arr1.push_back( n);
		char symbol, space;
		double num;
		getchar();//忽略读入第一个数字后的空格;
		while (scanf("%c %lf%c", &symbol, &num, &space) != EOF) {
			arr2.push_back(symbol);
			arr1.push_back(num);
			if (space != ' ')	break;
		}
		//乘除
		for (auto it = arr2.begin();it != arr2.end();it++) {
			if (*it == '*') {
				temp2 = temp1;
				temp1 = arr1[it - arr2.begin()] * arr1[it - arr2.begin() + 1];
				sum += temp1 - temp2;
				arr2.erase(it);//删去这个符号
				it--;
				auto it3 = arr1.begin();
				arr1.erase(it3 + (it - arr2.begin()) + 1);//删除数字
				arr1.erase(it3 + (it - arr2.begin()) + 1);//删除数字
				arr1.insert(arr1.begin() + (it - arr2.begin() + 1), temp1);
			}
			else if (*it == '/') {
				temp2 = temp1;
				temp1 = arr1[it - arr2.begin()] / arr1[it - arr2.begin() + 1];
				sum += temp1 - temp2;
				arr2.erase(it);//删去这个符号
				it--;
				auto it3 = arr1.begin();
				arr1.erase(it3 + (it - arr2.begin()) + 1);//删除数字
				arr1.erase(it3 + (it - arr2.begin()) + 1);//删除数字
				arr1.insert(arr1.begin() + (it - arr2.begin() + 1), temp1);
			}
		}
		/*加减*/
		for (auto it = arr2.begin();it != arr2.end();it++) {
			if (*it == '-') {
				temp2 = temp1;
				temp1= arr1[it - arr2.begin()] - arr1[it - arr2.begin() + 1];
				sum += temp1-temp2;
				arr2.erase(it);//删去这个符号
				it--;
				auto it3 = arr1.begin();
				arr1.erase(it3 + (it - arr2.begin())+1);//删除数字
				arr1.erase(it3 + (it - arr2.begin())+1);//删除数字
				arr1.insert(arr1.begin() + (it - arr2.begin()+1), temp1);
			}
			else if (*it == '+') {
				temp2 = temp1;
				temp1 = arr1[it - arr2.begin()] + arr1[it - arr2.begin() + 1];
				sum += temp1-temp2;
				arr2.erase(it);//删去这个符号
				it--;
				auto it3 = arr1.begin();
				arr1.erase(it3 + (it - arr2.begin())+1);//删除数字
				arr1.erase(it3 + (it - arr2.begin())+1);//删除数字
				arr1.insert(arr1.begin() + (it - arr2.begin()+1) , temp1);
			}

		}
		cout << fixed<<setprecision(2) << sum << endl;
	}
	return 0;
}

思考了将近两个小时,做出来的题目;

自己思考的方向不对,代码也比较复杂;

由于vj系统坏了,没有提交,应该也ac不了,但是测试样例能够通过;

在写这道题的过程中学到了一些新的知识:

1.以0为读入结束使用while (cin>>n&&n!=0)

2.一直读入多个数据直到遇到回车:
 

while (scanf("%c %lf%c", &symbol, &num, &space) != EOF){

           if (space == '\n')    break;
}

3.删除vector中指定下标元素(使用迭代器)

vector<int> nums(n);

1. 删除指定一个 i,
auto it = nums.begin();
nums.erase(it+i);

4..在a[m]前面插入temp1

a.insert(a.begin()+m, temp1);

5.若for循环定义了迭代器,可以直接使用erase删除该元素;

a.erase(it);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou_fan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值