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

一直以来都是用的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. SQL Server 2008 R2——VC++ ADO 操作 存储过程

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  2. Java异常处理和设计【转】

    Java异常处理和设计 在程序设计中,进行异常处理是非常关键和重要的一部分.一个程序的异常处理框架的好坏直接影响到整个项目的代码质量以及后期维护成本和难度.试想一下,如果一个项目从头到尾没有考虑过异常 ...

  3. APACHE重写去除入口文件index.php

    下面我说下 apache 下 ,如何 去掉URL 里面的 index.php 例如: 你原来的路径是: localhost/index.php/index 改变后的路径是: localhost/ind ...

  4. ASP.NET中iframe框架点击左边页面链接,右边显示链接页面内容

    首先是主页面main.aspx <body style="background-color: #AFEEEE"> <form id="form1&quo ...

  5. Linux gcc命令

    一.简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objectiv ...

  6. Java SWT 做计算器。

    java  --  swt  - -  计算器 环境搭建 安装java.eclipse.以及对应的swt插件. 开始工程 建立工程: 在java下建立一个在其他 —- WindowsBuilder — ...

  7. log4j日志优先级导致的不输出日志

    在sae部署微信代码的时候,发现它的默认日志很不友好,看起来很费劲,详细看了一下说明发现它可以根据log4j的输出级别而分类输出,就拖了一个log4j的xml文件扔进项目代码,然后部署运行,发现没有日 ...

  8. 探索 OpenStack 之(15):oslo.messaging 和 Cinder 中 MessageQueue 消息的发送和接收

    前言:上一篇文章 只是 RabbitMQ 的科普,本文将仔细分析 Cinder 中 RabbitMQ 的各组件的使用.消息的发送和接收等.由于各流程步骤很多,本文只会使用若干流程图来加以阐述,尽量做到 ...

  9. elastic search查询命令集合

    Technorati 标签: elastic search,query,commands 基本查询:最简单的查询方式 query:{"term":{"title" ...

  10. Linux与Windows 解压乱码 UTF8BOM读取问题

    Linux 与 Windows 文件乱码问题 这几天需要在linux下用CNN跑数据,但是把数据和数据列表list上传到linux下时却出现了不少乱码的问题.将这两天碰到的编码问题简单的总结一下. 1 ...