C++ Primer Plus第六版编程题(第2、3章)
第2章
题目
程序
2.1:
#include <iostream>
int main()
{
using namespace std;
cout<<"My name is : mxh."<<endl;
cout<<"My address is : university."<<endl;
return 0;
}
2.2:
#include <iostream>
int main()
{
using namespace std;
cout<<"How long is it: ";
int l;
cin>>l;
cout<<"It is "<<l*220<<" 码."<<endl;
return 0;
}
2.3:
#include <iostream>
using namespace std;
void animal();
void learn();
int main()
{
animal();
animal();
learn();
learn();
return 0;
}
void animal()
{
cout<<"Three blind mice"<<endl;
}
void learn()
{
cout<<"See how they run"<<endl;
}
2.4:
#include <iostream>
int main()
{
using namespace std;
cout<<"Enter your age: ";
int age;
cin>>age;
cout<<"It contains "<<age*12<<" months."<<endl;
return 0;
}
2.5:
#include <iostream>
using namespace std;
int trans(int c);
int main()
{
cout<<"Please enter a Celsius value: ";
int c;
cin>>c;
int f = trans(c);
cout<<c<<" degrees Celsius is "<<f<<" degrees Fahrenheit."<<endl;
return 0;
}
int trans(int c)
{
int f=1.8*c+32.0;
return f;
}
2.6:
#include <iostream>
using namespace std;
int trans(double light);
int main()
{
cout<<"light years: ";
double light;
cin>>light;
int ast=trans(light);
cout<<light<<" light years = "<<ast<<" astronomical units."<<endl;
return 0;
}
int trans(double light)
{
int ast = 63240*light;
return ast;
}
2.7:
#include <iostream>
using namespace std;
void time(int h,int m);
int main()
{
int h,m;
time(h,m);
return 0;
}
void time(int h,int m)
{
cout<<"Enter the number of hours: ";
cin>>h;
cout<<"Enter the number of minutes: ";
cin>>m;
cout<<"Time: "<<h<<" : "<<m<<endl;
}
第3章
题目:
程序:
3.1:
#include <iostream>
int main()
{
using namespace std;
int tall;
const int n=12;
cout<<"How tall are you(inch):____\b\b\b\b";
cin>>tall;
cout<<"It is "<<tall/n<<" feet and "<<tall%n<<" inch.\n";
return 0;
}
3.2:
#include <iostream>
int main()
{
using namespace std;
int feet,inch,weight;
const int m=12;
const double n=0.0254;
const double w=2.2;
cout<<"Your height: ";
cin>>feet>>inch;
cout<<feet<<" feet and "<<inch<<" inch."<<endl;
cout<<"Your weight: ";
cin>>weight;
double h=(feet*m+inch)*n;
double W=weight*w;
double BMI=W/(h*h);
cout<<"The BMI is: "<<BMI<<endl;
return 0;
}
3.3:
#include <iostream>
int main()
{
using namespace std;
int deg,min,sec;
const double m=60;
const double n=60;
cout<<"Enter degrees,minutes, and seconds :";
cout<<"First,enter the degrees:";
cin>>deg;
cout<<"The minutes:";
cin>>min;
cout<<"The seconds:";
cin>>sec;
double d=deg+min/m+sec/m/n;
cout<<deg<<"degrees,"<<min<<"minutes,"<<sec<<"seconds="<<d<<"degrees";
return 0;
}
3.4:
#include <iostream>
int main()
{
using namespace std;
int d,h,m,s;
long sec;
const int dTh=24;
const int hTm=60;
const int mTs=60;
cout<<"Enter the seconds:";
cin>>sec;
s=sec%mTs;
m=sec/mTs;
h=m/hTm;
m=m%hTm;
d=h/dTh;
h=h%dTh;
cout<<sec<<" second s = "<<d<<" days, "<<h<<" hours, "<<m<<" minutes, "<<s<<" seconds";
return 0;
}
3.5:
#include <iostream>
int main()
{
using namespace std;
long long gp,ap;
double p;
cout<<"The world's population: ";
cin>>gp;
cout<<"The US population: ";
cin>>ap;
p=double(ap)/double(gp)*100;
cout<<"The US is "<<p<<"% of the world.";
return 0;
}
3.7:
#include <iostream>
int main()
{
using namespace std;
double e,a;
const double l=62.14, c=3.875;
cout<<"The European fuel consumption: ";
cin>>e;
a=c*l/e;
cout<<"The US fuel consumption: "<<a<<endl;
return 0;
}