C++primerplus编程练习答案

  • 格式:doc
  • 大小:45.00 KB
  • 文档页数:13

. . . .

. 资料. .. . C++ primer plus

编程练习答案

注:本人暑假正在看这本书,顺便就把题目做了,均经过了编译器通过,无注释。

第二章

1:#include

#define max 10

using namespace std;

void main()

{

char name[max],dizhi[max];

cout<<"请输入: ";

cin>>name;

cout<<"请输入地址: ";

cin>>dizhi;

cout<<"--->"<"<

}

2:#include

using namespace std;

void main() . . . .

. 资料. .. . {

long juli;

cout<<"请输入距离long(1 long 为220码):";

cin>>juli;

cout<<"按照您输入的距离是:"<

}

3:#include

using namespace std;

void blind()

{

cout<<"Three blind mice\n";

}

void run()

{

cout<<"See how they run\n";

}

void main()

{

for(int i=0;i<2;i++)

blind();

for(int j=0;j<2;j++) . . . .

. 资料. .. . run();

}

4:#include

using namespace std;

void month(int age)

{

cout<<"该年龄一共包含"<

}

void main()

{

int age;

cout<<"请输入年龄:";

cin>>age;

month(age);

}

5:#include

using namespace std;

double fahrenheit(double celsius)

{

return 1.8*celsius+32.0;

}

void main() . . . .

. 资料. .. . {

double celsius;

cout<<"please enter a celsius value:";

cin>>celsius;

cout<

"<

}

6:#include

using namespace std;

double astronomical(double light)

{

return 63240*light;

}

void main()

{

double light;

cout<<"Enter the number of light years:";

cin>>light;

cout<

astronomical units.\n";

}

7:#include . . . .

. 资料. .. . using namespace std;

void display(int hours,int minutes)

{

cout<<"Time: "<

}

void main()

{

int hour,minute;

cout<<"please input the time of hour:";

cin>>hour;

cout<<"please input the time of minute:";

cin>>minute;

display(hour,minute);

}

第三章

1:#include

using namespace std;

const float danwei=0.0833333;

void iswap(int cun)

{

cout<<"您的身高为: "<

. 资料. .. . }

void main()

{

int cun;

cout<<"请输入英寸单位的身高(整数):_______\b\b\b\b\b\b";

cin>>cun;

iswap(cun);

}

2:#include

using namespace std;

const double yingchi=12;

const double bang=2.2;

const double memter=0.0245;

void caculate(double chi,double cun,double weight)

{

double BMI;

double yingcun,mi,qianke;

yingcun=cun+chi*yingchi;

mi=yingcun*memter;

qianke=weight/bang;

BMI=qianke/(mi*mi); . . . .

. 资料. .. . cout<<"您的BMI值为: "<

}

void main()

{

double chi,cun,weight;

cout<<"请输入身高(以几英尺几英寸方式输入): ";

cin>>chi>>cun;

cout<<"请输入体重(以磅为单位): ";

cin>>weight;

caculate(chi,cun,weight);

}

3:#include

using namespace std;

void main()

{

double degrees,minutes,seconds,sum;

cout<<"Enter a latitude in degrees,minutes,and

seconds:"<

cout<<"First,enter the degrees: ";

cin>>degrees;

cout<<"Next,enter the minutes of arc: ";

cin>>minutes; . . . .

. 资料. .. . cout<<"Finally,enter the seconds of arc: ";

cin>>seconds;

sum=degrees+minutes/60+seconds/3600;

cout<

minutes,"<

degrees."<

}

4:#include

using namespace std;

const long m=60;

const long h=60;

const long d=24;

int sumday(long seconds)

{

long hour,minute;

minute=seconds/m;

hour=minute/h;

return hour/d;

}

int sumhour(long seconds,int day)

{

long minute; . . . .

. 资料. .. . seconds=seconds-day*d*h*m;

minute=seconds/m;

return minute/h;

}

int summinute(long seconds,int day,int hour)

{

seconds=seconds-(day*d*h*m+hour*h*m);

return seconds/m;

}

int sumsecond(long seconds,int day,int hour,int minute)

{

return

seconds=seconds-(day*d*h*m+hour*h*m+minute*m);

}

void main()

{

long seconds;

int day,hour,minute,second;

cout<<"Enter the number of seconds: ";

cin>>seconds;

day=sumday(seconds);

hour=sumhour(seconds,day);