当前位置:文档之家› C++面向对象程序设计答案1~8章 谭浩强

C++面向对象程序设计答案1~8章 谭浩强

C++面向对象程序设计答案1~8章 谭浩强
C++面向对象程序设计答案1~8章 谭浩强

第一章

5:

#include using namespace std;

int main()

{

cout<<"This"<<"is"; cout<<"a"<<"C++";

cout<<"program."<

}

6:

#include using namespace std;

int main()

{

int a,b,c;

a=10;

b=23;

c=a+b;

cout<<"a+b=";

cout<

cout<

return 0;

}

7:

#include using namespace std;

int main()

{

int a,b,c;

int f(int x,int y,int z); cin>>a>>b>>c;

c=f(a,b,c);

cout<

return 0;

}

int f(int x,int y,int z)

{

int m;

if (x

else m=y;

if (z

return(m);

}

8: #include

using namespace std;

int main()

{

int a,b,c;

cin>>a>>b;

c=a+b;

cout<<"a+b="<

return 0;

}

9:

#include

using namespace std;

int main()

{int add(int x,int y);

int a,b,c;

cin>>a>>b;

c=add(a,b);

cout<<"a+b="<

return 0;

}

int add(int x,int y)

{int c;

c=x+y;

return(c);

}

10:

#include

using namespace std;

int main()

{void sort(int x,int y,int z);

int x,y,z;

cin>>x>>y>>z;

sort(x,y,z);

return 0;

}

void sort(int x, int y, int z)

{

int temp;

if (x>y)

{temp=x;x=y;y=temp;}

//{ }内3个语句的作用是将

x和y的值互换)

if (z

cout<

dl;

else if (z

cout<

dl;

else

cout<

dl;

}

11:

#include

using namespace std;

int main()

{int max(int a,int b,int c=0);

int a,b,c;

cin>>a>>b>>c;

cout<<"max(a,b,c)="<

,b,c)<

cout<<"max(a,b)="<

b)<

return 0;

}

int max(int a,int b,int c)

{if(b>a) a=b;

if(c>a) a=c;

return a;

}

12:

#include

using namespace std;

int main()

{

void change(int ,int );

int a,b;

cin>>a>>b;

if(a

cout<<"max="<

min="<

return 0;

}

void change(int ,int )

{

int r1,r2,temp;

temp=r1;

r1=r2;

r2=temp;

}

13:

#include

using namespace std;

int main()

{void sort(int &,int &,int &); int a,b,c,a1,b1,c1;

cout<<"Please enter 3 integers:";

cin>>a>>b>>c;

a1=a;b1=b;c1=c;

sort(a1,b1,c1);

cout<

return 0;

}

void sort(int &i,int &j,int &k) { void change(int &,int &);

if (i>j) change(i,j);

if (i>k) change(i,k);

if (j>k) change(j,k);

} void change(int &x,int &y)

{ int temp;

temp=x;

x=y;

y=temp;

}

14:

#include

#include

using namespace std;

int main()

{ string s1="week",s2="end";

cout<<"s1="<

cout<<"s2="<

s1=s1+s2;

cout<<"The new string

is:"<

return 0;

}

15:

#include

#include

using namespace std;

int main()

{ string str;

int i,n;

char temp;

cout<<"please input a

string:";

cin>>str;

n=str.size();

for(i=0;i

{temp=str[i];str[i]=str[n-

i-1];str[n-i-1]=temp;}

cout<

return 0;

}

16:

#include

#include

using namespace std;

int main()

{ int i;

string

str[5]={"BASIC","C","FORTRA

N","C++","PASCAL"};

void sort(string []);

sort(str);

cout<<"the sorted

strings :"<

for(i=0;i<5;i++)

cout<

cout<

return 0;

}

void sort(string s[])

{int i,j;

string t;

for (j=0;j<5;j++)

for(i=0;i<5-j;i++)

if (s[i]>s[i+1])

{t=s[i];s[i]=s[i+1];s[i+1]=t;}

}17: #include

#include

using namespace std;

int main()

{

long

c[5]={10100,-123567,

1198783,-165654, 3456};

int a[5]={1,9,0,23,-45};

float b[5]={2.4, 7.6, 5.5,

6.6, -2.3 };

void sort(int []);

void sort(float []);

void sort(long []);

sort(a);

sort(b);

sort(c);

return 0;

}

void sort(int a[])

{int i,j,t;

for (j=0;j<5;j++)

for(i=0;i<5-j;i++)

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<

for(i=0;i<5;i++)

cout<

}

void sort(long a[])

{int i,j;

long t;

for (j=0;j<5;j++)

for(i=0;i<5-j;i++)

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<

for(i=0;i<5;i++)

cout<

}

void sort(float a[])

{int i,j;

float t;

for (j=0;j<5;j++)

for(i=0;i<5-j;i++)

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<

for(i=0;i<5;i++)

cout<

} 18: #include

#include

using namespace std;

template

void sort(T a[])

{int i,j,min;

T t;

for(i=0;i<5;i++)

{min=i;

for (j=i+1;j<5;j++)

if(a[min]>a[j])

min=j;

t=a[i]; a[i]=a[min];

a[min]=t;

}

cout<<"the sorted

numbers :"<

for(i=0;i<5;i++)

cout<

cout<

}

int main()

{ int a[5]={1,9,0,23,-45};

float b[5]={2.4, 7.6, 5.5,

6.6, -2.3 };

long c[5]={10100,-123567,

1198783,-165654, 3456};

sort(a);

sort(b);

sort(c);

return 0;

}

第二章

1

#include

using namespace std;

class Time

{

public:

void set_time();

void show_time();

private:

//成员改为公用的

int hour;

int minute;

int sec;

};

void Time::set_time()

//在main函数之前定义

{

cin>>hour;

cin>>minute;

cin>>sec;

}

void Time::show_time()

//在main函数之前定义

{

cout<

":"<

}

int main()

{Time t1;

t1.set_time();

t1.show_time();

return 0;

}

2:

#include

using namespace std;

class Time

{public:

void set_time(void)

{cin>>hour;

cin>>minute;

cin>>sec;

}

void show_time(void)

{cout<

private: int hour;

int minute;

int sec;

};

Time t;

int main()

{

t.set_time();

t.show_time();

return 0;

}

3:

#include

using namespace std; class Time

{public:

void set_time(void);

void show_time(void);

private:

int hour;

int minute;

int sec;

};

void Time::set_time(void) {cin>>hour;

cin>>minute;

cin>>sec;

}

void Time::show_time(void) {cout<

Time t;

int main()

{ t.set_time();

t.show_time();

return 0;

}

4:

//xt2-4-1.cpp(main.cpp)

#include

using namespace std;

#include "xt2-4.h"

int main()

{Student stud;

stud.set_value();

stud.display();

return 0;

}

//xt2-4-2.cpp(即

student.cpp)

#include "xt2-4.h"

//在此文件中进行函数的定

#include

using namespace std;

//不要漏写此行

void Student::display( )

{ cout<<"num:"<

l;

cout<<"name:"<

dl;

cout<<"sex:"<

}

void Student::set_value()

{ cin>>num;

cin>>name;

cin>>sex;

}

5:

//xt2-5-1.cpp(file1.cpp)

#include

#include "xt2-5.h"

int main()

{Array_max arrmax;

arrmax.set_value();

arrmax.max_value();

arrmax.show_value();

return 0;

}

//xt2-5-2.cpp(arraymax.cpp)

#include

using namespace std;

#include "xt2-5.h"

void Array_max::set_value()

{ int i;

for (i=0;i<10;i++)

cin>>array[i];

}

void Array_max::max_value()

{int i;

max=array[0];

for (i=1;i<10;i++)

if(array[i]>max)

max=array[i];

}

void

Array_max::show_value()

{cout<<"max="<

;

}

6:解法一

#include

using namespace std;

class Box

{public:

void get_value();

float volume();

void display();

public:

float lengh;

float width;

float height;

};

void Box::get_value()

{ cout<<"please input lengh, width,height:";

cin>>lengh;

cin>>width;

cin>>height;

}

float Box::volume()

{ return(lengh*width*height );}

void Box::display()

{ cout<

int main()

{Box box1,box2,box3;

box1.get_value();

cout<<"volmue of bax1 is "; box1.display();

box2.get_value();

cout<<"volmue of bax2 is "; box2.display();

box3.get_value();

cout<<"volmue of bax3 is "; box3.display();

return 0;

}

解法二:

#include

using namespace std;

class Box

{public:

void get_value();

void volume();

void display();

public:

float lengh;

float width;

float height;

float vol;

};

void Box::get_value() { cout<<"please input lengh,

width,height:";

cin>>lengh;

cin>>width;

cin>>height;

}

void Box::volume()

{ vol=lengh*width*height;}

void Box::display()

{ cout<

int main()

{Box box1,box2,box3;

box1.get_value();

box1.volume();

cout<<"volmue of bax1 is ";

box1.display();

box2.get_value();

box2.volume();

cout<<"volmue of bax2 is ";

box2.display();

box3.get_value();

box3.volume();

cout<<"volmue of bax3 is ";

box3.display();

return 0;

}

第三章

2:

#include

using namespace std;

class Date

{public:

Date(int,int,int);

Date(int,int);

Date(int);

Date();

void display();

private:

int month;

int day;

int year;

};

Date::Date(int m,int d,int

y):month(m),day(d),year(y)

{ }

Date::Date(int m,int

d):month(m),day(d)

{year=2005;}

Date::Date(int m):month(m)

{day=1;

year=2005;

}

Date::Date()

{month=1;

day=1;

year=2005;

}

void Date::display()

{cout<

/"<

int main()

{

Date d1(10,13,2005);

Date d2(12,30);

Date d3(10);

Date d4;

d1.display();

d2.display();

d3.display();

d4.display();

return 0;

}

3:

#include

using namespace std;

class Date

{public:

Date(int=1,int=1,int=2005);

void display(); private:

int month;

int day;

int year;

};

Date::Date(int m,int d,int y):month(m),day(d),year(y) { }

void Date::display()

{cout<

int main()

{

Date d1(10,13,2005); Date d2(12,30);

Date d3(10);

Date d4;

d1.display();

d2.display();

d3.display();

d4.display();

return 0;

}

4:

#include

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void display(); private:

int num;

float score;

}; void Student::display()

{cout<

"<

int main()

{Student stud[5]={

Student(101,78.5),Student(1

02,85.5),Student(103,98.5),

Student(104,100.0),Student(

105,95.5)};

Student *p=stud;

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

p->display();

return 0;

}

5:

#include

using namespace std;

class Student

{public:

Student(int n,float

s):num(n),score(s){}

int num;

float score;

};

void main()

{Student stud[5]={

Student(101,78.5),Student(1

02,85.5),Student(103,98.5),

Student(104,100.0),Student(

105,95.5)};

void max(Student* );

Student *p=&stud[0];

max(p);

}

void max(Student *arr)

{float

max_score=arr[0].score;

int k=0;

for(int i=1;i<5;i++)

if(arr[i].score>max_score)

{max_score=arr[i].score;k=i;}

cout<

"<

}

6:

#include

using namespace std;

class Student

{public:

Student(int n,float

s):num(n),score(s){}

void change(int n,float s)

{num=n;score=s;}

void

display(){cout<

"<

private:

int num;

float score;

};

int main()

{Student stud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return 0;

}

7: 解法一

#include

using namespace std;

class Student

{public:

Student(int n,float

s):num(n),score(s){}

void change(int n,float s)

{num=n;score=s;}

void display() {cout<

"<

//可改为:void display() const {cout<

private:

int num;

float score;

};

int main()

{const Student stud(101,78.5);

stud.display();

//stud.change(101,80.5); stud.display();

return 0;

}

解法二:

#include

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s) const {num=n;score=s;}

void display() const {cout<

"<

private:

mutable int num;

mutable float score;

};

int main()

{const Student stud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return 0;

}

解法三:

#include

using namespace std;

class Student

{public:

Student(int n,float

s):num(n),score(s){}

void change(int n,float s)

{num=n;score=s;}

void display()

{cout<

"<

private:

int num;

float score;

};

int main()

{Student stud(101,78.5);

Student *p=&stud;

p->display();

p->change(101,80.5);

p->display();

return 0;

}

8:

#include

using namespace std;

class Student

{public:

Student(int n,float

s):num(n),score(s){}

void change(int n,float s)

{num=n;score=s;}

void display()

{cout<

"<

private:

int num;

float score;

};

int main()

{Student stud(101,78.5);

void fun(Student&);

fun(stud);

return 0;

}

void fun(Student &stu)

{stu.display();

stu.change(101,80.5);

stu.display();

}

9:

#include

using namespace std;

class Product

{public:

Product(int n,int q,float

p):num(n),quantity(q),price(

p){};

void total();

static float average();

static void display();

private:

int num;

int quantity;

float price;

static float discount;

static float sum;

static int n;

};

void Product::total()

{float rate=1.0;

if(quantity>10)

rate=0.98*rate;

sum=sum+quantity*price*ra

te*(1-discount);

n=n+quantity;

}

void Product::display() {cout<

cout<

float Product::average() {return(sum/n);}

float Product::discount=0.05; float Product::sum=0;

int Product::n=0;

int main()

{

Product Prod[3]={

Product(101,5,23.5),Product( 102,12,24.56),Product(103,1 00,21.5)

};

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

Prod[i].total();

Product::display();

return 0;

}

10:

#include

using namespace std;

class Date;

class Time

{public:

Time(int,int,int);

friend void display(const Date &,const Time &); private:

int hour;

int minute;

int sec;

};

Time::Time(int h,int m,int s)

{hour=h;

minute=m;

sec=s;

}

class Date

{public:

Date(int,int,int);

friend void display(const

Date &,const Time &);

private:

int month;

int day;

int year;

};

Date::Date(int m,int d,int y)

{month=m;

day=d;

year=y;

}

void display(const Date

&d,const Time &t)

{

cout<

<<"/"<

cout<

e<<":"<

}

int main()

{

Time t1(10,13,56);

Date d1(12,25,2004);

display(d1,t1);

return 0;

}

11:

#include

using namespace std;

class Time;

class Date

{public:

Date(int,int,int);

friend Time;

private:

int month;

int day;

int year;

};

Date::Date(int m,int d,int

y):month(m),day(d),year(y){

}

class Time

{public:

Time(int,int,int);

void display(const Date

&);

private:

int hour;

int minute;

int sec;

};

Time::Time(int h,int m,int

s):hour(h),minute(m),sec(s){

}

void Time::display(const

Date &d)

{

cout<

<<"/"<

cout<

":"<

}

int main()

{

Time t1(10,13,56);

Date d1(12,25,2004);

t1.display(d1);

return 0;

}

12:

#include

using namespace std; template class Compare

{public:

Compare(numtype

a,numtype b);

numtype max();

numtype min(); private:

numtype x,y;

};

template Compare::Compa re(numtype a,numtype b) {x=a;y=b;}

template numtype

Compare::max() {return (x>y)?x:y;} template numtype

Compare::min() {return (x

int main()

{Compare cmp1(3,7); cout<

cout<

Compare

cmp2(45.78,93.6);

cout<

the Maximum of two float

numbers."<

cout<

Minimum of two float

numbers."<

Compare

cmp3('a','A');

cout<

the Maximum of two

characters."<

cout<

Minimum of two

characters."<

return 0;

}

第四章

1:

#include

using namespace std;

class Complex

{public:

Complex(){real=0;imag=0;}

Complex(double r,double

i){real=r;imag=i;}

double get_real();

double get_imag();

void display();

private:

double real;

double imag;

};

double Complex::get_real()

{return real;}

double Complex::get_imag()

{return imag;}

void Complex::display()

{cout<<"("<

<<"i)"<

Complex operator +

(Complex &c1,Complex &c2)

{

return

Complex(c1.get_real()+c2.ge

t_real(),c1.get_imag()+c2.get

_imag());

}

int main()

{Complex

c1(3,4),c2(5,-10),c3;

c3=c1+c2;

cout<<"c3=";

c3.display();

return 0;

}

2:

#include

using namespace std;

class Complex

{public:

Complex(){real=0;imag=0;}

Complex(double r,double

i){real=r;imag=i;}

Complex

operator+(Complex &c2);

Complex

operator-(Complex &c2);

Complex

operator*(Complex &c2);

Complex

operator/(Complex &c2);

void display();

private:

double real;

double imag;

};

Complex

Complex::operator+(Comple x &c2)

{Complex c;

c.real=real+c2.real;

c.imag=imag+c2.imag; return c;}

Complex

Complex::operator-(Complex &c2)

{Complex c;

c.real=real-c2.real;

c.imag=imag-c2.imag; return c;}

Complex

Complex::operator*(Comple x &c2)

{Complex c;

c.real=real*c2.real-imag*c2.i mag;

c.imag=imag*c2.real+real*c2 .imag;

return c;}

Complex

Complex::operator/(Comple x &c2)

{Complex c;

c.real=(real*c2.real+imag*c2 .imag)/(c2.real*c2.real+c2.i mag*c2.imag);

c.imag=(imag*c2.real-real*c

2.imag)/(c2.real*c2.real+c2.i mag*c2.imag);

return c;}

void Complex::display() {cout<<"("<

<<"i)"<

int main()

{Complex

c1(3,4),c2(5,-10),c3;

c3=c1+c2;

cout<<"c1+c2=";

c3.display();

c3=c1-c2;

cout<<"c1-c2=";

c3.display();

c3=c1*c2;

cout<<"c1*c2=";

c3.display();

c3=c1/c2;

cout<<"c1/c2=";

c3.display();

return 0;

}

3:

#include

//用VC++时改为∶

#include

using namespace std;

//用VC++时为取消此行

class Complex

{public:

Complex(){real=0;imag=0;}

Complex(double r,double

i){real=r;imag=i;}

Complex

operator+(Complex &c2);

Complex operator+(int

&i);

friend Complex

operator+(int&,Complex &);

void display();

private:

double real;

double imag;

};

Complex

Complex::operator+(Comple

x &c)

{return

Complex(real+c.real,imag+c.i

mag);}

Complex

Complex::operator+(int &i)

{return

Complex(real+i,imag);}

void Complex::display()

{cout<<"("<

<<"i)"<

Complex operator+(int

&i,Complex &c)

{return

Complex(i+c.real,c.imag);}

int main()

{Complex

c1(3,4),c2(5,-10),c3;

int i=5;

c3=c1+c2;

cout<<"c1+c2=";

c3.display();

c3=i+c1;

cout<<"i+c1=";

c3.display();

c3=c1+i;

cout<<"c1+i=";

c3.display();

return 0;

}

4:

#include

using namespace std;

class Matrix

//定义Matrix类

{public:

Matrix();

//默认构造函数

friend Matrix operator+(Matrix &,Matrix &); //重载运算符“+”

void input(); //输入数据函数

void display(); //输出数据函数

private:

int mat[2][3];

};

Matrix::Matrix()

//定义构造函数

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

for(int j=0;j<3;j++)

mat[i][j]=0;

}

Matrix operator+(Matrix &a,Matrix &b) //定义重载运算符“+”函数{Matrix c;

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

for(int j=0;j<3;j++)

{c.mat[i][j]=a.mat[i][j]+b.mat [i][j];}

return c;

}

void Matrix::input() //定义输入数据函数{cout<<"input value of matrix:"<

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

for(int j=0;j<3;j++)

cin>>mat[i][j];

}

void Matrix::display() //定义输出数据函数

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

{for(int j=0;j<3;j++)

{cout<

cout<

}

int main()

{Matrix a,b,c;

a.input();

b.input();

cout<

a:"<

a.display();

cout<

b:"<

b.display();

c=a+b;

//用重载运算符“+”实现两

个矩阵相加

cout<

Matrix a + Matrix b :"<

c.display();

return 0;

}

5:

#include

//using namespace std;

class Matrix

{public:

Matrix();

friend Matrix

operator+(Matrix &,Matrix

&);

friend ostream&

operator<<(ostream&,Matrix

&);

friend istream&

operator>>(istream&,Matrix

&);

private:

int mat[2][3];

};

Matrix::Matrix()

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

for(int j=0;j<3;j++)

mat[i][j]=0;

}

Matrix operator+(Matrix

&a,Matrix &b)

{Matrix c;

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

for(int j=0;j<3;j++)

{c.mat[i][j]=a.mat[i][j]+b.mat

[i][j];

}

return c;

}

istream&

operator>>(istream

&in,Matrix &m)

{cout<<"input value of

matrix:"<

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

for(int j=0;j<3;j++)

in>>m.mat[i][j];

return in;

}

ostream&

operator<<(ostream

&out,Matrix &m)

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

{for(int j=0;j<3;j++)

{out<

out<

return out;

}

int main()

{ Matrix a,b,c;

cin>>a;

cin>>b;

cout<

a:"<

cout<

b:"<

c=a+b;

cout<

}

6:

#include

using namespace std;

class Complex

{public:

Complex(){real=0;imag=0;} Complex(double

r){real=r;imag=0;}

Complex(double r,double i){real=r;imag=i;}

operator double(){return real;}

void display(); private:

double real;

double imag;

};

void Complex::display() {cout<<"("<

"<

int main()

{Complex c1(3,4),c2; double d1;

d1=2.5+c1;

cout<<"d1="<

c2=Complex(d1);

cout<<"c2=";

c2.display();

return 0;

}

7:

#include

using namespace std;

class Student

{public:

Student(int,char[],char,float);

int get_num(){return

num;}

char * get_name(){return

name;}

char get_sex(){return sex;}

void display()

{cout<<"num:"<

ame:"<

ex<<"\nscore:"<

\n";}

private:

int num;

char name[20];

char sex;

float score;

};

Student::Student(int n,char

nam[],char s,float so)

{num=n;

strcpy(name,nam);

sex=s;

score=so;

}

class Teacher

{public:

Teacher(){}

Teacher(Student&);

Teacher(int n,char

nam[],char sex,float pay);

void display();

private:

int num;

char name[20];

char sex;

float pay;

};

Teacher::Teacher(int n,char

nam[],char s,float p)

{num=n;

strcpy(name,nam);

sex=s;

pay=p;

}

Teacher::Teacher(Student&

stud)

{num=stud.get_num();

strcpy(name,stud.get_name(

));

sex=stud.get_sex();

pay=1500;}

void Teacher::display()

{cout<<"num:"<

ame:"<

ex<<"\npay:"<

}

int main()

{Teacher

teacher1(10001,"Li",'f',1234.

5),teacher2;

Student

student1(20010,"Wang",'m',

89.5);

cout<<"student1:"<

student1.display();

teacher2=Teacher(student1);

cout<<"teacher2:"<

teacher2.display();

return 0;

}

第五章

1:

#include

using namespace std;

class Student

{public:

void get_value()

{cin>>num>>name>>sex;} void display( )

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

private :

int num;

char name[10];

char sex;

};

class Student1: public Student

{public:

void get_value_1()

{get_value();

cin>>age>>addr;}

void display_1()

{ cout<<"age: "<

cout<<"address: "<

int age;

char addr[30];

};

int main()

{Student1 stud1;

stud1.get_value_1();

stud1.display();

stud1.display_1();

return 0;

}

2:

#include using namespace std;

class Student

{public:

void get_value()

{cin>>num>>name>>sex;}

void display( )

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

private :

int num;

char name[10];

char sex;

};

class Student1: private

Student

{public:

void get_value_1()

{get_value();

cin>>age>>addr;}

void display_1()

{display();

cout<<"age:

"<

用派生类的私有成员,正确。

cout<<"address:

"<

派生类的私有成员,正确。

private:

int age;

char addr[30];

};

int main()

{Student1 stud1;

stud1.get_value_1();

stud1.display_1();

return 0;

}

3:

#include

using namespace std;

class Student

//声明基类

{public:

//基类公用成员

void get_value();

void display( );

protected :

//基类保护成员

int num;

char name[10];

char sex;

};

void Student::get_value()

{cin>>num>>name>>sex;}

void Student::display( )

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex: "<

}

class Student1: protected

Student //

声明一个保护派生类

{public:

void get_value_1();

void display1( );

private:

int age;

char addr[30];

};

void Student1::get_value_1()

{get_value();

cin>>age>>addr;

}

void Student1::display1( )

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

cout<<"age:

"<

cout<<"address:

"<

}

int main( )

{Student1 stud1; //stud1是派生类student1类的对象

stud1.get_value_1();

//调用派生类对象stud1的公用成员函数

stud1.display1( ); //调用派生类对象stud1的公用成员函数

return 0;

}

4: 解法一

#include

using namespace std;

class Student //声明基类

{public:

//基类公用成员

void get_value();

void display( ); protected : //基类保护成员

int num;

char name[10];

char sex;

}; void Student::get_value()

{cin>>num>>name>>sex;}

void Student::display( )

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex: "<

}

class Student1: public

Student //

声明一个公用派生类

{public:

void get_value_1();

void display1( );

private:

int age;

char addr[30];

};

void Student1::get_value_1()

{get_value();

cin>>age>>addr;

}

void Student1::display1( )

{cout<<"num:

"<

引用基类的保护成员,合法

cout<<"name:

"<

引用基类的保护成员,合法

cout<<"sex:

"<

引用基类的保护成员,合法

cout<<"age:

"<

引用派生类的私有成员,合

cout<<"address:

"<

派生类的私有成员,合法

}

int main( )

{Student1 stud1;

//stud1是派生类student1

类的对象

stud1.get_value_1();

//调用派生类对象stud1的

公用成员函数get_value_1

stud1.display1( );

//调用派生类对象stud1的

公用成员函数display1

return 0;

}

解法二

#include

using namespace std;

class Student

//声明基类

{public:

//基类公用成员

void get_value();

void display( );

protected :

//基类保护成员

int num;

char name[10];

char sex;

};

void Student::get_value()

{cin>>num>>name>>sex;}

void Student::display( )

{cout<<"num:

"<

cout<<"name:"<

dl;

cout<<"sex:"<

}

class Student1: protected

Student //

声明一个公用派生类

{public:

void get_value_1();

void display1( ); private:

int age;

char addr[30];

};

void Student1::get_value_1() {cin>>age>>addr;}

void Student1::display1( )

{cout<<"age:"<

cout<<"address:"<

}

int main( )

{Student1 stud1; //stud1是派生类student1类的对象

stud1.get_value();

stud1.get_value_1(); stud1.display( );

stud1.display1();

//合法。display1是派生类中的公用成员函数

return 0;

}

5:

class A //A为基类

{public:

void f1( );

int i;

protected:

void f2();

int j;

private:

int k;

}; class B: public A

//B为A的公用派生类

{public:

void f3( );

protected:

int m;

private:

int n;

};

class C: public B

//C为B的公用派生类

{public:

void f4();

private:

int p;

};

int main()

{A a1;

//a1是基类A的对象

B b1;

//b1是派生类B的对象

C c1;

//c1是派生类C的对象

return 0;

}

6:

#include

using namespace std;

class A

{public:

void f1( );

protected:

void f2();

private:

int i;

};

class B: public A

{public:

void f3( );

int k;

private:

int m;

};

class C: protected B

{public:

void f4();

protected:

int n;

private:

int p;

};

class D: private C

{public:

void f5();

protected:

int q;

private:

int r;

};

int main()

{A a1;

B b1;

C c1;

D d1;

return 0;

}

7:

#include

using namespace std;

class A

{

public:

A(){a=0;b=0;}

A(int i){a=i;b=0;}

A(int i,int j){a=i;b=j;}

void

display(){cout<<"a="<

b="<

private:

int a;

int b;

};

class B : public A

{

public:

B(){c=0;}

B(int i):A(i){c=0;}

B(int i,int j):A(i,j){c=0;}

B(int i,int j,int k):A(i,j){c=k;}

void display1()

{display();

cout<<" c="<

}

private:

int c;

};

int main()

{ B b1;

B b2(1);

B b3(1,3);

B b4(1,3,5);

b1.display1();

b2.display1();

b3.display1();

b4.display1();

return 0;

}

8:

#include

using namespace std;

class A

{

public:

A(){cout<<"constructing A "<

~A(){cout<<"destructing A "<

};

class B : public A

{

public:

B(){cout<<"constructing B "<

~B(){cout<<"destructing

B "<

};

class C : public B

{

public:

C(){cout<<"constructing

C "<

~C(){cout<<"destructing

C "<

};

int main()

{ C c1;

return 0;

}

9:

#include

#include

using namespace std;

class Teacher

{public:

Teacher(string nam,int

a,char s,string tit,string

ad,string t);

void display();

protected:

string name;

int age;

char sex;

string title;

string addr;

string tel;

};

Teacher::Teacher(string

nam,int a,char s,string

tit,string ad,string t):

name(nam),age(a),sex(s),titl

e(tit),addr(ad),tel(t){ }

void Teacher::display()

{cout<<"name:"<

dl;

cout<<"age"<

cout<<"sex:"<

cout<<"title:"<

cout<<"address:"<

ndl;

cout<<"tel:"<

}

class Cadre

{public:

Cadre(string nam,int

a,char s,string p,string

ad,string t);

void display();

protected:

string name;

int age;

char sex;

string post;

string addr;

string tel;

};

Cadre::Cadre(string nam,int

a,char s,string p,string

ad,string t):

name(nam),age(a),sex(s),pos

t(p),addr(ad),tel(t){}

void Cadre::display()

{cout<<"name:"<

dl;

cout<<"age:"<

cout<<"sex:"<

cout<<"post:"<

cout<<"address:"<

cout<<"tel:"<

}

class Teacher_Cadre:public Teacher,public Cadre {public:

Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w);

void show( );

private:

float wage;

};

Teacher_Cadre::Teacher_Cad re(string nam,int a,char s,string t,string p,string ad,string tel,float w):

Teacher(nam,a,s,t,ad,tel),Cad re(nam,a,s,p,ad,tel),wage(w) {}

void Teacher_Cadre::show( ) {Teacher::display();

cout<<"post:"<

cout<<"wages:"<

}

int main( )

{Teacher_Cadre

te_ca("Wang-li",50,'f',"prof." ,"president","135 Beijing Road,Shanghai","(021)61234

567",1534.5);

te_ca.show( );

return 0;

}

10:

#include

#include

using namespace std;

class Teacher

//教师类

{public:

Teacher(int,char [],char);

//声明构造函数

void display();

//声明输出函数

private:

int num;

char name[20];

char sex;

};

Teacher::Teacher(int n,char

nam[],char s) //定义构

造函数

{num=n;

strcpy(name,nam);

sex=s;

}

void Teacher::display()

//定义输出函数

{cout<<"num:"<

;

cout<<"name:"<

dl;

cout<<"sex:"<

}

class BirthDate

//生日类

{public:

BirthDate(int,int,int);

//声明构造函数

void display();

//声明输出函数

void change(int,int,int);

//声明修改函数

private:

int year;

int month;

int day;

};

BirthDate::BirthDate(int y,int

m,int d) //定义构造

函数

{year=y;

month=m;

day=d;

}

void BirthDate::display()

//定义输出函数

{cout<<"birthday:"<

<"/"<

l;}

void BirthDate::change(int

y,int m,int d) //定义修

改函数

{year=y;

month=m;

day=d;

}

class Professor:public

Teacher

//教授类

{public:

Professor(int,char

[],char,int,int,int,float);

//声明构造函数

void display();

//声明输出函数

void change(int,int,int); //声明修改函数

private:

float area;

BirthDate birthday; //定义BirthDate类的对象作为数据成员

};

Professor::Professor(int

n,char nam[20],char s,int y,int m,int d,float a):

Teacher(n,nam,s),birthday(y, m,d),area(a){ } //定义构造函数

void Professor::display() //定义输出函数{Teacher::display(); birthday.display();

cout<<"area:"<

void Professor::change(int y,int m,int d) //定义修改函数{birthday.change(y,m,d);

}

int main()

{Professor

prof1(3012,"Zhang",'f',1949, 10,1,125.4); //定义Professor对象prof1

cout<

prof1.display();

//调用prof1对象的display 函数

cout<

prof1.change(1950,6,1); //调用prof1对象的change

函数

prof1.display();

//调用prof1对象的display

函数

return 0;

}

第六章

1:

//xt6-1/cpp

#include //

如用VC++应改为∶#include

using namespace std; //

如用VC++应取消此行

#include "cylinder.h"

#include "point.cpp"

#include "circle.cpp"

#include "cylinder.cpp"

int main()

{Cylinder cy1(3.5,6.4,5.2,10);

cout<<"\noriginal

cylinder:\nx="<

", y="<

h="<

ea="<

<<",

volume="<

ndl;

cy1.setHeight(15);

cy1.setRadius(7.5);

cy1.setPoint(5,5);

cout<<"\nnew

cylinder:\n"<

Point &pRef=cy1;

cout<<"\npRef as a

point:"<

Circle &cRef=cy1;

cout<<"\ncRef as a

Circle:"<

return 0;

}

3:解法一

#include

using namespace std;

class Point

{public:

Point(float a,float

b):x(a),y(b){}

~Point(){cout<<"executing

Point destructor"<

private:

float x;

float y;

};

class Circle:public Point

{public:

Circle(float a,float b,float

r):Point(a,b),radius(r){}

~Circle(){cout<<"executing

Circle destructor"<

private:

float radius;

};

int main()

{Point *p=new

Circle(2.5,1.8,4.5);

delete p;

return 0;

}

3:解法二

#include

using namespace std;

class Point

{public:

Point(float a,float

b):x(a),y(b){}

~Point(){cout<<"executing

Point destructor"<

private:

float x;

float y;

};

class Circle:public Point {public:

Circle(int a,int b,int r):Point(a,b),radius(r){}

~Circle(){cout<<"executing Circle destructor"<

float radius;

};

int main()

{Point *p=new Circle(2.5,1.8,4.5);

Circle *pt=new Circle(2.5,1.8,4.5);

delete pt;

return 0;

}

3:解法三

#include

using namespace std;

class Point

{public:

Point(float a,float b):x(a),y(b){}

virtual

~Point(){cout<<"executing Point destructor"<

float x;

float y;

};

class Circle:public Point {public:

Circle(float a,float b,float r):Point(a,b),radius(r){} virtual

~Circle(){cout<<"executing Circle destructor"<

float radius; };

void main()

{Point *p=new

Circle(2.5,1.8,4.5);

delete p;

}

4:

#include

using namespace std;

//定义抽象基类Shape

class Shape

{public:

virtual double area() const

=0; //纯虚函

};

//定义Circle类

class Circle:public Shape

{public:

Circle(double r):radius(r){}

//结构函数

virtual double area() const

{return

3.14159*radius*radius;};

//定义虚函数

protected:

double radius;

//半径

};

//定义Rectangle类

class Rectangle:public Shape

{public:

Rectangle(double w,double

h):width(w),height(h){}

//结构函数

virtual double area() const

{return width*height;}

//定义虚函数

protected:

double width,height;

//宽与高

};

class Triangle:public Shape

{public:

Triangle(double w,double

h):width(w),height(h){}

//结构函数

virtual double area() const

{return 0.5*width*height;}

//定义虚函数

protected:

double width,height;

//宽与高

};

//输出面积的函数

void printArea(const Shape

&s)

{cout<

//输出s的面积

int main()

{

Circle circle(12.6);

//建立Circle类对象circle

cout<<"area of circle

=";

printArea(circle);

//输出circle的面积

Rectangle

rectangle(4.5,8.4);

//建立Rectangle类对象

rectangle

cout<<"area of rectangle

=";

printArea(rectangle);

//输出rectangle的面积

Triangle triangle(4.5,8.4);

//建立Triangle类对象

cout<<"area of triangle

=";

printArea(triangle);

//输出triangle的面积

return 0;

}

5:

#include

using namespace std;

//定义抽象基类Shape class Shape

{public:

virtual double area() const =0;

//纯虚函数

};

//定义Circle(圆形)类

class Circle:public Shape {public:

Circle(double r):radius(r){} //结构函数

virtual double area() const {return

3.14159*radius*radius;};

//定义虚函数protected:

double radius; //半径

};

//定义Square(正方形)类class Square:public Shape {public:

Square(double s):side(s){} //结构函数

virtual double area() const {return side*side;} //定义虚函数protected:

double side;

};

//定义Rectangle(矩形)类class Rectangle:public Shape {public:

Rectangle(double w,double h):width(w),height(h){}

//结构函数

virtual double area() const

{return width*height;}

//定义虚函数

protected:

double width,height;

//宽与高

};

//定义Trapezoid(梯形)类

class Trapezoid:public Shape

{public:

Trapezoid(double t,double

b,double

h):top(t),bottom(t),height(h)

{} //结构函数

virtual double area() const

{return

0.5*(top+bottom)*height;}

//定义虚函数

protected:

double top,bottom,height;

//上底、下底与高

};

//定义Triangle(三角形)类

class Triangle:public Shape

{public:

Triangle(double w,double

h):width(w),height(h){}

//结构函数

virtual double area() const

{return 0.5*width*height;}

//定义虚函数

protected:

double width,height;

//宽与高

};

int main()

{

Circle circle(12.6);

//建立Circle类对象circle

Square square(3.5);

//建立Square类对象square

Rectangle

rectangle(4.5,8.4);

//建立Rectangle类对象

rectangle

Trapezoid

trapezoid(2.0,4.5,3.2);

//建立Trapezoid类对象

trapezoid

Triangle triangle(4.5,8.4);

//建立Triangle类对象

Shape

*pt[5]={&circle,&square,&re

ctangle,&trapezoid,&triangle

};

//定义基类指针数组pt,使

它每一个元素指向一个派

生类对象

double areas=0.0;

//areas为总面积

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

{areas=areas+pt[i]->area();}

cout<<"totol of all

areas="<

输出总面积

return 0;

}

第七章

1:解法一

#include

#include

using namespace std;

int main()

{double a,b,c,s,area;

cout<<"please input a,b,c:";

cin>>a>>b>>c;

if (a+b<=c)

cerr<<"a+b<=c,error!"<

;

(完整版)谭浩强c程序设计课后习题答案

谭浩强c++程序设计课后答案 娄警卫

第一章1.5题 #include using namespace std; int main() { cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0; 1.6题 #include using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout< using namespace std; int main() { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout< using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< using namespace std; int main() { int a,b,c; int add(int x,int y); cin>>a>>b; c=add(a,b); cout<<"a+b="<

C语言程序设计谭浩强第四版期末复习重点

C语言程序设计谭浩强第四版期末复习重点 Newly compiled on November 23, 2020

第一章 程 序设计和C 语言 .什么是计算机程序 程序.. :一组计算机能识别和执行的指令。只要让计算机执行这个程序,计算机就会自动地、有条不紊地进行工作 计算机的一切操作都是由程序控制的,离开程序,计算机将一事无成。 什么是计算机语言 计算机语言:人和计算机交流信息的、计算机和人都能识别的语言。 计算机语言发展阶段:机器语言(由0和1组成的指令) 符号语言(用英文字母和数字表示指令) 高级语言(接近于人的自然语言和数学语言) 面向过程的语言(非结构化的语言、结构化语言);面向对象的语言 语言的发展及其特点 C 语言是一种用途广泛、功能强大、使用灵活的过程性编程语言,既可用于编写应用软件,又能用于编写系统软件。因此C 语言问世以后得到迅速推广。 C 语言主要特点: 语言简洁、紧凑,使用方便、灵活。(只有37个关键字、9种控制语句;程序书写形式 自由,源程序短) 运算符丰富。(34种运算符;把括号、赋值、强制类型转换等都作为运算符处理;表达 式类型多样化) 数据类型丰富。(包括:整型、浮点型、字符型、数组类型、指针类型、结构体类型、共用体类型;C99又扩充了复数浮点类型、超长整型、布尔类型;指针类型数据,能用来实现各种复杂的数据结构的运算。)

具有结构化的控制语句。(如if…else语句、while语句、do…while语句、switch语句、for语句用函数作为程序的模块单位,便于实现程序的模块化;C语言是完全模块化和结构化的语言) 语法限制不太严格,程序设计自由度大。(对数组下标越界不做检查;对变量的类型使用比较灵活,例如,整型量与字符型数据可以通用;C语言允许程序编写者有较大的自由度,因此放宽了语法检查) 允许直接访问物理地址,能进行位操作,可以直接对硬件进行操作。(C语言具有高级语言的功能和低级语言的许多功能,可用来编写系统软件;这种双重性,使它既是成功的系统描述语言,又是通用的程序设计语言) 用C语言编写的程序可移植性好。(C的编译系统简洁,很容易移植到新系统;在新系统上运行时,可直接编译“标准链接库”中的大部分功能,不需要修改源代码;几乎所有计算机系统都可以使用C语言) 生成目标代码质量高,程序执行效率高。 1.C语言允许用两种注释方式:计算法 3.编写程序 4.对源程序进行编辑、编译和连接 5. 运行程序,分析结 6.编写程序文档 第二章算法——程序的灵魂 一个程序主要包括以下两方面的信息: (1) 对数据的描述。在程序中要指定用到哪些数据以及这些数据的类型和数据的组织形式,这就是数据结构。 (2) 对操作的描述。即要求计算机进行操作的步骤,也就是算法。 数据是操作的对象,操作的目的是对数据进行加工处理,以得到期望的结果。 着名计算机科学家沃思(Nikiklaus Wirth)提出一个公式:算法 + 数据结构 = 程序

C程序设计第四版谭浩强完整版课后习题答案

C程序设计第四版谭浩强完整版课后习题答案集团标准化办公室:[VV986T-J682P28-JP266L8-68PNN]

C程序设计(第四版)(谭浩强)第一章课后习题答案 P006 向屏幕输出文字. #include<>代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to \n"); return 0; } P008 求两个数的和. #include<> int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum);

return 0; } P008 调用函数比较两个数的大小. #include<> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c); return 0; } int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); }

P015 三个数的大小.(数字0表示课后练习题) #include<> int main() { int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); printf("The max is :%d .\n",d); } int max(int x , int y , int z) { int m; if (x>y && x>z) m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); }

C程序设计第四版谭浩强_习题&例题第7章

第7章函数 例7.1 #include int main() { void print_star(); void print_message(); print_star(); print_message(); print_star(); return 0; } void print_star() { printf("***********************************\n"); } void print_message() { printf("How do you do!\n"); } 例7.2 int max(int x,int y) { int z; z=x>y?x:y; return(z); } #include int main() { int max(int x,int y); int a,b,c; printf("please enter two integer number:"); scanf("%d,%d",&a,&b); c=max(a,b); printf("max is %d\n",c); return 0; }

例7.3 #include int main() { int max(float x,float y); float a,b; int c; printf("please enter two integer number:"); scanf("%f,%f",&a,&b); c=max(a,b); printf("max is %d\n",c); return 0; } int max(float x,float y) { float z; z=x>y?x:y; return(z); } 例7.4 #include int main() { float add(float x,float y); float a,b,c; printf("please enter a and b:"); scanf("%f,%f",&a,&b); c=add(a,b); printf("sum is %f\n",c); return 0; } float add(float x,float y) { float z; z=x+y; return(z); }

《C语言程序设计》_谭浩强版教案

《C 语言程序设计》教案 职称: 助教 ____________ 单 位:湖南理工职业技术学院 学院(教研室):风能工程学院 工业机器人专业 授课教师: 周常欣

教 学 重 占 八、、 和 难 占 八、、 一、 程序设计和 C 语言 重点:计算机程序、计算机语言、 C 语言编译软件的安装、最简单的 C 语言程序 二、 算法:程序的灵魂 重点:简单的算法举例、算法的特性、用流程图表示算法。 三、 顺序结构程序设计 重点:C 语言的数据类型、C 语句的种类、赋值语句、数据的输入输出及输入输出 中最常用的 控制格式。 四、 选择结构程序设计 重点:关系运算符与逻辑运算符及其组成的具有逻辑值的表达式、 二条分支语句的格 式及基本应用、多分支的选择语句。 五、 循环结构程序设计 重点:C 构成循环的四种方法,尤其是后三种方法、 break 与continue 语句的基本作 用。 难点:while 语句;do-while 语句;for 语句;循环的嵌套; break 与continue 语句。 六、 数组 重点:一维数组、二维数组的定义与引用;字符数组的定义与引用、常用字符串处 理函数及字符处理函数;数组的应用 难点:二维数组的定义与引用;字符数组;数组的应用 七、 函数 重点:函数的定义;函数的参数和函数的值;函数的调用;函数的嵌套调用;函数的 递归调 用;数组作为函数参数;变量作用域;量存储类别; 难点:函数定义、函数调用、函数声明等基本基本概念;函数的嵌套调用与递归调 用;数组作 为函数的参数、变量的存储类别与作用域。 八、 指针 重点和难点:指针与地址的基本概念、指针与变量的关系;指针与数组;指针与字 符串、指针 数组与二级指针;指针的应用 九、 建立自己的数据类型 重点和难点:定义和使用结构体变量、用指针处理链表、共用体类型 十、文件 重点:文件的基本知识、 fopen 、fclose 函数打开与关闭文件、顺序读与数据文件、 随机读写数据文件 难点:用二进制方式向文件读写一组数据。 十一、常见错误分析 重点和难点:文件的基本概念;文件的打开、关闭、常用读写方法。 十二、数组高级应用 重点:不定长数组与二维数组的应用 难点:不定长数组与二维数组的应用 十三、综合应用 重点:数组、if 语句、循环语句、函数知识的综合应用 难点:二维数组、指针 教材、 参 考书 教材:《C 程序设计》(第四版) 谭浩强著 清华大学出版社2010年6月 参考书:《C 程序设计语言》 Kernighan&Ritchie 机械工业出版社 《C 语言程序设计》教案 第1-2课时

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语言程序设计(第二版)习题答案

C语言程序设计(第2版)课后习题答案 第一章 1.请参照本章例题,编写一个C程序,输出以下信息: ************************** Very good! ************************** 解: #include void main() { printf("**************************"); printf("\n"); printf("Very good!\n"); printf("\n"); printf("**************************"); } 2.编写一个C程序,输入a、b、c三个值,输出其中最大值。 解: #include void main() { int a,b,c,max; printf("请输入三个数a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if(max #include void main() { double P, r=0.1, n=10; P=pow((1+r), n);

printf("%lf\n", P); } 3.请编程序将"China"译成密码,译码规律是用原来字母后面的第4个字母代替原来的字母。例如,字母"A"后面第4个字母是"E","E"代替"A"。因此,"China"应译为"Glmre"。请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为'C'、'h'、'i'、'n'、'a',经过运算,使c1、c2、c3、c4、c5分别变为'G'、'l'、'm'、'r'、'e',并输出。 解: #include void main() { char c1='C',c2='h',c3='i',c4='n',c5='a'; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf("密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 第三章 3.用下面的scanf函数输入数据,使a=3, b=7, x=8.5, y=71.82, c1='A', c2='a'。问在键盘上如何输入? 解: #include void main() { int a, b; float x, y; char c1, c2; scanf("a=%d, b=%d", &a, &b); scanf(" x=%f, y=%e", &x, &y); scanf(" c1=%c, c2=%c",&c1, &c2); } a=3, b=7 x=8.5, y=71.82 c1=A, c2=a 5. 设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf输入数据,输出计算结果,输出时要求文字说明,取小数点后2位数字。请编程序。 解: #include void main() { float r,h,C1,Sa,Sb,Va,Vb; scanf("%f,%f",&r,&h);

C程序设计第四版谭浩强完整版课后习题答案

C程序设计(第四版)(谭浩强)第一章课后习题答案 #include<>代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to \n"); return 0; } #include<> int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum); return 0; } P008 调用函数比较两个数的大小. #include<> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c); return 0; } int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); }

P015 三个数的大小.(数字0表示课后练习题) #include<> int main() { int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); printf("The max is :%d .\n",d); } int max(int x , int y , int z) { int m; if (x>y && x>z) m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); } C程序设计(第四版)(谭浩强)第2章课后 习题答案 算法——程序的灵魂 P017 计算机1-5相乘的积. #include<> int main() { int i,s=1; for(i=1;i<6;i++) { s=s*i; n",s); return 0; } #include<> int main() { int i,s=1; for(i=1;i<12;i++) 可以是i=i+2 { if(i%2!=0) s=s*i; else continue; }

C语言程序设计课后习题答案第四版谭浩强完整版

C语言程序设计课后习 题答案第四版谭浩强 HUA system office room 【HUA16H-TTMS2A-HUAS8Q8-HUAH1688】

第1章程序设计和C语言1 1.1什么是计算机程序1 1.2什么是计算机语言1 1.3C语言的发展及其特点3 1.4最简单的C语言程序5 1.4.1最简单的C语言程序举例6 1.4.2C语言程序的结构10 1.5运行C程序的步骤与方法12 1.6程序设计的任务14 1-5 #include int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; }

1-6#include int main() {int a,b,c,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

2.2简单的算法举例17 2.3算法的特性21 2.4怎样表示一个算法22 2.4.1用自然语言表示算法22 2.4.2用流程图表示算法22 2.4.3三种基本结构和改进的流程图26 2.4.4用N S流程图表示算法28 2.4.5用伪代码表示算法31 2.4.6用计算机语言表示算法32 2.5结构化程序设计方法34 习题36 第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例37 3.2数据的表现形式及其运算39 3.2.1常量和变量39 3.2.2数据类型42

C语言程序设计谭浩强重点笔记

C语言设计 学习笔记 早晨:06:40 起床 07:20——08:20 英语 1小时 新概念英语(单词、语法、听读背) 大学英语(单词、语法、听读背) 上午:08:30——10:30 计算机基础 2小时 10:50——11:30 计算机科学技术导论 计算机组成原理 微机原理及接口技术 Intel微处理器结构编程与接口 深入理解计算机系统 80x86汇编语言程序设计 8086-8088宏汇编语言程序设计教程 BIOS研发技术剖析 自己动手写操作系统 操作系统原理 Windows操作系统原理 Windows内部原理系列 Windows程序内部运行原理 计算机网络第五版 中午:12:00——02:00 午休 下午:02:30——04:30 计算机应用及编程 Windows用户管理指南、AD配置指南、网络专业 指南、Windows即学即会教程 Windows下32位汇编语言程序设计、C#编程 晚上:05:30——08:00 锻炼、晚餐 08:00——09:00 辅导 09:00——11:00 专业基础 2小时 大学数学、大学物理、电机及拖动、电力电子技 术、通信技术 11:30 休息

目录 第一章C语言概述................................................................................................................................. - 1 - 1.1 C程序结构特点16 ................................................................................................................ - 1 - 1.2 C程序上机步骤17 ................................................................................................................... - 1 -第二章程序的灵魂——算法23 ............................................................................................................ - 2 - 2.1 算法24 ..................................................................................................................................... - 2 - 2.2 算法的三种基本结构............................................................................................................... - 2 - 2.3 结构化程序设计方法42 .......................................................................................................... - 2 -第三章数据类型运算符与表达式48 .................................................................................................. - 2 - 3.1 C语言的基本元素48 ............................................................................................................... - 2 - 3.2 C的数据类型48 ....................................................................................................................... - 2 - 3.3 常量与变量48 .......................................................................................................................... - 3 - 3.4 基本类型................................................................................................................................... - 3 - 3.5 变量63 ..................................................................................................................................... - 4 - 3.6 不同类型数据间的混合运算................................................................................................... - 5 - 3.7 函数的调用过程(补充)....................................................................................................... - 5 -第四章最简单的C程序设计——顺序程序设计77 ........................................................................... - 5 - 4.1 字符数据的输入输出............................................................................................................... - 5 -第五章选择结构的程序设计97 ............................................................................................................ - 6 -第六章循环结构程序设计..................................................................................................................... - 6 - 6.1 语句标号................................................................................................................................... - 6 - 6.2 break语句和continue语句 ...................................................................................................... - 6 -第七章数组132 ...................................................................................................................................... - 6 - 7.1 构造类型................................................................................................................................... - 6 - 7.2 数组133 ................................................................................................................................... - 6 - 7.3 二维数组................................................................................................................................... - 7 - 7.4 字符串——字符数组............................................................................................................... - 7 - 7.5 字符串处理函数#include ...................................................................................... - 7 -第八章函数153 ...................................................................................................................................... - 8 - 8.1 c程序的结构154 ...................................................................................................................... - 8 - 8.2 函数调用参数传递................................................................................................................... - 8 - 8.3 函数变量的作用范围............................................................................................................... - 8 - 8.4 变量的存储类别....................................................................................................................... - 9 -第九章预处理命令197 ........................................................................................................................ - 10 - 9.1 预编译命令作用..................................................................................................................... - 10 -第十章指针211 .................................................................................................................................... - 11 - 10.1 变量的访问方式................................................................................................................... - 11 - 10.2 指针变量............................................................................................................................... - 11 -第十一章结构体270 ............................................................................................................................ - 12 - 11.1 结构体270 ............................................................................................................................ - 12 -

C语言程序设计(第三版)-谭浩强_笔记

C语言程序设计(第三版)-谭浩强_笔记 第一章概述 1. C语言的特点 ①语言简洁、紧凑,使用方便、灵活。共有32个关键字,9种控制语句。 ②运算符丰富,公有34种运算符。 ③数据结构丰富,数据类型有:整型、实型、字符型、数组、指针、结构体、共用体等。 ④具有结构化的控制语句(如if…else、while、do…while、switch、for) ⑤语法限制不太严格,程序设计自由度大。 ⑥允许直接访问物理地址,能进行位(bit)操作,可以直接对硬件操作。 ⑦生成目标代码质量高,程序执行效率高。 ⑧可移植性好。 2. C语言的用途 C虽不擅长科学计算和管理领域,但对操作系统和系统实用程序以及对硬件进行操作方面,C有明显的优势。现在很多大型应用软件也用C编写。 第二章数据类型、运算符与表达式 1. C的数据类型 C的数据类型包括:整型、字符型、实型或浮点型(单精度和双精度)、枚举类型、数组类型、结构体类型、共用体类型、指针类型和空类型。 2.常量与变量 常量其值不可改变,符号常量名通常用大写。变量其值可以改变,变量名只能由字母、数字和下划线组成,且第一个字符必须为字母或下划线。否则为不合法的变量名。变量在编译时为其分配相应存储单元。3.整型数据 整型常量的表示方法:十进制不用说了,八进制以0开头,如0123,十六进制以0x开头,如0x1e。 整型变量分为:基本型(int)、短整型(short int)、长整型(long int)和无符号型。不同机器上各类数据所占内存字节数不同,一般int型为2个字节,long型为4个字节。 4.实型数据 实型常量表示形式:十进制形式由数字和小数点组成(必须有小数点),如:0.12、.123、123 0.0等。指数形式如123e3代表123×10的三次方。 实型变量分为单精度(float)和双精度(double)两类。在一般系统中float型占4字节,7位有效数字,

C程序设计 谭浩强 期末考试题

一、填空题 1.输入一个不包含空格的字符串,将字符串反序输出,如:“abc12”的输出为“21cba”。 #include <> void f(char *p) { char *p1, *p2; char c; p1=p2= ; while( *p2++) ; ; while ( ) { c=*p1; *p1= ; *p2= ; p1++; p2--; } } void main() { char s[200]; printf(“输入一个字符串:”); scanf( ); f(s); printf(“字符串反序:%s\n”,s); } 2.输入1个长整数,求各位数字的平方和。 例如:输入 123,输出14。 #include <> void main( ) { int digit; long in,s; scanf("%ld", &in); ; ; while(in>0){ ; s=s+digit*digit; ; } printf("sum=%ld\n", s);

} 二、程序阅读题 1.写出下面程序运行结果(5分) #include <> #define MSIZE 8 void main() { char im[MSIZE][MSIZE+1]={ “********”, “########”, “#**#***#”, “####***#”, “********”, “#*******”, “********”, “########” }; int i,j; for(j= MSIZE -1;j>=0;j--) { for(i=0;i void main() { char *str1="", *str2=”123424315”; int x=0, i; for(i=0;str1[i]!='\0'&& str2[i]!='\0';i++) if(str1[i]==str2[i]) x++; printf("%d\n",x); 3.写出下列程序的输出结果(4分) #include <> main() { int a=4,b=6; printf("a=%d\n",a<<1); printf("b=%d\n",b>>1); } 4. 写出调用函数f(-123)的输出结果是多少。(6分)

.c程序设计谭浩强第二章习题与答案

习题 2.1什么是算法?是从日常生活中找三个例子,描述他们的算法? 答:对操作的描述,即操作步骤,就是算法。 广义的说;为解决一个问题而采取的方法和步骤,就称为“算法”。 例:(略) 2.2什么叫结构化的算法?为什么要提倡结构化的算法? 答:由基本节构所构成的算法属于“结构化”的算法。 结构化的算法便于编写、阅读、便于修改和维护。这就减少了程序出错的机会、提高了程序的可靠性,保证了程序的质量。 2.3试述三种基本结构的特点,你能否自己另外设计两种基本结构(要符合基本结构的特 点)。 2.4

答:基本结构有以下共同点: 1:只有一个入口。图2-14-------2-17中的a点为入口。 2:只有一个出口。图2-14-------2-17中的b点为出口。注意,一个判断框有两个出口,但一个选择结构只有一个出口。不能混淆。 3:结构内的每一部分都有被执行到的机会。也就是说,对每一个框来说,都应当有一条到出口的路径通过它。图2-20中就没有一条从入口到出口的路径通过A框。 4:结构内不存在死循环(无终止的循环)。图2-21就是一个死循环。 需要说明的是基本结构并不一定只限于以上3中,只要有以上四种特点就可以。人们可以自己定义之。例:如下两图

2.5用传统流程图表示求解一下问题的算法。 (1)有两个瓶子A和B,分别放醋和酱油,要求将他们互换。 #include void main() { int a; int b; int c; a=10;b=5; printf("%d,%d\n",a,b); c=a;a=b;b=c; printf("%d,%d\n",a,b); } (2)一次将10个数输入,要求将将其中最大的数输出。 #include void main() { int a[10]; int i;

相关主题
文本预览
相关文档 最新文档