帮一个同学写的,非计算机类专业,应付交差,也没什么功能,两个数的加减乘除运算,以及三角函数的运算。要求用到模板、运算符重载和异常处理。

一直以来都是用的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. 1.C#WinForm基础制作简单计算器

    利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...

  2. 菜鸟学Android编程——简单计算器《一》

    菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...

  3. PAT 06-1 简单计算器

    想看一般简单计算器实现的看客不好意思了,这不是你想要点东西,此处题设为“只能进行加减乘除”.“都是整数”.”优先级相同“和"从左到右".此题来自PAT(http://www.pat ...

  4. php大力力 [005节] php大力力简单计算器001

    2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...

  5. PHP实现简单计算器

    <!--简单的计算器--> <!DOCTYPE html> <html> <head> <title>PHP实现简单计算器</titl ...

  6. c#部分---网吧充值系统;简易的闹钟;出租车计费;简单计算器;对战游戏;等额本金法计算贷款还款利息等;随机生成10个不重复的50以内的整数;推箱子;

    网吧充值系统namespace ConsoleApplication1 { class Program { struct huiyuan { public string name; public st ...

  7. JavaWeb学习记录(二十)——Model1模式(javaBean+jsp)实现简单计算器案例

    ¨JSP技术提供了三个关于JavaBean组件的动作元素,即JSP标签,它们分别为: ¨<jsp:useBean>标签:用于在JSP页面中查找或实例化一个JavaBean组件. ¨< ...

  8. 一个用WPF做的简单计算器源代码

    一.界面设计XAML代码 <Window x:Class="fengjisuanqi.MainWindow" xmlns="http://schemas.micro ...

  9. hdu 1237 简单计算器

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单计算器 Description 读入一个只包含 +, -, *, / 的非负整数计算表达式, ...

  10. 李洪强漫谈iOS开发[C语言-042]-简单计算器

    李洪强漫谈iOS开发[C语言-042]-简单计算器

随机推荐

  1. Syslog

    一.简介 syslog是一种工业标准的协议,可用来记录设备的日志.在UNIX系统,路由器.交换机等网络设备中,系统日志(System Log)记录系统中任何时间发生的大小事件.管理者可以通过查看系统记 ...

  2. 【转】LINUX 5 常用ftp telnet配置

    LINUX 5 常用ftp telnet配置 一.解决远程登陆乱码问题 目标:在xwindow和其console中使用中文界面,在纯console中使用英文 在/etc/profile最后加上一行 e ...

  3. 动手学习TCP: 环境搭建

    前一段时间通过Wireshark抓包,定位了一个客户端和服务器之间数据传输的问题.最近就抽空看了看<TCP/IP详解 卷1>中关于TCP的部分,书中用了很多例子展示了TCP/IP协议中的一 ...

  4. Hadoop_HDFS架构和HA机制

    Hadoop学习笔记总结 01.HDFS架构 1. NameNode和ResourceManager NameNode负责HDFS,从节点是DataNode:ResourceManager负责MapR ...

  5. #include <NOIP2009 Junior> 细胞分裂 ——using namespace wxl;

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...

  6. 《TCP/IP详解 卷一》读书笔记-----广播&多播&IGMP

    1.广播和多播都只适用于UDP,因为TCP是面向连接的,需要将两台主机的两个进程绑定在一起,即IP地址和端口对 2.通常,网卡能看到网络中的每一个数据帧,但是往往它只接受目的地址与自己MAC地址相同的 ...

  7. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  8. UVA 12266 Stock prices --优先队列

    优先队列. 做法:维护两个优先队列:quesell  和  quebuy, 一个是小值优先,一个是大值优先.每次push的时候,都取各自的Top元素,比较价格,如果卖的比卖的出价低,则成交,各自的要买 ...

  9. Android系列之网络(二)----HTTP请求头与响应头

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  10. Unity3D手势及重力加速度(神庙逃亡操作)

    Unity实现神庙逃亡操作 现在特别火的跑酷游戏<神庙逃亡>是用Unity3D引擎开发的 游戏的操作:用手指拨动(划动)人物就转向,利用手机的重力感应进行人物左右调整. 今天用Unity来 ...