VisualC++面向对象编程教程第2版(王育坚)清华大学出版社课后答案

更新时间:2023-12-16 14:21:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

2-45编写一个程序,输入三角形的三条边的边长,求三角形的面积。 2-45

#include #include int main() { int a,b,c; int s,S; cin>>a>>b>>c; s=(a+b+c)/2; S=powl(s*(s-a)*(s-b)*(s-c),0.5); if(S==0) cout<<\此三边不能构成三角形!\ else { cout<<\面积:\ cout<

2-46从键盘输入一个大写字母,然后改用小写字母在屏幕输出。 2-46

#include int main() { char ch; int a; cout<<\输入一个大写字母:\ cin>>ch; if(ch>=65 && ch<=91) { ch=ch+32; // a=(int)ch; cout<

2-47用户输入两个整数,编程输出稍大于第一个整数而又是第2个整数的倍数的数。计算

公式是:valuel+value2-value1%value2. 2-47

#include int main() { int value1,value2; int result; cin>>value1>>value2; result=value1+value2-value1%value2; cout<<\该整数为:\ return 0; }

2-48华氏温度转换为摄氏温度的公式是:C=(F-32)*5/9.。编写一个程序,输入一个华氏

温度,程序输出相应的摄氏温度。请将32和5/9用const型变量表示。 2-48

#include int main() { const float i=32.0; const float j=5.0/9.0; float degFahr; float degCel; cin>>degFahr;

// for(degFahr=0;degFahr<=300;degFahr+=10) // { degCel=j*(degFahr-i); cout<<\华氏温度:\ cout<<\摄氏温度:\// } return 0; }

2-49 从键盘输入20个整数,检查100是否存在于这些整数中,若是的话,求出他是第几个

被输入的。 2-49

#include int main() { int Array[20]; int i,count=0,c=0; for(i=0;i<20;i++)

cin>>Array[i]; for(i=0;i<20;i++) { if(Array[i]==100 && count==0) { cout<<\存在该数组中!\ count++; } if(Array[i]!=100) { c++; if(c==19) cout<<\不存在该数组中!\ } } for(i=0;i<20;i++) { if(Array[i]==100) cout<<\它是第\个被输入的!\ } return 0; }

2-50 从键盘输入一个N X N的整型数组,并将每一行的最大值显示输出。 2-50

#include int main() { int Array[100][100]={0}; int n,temp; cin>>n; for(int i=0;i>Array[i][j]; for(int x=0;x cout<<\每一行的最大值:\ for(i=0;i2-51 输入三个整数,采用指针方法将三个数按从大到小的顺序输出。 2-51

#include #include int main() { int Array[3]; int *pA=Array; int *a=Array; int i; int temp; for(i=0;i<3;i++) cin>>Array[i]; for(i=0;i<3;i++) { a++; if(*pA<*a) { temp=*pA; *pA=*a; *a=temp; a++; pA++; } a=&Array[0]; } for(i=0;i<3;i++) cout<

2-52 采用指针方法将一个数组中的所有元素颠倒顺序,结果仍然存放在原来的数组中,要求使用最少的辅助存储单元 2-52

#include int main()

{ int n; cout<<\输入数组的长度(小于100):\ cin>>n; cout<<\输入\个数:\ int Array[100]; int *pA=Array; int i; for(i=0;i>Array[i]; pA++; } for(i=0;i

2-53输入两个字符串,如果两个字符串的字符和长度都相同(认为它们相等),在屏幕上输出“Equal”,否则在屏幕上输出“Unequal”。要求使用字符指针。 2-53

#include int main() { char * pstr=\ char str1[100],str2[100]; cout<

for(i=0;i

2-54 编程将一个整数转换成对应的数字串,例如将值1234转换为数字串“1234”。 2-54

#include int main() { int number; int Array[100]; int i,j; cin>>number; for(i=0;number!=0;i++) { Array[i]=number; number=number/10; } j=i; for(i=j;i>=0;i--) cout<

2-55 编程求两个复数的和。 2-55

#include int main() { float a[2],b[2],c[2]; int i; for(i=0;i<2;i++) cin>>a[i]; for(i=0;i<2;i++) cin>>b[i];

for(i=0;i<2;i++) c[i]=a[i]+b[i]; cout<

2-56 使用结构变量表示每个学生的信息:姓名、学号和三门课的成绩。从键盘输入10个学

生的数据,然后输出每个学生的姓名和三门课的平均成绩。 2-56

#include struct student { char num[10]; char name[20]; float grade[3]; float average; }; int main() { student stu[10]; int i,j; float sum=0; for(i=0;i<=9;i++) { printf(\ scanf(\ printf(\ scanf(\ printf(\ for(j=0;j<3;j++) { scanf(\ sum+=stu[i].grade[j]; } stu[i].average=sum/3;

printf(\ } return 0; }

2-57 用结构数组建立并初始化一个工资表,然后输入一个人的姓名,查询其工资情况,并在屏幕上输出。 2-57

2-58用枚举值MON、TUE、WED、THU、FRI、SAT和SUN表示一个星期中的7天。从键盘输入一个0~6之间的整数,根据输入的整数输出对应的英文缩写。 2-58

2-59 编写一个求解一元二次方程的根的程序,方程的系数由用户输入。 2-59

#include #include int main() { int a,b,c; int R; float p; float x1,x2; cin>>a>>b>>c; R=b*b-4*a*c; if(R>=0) { p=sqrt(R); x1=(-b+p)/(2*a); x2=(-b-p)/(2*a); if(x1==x2) cout<<\仅且只有一个根\ else cout<<\有两个不同的根\ } else if(R<0) cout<<\无根\ return 0; }

2-60 从键盘输入一个字符,判断输入的字符是m、a、n或其他字符。如果是m则输出“Good morning!”;如果是a则输出“Good afternoon!”;如果是n则输出“Good night!”;如果是其他字符则输出“I can‘t undersrand!”。 2-60

2-61 编程实现两个整数的加、减、乘、除四则运算,运算式形如“32+120”。

2-61

2-62编写一个程序,利用swith语句将百分制的学生成绩转换为优、良、中、及格和不及格5分制成绩。 2-62

2-63从键盘输入一个字符,判断输入的字符是数字、空格还是其他字符,并给出相应的提示信息。 2-63

#include int main() { char ch; cout<<\请输入一个字符:\ cin.get(ch); if(ch==' ') cout<<\这是一个空格!\ else if((ch>='0')&&(ch<='9')) cout<<\这是一个数字!\ else cout<<\这是一个其他字符!\ return 0; }

2-64 从键盘输入一个字符序列,编程统计其中的数字个数和英文字母个数。输入的字符序列以“#”作为结束符。 2-64

#include int main() { char symbol[100]; int i,end,j=0,k=0; cin>>symbol; for(i=0;i<100;i++) if(symbol[i]=='#') { end=i; break; } for(i=0;i='0') && (symbol[i]<='9')) j++; else k++; }

cout<<\ cout<<\ return 0; }

2-65 输入一个由若干单词组成的文本串,每个单词之间用一些空格分隔,统计此文本串单词的个数。 2-65

#include int main() { char str[1000]; int count=1; cin.get(str,1000); // while(!str[0]) // { for(int i=0;str[i]!=0;i++) if(str[i]==' ' && str[i+1]!=' ') count++; // } cout<<\ return 0; }

2-67 编程求π值,使用如下公式:π/4=1-1/3+1/5-1/7+?,直到最后一项的绝对值小于10-6为止。 2-67(1)

#include #include int main() { int i,n=1,j=1; double s=0; for(i=0;(2*i+1)

cout<

2-67(2)

#include #include int main() {

float i=0; double s=0;

for(i=0;2*i+1

s+=pow(-1,i)*(1/(2*i+1)); }

cout<

2-68 把100~150之间不能被3整除的数输出,要求一行输出10个数。 2-68

2-69 编程输出一个九九乘法表。 2-69

#include int main() { char table[9][9]; int i,j,X,Y; for(i=0;i<9;i++) { for(j=0;j<9;j++) { X=i+1; Y=j+1; if(X>=Y) cout<

cout<

2-70编程计算整型数各位数字之和,例如数2007各位数字之和为2+0+0+7=9. 2-70

#include int main() { int num; int sum=0,i; cin>>num; for(i=0;num!=0;i++) { sum+=num; num=num/10; } cout<<\ return 0; }

2-71 输入n个整数,利用冒泡排序法将它们从小到大排列,并在屏幕上输出。 2-71

#include int main() { int n; int i,j,temp; int num[100]; cin>>n; for(i=0;i>num[i]; for(i=0;i

for(i=0;i

2-72 编程求出从键盘输入的10个数之和,遇到负数时终止输入求和。 2-72

#include int main() { int Array[10]; int sum=0; for(int i=0;i<10;i++) { cin>>Array[i]; if(Array[i]>0) sum+=Array[i]; else break; } cout<

2-73 编程求出从键盘输入的10个数中所有正数之和,负数不参加求和。 2-73

#include int main() { int num[10]; int i,sum=0; for(i=0;i<=9;i++) { cin>>num[i]; if(num[i]>0) sum+=num[i]; } cout<<\return 0; }

2-74 设计函数prime(),它只带一个整型参数,当这个参数的值是素数时,该函数返回非0,否则返回0.利用这个函数编写一个程序来验证哥德巴赫猜想:任何一个大于2的偶数都可以表示成两个素数之和。 2-74

#include int prime(int x) { int i; for(i=2;i>a; if(a>2 && a%2==0) { for(j=2;j

2-75 编制如下函数原型的函数:int index(const char *str ,char c),这个函数返回字符串str中第一次出现字符c的位置。 2-75

#include

int index(const char *str,char c) { int count=1; int Ccount=0;

for(int i=0;str[i]!='\\0';i++) { if(str[i]==c && Ccount==0) { Ccount++; break; } count++; } return count; } int main() { char string[100]; char C; int V=0; int result; cin>>string; cin>>C; while(string[V]!='\\0') V++; result=index(string,C); if((V+1)==result) cout<<\此字符不在字符串中!\ else cout<

2-76 首先编写以下函数声明的函数:void swap(float* px,float*py),该函数用于交换两个实型变量的值,然后编写一个主函数验证函数swap()的功能。 2-76

#include

void swap(float *px,float *py) { float temp; temp=*px; *px=*py; *py=temp; }

int main() {

float X,Y; cin>>X>>Y; swap(&X,&Y); cout<<\使用swap函数的值\ cout<

2-77 定义将一个字符串反转的函数,例如将字符串“abcd”反转为“dcba”。 2-77

#include #include int Array(char *s) { int j,n; char string[100]; strcpy(string,s); for(j=0;j<100;j++) if(string[j]=='\\0') n=j; return n; }

int main() { char str[100]; int i,z; cin>>str; z=Array(str); // cout<=0;i--) cout<

2-78 首先编写一个冒泡排序函数,然后在主函数中调用排序函数对10个整数从小到达进行排序。提示:采用数组名作为函数参数。 2-78

2-79将习题2-76中的swap()函数改为内联函数,并实现相同的程序功能。 2-79

#include

inline void swap(float *px,float *py) { float temp; temp=*px; *px=*py; *py=temp; }

int main() { float X,Y; cin>>X>>Y; swap(&X,&Y); cout<<\使用swap函数的值\ cout<

2-80 编写一个函数 maxmin(),该函数有两个实型参数,执行函数后,第一个参数为两个参数中值较大者,第二个参数为较小者。要求使用引用作为函数参数,并编写主函数验证函数功能。 2-80

#include

void maxmin(int &A,int &B) { int temp; if(A

B=temp; } }

int main() { int X,Y; cin>>X>>Y; maxmin(X,Y); cout<

2-81 编写一个函数 swapstruct(),实现交换两个结构变量的功能。编写主函数验证函数swapstruct()的功能,要求使用引用传递参数。 2-81

#include #include

struct student { char name[20]; int score; };

void swapstruct(student &s1,student &s2) { int temp; char s[20]; strcpy(s,s1.name); strcpy(s1.name,s2.name); strcpy(s2.name,s); temp=s1.score; s1.score=s2.score; s2.score=temp; }

int main() { student stu1={\ student stu2={\ cout<<\ cout<<\ swapstruct(stu1,stu2); cout<<\ cout<<\return 0;

}

2-82 编写一个程序,在主函数main()的外部和内部分别声明两个同名的整型变量并赋值,然后在主函数main()中分别访问两个变量。 2-82

#include int i=3; int main() { cout<

2-83 一个程序由两个C++源文件组成,在一个源文件中定义主函数main()并声明一个外部整型变量n,在另一个源文件中定义一个不带参数的函数factorial(void),该函数用于计算变量n的阶乘。编程在主函数main()中输入一个整数并求它的阶乘。 2-83

#include int factorial(int x) { int y,pro=1; for(y=x;y>0;y--) pro*=y; return (pro); }

void main() { int n,R; cout<<\ cin>>n; R=factorial(n); cout<<\}

2-84 采用外部函数的方式使用习题2-75中的函数index(),即在一个源文件中定义该函数,

然后在另一个源文件中调用该函数。 2-84

2-85 编写一段程序,利用new运算动态分配float型、long型和char型三个内存单元,将它们的首地址分别赋给指针pf、pl和pc。给这些存储但愿赋值,并在屏幕上显示它们的值。最后利用delete运算释放所有动态分配的内存单元。 2-85

#include int main() { float *pf=new float; *pf=3.14; long *pl=new long; *pl=2135567889; char *pc=new char; *pc='A'; cout<<*pf<

2-86 编写一个程序,用new运算为一个整型数组动态分配内存空间,对其进行赋值,并在屏幕上输出。 2-86

#include int main() { int size; int num; cin>>size; int *f=new int[size]; for(int i=0;i>num; f[i]=num; } for(i=0;i

width=w; } ~CRectangle(){} void Move(double , double); void Size(double ,double); void Where(); void Area(); };

void CRectangle::Move(double x, double y) { cout<<\矩形按向量(\移动\ cout<<\现在矩形左上角所在的位置:\ cout<<\}

void CRectangle::Size(double cl, double cw) { cout<<\要更改的长和宽:\ length=cl; width=cw; cout<

void CRectangle::Where() { cout<<\现在矩形左上角所在的位置:\ cout<<\}

void CRectangle::Area() {

double area;

area=length*width; cout<

int main() { CRectangle cr(2,3,5,4); cr.Where(); cr.Area(); cr.Move(1,2); cr.Size(2,3);

return 0; } 3-48

#include #include class Bank{ private: char number[20]; int money; public: void setnumber(char*num,int mon) {

strcpy(number,num); money=mon; } void changer(int mon) { money+=mon; } void show() { cout<<\当前的总钱数为:\ } };

void main() { char num[20]; int money; int addmoney; cout<<\输入用户名和存入的钱:\ cin>>num>>money; Bank bank; bank.setnumber(num,money); bank.show(); cout<<\输入转账的钱:\ cin>>addmoney;

bank.changer(addmoney); bank.show(); }

3-49

#include using namespace std;

class Product {

private: char name[20]; double price; int m_count; public: Product(){} ~Product(){} void setproduct(); void sellproduct(); void show(); };

void Product::setproduct() { int n; cout<<\输入生产产品数量:\ cin>>n; m_count=n; }

void Product::sellproduct() { int n; cout<<\输入销售产品数量:\ cin>>n; m_count-=n; }

void Product::show() { cout<<\输出剩余产品数量:\ cout<

int main()

{ Product p; p.setproduct(); p.sellproduct(); p.show(); return 0; }

3-50 建立一个名为 Student 的类,该类有以下几个私有成员变量:学生姓名、学号、性别和年龄。还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕上输出学生信息。

#include #include class Student {

public:

Student(char *name,char *num,char *sex,int age); ~Student(); void show(); private:

char *Name; char *Num; char *Sex; int Age; };

Student::Student(char *name,char *num,char *sex,int age) {

Name=new char[strlen(name)+1];//注意字符串的赋值

strcpy(Name,name);

Num=new char[strlen(num)+1]; strcpy(Num,num);

Sex=new char[strlen(sex)+1]; strcpy(Sex,sex); Age=age; }

Student::~Student() {

delete Name; delete Num; delete Sex; }

void Student::show() {

cout<<\姓名:\ cout<<\学号:\ cout<<\性别:\ cout<<\年龄:\}

void main() {

Student student(\张三\男\ student.show(); } 3-51

设计一个CPetrol类,包含以下几个私有成员:90号 93号 98 号汽油加油量和单价,当天的总收入。类还包含以下几个成员函数:设置有关数据成员的构造函数,输入加油量并计算总收入的成员函数。利用类编写主函数:假设加油站某天90 93 98 号汽油单价分别为3.96 4.05 4.38 计算并输出加油站一天的收入。

#include using namespace std; class CPetrol {

public: CPetrol();

void setamount(); double total;

private:

double am_90; double am_93; double am_98;

double price_90; double price_93; double price_98; };

CPetrol::CPetrol() {

price_90=3.96; price_93=4.05; price_98=4.38; }

void CPetrol::setamount() {

cout<<\ cin>>am_90>>am_93>>am_98;

total=am_90*price_90+am_93+price_93+am_98+price_98; }

void main() {

CPetrol c; c.setamount();

cout<<\ } 3-52

修改习题 3-50 中的类 Student,添加一个静态成员变量,用于表示已创建对象的数量; 添加两个静态成员函数,一个用于输出已创建对象的数量,一个用于输出一个学生的姓名和 学号。

#include #include class Student {

public:

Student(char *name,char *num,char *sex,int age); ~Student();

staticvoid show(Student &a); staticvoid showstudentnum(); private:

char *Name; char *Num; char *Sex; int Age;

static int studentnum; };

int Student::studentnum=0;

Student::Student(char *name,char *num,char *sex,int age) {

studentnum++;

Name=new char[strlen(name)+1]; strcpy(Name,name);

Num=new char[strlen(num)+1]; strcpy(Num,num);

Sex=new char[strlen(sex)+1]; strcpy(Sex,sex); Age=age; }

Student::~Student() {

delete Name; delete Sex; }

void Student::showstudentnum() {

cout<<\学生的数量是:\}

void Student::show(Student &a) {

cout<<\姓名:\ cout<<\学号:\ cout<<\性别:\ cout<<\年龄:\}

void main() {

Student student1(\张三\男\

Student::show(student1);//注意调用方式,静态成员可以通过类名调用 Student::showstudentnum(); //注意调用方式 Student student2(\李四\男\ Student::show(student2); Student::showstudentnum(); }

3-53

编写程序用静态成员的方法实现对班费的管理,要求定义一个类Student,除了声明一个存放班费的静态成员,还要分别定义一个上交班费的成员函数Contribute(),花费班费的成员函数Spend()和显示班费的静态成员函数Display()

//student.cpp

#include using namespace std; class Student {

private:

static double fee; //fee --班费,静态成员数据 public:

Student(){} //默认构造函数,析构函数 ~Student(){}

void Contribute(double n); // n --上缴的班费数额 void Spend(double n); // n --花费班费数量 static void Display(); //静态成员函数 };

double Student::fee=0; //类声明外面对静态数据成员初始化 //类方法

void Student::Contribute(double n) {

fee=fee+n; }

void Student::Spend(double n) {

if(fee

cout<<\班费不够,请求失败!\\n\ else

fee=fee-n; }

void Student::Display() {

cout<<\现有班费:\ }

int main() {

Student stu; stu.Display();

stu.Contribute(103.4); //交钱 stu.Display();

stu.Spend(42.3); //花钱 stu.Display(); return 0; }

放在了一个文件里了,上面是类声明,下面是测试小程序,运行过了,没问题 3-54

定义一个类A,该类除了有两个数据成员x,y外,还有一个对象备份函数copy。

copy函数的功能说明如下:对于类A的对象a1和a2,函数调用a1.copy(a2)表示将对象a2赋值给对象a1.(提示利用this指针防止一个对象对自己赋值)

#include using namespace std;

class Test {

private: char *a,*b; public: Test() {

a = new char[100]; b = new char[100]; }

~Test() {

delete []a; delete []b; }

Test ?(Test &B) {

if(this == &B) return *this; int len = strlen(B.a);

delete []a;

a = new char[len+1]; strcpy(a,B.a); len = strlen(B.b); delete []b;

b = new char[len+1]; strcpy(b,B.b); return *this; }

void mytest(char *str1,char *str2) {

strcpy(a,str1); strcpy(b,str2); }

void myprint() {

cout << a << \ } }A,B; int main() {

B.mytest(\ B.myprint(); A.copy(B); A.myprint(); return 0; } 3-55

将习题 3-45 中类 Date 的“日期加一天”成员函数改为友员函数。

#include class date{ int year; int month; int day;

本文来源:https://www.bwwdw.com/article/1yd5.html

Top