问题描述
(1)定义五个函数完成加(+),减(-),乘(*),除(/),取余(%)的功能,函数类
型定义为整型;(2)尝试使用 double 类型改变上面的函数,使其能够实现浮点型运算;(3)
在主函数中调用以上函数,并将计算结果显示在屏幕上。
实现程序
代码如下(示例):
#include <iostream>
using namespace std;
int pl(double x,double y) //定义加法函数
{
double p;
p=x+y;
return(p);
}
int mi(double x1,double y1) //定义减法函数
{
double m;
m=x1-y1;
return(m);
}
int multiply(double x2,double y2) //定义乘法函数
{
double mu;
mu=x2*y2;
return(mu);
}
int divide(double x3,double y3) //定义除法函数
{
double d;
d=x3/y3;
return(d);
}
int remainder(int x4,int y4) //定义取余函数
{
int r;
r=x4%y4;
return(r);
}
int main()
{
double a,b,c,c1,c2,c3,c4;
cout<<"please enter two numbers:";
cin>>a>>b;
c=pl(a,b); //调用pl函数
cout<<a<<"+"<<b<<"="<<c<<endl;
c1=mi(a,b); //调用mi函数
cout<<a<<"-"<<b<<"="<<c1<<endl;
c2=multiply(a,b); //调用multiply函数
cout<<a<<"*"<<b<<"="<<c2<<endl;
c3=divide(a,b); //调用divide函数
cout<<a<<"/"<<b<<"="<<c3<<endl;
c4=remainder(a,b); //调用remainder函数
cout<<a<<"%"<<b<<"="<<c4<<endl;
return 0;
}
实验结果
如下(示例):
please enter two numbers:2 3
2+3=5
2-3=-1
2*3=6
2/3=0
2%3=2
--------------------------------
Process exited after 3.126 seconds with return value 0
请按任意键继续. . .
尝试使用 double 类型改变上面的函数,使其能够实现浮点型运算,将上面代码函数定义类型改成double就行,其他不变。
please enter two numbers:2 3
2+3=5
2-3=-1
2*3=6
2/3=0.666667
2%3=2
--------------------------------
Process exited after 2.597 seconds with return value 0
请按任意键继续. . .
喜欢的朋友点赞加关注哦!欢迎评论,给出更好的建议和意见哦!