2025-4-19 17:52:32 [显示全部楼层]
96浏览
查看: 96|回复: 0

[项目分享] C++简易计算器(支持一元一次方程),开源

[复制链接]
此为全部代码。我只加了一个一元一次功能以及解题步骤功能,具体作者是谁我不知道,这是我以前的作品,直到现在才发。想要增加新代码可以自己加(既然是我搬运的并做了一些小更改那就不需要标明是我的程序了)可以加一个清屏的代码。注释不全是我加的,只有差不多两处我自己写的才是我加的,其他都是原作者
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5. // 快速幂函数,用于计算 a 的 b 次幂
  6. long long Poww(long long a, long long b) {
  7.         long long ans = 1;
  8.         long long base = a;
  9.         while (b != 0) {
  10.                 if (b & 1)
  11.                         ans *= base;
  12.                 base *= base;
  13.                 b >>= 1;
  14.         }
  15.         return ans;
  16. }
  17. // 阶乘函数,计算 n 的阶乘
  18. long long f(int n) {
  19.         if (n == 1)
  20.                 return 1;
  21.         if (n > 1)
  22.                 return n * f(n - 1);
  23. }
  24. // 计算表达式的值,并输出每一步的计算过程
  25. double cal(double s, double e, string str);
  26. // 获取一个操作数或括号内表达式的值,并输出计算过程
  27. double getVal(double &s, double e, string str) {
  28.         if (str[s] == '(' || str[s] == '[' || str[s] == '{') {
  29.                 double news, newe, i;
  30.                 i = 1;
  31.                 news = s + 1;
  32.                 while (i) {
  33.                         s++;
  34.                         if (s == e)
  35.                                 break;
  36.                         if (str[s] == '(' || str[s] == '[' || str[s] == '{')
  37.                                 i++;
  38.                         else if (str[s] == ')' || str[s] == ']' || str[s] == '}')
  39.                                 i--;
  40.                 }
  41.                 newe = s - 1;
  42.                 s++;
  43.                 double result = cal(news, newe, str);
  44.                 cout << "计算括号内表达式 " << str.substr(news, newe - news + 1) << " 的值为: " << result << endl;
  45.                 return result;
  46.         }
  47.         double val;
  48.         if (str[s] == 's') {
  49.                 s = s + 3;
  50.                 double arg = getVal(s, e, str);
  51.                 val = sin(arg);
  52.                 cout << "计算 sin(" << arg << ") 的值为: " << val << endl;
  53.         } else if (str[s] == 'c') {
  54.                 s = s + 3;
  55.                 double arg = getVal(s, e, str);
  56.                 val = cos(arg);
  57.                 cout << "计算 cos(" << arg << ") 的值为: " << val << endl;
  58.         } else if (str[s] == 't') {
  59.                 s = s + 3;
  60.                 double arg = getVal(s, e, str);
  61.                 val = tan(arg);
  62.                 cout << "计算 tan(" << arg << ") 的值为: " << val << endl;
  63.         } else if (str[s] == 'a') {
  64.                 s++;
  65.                 if (str[s] == 's') {
  66.                         s = s + 3;
  67.                         double arg = getVal(s, e, str);
  68.                         val = asin(arg);
  69.                         cout << "计算 asin(" << arg << ") 的值为: " << val << endl;
  70.                 } else if (str[s] == 'c') {
  71.                         s = s + 3;
  72.                         double arg = getVal(s, e, str);
  73.                         val = acos(arg);
  74.                         cout << "计算 acos(" << arg << ") 的值为: " << val << endl;
  75.                 } else if (str[s] == 't') {
  76.                         s = s + 3;
  77.                         double arg = getVal(s, e, str);
  78.                         val = atan(arg);
  79.                         cout << "计算 atan(" << arg << ") 的值为: " << val << endl;
  80.                 }
  81.         } else if (str[s] >= '0' && str[s] <= '9') {
  82.                 val = str[s++] - '0';
  83.                 while (s < e && str[s] >= '0' && str[s] <= '9') {
  84.                         val *= 10;
  85.                         val += str[s++] - '0';
  86.                 }
  87.                 if (str[s] == '!') {
  88.                         int n = static_cast<int>(val);
  89.                         val = f(n);
  90.                         cout << "计算 " << n << "! 的值为: " << val << endl;
  91.                         s++;
  92.                 }
  93.                 if (str[s] == '.') {
  94.                         s = s + 1;
  95.                         double val2 = str[s++] - '0';
  96.                         while (s < e && str[s] >= '0' && str[s] <= '9') {
  97.                                 val2 *= 10;
  98.                                 val2 += str[s++] - '0';
  99.                         }
  100.                         while (val2 >= 1) {
  101.                                 val2 /= 10;
  102.                         }
  103.                         val = val + val2;
  104.                         cout << "处理小数部分,得到值为: " << val << endl;
  105.                 }
  106.         }
  107.         return val;
  108. }
  109. // 处理乘、除、幂运算,并输出计算过程
  110. double getVal1(double &s, double e, string str) {
  111.         double val;
  112.         val = getVal(s, e, str);
  113.         while (1) {
  114.                 if (s < e && str[s] == '*') {
  115.                         double right = getVal(++s, e, str);
  116.                         double prev = val;
  117.                         val *= right;
  118.                         cout << "计算 " << prev << " * " << right << " 的值为: " << val << endl;
  119.                 } else if (s < e && str[s] == '/') {
  120.                         double right = getVal(++s, e, str);
  121.                         double prev = val;
  122.                         val /= right;
  123.                         cout << "计算 " << prev << " / " << right << " 的值为: " << val << endl;
  124.                 } else if (s < e && str[s] == '^') {
  125.                         double right = getVal(++s, e, str);
  126.                         double prev = val;
  127.                         val = Poww(static_cast<long long>(val), static_cast<long long>(right));
  128.                         cout << "计算 " << prev << " ^ " << right << " 的值为: " << val << endl;
  129.                 } else
  130.                         return val;
  131.         }
  132. }
  133. // 处理加、减运算,并输出计算过程
  134. double cal(double s, double e, string str) {
  135.         double sum = 0;
  136.         if (str[s] != '-')
  137.                 sum = getVal1(s, e, str);
  138.         while (s < e) {
  139.                 if (str[s] == '+') {
  140.                         double right = getVal1(++s, e, str);
  141.                         double prev = sum;
  142.                         sum += right;
  143.                         cout << "计算 " << prev << " + " << right << " 的值为: " << sum << endl;
  144.                 } else if (str[s] == '-') {
  145.                         double right = getVal1(++s, e, str);
  146.                         double prev = sum;
  147.                         sum -= right;
  148.                         cout << "计算 " << prev << " - " << right << " 的值为: " << sum << endl;
  149.                 }
  150.         }
  151.         return sum;
  152. }
  153. // 解析方程,将方程分为左右两部分
  154. void parseEquation(const string& equation, string& left, string& right) {
  155.         size_t equalPos = equation.find('=');
  156.         if (equalPos != string::npos) {
  157.                 left = equation.substr(0, equalPos);
  158.                 right = equation.substr(equalPos + 1);
  159.         }
  160. }
  161. // 合并同类项,计算 x 的系数和常数项
  162. void collectTerms(const string& expr, double& xCoeff, double& constant) {
  163.         double currentCoeff = 1;
  164.         double currentSign = 1;
  165.         double num = 0;
  166.         bool hasX = false;
  167.         for (size_t i = 0; i < expr.length(); ++i) {
  168.                 if (isdigit(expr[i])) {
  169.                         num = num * 10 + (expr[i] - '0');
  170.                 } else if (expr[i] == 'x') {
  171.                         hasX = true;
  172.                         if (i == 0 || !isdigit(expr[i - 1])) {
  173.                                 num = 1;
  174.                         }
  175.                 } else if (expr[i] == '+' || expr[i] == '-') {
  176.                         if (hasX) {
  177.                                 xCoeff += currentSign * currentCoeff * num;
  178.                         } else {
  179.                                 constant += currentSign * num;
  180.                         }
  181.                         num = 0;
  182.                         hasX = false;
  183.                         currentSign = (expr[i] == '+') ? 1 : -1;
  184.                         currentCoeff = 1;
  185.                 }
  186.         }
  187.         if (hasX) {
  188.                 xCoeff += currentSign * currentCoeff * num;
  189.         } else {
  190.                 constant += currentSign * num;
  191.         }
  192. }
  193. // 解一元一次方程
  194. double solveEquation(const string& equation) {
  195.         string left, right;
  196.         parseEquation(equation, left, right);
  197.         double leftXCoeff = 0, leftConstant = 0;
  198.         double rightXCoeff = 0, rightConstant = 0;
  199.         collectTerms(left, leftXCoeff, leftConstant);
  200.         collectTerms(right, rightXCoeff, rightConstant);
  201.         double totalXCoeff = leftXCoeff - rightXCoeff;
  202.         double totalConstant = rightConstant - leftConstant;
  203.         if (totalXCoeff == 0) {
  204.                 if (totalConstant == 0) {
  205.                         cout << "方程有无数解。" << endl;
  206.                 } else {
  207.                         cout << "方程无解。" << endl;
  208.                 }
  209.                 return 0;
  210.         }
  211.         return totalConstant / totalXCoeff;
  212. }
  213.         while (cin >> x) {
  214.                 if (x.find('x') != string::npos && x.find('=') != string::npos) {
  215.                         // 包含未知数 x 且有等号,认为是一元一次方程
  216.                         cout << "开始解方程: " << x << endl;
  217.                         double solution = solveEquation(x);
  218.                         if (solution != 0) {
  219.                                 cout << "方程的解为: x = " << solution << endl;
  220.                         }
  221.                 } else {
  222.                         // 普通表达式,进行计算并输出步骤
  223.                         cout << "开始计算表达式: " << x << endl;
  224.                         double result = cal(0, x.length(), x);
  225.                         cout << "最终计算结果为: " << result << endl;
  226.                 }
  227.         }
  228.         return 0;
  229. }
复制代码
示范:
C++简易计算器(支持一元一次方程),开源图1
代码缩进风格与上贴一致。

计算器1.zip

363.97 KB, 下载次数: 11

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail