c++实现简单计算器
帮一个同学写的,非计算机类专业,应付交差,也没什么功能,两个数的加减乘除运算,以及三角函数的运算。要求用到模板、运算符重载和异常处理。
一直以来都是用的java,没怎么用过c++,就当是复习了一下c++语法。
代码如下:
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib> using namespace std; //四则运算
template <class T> class ElementaryArithmetic{
private:
T result;
T operand1, operand2;
char operators;
public:
//四则运算
void Calculate();
//加法运算
void add(T, T);
//减法运算
void subtraction(T, T);
//乘法运算
void multiplication(T, T);
//除法运算
void divide(T, T);
//输出运算符重载
template <class E> friend ostream &operator<<(ostream&, ElementaryArithmetic<E> &);
}; //四则运算
template <class T> void ElementaryArithmetic<T>::Calculate(){
int type; loop1:
system("cls");
cout << endl << "*******************" << endl;
cout << "* 1.加法运算 *" << endl;
cout << "* 2.减法运算 *" << endl;
cout << "* 3.乘法运算 *" << endl;
cout << "* 4.除法运算 *" << endl;
cout << "*******************" << endl << endl;
cout << "请输入菜单项(1-4):";
try{
cin >> type;
if (type != && type != && type != && type != )
throw ;
}
catch (int e){
cout << endl << "输入错误,请重新输入选项...";
system("pause");
goto loop1;
} cout << endl << "请输入两个数字:";
cin >> operand1 >> operand2;
if (type == ){
add(operand1, operand2);
operators = '+';
}
else if (type == ){
subtraction(operand1, operand2);
operators = '-';
}
else if (type == ){
multiplication(operand1, operand2);
operators = '*';
}
else if (type == ){
divide(operand1, operand2);
operators = '/';
} } //加法运算
template <class T> void ElementaryArithmetic<T>::add(T operand1,T operand2){
result = operand1 + operand2;
} //减法运算
template <class T> void ElementaryArithmetic<T>::subtraction(T operand1, T operand2){
result = operand1 - operand2;
} //乘法运算
template <class T> void ElementaryArithmetic<T>::multiplication(T operand1, T operand2){
result = operand1 * operand2;
} //除法运算
template <class T> void ElementaryArithmetic<T>::divide(T operand1, T operand2){
try{
//除数为0,出现异常
if ((operand2 - ) < 1e- && (operand2 - ) > -1e-)
throw ;
}
catch (int){
throw ;
}
result = operand1 / operand2;
} //输出运算符重载
template <class E> ostream& operator<<(ostream &os, ElementaryArithmetic<E> &result){
os << endl << "计算结果 : " << result.operand1 << result.operators << result.operand2 << '=' << result.result << endl;
return os;
} //三角函数
class Trigonometric{
private:
double radian;
string type;
double result;
public:
//三角函数计算
void Calculate();
//输出运算符重载
friend ostream &operator<<(ostream&, Trigonometric &);
}; //三角函数计算
void Trigonometric::Calculate(){
int option; loop2:
system("cls");
cout << "*******************" << endl;
cout << "* 1.求正弦 *"<< endl;
cout << "* 2.求余弦 *"<< endl;
cout << "* 3.求正切 *"<< endl;
cout << "*******************" << endl << endl;
cout << "请输入菜单项(1-3):";
try{
cin >> option;
if (option != && option != && option != && option != )
throw ;
}
catch (int e){
cout << endl << "输入错误,请重新输入选项..." ;
system("pause");
goto loop2;
} cout << endl << "请输入弧度:";
cin >> radian; if (option == ){
result = sin(radian);
type = "sin";
}
else if (option == ){
result = cos(radian);
type = "cos";
}
else if (option == ){
result = tan(radian);
type = "tan";
}
} //输出运算符重载
ostream &operator<<(ostream &os, Trigonometric &result){
os << endl << "计算结果 : " << result.type << "(" << result.radian << ") = " << result.result << endl;
return os;
} int main(){
int type; loop:
while (true){
system("cls");
cout << "*******主菜单**********" << endl;
cout << "* *" << endl;
cout << "* 1. 四则运算 *" << endl;
cout << "* 2. 三角函数 *" << endl;
cout << "* 3. 退出程序 *" << endl;
cout << "* *" << endl;
cout << "***********************" << endl << endl;
cout << "请输入菜单项(1-3):"; try{
cin >> type;
if (type != && type != && type != )
throw - ; if (type == ){
ElementaryArithmetic<double> calc;
calc.Calculate();
cout << calc;
}
else if (type == ){
Trigonometric calc;
calc.Calculate();
cout << calc;
}
else if (type == )
break;
}
catch (int e){
if (e == -){
cout << endl << "输入错误,请重新输入选项...";
system("pause");
goto loop;
}
else if (e == )
cout << "除数不能为 0 " << endl; }
cout << endl;
system("pause");
}
return ;
}
好吧,其实我也不知道为什么要求用模板和运算符重载,感觉没什么必要,典型的作业代码,不过也可能是我思想的局限性。总之,就这样吧。
c++实现简单计算器的更多相关文章
- 1.C#WinForm基础制作简单计算器
利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...
- 菜鸟学Android编程——简单计算器《一》
菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...
- PAT 06-1 简单计算器
想看一般简单计算器实现的看客不好意思了,这不是你想要点东西,此处题设为“只能进行加减乘除”.“都是整数”.”优先级相同“和"从左到右".此题来自PAT(http://www.pat ...
- php大力力 [005节] php大力力简单计算器001
2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...
- PHP实现简单计算器
<!--简单的计算器--> <!DOCTYPE html> <html> <head> <title>PHP实现简单计算器</titl ...
- c#部分---网吧充值系统;简易的闹钟;出租车计费;简单计算器;对战游戏;等额本金法计算贷款还款利息等;随机生成10个不重复的50以内的整数;推箱子;
网吧充值系统namespace ConsoleApplication1 { class Program { struct huiyuan { public string name; public st ...
- JavaWeb学习记录(二十)——Model1模式(javaBean+jsp)实现简单计算器案例
¨JSP技术提供了三个关于JavaBean组件的动作元素,即JSP标签,它们分别为: ¨<jsp:useBean>标签:用于在JSP页面中查找或实例化一个JavaBean组件. ¨< ...
- 一个用WPF做的简单计算器源代码
一.界面设计XAML代码 <Window x:Class="fengjisuanqi.MainWindow" xmlns="http://schemas.micro ...
- hdu 1237 简单计算器
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单计算器 Description 读入一个只包含 +, -, *, / 的非负整数计算表达式, ...
- 李洪强漫谈iOS开发[C语言-042]-简单计算器
李洪强漫谈iOS开发[C语言-042]-简单计算器
随机推荐
- BaseDao
public class BaseDao { private static Log logger = LogFactory.getLog(BaseDao.class); // 查询数据 public ...
- putExtra方法
[开篇骂几句:fuck]1.扯淡intent.putExtra()怎么使用?2.胡说intent.putExtra(); [扯淡:其实你在问它怎么用的时候,你要明白,你知道不知道这是个什么东东,有必要 ...
- jquery parent和parents,children和find
parent返回匹配元素的父元素的元素集合:parents返回匹配元素的祖先元素的元素集合. children返回匹配元素的子元素的元素集合:find返回匹配元素的后代元素的元素集合.
- uva 12655 Trucks [LCA](树链剖分+MST)
The Subtle Balloons Company (SBC) is the main balloon provider for programming contests; it hashuge ...
- Android资源下载
各个版本,在线源码: http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/ ...
- 聚合数据天气预报API-ajax 通过城市名取数据
如需要,可申请聚合数据天气预报API:https://www.juhe.cn/docs/api/id/39,并生成AppKey. 接口地址:http://v.juhe.cn/weather/index ...
- [转]artDialog
本文转自:http://aui.github.io/artDialog/ http://aui.github.io/artDialog/doc/index.html artDialog —— 经典的网 ...
- [转]Ionic最佳实践-使用模态窗口modal
本文转自:http://m.blog.csdn.net/blog/betreex/45649689 原文地址:Ionic最佳实践-使用模态窗口modal 模态窗口的结构 在Ionic中,模态窗口通过$ ...
- 什么办法可以替代distinct
今天在论坛上看到一个面试题,是说有什么办法可以替代distinct,得到同样的结果.答案都被大家说的差不多了,发现挺有意思的,就记录一下: SQL> select num from t1; ...
- NOIP2010提高组 关押罪犯 -SilverN
(洛谷P1525) 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”( ...