c++实现简单计算器

时间:2022-05-07
本文章向大家介绍c++实现简单计算器,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

帮一个同学写的,非计算机类专业,应付交差,也没什么功能,两个数的加减乘除运算,以及三角函数的运算。要求用到模板、运算符重载和异常处理。

一直以来都是用的java,没怎么用过c++,就当是复习了一下c++语法。

代码如下:

  1 #include<iostream>
  2 #include<string>
  3 #include<cmath>
  4 #include<cstdlib>
  5 
  6 using namespace std;
  7 
  8 //四则运算
  9 template <class T> class ElementaryArithmetic{
 10 private:
 11     T result;
 12     T operand1, operand2;
 13     char operators;
 14 public:
 15     //四则运算
 16     void Calculate(); 
 17     //加法运算
 18     void add(T, T); 
 19     //减法运算
 20     void subtraction(T, T); 
 21     //乘法运算
 22     void multiplication(T, T);
 23     //除法运算
 24     void divide(T, T); 
 25     //输出运算符重载
 26     template <class E> friend ostream &operator<<(ostream&, ElementaryArithmetic<E> &);
 27 };
 28 
 29 //四则运算
 30 template <class T> void ElementaryArithmetic<T>::Calculate(){
 31     int type;
 32 
 33 loop1:
 34     system("cls");
 35     cout << endl << "*******************" << endl;
 36     cout << "*   1.加法运算    *" << endl; 
 37     cout << "*   2.减法运算    *" << endl;  
 38     cout << "*   3.乘法运算    *" << endl;  
 39     cout << "*   4.除法运算    *" << endl; 
 40     cout << "*******************" << endl << endl;
 41     cout << "请输入菜单项(1-4):";
 42     try{
 43         cin >> type;
 44         if (type != 1 && type != 2 && type != 3 && type != 4)
 45             throw 1;
 46     }
 47     catch (int e){
 48         cout << endl << "输入错误,请重新输入选项...";
 49         system("pause");
 50         goto loop1;
 51     }
 52     
 53     cout << endl << "请输入两个数字:";
 54     cin >> operand1 >> operand2;
 55     if (type == 1){
 56         add(operand1, operand2);
 57         operators = '+';
 58     }
 59     else if (type == 2){
 60         subtraction(operand1, operand2);
 61         operators = '-';
 62     }
 63     else if (type == 3){
 64         multiplication(operand1, operand2);
 65         operators = '*';
 66     }
 67     else if (type == 4){
 68         divide(operand1, operand2);
 69         operators = '/';
 70     }
 71     
 72 }
 73 
 74 //加法运算
 75 template <class T> void ElementaryArithmetic<T>::add(T operand1,T operand2){
 76     result = operand1 + operand2;
 77 }
 78 
 79 //减法运算
 80 template <class T> void ElementaryArithmetic<T>::subtraction(T operand1, T operand2){
 81     result = operand1 - operand2;
 82 }
 83 
 84 //乘法运算
 85 template <class T> void ElementaryArithmetic<T>::multiplication(T operand1, T operand2){
 86     result = operand1 * operand2;
 87 }
 88 
 89 //除法运算
 90 template <class T> void ElementaryArithmetic<T>::divide(T operand1, T operand2){
 91     try{
 92         //除数为0,出现异常
 93         if ((operand2 - 0) < 1e-8 && (operand2 - 0) > -1e-8)
 94             throw 0;
 95     }
 96     catch (int){
 97         throw ;
 98     }
 99     result = operand1 / operand2;
100 }
101 
102 //输出运算符重载
103 template <class E> ostream& operator<<(ostream &os, ElementaryArithmetic<E> &result){
104     os << endl << "计算结果 : " << result.operand1 << result.operators << result.operand2 << '=' << result.result << endl;
105     return os;
106 }
107 
108 //三角函数
109 class Trigonometric{
110 private:
111     double radian;
112     string type;
113     double result;
114 public:
115     //三角函数计算
116     void Calculate();
117     //输出运算符重载
118     friend ostream &operator<<(ostream&, Trigonometric &);
119 };
120 
121 //三角函数计算
122 void Trigonometric::Calculate(){
123     int option;
124 
125 loop2:
126     system("cls");
127     cout << "*******************" << endl;
128     cout << "*    1.求正弦      *"<< endl; 
129     cout << "*    2.求余弦      *"<< endl;
130     cout << "*    3.求正切      *"<< endl;
131     cout << "*******************" << endl << endl;
132     cout << "请输入菜单项(1-3):";
133     try{
134         cin >> option;
135         if (option != 1 && option != 2 && option != 3 && option != 4)
136             throw 2;
137     }
138     catch (int e){
139         cout << endl << "输入错误,请重新输入选项..." ;
140         system("pause");
141         goto loop2;
142     }
143     
144 
145     cout << endl << "请输入弧度:";
146     cin >> radian;
147 
148     if (option == 1){
149         result = sin(radian);
150         type = "sin";
151     }
152     else if (option == 2){
153         result = cos(radian);
154         type = "cos";
155     }
156     else if (option == 3){
157         result = tan(radian);
158         type = "tan";
159     }
160 }
161 
162 //输出运算符重载
163 ostream &operator<<(ostream &os, Trigonometric &result){
164     os << endl << "计算结果 : " << result.type << "(" << result.radian << ") = " << result.result << endl;
165     return os;
166 }
167 
168 int main(){
169     int type;
170 
171 loop:
172     while (true){
173         system("cls");
174         cout << "*******主菜单**********" << endl;
175         cout << "*                     *" << endl;
176         cout << "*   1. 四则运算       *" << endl;
177         cout << "*   2. 三角函数       *" << endl;
178         cout << "*   3. 退出程序       *" << endl;
179         cout << "*                     *" << endl;
180         cout << "***********************" << endl << endl;
181         cout << "请输入菜单项(1-3):";
182 
183         try{
184             cin >> type;
185             if (type != 1 && type != 2 && type != 3)
186                 throw - 1;
187         
188             if (type == 1){
189                 ElementaryArithmetic<double> calc;
190                 calc.Calculate();
191                 cout << calc;
192             }
193             else if (type == 2){
194                 Trigonometric calc;
195                 calc.Calculate();
196                 cout << calc;
197             }
198             else if (type == 3)
199                 break;
200         }
201         catch (int e){
202             if (e == -1){
203                 cout << endl << "输入错误,请重新输入选项...";
204                 system("pause");
205                 goto loop;
206             }
207             else if (e == 0)
208                 cout << "除数不能为 0 " << endl;
209             
210         }
211         cout << endl;
212         system("pause");
213     }
214     return 0;
215 }

好吧,其实我也不知道为什么要求用模板和运算符重载,感觉没什么必要,典型的作业代码,不过也可能是我思想的局限性。总之,就这样吧。