标签:erase algo while char str div 中缀 int cas
#include <iostream> #include <vector> #include <cstring> #include <algorithm> using namespace std; int tonum(string s, int l) { int n = 1, sum = 0; for(int i = l - 1; i > 1; i--) { sum += (s[i] - ‘0‘) * n; n *= 10; } return sum; } int main() { int n; vector<int> v; while(cin >> n) { v.push_back(n); if(getchar() == ‘\n‘) { string s; while (getline(cin, s)) { if (s[0] == ‘a‘) { v.push_back(tonum(s, s.length())); for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; } if (s[0] == ‘c‘) { string s1, s2; int cnt = 0, num1, num2, temp; for(int k = 0; k < s.length(); k++) { if(s[k] == ‘\\‘) cnt++; if(cnt == 2) { s1 = s.substr(0, k); s2 = s.substr(k - 1, s.length() - k + 1);//这里比较麻烦 为了前面的统一化转数字 break; } } num1 = tonum(s1, s1.length()); num2 = tonum(s2, s2.length()); for (int i = 0; i < v.size(); i++) { if(v[i] == num1) { temp = v[i]; v.erase(v.begin() + i); v.insert(v.begin() + i, num2); } } for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } if (s[0] == ‘s‘) { sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } if (s[0] == ‘d‘) { int num = tonum(s, s.length()); for (int i = 0; i < v.size(); i++) { if(v[i] == num) v.erase(v.begin() + i); } for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } } } } return 0; }
#include <iostream> #include <vector> #include <map> #include <stack> using namespace std; string toSuf(string s) { map<char, int> isp, icp; //isp--in stack preority icp--in coming preority isp[‘(‘] = 1; isp[‘*‘] = 5; isp[‘/‘] = 5; isp[‘+‘] = 3; isp[‘-‘] = 3; isp[‘)‘] = 6; icp[‘(‘] = 6; icp[‘*‘] = 4; icp[‘/‘] = 4; icp[‘+‘] = 2; icp[‘-‘] = 2; icp[‘)‘] = 1; string sufstr; stack<char> opt;//operator for (int i = 0; i < s.length(); i++) { if (isalpha(s[i]) || (s[i] >= ‘0‘ && s[i] <= ‘9‘)) { sufstr += s[i]; } else if (opt.empty() || icp[s[i]] > isp[opt.top()]) { opt.push(s[i]); } else { if(s[i] == ‘)‘) { while(opt.top() != ‘(‘) { sufstr += opt.top(); opt.pop(); } opt.pop(); } else { while(!opt.empty() && isp[opt.top()] >= icp[s[i]]) { sufstr += opt.top(); opt.pop(); } opt.push(s[i]); } } } while(!opt.empty()) { sufstr += opt.top(); opt.pop(); } return sufstr; } double calSuf(string s) { stack<double> opd;//operand for(int i = 0; i < s.length(); i++) { if(s[i] >= ‘0‘ && s[i] <= ‘9‘) { opd.push(s[i] - ‘0‘); } else { double opd2 = opd.top(); opd.pop(); double opd1 = opd.top(); opd.pop(); switch (s[i]) { case ‘+‘: opd.push(opd1 + opd2); break; case ‘-‘: opd.push(opd1 - opd2); break; case ‘*‘: opd.push(opd1 * opd2); break; case ‘/‘: opd.push(opd1 / opd2); break; } } } return opd.top(); } int main() { int sum1, sum2; string s1, s2, sufs; cout << "请输入中缀表达式: "; cin >> s1; sufs = toSuf(s1); cout << "后缀表达式为: " << sufs << endl; cout<< "结果为: " << calSuf(sufs) << endl; cout << "请输入后缀表达式: "; cin >> s2; cout << "结果为: " << calSuf(s2) << endl; return 0; }
标签:erase algo while char str div 中缀 int cas
原文地址:https://www.cnblogs.com/ache/p/12563380.html