如何让我的计算器在计算答案后输出完整的表达式?



我正在为C++类创建一个计算器,我正在尝试输出一个完整的方程,而不仅仅是让程序吐出答案。我很难弄清楚如何根据情况进行更改。我尝试了一个if语句,但最终没有成功。对我的代码的任何批评都是值得赞赏的,但我正在寻找一个相对简单的解决方案,这样我就可以输出一个完整的方程,而不仅仅是一个答案,以创建更加用户友好的体验。

这是我的代码:

#include "pch.h"
#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
using namespace std;
//---------- Function Prototypes -----------
void print_menu();
double get_value();
double divide(double, double);
double subtraction(double, double);
double multiplication(double, double);
double SARCC(double, double);
double VolRCC(double, double);
double SARCCone(double, double);
void ShowProgramHeader();
//--------------  Main -------------------
int main() {
ShowProgramHeader();
double operand1, operand2, answer;
int choice, valid_choice;
do {
print_menu();
cin >> choice;
valid_choice = 1;           // assume choice is valid
switch (choice) {
case 0:                    // program will exit
break;
case 1:                    // addition
operand1 = get_value();
operand2 = get_value();
answer = operand1 + operand2;
break;
case 2:                    // division
operand1 = get_value();
operand2 = get_value();
answer = divide(operand1, operand2);
break;
case 3: // subtraction
operand1 = get_value();
operand2 = get_value();
answer = subtraction(operand1, operand2);
break;
case 4: // multiplication
operand1 = get_value();
operand2 = get_value();
answer = multiplication(operand1, operand2);
break;
case 5: // SA right circular cylinder
operand1 = get_value();
operand2 = get_value();
answer = SARCC(operand1, operand2);
break;
case 6: // Vol. right circular cylinder
operand1 = get_value();
operand2 = get_value();
answer = VolRCC(operand1, operand2);
break;
case 7: // SA right circular cone
operand1 = get_value();
operand2 = get_value();
answer = SARCCone(operand1, operand2);
break;
default:
valid_choice = 0;   // choice is invalid
cout << "Invalid Choice." << endl;  
}
if (valid_choice) {   // if choice is valid, print the answer
cout << endl << "Answer= " << answer  <<  endl;
}
} while (choice != 0);    // if not 0, loop back to start
return 0;
}
//--------------  Functions -------------------
//----------------- get_value function ----------------
double get_value() {
double temp_value;
cout << "Please, enter a value: ";
cin >> temp_value;
cout << "Thanks." << endl;
return temp_value;
}
//-------------------- print_menu function -------------
void print_menu() {
cout << endl;
cout << "Add (1)" << endl;
cout << "Divide (2)" << endl;
cout << "Subtract (3)" << endl;
cout << "Multiply (4)" << endl;
cout << "Surface Area of Right Circular Cylinder (5)" << endl;
cout << "Volume of Right Circular Cylinder (6)" << endl;
cout << "Surface Area of Right Circular Cone (7)" << endl;
cout << "Exit (0)" << endl;
cout << "Enter your choice: ";
}
//---------- Show Program Header function ----------
void ShowProgramHeader() {
cout << "Grant Dearing" << "n" << "CS120-02" << "n" << "Lab 6" << "n" << "5 October 2018" << endl;
}
//----------- Subtraction Function --------------
double subtraction(double subtractor, double subtractee) {
return (subtractee - subtractor);
}
//----------- Multiplication Function ---------
double multiplication(double multiplier1, double multiplier2) {
return (multiplier1 * multiplier2);
}
//----------- Divison Function ---------
double divide(double dividend, double divisor) {
if (divisor == 0) {
return 0;  // avoids divide by zero errors
}
else 
return (dividend / divisor);
}
//---------- SA Right Circular Cylinder --------
double SARCC(double radius, double height) {
return ((2 * M_PI*radius*height) + (2 * M_PI*pow(radius, 2)));
}
//--------- Vol Right Circular Cylinder --------
double VolRCC(double radius, double height) {
return (M_PI*pow(radius, 2)*height);
}
//---------- SA Right Circular Cone --------
double SARCCone(double radius, double height) {
return ((M_PI*radius)*(radius + (sqrt(pow(height, 2) + pow(radius, 2)))));
}

对于添加,请尝试类似

case 1:                    // addition
operand1 = get_value();
operand2 = get_value();
answer = operand1 + operand2;
cout << operand1 << " + " << operand2 << " = " << answer << endl;
break;

然后对所有其他情况执行相同的操作,并删除cout << endl << "Answer= " << answer << endl;行。

解决这个问题的简单方法是有一个大cout << var1 << ' + ' << var2 << ' = ' << answer << endl;,对于你的计算器可以做的每种计算。当你做圆锥体或其他任何东西的表面积时,这可能会很长,如果你犯了一个错误,你说你正在评估的公式和你实际正在评估的公式可能不匹配。

处理此问题的正确方法可能是使用表达式树系统。你会有一个类(ExpressionNode(来表示运算的实例,如加法、除法或提高到幂,或者只是一个文字数字,如5或Pi。该类将有一个方法来计算它所表示的表达式并返回一个double,如有必要,首先计算操作的左右参数。它还将具有打印其表示的表达式的方法,如果它只是一个数字,则打印出数字,或者打印左参数' + ',然后打印右参数(如果它表示加法(,依此类推。

然后,不同的计算器函数将接收输入并返回的不是最终计算的值,而是表示如何计算这些值的表达式树,主循环将打印出计算答案的表达式,然后对其进行计算并打印结果。

如果您不想自己实现这一切,Boost 库提供了一个"Proto"库,为您提供表达式树和评估它们的方法,但其文档假设您了解一些关于将表达式解析为表达式树的工作原理、什么是"终端"等。但是他们的教程有一个计算器示例。

最新更新