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]-简单计算器
随机推荐
- mysql table 最新更新时间
查看表的最后mysql修改时间 SELECT TABLE_NAME,UPDATE_TIME FROM information_schema.tables where TABLE_SCHEMA='d ...
- Android增加v7 appcompat源码
1.File ---- Import---- Existing Android Code Into Workspace 2.选择 <sdk>/extras/android/support/ ...
- paypal IPN 接口返回INVALID参数可能问题
工作于浏览器Chrome时,登录IPN Simulator页发送测试数据,payment_date栏位值中出现乱码,导致无法返回正确的VERIFIED,在此记录.
- DirectX API 编程起步 #02 窗口的诞生
在这篇文章里我们先用 windows API 制作一个窗口出来,以后再用 DirectX API 渲染的东西就会显示在这里,控制台那黑白的画面肯定是没法用的. 每次的代码都会更新到Github 首先贴 ...
- 聚合数据天气预报API-ajax 通过城市名取数据
如需要,可申请聚合数据天气预报API:https://www.juhe.cn/docs/api/id/39,并生成AppKey. 接口地址:http://v.juhe.cn/weather/index ...
- css3属性column知多少
CSS3 可以将文本内容设计成像报纸一样的多列布局.像下面这样: 这样的布局称为"多列布局". 对多列属性分别进行学习: 对于 column 的所有属性,ie10+ 支持,fire ...
- 【Android 我的博客APP】1.抓取博客首页文章列表内容——网页数据抓取
打算做个自己在博客园的博客APP,首先要能访问首页获取数据获取首页的文章列表,第一步抓取博客首页文章列表内容的功能已实现,在小米2S上的效果图如下: 思路是:通过编写的工具类访问网页,获取页面源代码, ...
- 翻译《Writing Idiomatic Python》(二):函数、异常
原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...
- android调试模拟器启动太慢,怎样才能更快的调试程序呢?
答: 1. 模拟器不关,直接按调试按钮系统会自动重新安装软件的.
- AppScan8.0简单扫描
上篇文章介绍了如何在WindowsXP中安装AppScan8.0,接着本篇就来说说怎么进行一次简单的扫描吧. AppScan8.0开始扫描 1.新建扫描,选择“常规扫描”,如下图: (常规.快速.综合 ...