Generation

generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式文本,自动计算结果

# #include <iostream> #include <string> #include <stack> #include <sstream> #include <cmath> using namespace std; int main() { string s = "2+3*4-5^2"; stringstream ss; stack<int> nums; stack<char> signs; int num; char sign; while (!s.empty()) { if (isdigit(s.back())) { ss << s.back(); s.pop_back(); if (s.empty() || !isdigit(s.back())) { ss >> num; nums.push(num); ss.clear(); } } else { if (s.back() == '^') { int num = nums.top(); nums.pop(); num = pow(num, 2); nums.push(num); }

Want to kickstart your project?Use the new AI Studio to create your code