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 }
好吧,其实我也不知道为什么要求用模板和运算符重载,感觉没什么必要,典型的作业代码,不过也可能是我思想的局限性。总之,就这样吧。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- console.log的那件事
- Codeforces Round #668 (Div. 2)A-D
- nodejs源码解析之udp服务器
- LeetCode | 35.搜索插入位置
- Redis | 源码阅读 —— 字符串
- 一个小白的角度看JavaScript Promise 完整指南
- 教会舍友玩 Git (再也不用担心他的学习)
- Python极简入门:数据类型、条件语句、循环语句、异常处理
- 手把手带你入门Python爬虫Scrapy
- 工作中巧用了这几个设计模式重构代码,女同事直呼666
- SourceMap知多少:介绍与实践
- (实战)Node.js 实现抢票小工具&短信通知提醒
- 目标检测 | Anchor free之CenterNet深度解析
- C++第二章 变量与基本类型
- springboot2结合mybatis拦截器实现主键自动生成