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 }

 

好吧,其實我也不知道為什么要求用模板和運算符重載,感覺沒什么必要,典型的作業代碼,不過也可能是我思想的局限性。總之,就這樣吧。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM