新增功能,实时说明,可以求平方根,并且可以让结果变成整型。
代码如下:
#include<iostream>
using namespace std;
void main();
void shuoming()
{
cout << "当符号为n时退出,为q时清空" << endl;
cout << "输入n退出,输入q清空" << endl;
cout << "输入c查看上一次结果" << endl;
cout << "输入h求平方根" << endl;
cout << "输入z去掉小数点后面的数字" << endl;
system("pause");
system("cls");
main();
}
float result_arr[2] = { 0 }; // 存放两次结果的数组
int arr_index = 0; // 当前存放结果的数组索引
void show_results()
{
cout << "上一次计算结果是: " << result_arr[1] << endl;
system("pause");
}
void sssm()
{
cout << "-----------------" << endl;
cout << "| 1、+加法 |" << endl;
cout << "-----------------" << endl;
cout << "| 2、-减法 |" << endl;
cout << "-----------------" << endl;
cout << "| 3、*乘法 |" << endl;
cout << "-----------------" << endl;
cout << "| 4、/除法 |" << endl;
cout << "-----------------" << endl;
cout << "| 5、^平方 |" << endl;
cout << "-----------------" << endl;
cout << "| 6、h开方 |" << endl;
cout << "------------------------------------" << endl;
cout << "| 7、c是查看上一次结果 |" << endl;
cout << "------------------------------------" << endl;
cout << "| 8、z是去掉小数点后面的数字 |" << endl;
cout << "------------------------------------" << endl;
}void main()
{
float num1 = 0, num2 = 0, final;
char fh = 0;
char select=0;
cout << "欢迎使用简易计算器" << endl;
cout << "-----------------" << endl;
cout << "| 1、进入 |" << endl;
cout << "------------------" << endl;
cout << "| 2、说明 |" << endl;
cout << "-----------------" << endl;
cin >> select;
if (select == '1')
{
system("cls");
cout << "请输入" << endl;
sssm();
goto Z;
}
else
shuoming();
Z: cin >> num1;
cin >> fh;
int a = 0;
double result;
while (fh != 'n')
{
switch (fh)
{
case'+':cin >> num2; final = num1 + num2; break;
case'-':cin >> num2; final = num1 - num2; break;
case'*':cin >> num2; final = num1 * num2; break;
case'/':cin >> num2; final = num1 / num2; a = num1 - (num2 * static_cast<int>(num1 / num2)); break;
case'^':final = num1 * num1; break;
case'~':cin >> num2; final = num1; for (int i = num2; i > 1; i--) { final = num1 * final; } break;
case'c': show_results(); cout<<"请输入是否要调用上一次结果(y/n):"<<endl;cin>>select;if(select=='y')final=result_arr[1];break; // 查看之前的结果
case 'h':if (num1 < 0)cout << "负数没有平方根!" << endl; else final = sqrt(num1); break;
case'z':goto a1;
default:cout << "请重新输入" << endl; break;
}
system("cls");
if (a != 0)
{
a1: int final1 = final;
printf("--------------------\n");
printf("|结果是%d····%d|\n", final1, a);
printf("--------------------\n");
sssm();
final = final1;
a = 0;
result_arr[arr_index % 2] = final; // 存放结果到数组中
arr_index++;
}
else
{
printf("-------------------------\n");
printf("|结果是%.2f\t|\n", final);
printf("-------------------------\n");
sssm();
result_arr[arr_index % 2] = final; // 存放结果到数组中
arr_index++;
}
a: cin >> fh;
if (fh == 'q')
{
printf("已清空\n");
goto Z;
}
else
{
num1 = final;
continue;
}
}
}