第七周作业
来源:互联网 发布:淘宝买花种子哪家靠谱 编辑:程序博客网 时间:2024/05/17 10:09
- #include<iostream>
- using namespace std;
- void display()
- {
- cout<<"This is an example."<<endl;
- }
- int main()
- {
- display();
- return 0;
- }
定义有参函数求两数中最小值
- #include<iostream>
- using namespace std;
- double min(double x,double y)
- {
- return x<y?x:y;
- }
- int main()
- {
- cout<<min(6.0,5.0)<<endl;
- return 0;
- }
- #include<iostream>
- using namespace std;
- double circleArea(double);
- int main()
- {
- double area=circleArea(5.0);
- cout<<"area="<<area<<endl;
- return 0;
- }
- double circleArea(double r)
- {
- double pi=3.14;
- double area=pi*r*r;
- return area;
- }
计算a,b之和
- #include<iostream>
- using namespace std;
- int sum(int x,int y)
- {
- int temp;
- temp=x+y;
- return temp;
- }
- int main()
- {
- int a,b,c;
- a=10;b=5;
- c=sum(a,b);
- cout<<a<<"+"<<b<<"="<<c<<endl;
- return 0;
- }
实参函数的顺序
- #include<iostream>
- using namespace std;
- int ncomp(int i,int j)
- {
- if(i>j)return 1;
- if(i==j)return 0;
- return -1;
- }
- int main()
- {
- int k=2;
- int n=ncomp(k,++k);
- cout<<n;
- return 0;
- }
结果为0
输入两整数,比较大小输出较大数
- #include<iostream>
- using namespace std;
- int max(int u,int v)
- {
- int w;
- w=u>v?u:v;
- return w;
- }
- int main()
- {
- int a,b,c;
- cout<<"please input two numbers:";
- cin>>a>>b;
- c=max(a,b);
- cout<<"a="<<a<<"b="<<b<<endl;
- cout<<"max is"<<c<<endl;
- return 0;
- }
求10 的平方
- #include<iostream>
- using namespace std;
- int sqr(int x)
- {
- x=x*x;
- return x;
- }
- int main()
- {
- int t=10;
- int s=sqr(t);
- cout<<"t="<<t<<'\t'
- <<"sqr("<<t<<")="<<s<<endl;
- return 0;
- }
值调用实现两个数据互换.
- #include<iostream>
- using namespace std;
- void swap(int u,int v);
- int main()
- {
- int a=3;
- int b=4;
- cout<<"a="<<a<<"\tb="<<b<<endl;
- swap(a,b);
- cout<<"a="<<a<<"\tb="<<b<<endl;
- return 0;
- }
- void swap(int u,int v)
- {
- int temp;
- temp=u;
- u=v;
- v=temp;
- }
- #include<iostream>
- using namespace std;
- void swap(int &u,int &v);
- int main()
- {
- int a=3;
- int b=4;
- cout<<"a="<<a<<"\tb="<<b<<endl;
- swap(a,b);
- cout<<"a="<<a<<"\tb="<<b<<endl;
- return 0;
- }
- void swap(int &u,int &v)
- {
- int temp;
- temp=u;
- u=v;
- v=temp;
- }
RETURN的作用
- #include<iostream>
- using namespace std;
- void display(int x,float y)
- {
- cout<<x<<""<<y;
- return;
- }
- int main()
- {
- float a;
- int b;
- cin>>b>>a;
- display(b,a);
- return 0;
- }
不同类型参数的示例程序(求一元二次方程的根)
- #include<iostream>
- #include<cmath>
- using namespace std;
- void GetRoots(/*in*/double,/*in*/double,/*in*/double,/*out*/double&,/*out*/double&);
- //函数GetRoots用于求一元二次方程的两个根;
- //前三个形参为流入参数,是值调用,分别用于接收主调函数传递的三个系数值;
- //后两个形参为流出参数,是引用调用,将计算后的两面三刀个根回传给主调函数。
- int main()
- {
- double a,b,c;
- double root1,root2;
- cout<<"input a,b,c:"<<endl;
- cin>>a>>b>>c;
- GetRoots(a,b,c,root1,root2);
- cout<<"root1="<<root1<<"rott2="<<root2<<endl;
- return 0;
- }
- void GetRoots(double a,double b,double c,double& root1,double& root2)
- {
- double temp;
- temp=b*b-4.0*a*c;
- root1=(-b+sqrt(temp))/(2.0*a);
- root2=(-b-sqrt(temp))/(2.0*a);
- }
递归求介乘
- #include<iostream>
- using namespace std;
- float Factorial(int n);
- int main()
- {
- int a;
- float f;
- cout<<"input an integer number:";
- cin>>a;
- f=Factorial(a);
- cout<<a<<"!="<<f<<endl;
- return 0;
- }
- float Factorial(int n)
- {
- float fact;
- if(n==0)
- fact=1;
- else
- fact=n*Factorial(n-1);
- return fact;
- }
0 0
- 第七周作业3
- 第七周作业1
- 第七周作业2
- 第七周作业 1
- 第七周作业三
- 第七周作业
- 第七周-小作业
- 第七周作业
- 第七周作业1
- 第七周作业
- 第七周C++作业
- 第七周作业
- 第七周C++作业
- 第七周作业1
- 第七周作业
- 第七周作业
- 第七周作业
- 第七周作业1
- unity3d KeyCode各键值说明
- linux设备驱动之——V4L2
- hdu 2892 多边形与园面积相交
- 第六周作业
- 学渣们都在启动逗B模式
- 第七周作业
- “精于算计”与“精于计算”我们应该更偏重哪方面?
- NDK 与 JNI 的关系
- 关于c语言在函数调用过程中栈布局动态变化的讨论
- 使用Pushlet将消息从服务器端推送到客户端
- 和机器学习和计算机视觉相关的数学
- 取N个随机数字
- Error: Could not create the Java Virtual Machine.
- 第九周作业