1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;
2.编程实现课本每一个编程习题。
例5.1
#include<iostream>
using namespace std;
void display()
{
cout<<"This is an example"<<endl;
}
int main()
{
display();
return 0;
}
例5.2
#include<iostream>
using namespace std;
double min(double x, double y)
{
return x < y ? x : y ;
}
int main()
{
cout<<min(6.5,5.0)<<endl;
return 0;
}
例5.3
#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;
}
例5.4
#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;
}
例5.5
#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;
}
例5.6
#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;
}
例5.7
#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;
}