C++初学(1) 简单的加减乘除取余运算代码
//---------------+-*/%算法----------------------------------------------------------
#include <iostream>
using namespace std;
// 函数原型声明
int Add(int e1, int e2);
int Sub(const int*pe1, const int*pe2);
int Mul(const int&re1, const int&re2);
int Div(int e1, int e2)throw(int); // 分母不能为0,否则抛出除0错
int Mod(int e1,int e2)throw(int,long); //分子 分母不能为0,否则抛出无意义错或除0错 void main() {
int e1, e2, result;
char optr; while (true) {
cout <<
"Please type in an expression which likes a @b,type 'q' to quit" << endl;
cin >> optr;
if (optr == 'q' || optr == 'Q')
break; // 结束循环
cin.putback(optr); // 将读入的字符退回到流中,让它可以被重新读入
cin >> e1;
cin >> optr;
cin >> e2; switch(optr) {
case '+':
result = Add(e1, e2);
break;
case '-':
result = Sub(&e1, &e2);
break;
case '*':
result = Mul(e1, e2);
break;
case '/':
result = Div(e1, e2);
break;
case '%':
result = Mod(e1, e2);
break;
}
}
system("pause");
} int Add(int e1, int e2) {
int result = e1 + e2;
cout << '=' << result << endl;
return result;
} int Sub(const int *pe1, const int *pe2) {
int result = *pe1 - *pe2; // 指针指向的内存地址里面存的数值相减
cout << '=' << result << endl;
return result;
} int Mul(const int &re1, const int &re2) {
int result = re1 * re2;
cout << '=' << result << endl;
return result;
} int Div(int e1, int e2)throw(int) {
try {
if (e2 == )
throw ;
int result = e1 / e2;
cout << '=' << result << endl;
return result;
}
catch(int) {
cout << "Denominator cannot be zero" << endl;
} } int Mod(int e1,int e2)throw(int,long)
{
try
{
if(e1==)
throw int();
if(e2==)
throw long(); int result=e1%e2;
cout << '=' << result << endl;
return result;
}
catch(int)
{
cout<<"It doesn't make sense when numerator is zero"<<endl;
}
catch(long)
{
cout<<"Denominator cannot be zero"<<endl;
}
}
C++初学(1) 简单的加减乘除取余运算代码的更多相关文章
- 使用PHP语言制作具有加减乘除取余功能的简单计算器
准备工作: 使用环境 :PHPStudy 开启Apache和Mysql 打开代码编辑器 <!DOCTYPE html> <html lang="en"> & ...
- 洛谷 P1226 【模板】快速幂||取余运算
题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...
- 快速幂 cojs 1130. 取余运算
cojs 1130. 取余运算 ★ 输入文件:dmod.in 输出文件:dmod.out 简单对比时间限制:10 s 内存限制:128 MB [题目描述] 输入b,p,k的值,求b^p ...
- 为什么Java的hash表的长度一直是2的指数次幂?为什么这个(hash&(h-1)=hash%h)位运算公式等价于取余运算?
1.什么是hash表? 答:简单回答散列表,在hash结构散列(分散)存放的一种数据集结构. 2.如何散列排布,如何均匀排布? 答:取余运算 3.Java中如何实现? 答:hash&(h-1) ...
- 洛谷P1226 【模板】快速幂||取余运算
题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod k=s” s为运算结果 S1: ...
- php取余运算(%) 注意事项
<?php //php取余运算(%)的那点事,php取余数用%符号,即为模运算 //理论上应该输出45才对,可是实际运算结果是44 $val=9.45; $result=$val*100; ec ...
- LuoguP1226 【模板】快速幂||取余运算
题目链接:https://www.luogu.org/problemnew/show/P1226 第一次学快速幂,将别人对快速幂原理的解释简要概括一下: 计算a^b时,直接乘的话计算次数为b,而快速幂 ...
- 洛谷——P1226 取余运算||快速幂
P1226 取余运算||快速幂 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod ...
- python 取余运算
python中取余运算逻辑如下: 如果a 与d 是整数,d 非零,那么余数 r 满足这样的关系: a = qd + r , q 为整数,且0 ≤ |r| < |d|. 经过测试可发现,pytho ...
随机推荐
- pnputil
http://mb.yidianzixun.com/article/0FYSZgnB?s=mb&appid=mibrowser C:\Users\Administrator>pnputi ...
- MySql常用函数积累
--MySql查看表结构 select column_name,data_type,CHARACTER_MAXIMUM_LENGTH,column_comment from information_s ...
- 赵雅智_Swift(2)_swift常量和变量
分号 Swift 并不强制要求你在每条语句的结尾处使用分号(;) 你打算在同一行内写多条独立的语句必需要用分号 let cat = "? ?? ? "; println(cat) ...
- C#中Queue<T>类的使用以及部分方法的源代码分析
Queue<T>类 表示对象的先进先出集合. 队列在按接收顺序存储消息方面很实用,以便于进行顺序处理. 存储在 Queue,<T> 中的对象在一端插入,从还有一端移除. Que ...
- 《Deep Learning》全书已完稿_附全书电子版
Deep Learning第一篇书籍最终问世了.站点链接: http://www.deeplearningbook.org/ Bengio大神的<Deep Learning>全书电子版在百 ...
- springmvc学习笔记(18)-json数据交互
springmvc学习笔记(18)-json数据交互 标签: springmvc springmvc学习笔记18-json数据交互 springmvc进行json交互 环境准备 加入json转换的依赖 ...
- openwrt procd
接着前面写过的一篇关于 procd 的笔记. procd 在 STATE_INIT 时会运行 /etc/inittab 中描述的几个级别指定程序. procd_inittab_run("re ...
- openwrt hotplug
由内核发出 event 事件. kobject_uevent() 产生 uevent 事件(lib/kobject_uevent.c 中), 产生的 uevent 先由 netlink_broadca ...
- 翻译:A Tutorial on the Device Tree (Zynq) -- Part II
A Tutorial on the Device Tree (Zynq) -- Part II 设备树结构 Zynq的设备树如下: /dts-v1/; / { #address-cells = < ...
- mysql关闭skip-grant-tables快速重置mysql密码
如果你忘记了mysql密码几乎是没有什么好办法可以直接修改密码了,但我们可以在my.ini把加上skip-grant-tables,然后重启mysql就不需要密码了,这时我们再修改root密码,最后再 ...