文档库 最新最全的文档下载
当前位置:文档库 › C++PrimerPlus第六版第十一章课后习题

C++PrimerPlus第六版第十一章课后习题

C++PrimerPlus第六版第十一章课后习题
C++PrimerPlus第六版第十一章课后习题

1.头文件

#ifndef STONEWT_H_

#define STONEWT_H_

namespace VECTOR

{

class Vector

{

public:

enum Mode { RECT, POL};

private:

double x;

double y;

double mag;

doubleang;

Mode mode;

voidset_x();

voidset_y();

voidset_mag();

voidset_ang();

public:

Vector();

Vector(double n1, double n2, Mode form = RECT);

void Reset(double n1, double n2, Mode form = RECT);

~Vector();

doublexval () const { return x;}

doubleyval () const { return y;}

doublemagval () const { return mag;}

doubleangval () const { return ang;}

voidpolar_mode();

voidrect_mode();

Vector operator + (const Vector & v) const;

Vector operator - (const Vector & v) const;

Vector operator - () const;

Vector operator * (double m) const;

friend Vector operator * ( double m, const Vector & v);

friendstd::ostream& operator << (std::ostream&os, const Vector & v);

};

}

#endif;

函数定义

#include

#include "stonewt.h"

#include

usingstd::sqrt;

usingstd::sin;

usingstd::cos;

usingstd::atan;

usingstd::atan2;

usingstd::cout;

namespace VECTOR {

const double Rad_to_deg = 45.0/atan(1.0); void Vector::set_x()

{

x = mag * cos (ang);

}

void Vector::set_y()

{

y = mag * sin (ang);

}

void Vector::set_mag()

{

mag = sqrt (x * x + y * y);

}

void Vector::set_ang()

{

if (x ==0 && y == 0)

ang = 0.0;

else

ang = atan2 (y, x);

}

Vector::Vector()

{

x = y = mag = ang = 0;

mode = RECT;

}

Vector::Vector(double n1, double n2, Mode form ) {

mode = form;

if ( mode == RECT )

{

x = n1;

y = n2;

set_mag();

set_ang();

}

else if ( mode == POL )

{

mag = n1;

ang = n2;

set_x();

set_y();

}

else

{

cout<< "Vector set to 0.\n";

x = y = mag = ang = 0;

mode = RECT;

}

}

void Vector::Reset(double n1, double n2, Mode form ) {

mode = form;

if ( mode == RECT )

{

x = n1;

y = n2;

set_mag();

set_ang();

}

else if ( mode == POL )

{

mag = n1;

ang = n2;

set_x();

set_y();

}

else

{

cout<< "Vector set to 0.\n";

x = y = mag = ang = 0;

mode = RECT;

}

}

Vector::~Vector()

{

}

void Vector::polar_mode()

{

mode = POL;

}

void Vector::rect_mode()

{

mode = RECT;

}

Vector Vector::operator + (const Vector & v) const

{

return Vector(x + v.x, y + v.y);

}

Vector Vector::operator - (const Vector & v) const

{

return Vector(x - v.x, y - v.y);

}

Vector Vector::operator - () const

{

return Vector(-x, -y);

}

Vector Vector::operator * (double m) const

{

return Vector(m * x, m * y);

}

Vector operator * ( double m, const Vector & v)

{

return ( v * m );

}

std::ostream& operator << (std::ostream&os, const Vector & v) {

if ( v.mode == Vector::RECT )

{

os<< "(x,y) = (" <

}

else if ( v.mode == Vector::POL )

{

os<< "(mag, ang) = (" <

}

else

os<< "Vector object mode is invaild.\n";

returnos;

}

}

主函数

#include

#include

#include

#include

#include"stonewt.h"

int main()

{

using namespace std;

using VECTOR::Vector;

srand(time(0));

double director;

Vector step;

Vector result(0.0,0.0);

unsigned long steps = 0;

double target;

doubledstep;

fstreamfout;

fout.open( "step.text" );

cout<< "Enter target distance ( q to quit); ";

while (cin>> target )

{

cout<< "Enter step length : ";

if (!(cin>>dstep))

break;

while (result.magval() < target )

{

director = rand() % 360;

step.Reset(dstep, director, Vector::RECT);

result = result + step;

steps ++;

}

cout<< "After " << steps << " steps, the subject has the following location :\n";

cout<< result <

result.polar_mode ();

cout<< " or \n" << result <

cout<< "Average outward distance per step = " <

steps = 0;

result.Reset(0.0,0.0);

cout<< "Enter target distance ( q to quit); ";

}

cout<< "Bye!" <

cin.clear();

while (cin.get() != '\n')

continue;

system("Pause");

return 0;

}

2.头文件

#ifndef STONEWT_H_

#define STONEWT_H_

namespace VECTOR

{

class Vector

{

public:

enum Mode { RECT, POL};

private:

double x;

double y;

Mode mode;

void set_x(double mag, double ang);

void set_y(double mag, double ang);

double set_mag();

double set_ang();

public:

Vector();

Vector(double n1, double n2, Mode form = RECT);

void Reset(double n1, double n2, Mode form = RECT);

~Vector();

double xval () const { return x;}

double yval () const { return y;}

double magval () { return set_mag();}

double angval () { return set_ang();}

void polar_mode();

void rect_mode();

Vector operator + (const Vector & v) const;

Vector operator - (const Vector & v) const;

Vector operator - () const;

Vector operator * (double m) const;

friend Vector operator * ( double m, const Vector & v);

friend std::ostream& operator << (std::ostream&os, Vector & v);

};

}

#endif;

函数定义

#include

#include "stonewt.h"

#include

using std::sqrt;

using std::sin;

using std::cos;

using std::atan;

using std::atan2;

using std::cout;

namespace VECTOR {

const double Rad_to_deg = 45.0/atan(1.0); void Vector::set_x(double mag, double ang)

{

x = mag * cos(ang);

}

void Vector::set_y(double mag, double ang)

{

y = mag * sin(ang);

}

double Vector::set_mag()

{

return sqrt (x * x + y * y);

}

double Vector::set_ang()

{

if (x ==0 && y == 0)

return (0.0);

else

return atan2 (y, x);

}

Vector::Vector()

{

x = y = 0;

mode = RECT;

}

Vector::Vector(double n1, double n2, Mode form ) {

mode = form;

if ( mode == RECT )

{

x = n1;

y = n2;

set_mag();

set_ang();

}

else if ( mode == POL )

{

set_x(n1, n2/Rad_to_deg);

set_y(n1, n2/Rad_to_deg);

}

else

{

cout<< "Vector set to 0.\n";

x = y = 0;

mode = RECT;

}

}

void Vector::Reset(double n1, double n2, Mode form ) {

mode = form;

if ( mode == RECT )

{

x = n1;

y = n2;

set_mag();

set_ang();

}

else if ( mode == POL )

{

set_x(n1, n2/Rad_to_deg);

set_y(n1, n2/Rad_to_deg);

}

else

{

cout<< "Vector set to 0.\n";

x = y = 0;

mode = RECT;

}

}

Vector::~Vector()

{

}

void Vector::polar_mode()

{

mode = POL;

}

void Vector::rect_mode()

{

mode = RECT;

Vector Vector::operator + (const Vector & v) const

{

return Vector(x + v.x, y + v.y);

}

Vector Vector::operator - (const Vector & v) const

{

return Vector(x - v.x, y - v.y);

}

Vector Vector::operator - () const

{

return Vector(-x, -y);

}

Vector Vector::operator * (double m) const

{

return Vector(m * x, m * y);

}

Vector operator * ( double m, const Vector & v)

{

return ( v * m );

}

std::ostream& operator << (std::ostream&os, Vector & v)

{

if ( v.mode == Vector::RECT )

{

os<< "(x,y) = (" <

}

else if ( v.mode == Vector::POL )

{

os<< "(mag, ang) = (" <

}

else

os<< "Vector object mode is invaild.\n";

return os;

}

}

主函数

#include

#include

#include

#include"stonewt.h"

int main()

using namespace std;

using VECTOR::Vector;

srand(time(0));

double director;

Vector step;

Vector result(0.0,0.0);

unsigned long steps = 0;

double target;

double dstep;

cout<< "Enter target distance ( q to quit); ";

while (cin>> target )

{

cout<< "Enter step length : ";

if (!(cin>>dstep))

break;

while (result.magval() < target )

{

director = rand() % 360;

step.Reset(dstep, director, Vector::RECT);

result = result + step;

steps ++;

}

cout<< "After " << steps << " steps, the subject has the following location :\n";

cout<< result <

result.polar_mode ();

cout<< " or \n" << result <

cout<< "Average outward distance per step = "<

steps = 0;

result.Reset(0.0,0.0);

cout<< "Enter target distance ( q to quit); ";

}

cout<< "Bye!" <

cin.clear();

while (cin.get() != '\n')

continue;

system("Pause");

return 0;

}

3.头文件

#ifndef STONEWT_H_

#define STONEWT_H_

namespace VECTOR

{

class Vector

{

public:

enum Mode { RECT, POL};

private:

double x;

double y;

double mag;

double ang;

Mode mode;

void set_x();

void set_y();

void set_mag();

void set_ang();

public:

Vector();

Vector(double n1, double n2, Mode form = RECT);

void Reset(double n1, double n2, Mode form = RECT);

~Vector();

double xval () const { return x;}

double yval () const { return y;}

double magval () const { return mag;}

double angval () const { return ang;}

void polar_mode();

void rect_mode();

Vector operator + (const Vector & v) const;

Vector operator - (const Vector & v) const;

Vector operator - () const;

Vector operator * (double m) const;

friend Vector operator * ( double m, const Vector & v);

friend std::ostream& operator << (std::ostream&os, Vector & v);

};

}

#endif;

函数定义

#include

#include "stonewt.h"

#include

using std::sqrt;

using std::sin;

using std::cos;

using std::atan;

using std::atan2;

using std::cout;

namespace VECTOR {

const double Rad_to_deg = 45.0/atan(1.0); void Vector::set_x()

{

x = mag * cos(ang);

}

void Vector::set_y()

{

y = mag * sin(ang);

}

void Vector::set_mag()

{

mag = x * x + y * y;

}

void Vector::set_ang()

{

if (x ==0 && y == 0)

ang = 0.0;

else

ang = atan2 (y, x);

}

Vector::Vector()

{

x = y = mag = ang = 0 ;

mode = RECT;

}

Vector::Vector(double n1, double n2, Mode form ) {

mode = form;

if ( mode == RECT )

{

x = n1;

y = n2;

set_mag();

set_ang();

}

else if ( mode == POL )

{

mag = n1;

ang = n2;

set_x();

set_y();

}

else

{

cout<< "Vector set to 0.\n";

x = y = 0;

mode = RECT;

}

}

void Vector::Reset(double n1, double n2, Mode form ) {

mode = form;

if ( mode == RECT )

{

x = n1;

y = n2;

set_mag();

set_ang();

}

else if ( mode == POL )

{

mag = n1;

ang = n2;

set_x();

set_y();

}

else

{

cout<< "Vector set to 0.\n";

x = y = mag = ang = 0;

mode = RECT;

}

}

Vector::~Vector()

{

}

void Vector::polar_mode()

{

mode = POL;

}

void Vector::rect_mode()

{

mode = RECT;

}

Vector Vector::operator + (const Vector & v) const

{

return Vector(x + v.x, y + v.y);

}

Vector Vector::operator - (const Vector & v) const

{

return Vector(x - v.x, y - v.y);

}

Vector Vector::operator - () const

{

return Vector(-x, -y);

}

Vector Vector::operator * (double m) const

{

return Vector(m * x, m * y);

}

Vector operator * ( double m, const Vector & v)

{

return ( v * m );

}

std::ostream& operator << (std::ostream&os, Vector & v)

{

if ( v.mode == Vector::RECT )

{

os<< "(x,y) = (" <

}

else if ( v.mode == Vector::POL )

{

os<< "(mag, ang) = (" <

}

else

os<< "Vector object mode is invaild.\n";

return os;

}

}

主函数

#include

#include

#include

#include"stonewt.h"

int main()

{

using namespace std;

using VECTOR::Vector;

srand(time(0));

double director;

Vector step;

Vector result(0.0,0.0);

unsigned long steps = 0;

double target;

double dstep;

int N;

intnum;

double average, max, min, sum;

cout<< "Enter a number :" ;

cin>>num;

cout<< "Enter target distance ( q to quit); ";

cin>> target;

cout<< "Enter step length : ";

cin>>dstep;

N = num;

average = max = min = sum = 0;

while ( num )

{

while (result.magval() < target )

{

director = rand() % 360;

step.Reset(dstep, director, Vector::POL);

result = result + step;

steps ++;

}

cout<< "After " << steps << " steps once walk.\n";

if ( max == 0 || min == 0)

max = min = steps;

if ( max < steps )

max = steps;

if ( min > steps )

min = steps;

sum += steps;

num --;

steps = 0;

result.Reset(0.0,0.0);

}

average = sum/N;

cout<< "Average = " << average <

cout<< "max = " << max <

cout<< "min = " << min <

cout<< "Bye!" <

cin.clear();

while (cin.get() != '\n')

continue;

system("Pause");

return 0;

}

4.头文件

#ifndef STONEWT_H_

#define STONEWT_H_

#include

class Time

{

private:

int minutes;

int hours;

public:

Time();

Time(int h, int m = 0);

void Addmin(int m);

void Addhr(int h);

void reset(int h = 0, int m = 0);

friend Time operator + (const Time & t1, const Time & t2 ) ;

friend Time operator - (const Time & t1, const Time & t2 ) ;

friend Time operator * (double m, const Time & t) ;

friend std::ostream& operator << (std::ostream&os, const Time & t) ; };

#endif;

函数定义

#include"stonewt.h"

Time::Time()

{

hours = minutes = 0;

}

Time::Time(int h, int m)

{

hours = h;

minutes = m;

}

void Time::Addmin(int m)

{

minutes = minutes + m;

hours = hours + minutes / 60;

minutes = minutes % 60;

}

void Time::Addhr(int h)

{

hours = hours + h;

}

void Time::reset(int h , int m )

{

hours = h;

minutes = m;

}

Time operator + (const Time & t1, const Time & t2 )

{

Time temp;

temp.minutes = t1.minutes + t2.minutes;

temp.hours = temp.minutes / 60 + t1.hours + t2.hours;

temp.minutes = temp.minutes % 60;

return temp;

}

Time operator - (const Time & t1, const Time & t2 )

{

Time differ;

int tol1, tol2;

tol1 = t1.minutes + t1.hours * 60;

tol2 = t2.minutes + t2.hours * 60;

differ.minutes = tol1 - tol2;

differ.hours = differ.minutes / 60;

differ.minutes = differ.minutes % 60;

return differ;

}

Time operator * (double m, const Time & t)

{

Time temp;

temp.minutes = t.hours * 60 * m + t.minutes * m;

temp.hours = temp.minutes / 60;

temp.minutes = temp.minutes % 60;

return temp;

}

std::ostream& operator << (std::ostream&os, const Time & t) {

os<

return os;

}

主函数

#include"stonewt.h"

int main()

{

using namespace std;

Time aida(3, 35);

Time tosca(6, 48);

cout<< "Aida and Tosca :" <

Time temp;

temp = aida + tosca;

cout<< "Aida + Tosca: " << temp <

temp = tosca - aida;

cout<< "Aida - Tosca: " << temp <

temp = 10.0 * tosca;

cout<< "10.0 * Tosca: " << temp <

system("Pause");

return 0;

}

5.头文件

#ifndef STONEWT_H_

#define STONEWT_H_

usingnamespace std;

class Stonewt

{

public:

enum Mode{STN,INP, DP};

private:

enum { Lbs_per_stn = 14 };

int stone;

double pds_left;

int pounds_int;

double pounds;

Mode mode;

void set_stone();

void set_pds();

void set_pds_int();

public:

Stonewt(double lbs, Mode form);

Stonewt(int stn, double lbs, Mode form);

Stonewt();

~Stonewt();

void stn_mode();

void intpounds_mode();

void boupounds_mode();

operatorint() const;

operatordouble () const;

Stonewt operator + (const Stonewt&st) const;

Stonewt operator - (const Stonewt&st) const;

Stonewt operator * (double m)const;

friend Stonewt operator * (double n, const Stonewt&st);

friend ostream&operator<< (ostream&os, const Stonewt&st); };

#endif;

函数定义

#include

#include"stonewt.h"

void Stonewt::set_stone()

{

stone = int(pounds)/Lbs_per_stn;

pds_left = int(pounds)%Lbs_per_stn + pounds - int(pounds); }

void Stonewt::set_pds()

{

pounds = stone * Lbs_per_stn + pds_left;

}

void Stonewt::set_pds_int()

{

pounds = int (pounds);

}

Stonewt::Stonewt(double lbs, Mode form)

{

mode = form;

if( mode == STN)

{

stone = (int)lbs / Lbs_per_stn;

pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs;

set_pds();

set_pds();

}

elseif(mode ==INP)

{

pounds_int = int(lbs);

pounds = lbs;

set_stone();

}

elseif ( mode == DP)

{

pounds = lbs;

set_stone();

set_pds_int();

}

else

{

std::cout<<"Stonewt set to 0."<

stone = pounds = pds_left = 0;

mode = STN;

}

}

Stonewt::Stonewt(int stn, double lbs, Mode form) {

mode = form;

if( mode == STN)

{

stone = stn;

pds_left = lbs;

set_pds();

set_pds_int();

}

elseif ( mode == INP )

{

pounds_int = int ( stn * Lbs_per_stn + lbs);

pounds = stn * Lbs_per_stn + lbs ;

set_stone();

}

elseif ( mode == DP )

{

pounds = stn * Lbs_per_stn + lbs ;

set_stone();

set_pds_int();

}

else

{

std::cout<<"Stonewt set to 0."<

stone = pounds = pds_left = 0;

mode = STN;

}

C语言程序设计第四版第六章答案-谭浩强

1、用筛选法求100之内的素数。解: #include #include int main() {int i,j,n,a[101]; for (i=1;i<=100;i++) a[i]=i; a[1]=0; for (i=2;i int main() {int i,j,min,temp,a[11]; printf("enter data:\n"); for (i=1;i<=10;i++)

{printf("a[%d]=",i); scanf("%d",&a[i]); } printf("\n"); printf("The orginal numbers:\n"); for (i=1;i<=10;i++) printf("%5d",a[i]); printf("\n"); for (i=1;i<=9;i++) {min=i; for (j=i+1;j<=10;j++) if (a[min]>a[j]) min=j; temp=a[i]; a[i]=a[min]; a[min]=temp; } printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++) printf("%5d",a[i]); printf("\n"); return 0; } 3、求一个3×3的整型矩阵对角线元素之和。解: #include int main() { int a[3][3],sum=0; int i,j; printf("enter data:\n"); for (i=0;i<3;i++) for (j=0;j<3;j++) scanf("%3d",&a[i][j]); for (i=0;i<3;i++) sum=sum+a[i][i]; printf("sum=%6d\n",sum);

c++primerplus中文版第六版源代码

C++ primer plus 中文版第六版源代码 第二章到第四章,后续继续更新……… 第二章 1:#include void main() { using namespace std; int carrots; carrots=25; cout<<"I have "; cout<

2:#include int stonetolb(int); int main() { using namespace std; int stone; cout<<"Enter the weight in stone: "; cin>>stone; int pounds=stonetolb(stone); cout< void main()

{ using namespace std; int carrots; carrots=25; cout<<"How many carrots do you have?"<>carrots; cout<<"Here are two more."; carrots=carrots+2; cout<<"Now you have "< using namespace std; void main() { cout<<"Come up and C++ me some time.";

C语言第六章习题带答案

练习6-1答案 一、选择题 1.已知函数abc的定义为: void abc() {……} 则函数定义中void的含义是( A )。 A.执行函数abc后,函数没有返回值B.执行函数abc后,函数不再返回C.执行函数abc后,可以返回任意类型 D.以上三个答案全是错误的 2.已知:int p();,p是( C )。 A.int型变量 B.函数p的调用 C.一个函数声明,该函数的返回值是int型的值 D.强制类型转换表达式中的变量 3.以下正确的函数形式是( D )。 A.double fun(int x, int y) B.fun(int x, y) { z=x+y; return z; } { int z; return z; } C.fun(x, y) D.double fun(int x, int y) { int x, y; double z; z=x+y; return z; } { double z; z=x+y; return z; } 4.以下说法正确的是( C )。 A.定义函数时,形参的类型说明可以放在函数体内 B.return后边的值不能为表达式 C.如果函数值的类型与返回值表达式的类型不一致,以函数值类型为准 D.如果形参与实参的类型不一致,以实参类型为准 5.C语言允许函数值类型缺省定义,此时该函数值隐含的类型是( B )。 A.float 型B.int型C.long型D.double型6.下面函数调用语句含有实参的个数为( B )。 func((exp1, exp2), (exp3, exp4, exp5)); A.1 B.2 C.4 D.5 7.以下程序的功能是计算函数F(x, y, z)=(x+y)/(x-y)+(z+y)/(z-y)的值,请选择填空。 #include #include float f(float, float); main() {float x, y, z, sum; scanf("%f%f%f", &x, &y, &z); sum=f( ①B )+f( ②C ); (注:或者选择sum=f( ①C )+f( ②B )) printf("sum=%f\n", sum); } float f(float a, float b) {float value; value=a/b;

C Primer Plus第6版编程练习答案

Chapter 2 Programming Exercises PE 2--‐1 /* Programming Exercise 2-1 */ #include <> int main(void) { printf("Gustav Mahler\n"); printf("Gustav\nMahler\n"); printf("Gustav "); printf("Mahler\n"); return 0; } PE 2--‐3 /* Programming Exercise 2-3 */ #include <> int main(void) { int ageyears; /* age in years */ int agedays; /* age in days */ /* large ages may require the long type */ ageyears = 101; agedays = 365 * ageyears; printf("An age of %d years is %d days.\n", ageyears, agedays); return 0; } PE 2--‐4 /* Programming Exercise 2-4 */ #include <> void jolly(void); void deny(void); int main(void) { jolly(); jolly(); jolly(); deny(); return 0; } void jolly(void) { printf("For he's a jolly good fellow!\n"); } void deny(void) { printf("Which nobody can deny!\n"); } PE 2--‐6 /* Programming Exercise 2-6 */ #include <> int main(void) { int toes; toes = 10; printf("toes = %d\n", toes);

C Primer Plus (第六版)中文版 6.16编程练习

//******************6.15复习题************************** //*********** 6 ************************** #include int main(void) { int i, j; for (i = 0; i < 4; i++) //外层循环控制行内层循环控制列 { for (j = 0; j < 8; j++) { printf("$"); } printf("\n"); } return 0; } //******************6.16 编程练习 ************************** //****************** 一 ************************** #include #define SIZE 26 int main(void) { char array[SIZE]; int index = 0; array[0] = 'a'; printf("%c", array[0]); for (index = 1; index < SIZE; index++) { array[index] = 'a' + index; printf("%c", array[index]); } return 0; } //****************** 二 ************************** #include int main(void)

{ int i, j;//i控制行,j控制列计数作用 for (i = 0; i < 5; i++) { for (j = 0; j < =i ; j++) { printf("$"); } printf("\n"); } return 0; } //****************** 三 ************************** #include int main(void) { int i;//外层循环控制行 int j;//内层循环控制列 char ch = 'F'; for (i = 0; i < 6; i++) { for (j = 0; j <= i; j++) printf("%c", ch-j ); printf("\n"); } return 0; } //****************** 四 ************************** #include int main(void) { int i;//外层循环控制行 int j;//内层循环控制列 char ch = 'A'; for (i = 0; i < 6; i++) { for (j = 0; j <= i; j++) printf("%c", ch++ ); printf("\n");

CPrimerPlus第6版中文版勘误表

注意:下面的勘误中,红色字体为修改后的文字,提请各位读者注意。 第 6 页,” 1.6 语言标准”中的第 3 行,将 1987 年修改为 1978 年。 第 22 页,” 2. main ()函数”中的第 1 行, int main (void ) 后面的分号( ; )删除。 第 24 页,“5. 声明”的第 10 行,也就 是一个变量、函数或其他实体的名称。 第 27 页,图 2.3 中,下划线应该只包含括号中的内容;第 2 段的第 4 行,而不是存储 在 源代码 中的指令。 第 30页,“2.5.4 打印多个值”的第 4行,双引 号后面的第 1 个变量。 第 34页,“2.7.3 程序状态”第 2段的第 4 行,要尽量忠实 于代码来模拟。 第 35页,“2.10 本章小结”第 2段的第 1句,声明 语句为变量指定变量名, 并标识该变量中存 储的数据类型;本页倒数第 2 行,即 检查程序每执行一步后所有变量的值。 第37页,“2.12编程练习”中第1题,把你的名和姓打印在一行……把你的 名和姓分别打印在 两行……把你的 名和姓打印在一行……把示例的内容换成你的 名字。 第 40 页,第 1 行,用于把英 磅常衡盎司转换为… … 第44页,“3.4 C 语言基本数据类型”的第 1句,本节将 详细介绍C 语言的基本属性类型…… 第 46页,“5. 八进制和十六进制”的第 4句,十六进制数 3的二进制数 是 0011,十六进制数 5 的二进制数 是 0101;“6. 显示八进制和十六进制”的第 1 句,既可以使用 也可以 显示不同进制 的数;将“回忆一下……程序在执行完毕后不会立即关闭执行窗口”放到一个括号里。 第 47页,“2. 使用多种整数类型的原因”第 3句,过去的一台运行 Windows 3.x 的机器上。 第 53 页,图 3.5 下面的第 4 行“上面最后一个例子( printf ( “ ” a \\ is a backslash. ” \n ” ); )” 第 56页,正文的第 2行和第 4行应该分别为 printf ( “me32 = %““d”“\n ”, me32); printf ( “me32 = %d\n ” , me32); 第 61 页,“无符号类型”的最后 1 句,相当于 unsigned int (即两者之间添加一个空格 )。 第 62 页,程序清单 3.8 中的第 1 行,将 //* typesize.c -- 打印类型大小 */ 中的第一个斜杠删 除。 第 63页,“3.6 参数和陷阱”第 2行, printf ( “ Hello,pal. ” )(即 Hello, 和 pal. 之间没有空 格)。 第 64 页,程序清单 3.10 中的第 1 行,使用 转义序列。 第 75 页,倒数第 8行, 何时使用圆括号 取决于运算对象是类型还是特定量。 第82页,第11行, . 格式字符串包含了两个待打印项 number 和pies 对应的 ..... 第83页,表4.4中的“ L”修饰符的含义介绍中,应该是示例: ” %L ”、“%10.4Le” 第 84 页,表 4.5 中的第 1 行,即,从字段的左侧开始打印该 项(即,应该只保留一个 项);在 “ 0”标记的含义中,添加一行: 示例:"%010d"和"%08.3f"。 第86页,第1段的第2行,……字段宽度是容纳 待打印数字所需的……; 倒数第4段中,根据%x 打印出1f,根据%打印出1F 第87页,“4.4.4转换说明的意义”第 2段,……读者认为原始值 被替换成转换后的值。 第89页,“参数传递”第2行,把变量n1、n2、n3和n4的值传递给程序(即,保留一个顿号)。 第 93页,第 5行的 2121.45 的字体应该与第 4行的 42 的字体保持一致;表 4.6 上面的最后一 行,对于 double 类型要使用 1 修饰符。 第 94 页,表中的第 3 行,把对应的数值存储为 unsigned short int 类型;把“ j ”转换说明的 示例 放到“ z ”转换说明中;在“ j ”转换说明的含义中添加:示例:” %jd”、” %ju”。 第95页,“3.scanf () 的返回值”上面一段的倒数第 3行,如果在格式字符串中把空格放到 %c 的前面 。 第98页,倒数第2段,strlen () 函数(声明在string.h 头文件中)可用于 ... 。 第 100 页,” 4.8 编程练习”中的第 2 题,将该题中的“名和姓”统一替换为“名字” ;并执行 以下 操作;第 3题,将 a 、 b 项中的“输入”替换为” The input is ”,将“或”替换为“ or”, 将末尾1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

C Primer Plus第6版中文版勘误表教学提纲

C P r i m e r P l u s第6版中文版勘误表

注意:下面的勘误中,红色字体为修改后的文字,提请各位读者注意。 1.第6页,” 1.6语言标准”中的第3行,将1987年修改为1978年。 2.第22页,” 2. main()函数”中的第1行,int main (void)后面的分号(;)删除。 3.第24页,“5. 声明”的第10行,也就是一个变量、函数或其他实体的名称。 4.第27页,图2.3中,下划线应该只包含括号中的内容;第2段的第4行,而不是存储在源代 码中的指令。 5.第30页,“2.5.4 打印多个值”的第4行,双引号后面的第1个变量。 6.第34页,“2. 7.3 程序状态”第2段的第4行,要尽量忠实于代码来模拟。 7.第35页,“2.10 本章小结”第2段的第1句,声明语句为变量指定变量名,并标识该变量中存 储的数据类型;本页倒数第2行,即检查程序每执行一步后所有变量的值。 8.第37页,“2.12 编程练习”中第1题,把你的名和姓打印在一行……把你的名和姓分别打印在 两行……把你的名和姓打印在一行……把示例的内容换成你的名字。 9.第40页,第1行,用于把英磅常衡盎司转换为…… 10.第44页,“3.4 C语言基本数据类型”的第1句,本节将详细介绍C语言的基本属性类型…… 11.第46页,“5. 八进制和十六进制”的第4句,十六进制数3的二进制数是0011,十六进制数5 的二进制数是0101;“6.显示八进制和十六进制”的第1句,既可以使用也可以显示不同进制的数;将“回忆一下……程序在执行完毕后不会立即关闭执行窗口”放到一个括号里。 12.第47页,“2.使用多种整数类型的原因”第3句,过去的一台运行Windows 3.x的机器上。 13.第53页,图 3.5下面的第4行“上面最后一个例子(printf(“Gramps sez, \”a \\ is a backslash.\”\n”);)” 14.第56页,正文的第2行和第4行应该分别为printf(“me32= %“ “d” “\n”, me32); printf(“me32 = %d\n”, me32); 15.第61页,“无符号类型”的最后1句,相当于unsigned int(即两者之间添加一个空格)。 16.第62页,程序清单3.8中的第1行,将//* typesize.c -- 打印类型大小*/中的第一个斜杠删除。 17.第63页,“3.6参数和陷阱”第2行,printf(“Hello,pal.”)(即Hello,和pal.之间没有空格)。 18.第64页,程序清单3.10中的第1行,使用转义序列。 19.第75页,倒数第8行,何时使用圆括号取决于运算对象是类型还是特定量。 20.第82页,第11行,……格式字符串包含了两个待打印项number和pies对应的…… 21.第83页,表4.4中的“L”修饰符的含义介绍中,应该是示例:”%L f”、“%10.4L e” 22.第84页,表4.5中的第1行,即,从字段的左侧开始打印该项(即,应该只保留一个项); 在“0”标记的含义中,添加一行:示例:"%010d"和"%08.3f"。 23.第86页,第1段的第2行,……字段宽度是容纳待打印数字所需的……;倒数第4段中,根 据%x打印出1f,根据%X打印出1F 24.第87页,“4.4.4转换说明的意义”第2段,……读者认为原始值被替换成转换后的值。 25.第89页,“参数传递”第2行,把变量n1、n2、n3和n4的值传递给程序(即,保留一个顿 号)。 26.第93页,第5行的2121.45的字体应该与第4行的42的字体保持一致;表4.6上面的最后一 行,对于double类型要使用1修饰符。 27.第94页,表中的第3行,把对应的数值存储为unsigned short int类型;把“j”转换说明的示例 放到“z”转换说明中;在“j”转换说明的含义中添加:示例:”%jd”、”%ju”。

C语言第六章数组习题

C语言第六章数组习题 第六章数组 6.1 选择题 [题]在C语言中,引用数组元素时,其数组下标的数据类型允许是_____. A)整型常量B)整型表达式 C)整型常量或整型表达式D)任何类型的表达式 [题]以下对一维整型数组a的正确说明是_____。 A)int a(10);B)int n=10,a[n]; C)int n;D)#define SIZE 10 scanf(\%d,&n);int a[SIZE]; int a[n]; [题]若有说明:int a[l0];则对a数组元素的正确引用是_____。 A) a[10] B)a[3.5] C)a(5) D)a[10-10] [题]在C 语言中,一维数组的定义方式为:类型说明符数组名_____。 A)[常量表达式] B)[整型表达式] C)[整型常量]或[整型表达式] D)[整型常量] [题]以下能对一维数组a进行正确初始化的语句是_____。 A)int a[l0]=(0,0,0,0,0); B)int a[l0]={}; C)int a={0};D)int a[10]={10*1}; [题]以下对二维数组a的正确说明是_____。

1 A)int a[3]; B)float a(3,4); C)double a[1][4]; D)float a(3)(4); [题]若有说明:int a[3][4];则对a数组元素的正确引用是_____。 A)a[2][4] B)a[1,3] C)a[1+1][0] D)a(2)(1) [题]若有说明:int a[3][4];则对a数组元素的非法引用是_____。 A)a[0][2*1] B)a[1][3] C)a[4-2][0] D)a[0][4] [题]以下不能对二维数组a进行正确初始化的语句是_____。 A)int a[2][3]={0}; B)int a[3]={{1,2},{0}}; C)int a[2][3]={{l,2},{3,4},{5,6}}; D)int a[3]={1,2,3,4,5,6}; [题]若有说明:int a[3][4]={0};则下面正确的叙述是_____。 A)只有元素a[0][0]可得到初值0 B)此说明语句不正确 C)数组a中各元素都可得到初值,但其值不一定为0 D)数组a中每个元素均可得到初值0 [题]若有说明:int a[3][4];则数组a中各元素_____。 2

c语言 第6章作业

书面作业8 专业理科学号3120101717 姓名马凌浩 习题6 一、选择题 1.设float x=2.5, y=4.7; int a=7;,printf(“%.1f”, x+a%3*(int)(x+y)%2/4)的结果为 A 。 A.2.5 B.2.8 C.3.5 D.3.8 2.执行下列程序段的输出结果是 C 。 int a = 2; a += a *= a -= a *= 3; printf("%d", a); A.-6 B.12 C.0 D.2 3.设字符型变量x 的值是064,表达式“~ x ^ x << 2 & x”的值是 A 。 A.0333 B.333 C.0x333 D.020 4.设a 为整型变量,不能正确表达数学关系:1010 && a<15 D.!(a<=10) && !(a>=15) 5.设以下变量均为int 类型,表达式的值不为9 的是 C 。 A.(x=y=8,x+y,x+1) B.(x=y=8,x+y,y+1) C.(x=8,x+1,y=8,x+y) D.(y=8,y+1,x=y,x+1) 二.填空题 1 .-127 的原码为11111111 、反码为10000000 、补码为10000001 。 2.逻辑表达式x && 1 等价于关系表达式if(x==0) 表达式的值为0;else 表达式的值为1;。3.设int a=5, b=6; 则表达式(++a==b--)? ++a : --b 的值是7 。 4.设c = 'w', a = 1, b = 2, d = -5, 则表达式'x'+1>c, 'y'!=c+2, -a-5*b<=d+1, b==(a=2)的值 分别为 1 、0 、 1 、 1 。 5.运行以下程序后,如果从键盘上输入china#<回车>,则输出结果为c1=2,c2=5 。 #include int main(void) { int c1 = 0, c2 = 0; char ch; while((ch = getchar()) !=?#?) switch(ch){ case …a?: case …h?: c1++; default: c2++; } printf(“c1=%d,c2=%d\n”, c1, c2); return 0; }

C语言第六章数组习题答案

第六章 数组 习题答案 2、0 4 3、0 6 4、&a[i] i%4==0 printf("\n"); 5、i+j==3_ a[i][j] 6、12 7、a[i][j]+b[i][j] printf(“\n ”) 8、按行存放 9、( a[i]>a[j]) 10、将串str2复制到串str1中后再将串str3连接到串str1之后 三、阅读题 1、如右图所示 2、AQM 3、AzyD 4、9198 5、如右图所示 6、92 7、1,2,5,7, 8、2 9、v1=5,v2=8,v3=6,v4=1 10、a*b*c*d* 四、编程题 1(1)选择法排序 #include void main( ) {int i,j,n=10,p,a[10];int temp; for(i=0;i

for(j=i+1;j void main( ) {int i,j,n=10,p,a[10];int temp; for(i=0;ia[j]) {temp=a[i];a[i]=a[j];a[j]=temp;} printf("\n排序后的一维数组:\n"); for(i=0;i void main( ) {int a[11],i,n=10,k; int point; printf("\n 请输入原始数据:\n"); for(i=0;ik) {point=i; break;} if(i!=n) {for(i=n;i>point;i--) a[i]=a[i-1]; a[point]=k;} /*从后向前方式*/ else a[n]=k; printf("插入后的数是:\n"); for(i=0;i int main() {int i,j,upp,low,dig,spa,oth; char text[3][80];

c++ primer plus(第六版)第二至第六章课后编程练习全部答案

第二章:开始学习C++ //ex2.1--display your name and address #include int main(void) { using namespace std; cout<<"My name is liao chunguang and I live in hunan chenzhou.\n”;} //ex2.2--convert the furlong units to yard uints-把浪单位换位码单位 #include double fur2yd(double); int main() { using namespace std; cout<<"enter the distance measured by furlong units:"; double fur; cin>>fur; cout<<"convert the furlong to yard"< void mice(); void see(); using namespace std; int main() { mice(); mice(); see(); see(); return 0; }

c语言程序设计(第3版)的习题答案

1.5请参照本章例题,编写一个C程序,输出以下信息: ************ Very Goodj! ************ 解: main() { printf(" ************ \n"); printf("\n"); printf(" Very Good! \n"); printf("\n"); printf(" ************\n"); } 1.6编写一个程序,输入a b c三个值,输出其中最大者。 解:main() {int a,b,c,max; printf("请输入三个数a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if(max

cprimerplus第六版课后编程练习答案

第二章:开始学习C++ n”; } < intmain() { usingnamespacestd; cout<<"Entertheautomobilegasolineconsumptionfigurein\n" <<""; doubleUS_style; cin>>US_style; cout<<"ConvertstoEuropeanstyle(milespergallon):"<

snack[0].calory=200; snack[1].brand="B"; snack[1].weight=; snack[1].calory=400; snack[2].brand="C"; snack[2].weight=; snack[2].calory=500; for(inti=0;i<3;i++) { cout<<"brand:"< intmain() { usingnamespacestd; constintSize=3; intsuccess[Size]; cout<<"Enteryoursuccessofthethreetimes40metersrunning:\n"; cin>>success[0]>>success[1]>>success[2]; cout<<"success1:"< #include intmain() { usingnamespacestd; arrayad={0}; cout<<"Enteryoursuccessofthethreetimes40metersrunning:\n"; cin>>ad[0]>>ad[1]>>ad[2]; cout<<"success1:"<

数据结构(C语言版)第6章习题答案

第6章树和二叉树自测卷解答 一、下面是有关二叉树的叙述,请判断正误(每小题1分,共10分) (√)1. 若二叉树用二叉链表作存贮结构,则在n个结点的二叉树链表中只有n—1个非空指针域。(×)2.二叉树中每个结点的两棵子树的高度差等于1。 (√)3.二叉树中每个结点的两棵子树是有序的。 (×)4.二叉树中每个结点有两棵非空子树或有两棵空子树。 (×)5.二叉树中每个结点的关键字值大于其左非空子树(若存在的话)所有结点的关键字值,且小于其右非空子树(若存在的话)所有结点的关键字值。(应当是二叉排序树的特点) (×)6.二叉树中所有结点个数是2k-1-1,其中k是树的深度。(应2i-1) (×)7.二叉树中所有结点,如果不存在非空左子树,则不存在非空右子树。 (×)8.对于一棵非空二叉树,它的根结点作为第一层,则它的第i层上最多能有2i—1个结点。(应2i-1)(√)9.用二叉链表法(link-rlink)存储包含n个结点的二叉树,结点的2n个指针区域中有n+1个为空指针。 (正确。用二叉链表存储包含n个结点的二叉树,结点共有2n个链域。由于二叉树中,除根结点外,每一个结点有且仅有一个双亲,所以只有n-1个结点的链域存放指向非空子女结点的指针,还有n+1个空指针。)即有后继链接的指针仅n-1个。 (√)10. 〖01年计算机系研题〗具有12个结点的完全二叉树有5个度为2的结点。 最快方法:用叶子数=[n/2]=6,再求n2=n0-1=5 二、填空(每空1分,共15分) 1.由3个结点所构成的二叉树有5种形态。 2. 【计算机研2000】一棵深度为6的满二叉树有n1+n2=0+ n2= n0-1=31 个分支结点和26-1 =32个叶子。 注:满二叉树没有度为1的结点,所以分支结点数就是二度结点数。 3.一棵具有257个结点的完全二叉树,它的深度为9。 (注:用? log2(n) ?+1= ? 8.xx ?+1=9 4.【全国专升本统考题】设一棵完全二叉树有700个结点,则共有350个叶子结点。 答:最快方法:用叶子数=[n/2]=350 5. 设一棵完全二叉树具有1000个结点,则此完全二叉树有500个叶子结点,有499个度为2的结点,有1个结点只有非空左子树,有0个结点只有非空右子树。 答:最快方法:用叶子数=[n/2]=500 ,n2=n0-1=499。另外,最后一结点为2i属于左叶子,右叶子是空的,所以有1个非空左子树。完全二叉树的特点决定不可能有左空右不空的情况,所以非空右子树数=0. 6.【严题集6.7③】一棵含有n个结点的k叉树,可能达到的最大深度为n,最小深度为2。 答:当k=1(单叉树)时应该最深,深度=n(层);当k=n-1(n-1叉树)时应该最浅,深度=2(层),但不包括n=0或1时的特例情况。教材答案是“完全k叉树”,未定量。) 7. 【试题1】二叉树的基本组成部分是:根(N)、左子树(L)和右 子树(R)。因而二叉树的遍历次序有六种。最常用的是三种:前序法(即 按N L R次序),后序法(即按L R N次序)和中序法(也称

C primer plus(第五版)课后编程练习答案

第一章概览 编程练习 1.您刚刚被MacroMuscle有限公司(Software for Hard Bodies)聘用。该公司要进入欧洲市场,需要一个将英寸转换为厘米(1英寸=2.54 cm)的程序。他们希望建立的该程序可提示用户输入英寸值。您的工作是定义程序目标并设计该程序(编程过程的第1步和第2步)。 1.将英寸值转化为厘米值 2.显示“输入英寸值”->得到该值->转换为厘米值->存储->告知用户已结束 第二章C语言概述 编程练习 1.编写一个程序,调用printf()函数在一行上输出您的名和姓,再调用一次printf()函数在两个单独的行上输出您的名和姓,然后调用一对printf()函数在一行上输出您的名和姓。输出应如下所示(当然里面要换成您的姓名): Anton Bruckner Anton Bruckner Anton Bruckner 第一个输出语句 第二个输出语句 仍然是第二个输出语句 第三个和第四个输出语句 #include int main(void) { printf("He Jin\n"); printf("He\n"); printf("Jin\n"); printf("He Jin\n"); return(0); }

2.编写一个程序输出您的姓名及地址。 #include int main(void) { printf("Name:He Jin\n"); printf("Address:CAUC\n"); return(0); } 3.编写一个程序,把您的年龄转换成天数并显示二者的值。不用考虑平年( fractional year)和闰年(leapyear)的问题。 #include int main(void) { int age=22; printf("Age:%d\n",age); printf("Day:%d\n",age*356); return(0); } 4.编写一个能够产生下面输出的程序: For he's a jolly good fellow! For he's a jolly good fellow! For he's a jolly good fellow! Which nobody can deny! 程序中除了main()函数之外,要使用两个用户定义的函数:一个用于把上面的夸奖消息输出一次:另一个用于把最后一行输出一次。 #include void printf1(void); void printf2(void); int main(void) { printf1(); printf1(); printf1(); printf2(); return(0);

相关文档
相关文档 最新文档