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]-简单计算器
随机推荐
- 图片代替radio
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 浅析selenium的page object模式
selenium目前比较流行的设计模式就是page object,那么到底什么是page object呢,简单来说,就是把页面作为对象,在使用中传递页面对象,来使用页面对象中相应的成员或者方法,能更好 ...
- bzoj-2748 2748: [HAOI2012]音量调节(dp)
题目链接: 2748: [HAOI2012]音量调节 Time Limit: 3 Sec Memory Limit: 128 MB Description 一个吉他手准备参加一场演出.他不喜欢在演出 ...
- jQuery时间轴插件:jQuery Timelinr
前言 这是一款可用于展示历史和计划的时间轴插件,尤其比较适合一些网站展示发展历程.大事件等场景.该插件基于jQuery,可以滑动切换.水平和垂直滚动.支持键盘方向键.经过扩展后可以支持鼠标滚轮事件. ...
- 【转】Python中string的strip,lstrip,rstrip用法
Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是 ...
- alert,confirm和prompt
1.警告消息框alertalert 方法有一个参数,即希望对用户显示的文本字符串.该字符串不是 HTML 格式.该消息框提供了一个“确定”按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说,用 ...
- H5 canvas绘制出现模糊的问题
在之前做移动端小游戏幸运转盘.九宫格转盘,使用到了 canvas ,也是第一次在项目中使用 canvas 来实现. 近期测试人员反应 canvas 绘制的内容太模糊,心想着用 canvas 绘制出来的 ...
- 一个DOM元素绑定多个事件时,先执行冒泡还是捕获
绑定在被点击元素的事件是按照代码顺序发生,其他元素通过冒泡或者捕获“感知”的事件,按照W3C的标准,先发生捕获事件,后发生冒泡事件.所有事件的顺序是:其他元素捕获阶段事件 -> 本元素代码顺序事 ...
- AVR/Arduino定时/计数器、中断入门
在Arduino中,可以使用AnalogWrite来使用硬件产生490Hz/980Hz的pwm波,并可根据参数来设定占空比.不了解这个的同学可以去AnalogWrite学习下,SecretsOfArd ...
- 转载:有关SQL server connection Keep Alive 的FAQ(2)
转: http://blogs.msdn.com/b/apgcdsd/archive/2012/05/18/sql-server-connection-keep-alive-faq-2.aspx 在下 ...