




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一、二章1. 请编写程序输入三角形的三条边,判别它们能否形成三角形,若能,则判断是等边、等腰、还是一般三角形。程序可多次判别,程序结束的条件自行定义。【解答】未加入循环#include void main() float a, b, c ; cout a b c ; if ( a+b c & b+c a & c+a b ) if ( a = b & b = c ) cout 等边三角形! endl; else if ( a = b | a = c | b = c ) cout 等腰三角形! endl; else cout 一般三角形! endl; else cout 不能形成三角形! endl ;2. 编一程序显示如下图案,注意行数可由用户确定:* * * * * * * * * * * * * * * * * * * * *【解答】#include void main() int i,j,k; for( i=1; i=5; i+ ) for( k=1; k=5-i; k+ ) cout ; for( j=1; j=2*i-1; j+ ) cout *; cout endl; 第三章1输入a,b和c的值,编写一个程序求这三个数的最大值和最小值。要求把求最大值和最小值编写成函数,并使用指针或引用作为形式参数把结果返回函数main。【解答】(1)使用指针参数#includevoid fmaxmin( float,float ,float ,float *,float * );void main() float a,b,c,max,min; cout a b c; fmaxmin( a,b,c,&max,&min ); cout max= max endl; cout min= min y ) u = x; v = y; else u = y; v = x; ; if ( zu ) u = z; if ( zv ) v = z; *p1 = u; *p2 = v;(2)使用引用参数#includevoid fmaxmin( float,float ,float ,float& ,float& );void main()float a,b,c,max,min;cout a b c;fmaxmin( a,b,c,max,min );cout max= max endl;cout min= min y ) u = x; v = y; else u = y; v = x; ; if ( zu ) u = z; if ( zv ) v = z; p1 = u; p2 = v;2. 把以下程序中的print()函数改写为等价的递归函数。#include void print( int w ) for( int i = 1 ; i = w ; i + ) for( int j = 1 ; j = i ; j + ) cout i ; cout endl ; void main() print( 5 ) ; 运行显示:1 2 23 3 34 4 4 45 5 5 5 5【解答】#include void print(int w) int i; if( w ) print( w-1 ); for( i=1; i=w; i+ ) cout w ; cout endl; void main() print( 5 );3. 编写一个程序,包含三个重载的display函数和一个主函数。要求第一个函数输出double值,前面用字符串“a double:”引导,第二个函数输出一个int值,前面用字符串“a int:”引导,第三个函数输出一个char字符值,前面用字符串“a char:”引导,在主函数中分别用double、int和char型变量作为实参调用display函数。【解答】#include void display( double d ) cout a double: d endl; void display( int i ) cout a int: i endl; void display( char c ) cout a char: c endl; void main() double d = 1.5; int i = 100; char c = a; display( d ); display( i ); display( c );第4章1. 定义一个Book(图书)类,在该类定义中包括数据成员: bookname(书名)、price(价格)和number(存书数量);成员函数: display()显示图书的情况;borrow()将存书数量减1,并显示当前存书数量;restore()将存书数量加1,并显示当前存书数量。在main函数中,要求创建某一种图书对象,并对该图书进行简单的显示、借阅和归还管理。【解答】#include #include #include class Book public: void setBook(char*,float,int); void borrow(); void restore(); void display(); private: char bookname40; float price; int number;/在类外定义Book类的成员函数void Book:setBook(char *name, float pri, int num) strcpy(bookname, name); price=pri; number=num;void Book:borrow() if (number=0 ) cout 已没存书,退出! endl; abort(); number = number - 1; cout 借一次,现存书量为: number endl;void Book:restore() number = number + 1; cout 还一次,现存书量为: number endl;void Book:display() cout 存书情况: endl bookname: bookname endl price: price endl number: number endl;void main() char flag, ch; Book computer; computer.setBook( c+程序设计基础 , 32, 1000 ); computer.display(); ch = y; while ( ch = y ) cout flag; switch ( flag ) case b: computer.borrow(); break; case r: computer.restore(); cout ch; computer.display();2. 输入一行字符,按输入字符的反序建立一个字符结点的单向链表,并输出该链表中的字符。【解答】为了形成反序链表,每读入一个字符都把它插入在表头。#include #include struct node char ch; node *next;void show( node *head );void main() node *head, *p; char c; head = NULL; while( (c = getchar() != n )/输入一行字符 p = new node; /建立新结点 p-ch = c; p-next = head; /插入表头 head=p; show(head);void show( node *head ) /输出链表 node *p = head; cout 链表中的字符是: n; while( p ) cout ch; p = p-next; cout endl;第5章1定义一个Student类,在该类定义中包括:一个数据成员score(分数)及两个静态数据成员total(总分)和学生人数count;成员函数scoretotalcount(float s) 用于设置分数、求总分和累计学生人数;静态成员函数sum()用于返回总分;静态成员函数average()用于求平均值。在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。【解答】#include class student public:void scoretotalcount( float s ) score = s; total = total + score; count+; static float sum() return total; static float average() return total / count; private: float score; static float total; static float count;float student:total=0;float student:count=0;void main() int i,n; float s;cout n; student stu;for( i=1; i=n; i+ ) cout 请输入第 i s; stu.scoretotalcount( s ); cout 总分: student:sum() endl;cout 平均分: student:average() endl;2定义一个表示点的结构类型Point和一个由直线方程y = ax + b确定的直线类Line。结构类型Point有x和y两个成员,分别表示点的横坐标和纵坐标。Line类有两个数据成员a和b,分别表示直线方程中的系数a和b。Line类有一个成员函数print用于显示直线方程;友员函数setPoint(Line &l1,Line &l2)用于求两条直线的交点。在main函数中,建立两个直线对象,分别调用print函数显示两条直线的方程,并调用函数setPoint求这两条直线的交点。【解答】#include struct point float x; float y; ;class line public: line( float u, float v ) a = u; b = v; void print() cout y= a x+ b endl; friend point setpoint(line &l1,line &l2);private: float a, b;point setpoint( line &l1, line &l2 ) point p; p.x = ( l2.b - l1.b ) / ( l1.a - l2.a ); p.y = ( l1.a * l2.b - l2.a * l1.b ) / ( l1.a - l2.a ); return p;void main() point setp; line l1( 2, 3 ), l2( 4, 5 ); cout 直线l1: ; l1.print(); cout 直线l2: ; l2.print(); setp = setpoint( l1, l2 ); cout 直线l1和直线l2的交点:( setp.x , setp.y ) endl;第6章1将一组数据从大到小排列后输出,要求显示每个元素及它们在原数组中的下标。【解答】#include void main() int a = 38, 6, 29, 1, 25, 20, 6, 32, 78, 10 ; int index10; int i,j,temp; for( i=0; i10; i+ ) indexi = i; for( i=0; i=8; i+ ) for( j=i+1; j=9; j+ ) if( ai aj ) temp = ai; ai = aj; aj = temp; temp = indexi; indexi = indexj; indexj = temp; for( i=0; i10; i+ ) cout ai t indexi endl;2编写程序,按照指定长度生成动态数组,用随机数对数组元素赋值,然后逆置该数组元素。输出逆置前后的数组元素序列。要求逆置时不使用辅助数组。【解答】#include #include #include void printarray(int *p,int n);void adverse(int *p,int n);void main() int *p,n,i; coutn; p=new int n; / 产生动态数组 srand(time(0); for(i=0;in;i+) /产生随机数并存放到动态数组中 *(p+i)=rand()/1000; cout动态数组:; printarray(p,n); / 输出动态数组 adverse(p,n); / 对数组逆置 cout逆置数组:; printarray(p,n); / 输出逆置数组 / 输出数组函数 void printarray(int *p,int n) int i; for( i=0; in; i+ ) if (i % 5=0) coutendl; / 控制一行输出5个数据 coutarrayi=*(p+i) ; coutendl; / 对数组逆置函数void adverse(int *p,int n) int i,t;for (i=0;in/2;i+) t=*(p+i); *(p+i)=*(p+n-i-1); *(p+n-i-1)=t; 3. 编写程序统计给定英文文章中的给定单词出现的次数。英文文章存于文本文件,文件名由用户输入,给定单词也由用户输入。【解答】第7章1定义一个Rectangle类,它包含两个数据成员length和width;以及包含用于求长方形面积的成员函数。再定义Rectangle的派生类Rectangular,它包含一个新数据成员height和用来求长方体体积的成员函数。在main函数中,使用两个类,求某个长方形的面积和某个长方体的体积。【解答】#includeclass rectanglepublic: rectangle( float l,float w ) length = l;width = w; float area() return( length*width ); float getlength() return length; float getwidth() return width; private: float length; float width;class rectangular:public rectangle public: rectangular( float l,float w,float h ) : rectangle( l,w ) height = h; float getheight() return height; float volume() return area() *height; private: float height;void main() rectangle obj1( 2,8 ); rectangular obj2( 3,4,5 ); cout length= obj1.getlength() t width= obj1.getwidth() endl; cout rectanglearea= obj1.area() endl; cout length= obj2.getlength() t width= obj2.getwidth(); cout t height= obj2.getheight() endl; cout rectangularvolume= obj2.volume() endl;2 声明一个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员函数fn1()、fn2(),DerivedClass也有成员函数fn1()、fn2(),在主函数中声明一个DerivedClass的对象,分别用DerivedClass的对象以及BaseClass和DerivedClass的指针来调用fn1()、fn2(),观察运行结果。【解答】class circle public: circle(double r) radius = r; virtual double area() return 0.0; virtual double volume() return 0.0; protected: double radius;class sphere:public circle public: sphere( double r ):circle( r ) double area() return 4.0 * PI * radius * radius; double volume() return 4.0 * PI * radius * radius * radius / 3.0; ;class column:public circle public: column( double r,double h ):circle( r ) height = h; double area() return 2.0 * PI * radius * ( height + radius ); double volume() return PI * radius * radius * height; private: double height;void main() circle *p; sphere sobj(2); p = &sobj; cout 球体: endl; cout 体积 = volume() endl; cout 表面积 = area() endl; column cobj( 3,5 ); p = &cobj; cout 圆柱体: endl; cout 体积 = volume() endl; cout 表面积 = area() endl;2某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元。副教授的固定工资为3000元,每个课时补贴30元。讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。【解答】#include #include class teacher public: teacher( char tname,int time ) strcpy( name,tname ); coursetime = time; virtual int pay() = 0; virtual void print() = 0
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年气候变化对农业生态系统的影响预测
- 2023八年级生物上册 第五单元 生物圈中的其他生物第二章 动物的运动和行为 第三节 社会行为说课稿(新版)新人教版
- 2025年氯磺化聚乙烯项目提案报告
- 2025年高温高压机械密封项目提案报告范文
- 顺丰速运工作汇报
- 浙教版七年级科学上册说课稿 3.1地球的形状和内部结构
- Chapter6 When I was little教学设计小学英语朗文英语世界三年级下册-朗文英语世界
- 商品推销的语言技巧教学设计中职专业课-商品销售(推销实务)-市场营销-财经商贸大类
- 2025年起重机司机限桥式起重机证考试题库及答案
- 信息技术20作业a7
- 2026届海口市重点中学九年级数学第一学期期末达标测试试题含解析
- 基于边缘计算的导航算法优化-洞察及研究
- 实施指南(2025)《DA-T 59 - 2017 口述史料采集与管理规范》
- 高一物理力学知识点总结与测试题
- 广东省深圳市罗湖区2025-2026学年高三第一学期开学质量检测语文(含答案)
- 基于PLC的果园灌溉施肥系统设计
- 2025年武汉市中考英语试卷真题(含答案)
- 无人机清洗玻璃幕墙技术规范
- JGT472-2015 钢纤维混凝土
- 变压器市场需求分析报告
- SWITCH塞尔达传说旷野之息-1.6金手指127项修改使用说明教程
评论
0/150
提交评论