第二章 编程练习答案
补发第二章的代码,比较简单,感觉没什么要注释的。后面没写的几章有空接着补。
1.
#include<iostream>
int main()
{
using namespace std;
cout << "Harry";
cout << endl;
cout << "183********";
return 0;
}
2.
#include<iostream>
int longtoyard(int);
int main()
{
using namespace std;
cout << "Enter the distance in long: ";
int n;
cin >> n;
int m=longtoyard(n);
cout << n <<" long = ";
cout << m <<" yard." << endl;
return 0;
}
int longtoyard(int t)
{
int a;
a=t*220;
return a;
}
3
#include<iostream>
void A(int);
void B(int);
using namespace std;
int main()
{
A(2);
B(2);
return 0;
}
void A(int n)
{
int i;
for(i = 0; i < n; i++)
cout << "Three blind mice" << endl;
}
void B(int n)
{
int i;
for(i = 0; i < n; i++)
cout << "See how they run" << endl;
}
4.
#include<iostream>
int main()
{
using namespace std;
int n,m;
cout << "Enter yout age: ";
cin >> n;
m = n * 12;
cout << "your months: " << m;
return 0;
}
5.
#include<iostream>
double temperature(double);
int main()
{
using namespace std;
double n,m;
cout << "Please enter a Celsius value: ";
cin >> n;
m=temperature(n);
cout << n << " degrees Celsius is "
<< m << " degrees Fahrenheit.";
return 0;
}
double temperature(double n)
{
double m;
m = 1.8 * n + 32.0;
return m;
}
6.
#include<iostream>
double distance(double);
int main()
{
using namespace std;
double n, m;
cout << "Enter the number of light years: ";
cin >> n;
m = distance(n);
cout << n << " light years = "
<< m << " astronomical units.";
return 0;
}
double distance(double n)
{
double m;
m = n * 63240;
return m;
}
7.
#include<iostream>
void time(int, int);
using namespace std;
int main()
{
int h,m;
cout << "Enter the number of hours: ";
cin >> h;
cout << "Enter the number of minutes: ";
cin >> m;
time(h, m);
return 0;
}
void time(int h, int m)
{
cout <<"Time: "<< h <<":"<< m;
}