全RE求助
查看原帖
全RE求助
447562
像素旋转楼主2021/2/23 21:56
#include<iostream>
#include<string>
#include<stack>
#include<cmath>
using namespace std;
void getup(string&);
int main(void)
{
	int n;
	cin >> n;
	string a[50];
	stack<char> stk;
	cin.get();
	for (auto i = 0; i < n; i++) {
		getup(a[i]);
		int digit1 = 0, digit2 = 0, len=0;
		if (a[i][0] >= 'a' && a[i][0] <= 'c') {
			if (!stk.empty())
				stk.pop();
			stk.push(a[i][0]);
			int temp_j = 0;
			for (auto j = 2; j < a[i].length(); j++) {
				if (a[i][j] == ' ')
					temp_j = j;
			}
			for (auto j = 2; j < temp_j; j++)
				digit1 += (a[i][j] - '0') * pow(10, temp_j - j - 1);
			for (auto j = temp_j + 1; j < a[i].length(); j++)
				digit2 += (a[i][j] - '0') * pow(10, a[i].length() - j - 1);
			len += a[i].length() - 1;
		}
		else {
			len += a[i].length()+1;
			int temp_j = 0;
			for (auto j = 0; j < a[i].length(); j++)
				if (a[i][j] == ' ')
					temp_j = j;
			for (auto j = 0; j < temp_j; j++)
				digit1 += (a[i][j] - '0') * pow(10, temp_j - j - 1);
			for (auto j = temp_j + 1; j < a[i].length(); j++)
				digit2 += (a[i][j] - '0') * pow(10, a[i].length() - j - 1);
		}
		//此前len存储的为不包含答案的式子长度
		int temp_digit;
		int temp_len = 0;
		switch (stk.top()) {
		case 'a':
			temp_digit = digit1 + digit2;
			while (temp_digit) {
				temp_digit /= 10;
				temp_len++;
			}
			len += temp_len;
			cout << digit1 << "+" << digit2 << "=" << digit1 + digit2
				<< endl << len << endl;
			break;
		case 'b':
			temp_digit = digit1 - digit2;
			if (temp_digit < 0)
				len += 1;
			while (temp_digit) {
				temp_digit /= 10;
				temp_len++;
			}
			len += temp_len;
			cout << digit1 << "-" << digit2 << "=" << digit1 - digit2
				<< endl << len << endl; 
			break;
		case 'c':
			temp_digit = digit1 * digit2;
			while (temp_digit) {
				temp_digit /= 10;
				temp_len++;
			}
			len += temp_len;
			cout << digit1 << "*" << digit2 << "=" << digit1 * digit2
				<< endl << len << endl; 
			break;
		}
	}
	return 0;
}
void getup(string& a) {
	char t;
	while ((t = cin.get()) != '\n')
		a += t;
	return;
}

听说getline会re已经改掉了然并卵

2021/2/23 21:56
加载中...