C程序设计经典编程20题
- 格式:doc
- 大小:46.50 KB
- 文档页数:2
C语言程序设计50例(经典收藏)各位读友大家好,此文档由网络收集而来,欢迎您下载,谢谢C语言程序设计50例(经典收藏)【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
组成所有的排列后再去掉不满足条件的排列。
2.程序源代码:复制代码代码如下:#include ““#include ““main(){int i,j,k;printf(“\n”);for(i=1;i2) /*如果是闰年且月份大于2,总天数应该加一天*/sum++;printf(“It is the %dth day.”,sum);getch();}============================== ============================== ==【程序5】题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z 则将x与z的值进行交换,这样能使x 最小。
2.程序源代码:复制代码代码如下:#include ““#include ““main(){int x,y,z,t;scanf(“%d%d%d”,&x,&y,&z);if (x>y){t=x;x=y;y=t;} /*交换x,y的值*/if(x>z){t=z;z=x;x=t;} /*交换x,z的值*/if(y>z){t=y;y=z;z=t;} /*交换z,y的值*/printf(“small to big: %d %d %d\n”,x,y,z);getch();}============================================================ ==【程序6】题目:用*号输出字母C的图案。
c编程试题及答案1. 请写出C语言中声明一个整型变量的语句。
答案:int a;2. 在C语言中,如何定义一个函数?答案:返回类型函数名(参数列表) {// 函数体}3. 请解释C语言中的指针是什么?答案:指针是一种特殊的变量,其存储的是另一个变量的内存地址。
4. 在C语言中,如何使用指针来交换两个变量的值?答案:```cvoid swap(int *a, int *b) {int temp = *a;*a = *b;*b = temp;}```5. 请写出C语言中数组的定义方式。
答案:类型数组名[数组大小];6. 在C语言中,如何使用循环来遍历数组?答案:```cfor (int i = 0; i < 数组大小; i++) {// 访问数组元素:数组名[i]}```7. 请解释C语言中的结构体是什么?答案:结构体是一种用户自定义的数据类型,它允许将不同的数据类型组合成一个单一的数据结构。
8. 如何在C语言中定义一个结构体?答案:```cstruct 结构体名 {类型成员1;类型成员2;// ...};```9. 在C语言中,如何声明一个结构体变量?答案:```cstruct 结构体名变量名;```10. 请解释C语言中的文件操作。
答案:C语言提供了一系列的函数来实现文件的打开、读取、写入和关闭。
11. 如何在C语言中打开一个文件?答案:```cFILE *fp;fp = fopen("文件路径", "模式");```其中,"模式"可以是"r"(只读)、"w"(只写)、"a"(追加)等。
12. 在C语言中,如何读取文件中的数据?答案:```cint c;while ((c = fgetc(fp)) != EOF) {// 处理字符 c}```13. 如何在C语言中写入数据到文件?答案:```cfputc('字符', fp);```14. 在C语言中,如何关闭一个文件?答案:fclose(fp);15. 请写出C语言中使用条件语句的格式。
以下是一些经典的C语言题目:
1. 计算斐波那契数列的第n项
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. 实现字符串匹配算法(KMP算法)
27. 实现回文判断算法(Manacher算法)
28. 实现字符串分割算法(Strtok算法)
29. 实现字符串转换算法(Base64算法)
30. 实现字符串加密算法(Caesar算法)。
)(*)(*)(*c s b s a s s ---C 语言编程题1.(*)求分数序列:1/2,2/3,3/5,5/8,8/13,13/21...... 前20项的和。
main(){float i=1,j=2,t=0,s,n,m;for(n=1;n<=20;n++)s=i/j,m=i,i=j,j=m+j,t=t+s;printf("t=%f",t);}2.(*)从键盘输入一个字符串,再将其逆序输出。
(如:输入abcde ,输出edcba ) main(){int i;char c[10];scanf("%s",c);for(i=9;i>=0;i--)printf("%c",c[i]);}3.(*)已知abc+cba=1333,其中a 、b 、c 均为一位数,例如:617+716=1333, 518+815=1333, 试编程求出符合这一规律的a 、b 、c ,并输出结果。
main(){int a,b,c,x,y;for(a=1;a<=9;a++)for(b=0;b<=9;b++)for(c=1;c<=9;c++){ x=100*a+10*b+c;y=100*c+10*b+a;if(x+y==1333)printf("x=%d,y=%d",x,y);}}4.(*)利用海伦公式求三角形面积,三边长a,b,c 由键盘输入。
若输入的三边长不能构成 三角形,输出相应提示信息。
海伦公式如下:其中s=(a+b+c)/2三角形面积= #include"math.h"main(){ float a,b,c,s,area;scanf("%f,%f,%f",&a,&b,&c);s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));if(a+b>c&&a+c>b&&b+c>a)printf("area=%f",area);else.1 .printf("no");}5.(*)编程求出1!+2!+3!+…+8!+9!+10!的值并输出。
以下是一些经典的C语言程序设计题:
1. 打印数字:编写一个程序,打印从1到100的所有整数。
2. 判断数字:编写一个程序,接收用户输入的一个数字,判断它是正数、负数还是零。
3. 排序数组:编写一个程序,接收用户输入的10个整数,将它们按照从小到大的顺序排序并输出。
4. 查找数组:编写一个程序,接收用户输入的10个整数和一个目标值,在数组中查找目标值并输出其下标。
5. 计算阶乘:编写一个程序,接收用户输入的一个正整数n,计算n的阶乘并输出结果。
6. 计算斐波那契数列:编写一个程序,接收用户输入的两个正整数n和m,计算斐波那契数列的第n项和第m项的值并输出。
7. 字符串反转:编写一个程序,接收用户输入的字符串,将其反转并输出。
8. 字符串拼接:编写一个程序,接收用户输入的两个字符串,将它们拼接起来并输出。
9. 计算平均值:编写一个程序,接收用户输入的n个浮点数,计算它们的平均值并输出。
10. 判断回文串:编写一个程序,接收用户输入的字符串,判断它是否是回文串并输出结果。
以上题目都是经典的C语言程序设计题,可以帮助初学者掌握基本的编程技能和算法思想。
C语言经典程序100题(答案版)【程序1】题目:企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?1.程序分析:请利用数轴来分界,定位。
注意定义时需把奖金定义成长整型。
2.程序源代码:1.#include"stdio.h"2.#include"conio.h"3.main()4.{5.long int i;6.int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;7.scanf("%ld",&i);8.bonus1100000*0.1;9.bonus2bonus1+100000*0.75;10.bonus4bonus2+200000*0.5;11.bonus6bonus4+200000*0.3;12.bonus10bonus6+400000*0.15;13.if(i<100000)14.bonus i*0.1;15.else if(i<200000)16.bonus bonus1+(i-100000)*0.075;17.else if(i<400000)18.bonus bonus2+(i-200000)*0.05;19.else if(i<600000)20.bonus bonus4+(i-400000)*0.03;21.else if(i<1000000)22.bonus bonus6+(i-600000)*0.015;23.else24.bonus bonus10+(i-1000000)*0.01;25.printf("bonus%d",bonus);26.getch();27.}【程序2】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
一、求100至200间的全部素数。
二、人口增长预测。
据2005年末统计,我国人口为130756万人,如果人口的年增长率为1%,请计算到哪一年中国总人口超过15亿。
三、输入两个正整数a和b,求其最大公约数和最小公倍数。
四、鸡翁一,值钱五,鸡母一,值钱三,鸡雏三,值钱一,百钱买百鸡,问翁、母、雏各几何?五、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
六、求Sn=a+aa+aaa+…+aa…aaa(有n个a)之值,其中a是一个数字。
例如:2+22+222+2222+22222(n=5),n由键盘输入。
七、求1!+2!+3!+4!+5!+ (20)八、打印出所有"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该本身。
十、猴子吃桃问题。
猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。
第二天早上又将剩下的桃子吃掉一半,又多吃一个。
以后每天早上都吃了前一天剩下的一半零一个。
到第10天早上想再吃时,见只剩下一个桃子了。
求第一天共摘多少桃子。
十一、打印以下图案** * ** * * * ** * * * * * ** * * * ** * **十二、输入一个班全体学生的成绩,把不及格的学生成绩输出,并求及格学生的平均成绩。
十三、译密码。
为使电文保密,往往按一定规律将其转换成密码,收报人再按约定的规律将其译回原文。
编程将输入的字母变成其后的第4个字母。
十四、用公式计算л的近似值,直到最后一项的绝对值小于1E-6为止。
十五、三对情侣参加婚礼,三个新郞为A、B、C,三个新娘为X、Y、Z。
有人不知道谁和谁结婚,于是询问了六位新人中的三位,但听到的回答是这样的:A说他将和X结婚;X说她的未婚夫是C;C说他将和Z 结婚。
这人听后知道他们在开玩笑,全是假话。
请编程找出谁将和谁结婚。
十六、九九乘法表的设计制作。
十七、有一对兔子,出生后第3个月起每个月都生一对兔子。
第一题#include<iostream.h>#include<string.h>class Student{char name[10],num[10];public:Student(char*s1,char*s2){strcpy(name,s1);strcpy(num,s2);}void dsp(){cout<<"Name:"<<name<<endl<<"Number:"<<num<<endl;}};class Score{unsigned int mat,phy,eng;public:Score(unsigned int i1,unsigned int i2,unsigned int i3) :mat(i1),phy(i2),eng(i3){}void show(){cout<<"Mathematics:"<<mat<<"\nPhyics:"<<phy<<"\nEnglish:"<<eng<<endl;}};void main(){Student a("Wang","123456789");Score a1(72,82,92);a.dsp();a1.show();}第二题#include<iostream.h>#include<string.h>class Student{private:char name[10],num[10];friend voidshow(Student&st)//友元函数的声明和定义{cout<<"Name:"<<<<endl<<"Number:"<<num<<endl;}public:Student(char*s1,char*s2){strcpy(name,s1);strcpy(num,s2);}};class Score{unsigned int mat,phy,eng;friend void show_all(Student&,Score*);//友元函数的声明public:Score(unsigned int i1,unsigned int i2,unsigned int i3):mat(i1),phy(i2),eng(i3){}};void show_all(Student&st,Score*sc)//友元函数的定义{show(st);cout<<"Mathematics:"<<sc->mat<<"\nPhyics:"<<sc->phy<<"\nEnglish:"<<sc->eng<<endl;}void main(){Student wang("Wang","9901");Score ss(72,82,92);show_all(wang,&ss);}第三题#define strmax32#include<string.h>#include<iostream.h>class myclass{int x,y;char*string;public:myclass(int vx,int vy,char*str);friend int sum(myclass c1);//友元函数的声明friend int strlength(myclass c1);//友元函数的声明friend void print(myclass c1);//友元函数的声明};myclass::myclass(int vx,int vy,char*str){x=vx;y=vy;string=new char[strmax];strcpy(string,str);}int sum(myclass c1)//友元函数的定义{return c1.x+c1.y;}int strlength(myclass c1)//友元函数的定义{return strlen(c1.string);}void print(myclass c1)//友元函数的定义{cout<<"x="<<c1.x<<"y="<<c1.y<<endl;cout<<"string:"<<c1.string<<endl;}void main(){myclass c1(10,10,"my myclass object!");cout<<"the sum is:"<<sum(c1)<<endl;cout<<"the string is:"<<strlength(c1)<<endl;print(c1);}第四题#define maxcard32#include<iostream.h>class realset;enum errcode{noerr,overflow};//定义一个枚举class intset//定义一个整数集合类{int elem[maxcard];int card;public:intset(){card=0;}errcode addelem(int);//向集合中增加符点数元素void print();void settoreal(realset*set);//将当前对象的整数集合,转换成符点数集合};class realset//定义一个浮点数集合类{float elem[maxcard];int card;public:realset(){card=0;}errcode addelem(float);void print();};errcode intset::addelem(int elem1){for(int i=0;i<card;i++)if(elem[i]==elem1)return noerr;if(card<maxcard){elem[card++]=elem1;return noerr;}else return overflow;}void intset::print(){cout<<"{";for(int i=0;i<card-1;++i)cout<<elem[i]<<",";if(card>0)cout<<elem[card-1];cout<<"}\n";}void intset::settoreal(realset*set){for(int i=0;i<card;i++)set->addelem((float)elem[i]); }errcode realset::addelem(float elem1) {for(int i=0;i<card;i++)if(elem[i]==elem1)return noerr;if(card<maxcard){elem[card++]=elem1;return noerr;}else return overflow;}void realset::print(){cout<<"{";for(int i=0;i<card-1;++i)cout<<elem[i]<<",";if(card>0)cout<<elem[card-1];cout<<"}\n";}void main(){intset set1;realset*set2;set2=new realset;set1.addelem(12);set1.addelem(278);set1.addelem(54);set1.addelem(459);set1.print();set1.settoreal(set2);set2->print();delete set2;}第五题#include<iostream.h>#include<string.h>class Student;//声明引用的类名class Score{unsigned int mat,phy,eng;public:Score(unsigned int i1,unsigned int i2,unsigned int i3):mat(i1),phy(i2),eng(i3){} void show(){cout<<"Mathematics:"<<mat<<"\nPhyics:"<<phy<<"\nEnglish:"<<eng<<endl;}void show(Student&);};class Student{friend void Score::show(Student&);//声明友元成员char name[10],num[10];public:Student(char*s1,char*s2){strcpy(name,s1);strcpy(num,s2);}};void Score::show(Student&st){cout<<"Name:"<<<<"\n";show();}void main(){Student wang("Wang","9901");Score ss(72,82,92);ss.show(wang);}第六题#include<iostream.h>#include<string.h>class Student{friend class Score;//声明Score类为Student类的友元类char name[10],num[10];public:Student(char*s1,char*s2){strcpy(name,s1);strcpy(num,s2);}};class Score{unsigned int mat,phy,eng;public:Score(unsigned int i1,unsigned int i2,unsigned int i3):mat(i1),phy(i2),eng(i3){}void show(){cout<<"Mathematics:"<<mat<<"\nPhyics:"<<phy<<"\nEnglish:"<<eng<<endl;}void show(Student&);};void Score::show(Student&st){cout<<"Name:"<<<<"\n";show();}void main(){Student wang("Wang","9901");Score ss(72,82,92);ss.show(wang);}第七题#include<iostream.h>class stack;//超前声明stack类,因为node类中要将它声明为友员class node//定义node类{int data;//结点值node*prev;//指向上一结点的指针public:node(int d,node*n)//构造函数{data=d;prev=n;}friend class stack;//声明友元类};class stack//定义堆栈类{node*top;//堆栈头public:stack(){top=0;}void push(int i);//压栈int pop();//弹栈};void stack::push(int i){node*n=new node(i,top);top=n;}int stack::pop(){node*t=top;if(top){top=top->prev;int c=t->data;delete t;return c;}return0;}void main(){int c;stack s;for(int i=0;i<10;i++){cin>>c;s.push(c);}for(i=0;i<10;i++)cout<<s.pop()<<"";cout<<"\n";}第八题#include<iostream.h>class Complex{double real;double image;public:Complex(double r=0,double i=0){real=r;image=i;}friend void inputcomplex(Complex&comp);friend Complex addcomplex(Complex&c1,Complex&c2);friend Complex subcomplex(Complex&c1,Complex&c2);friend Complex mulcomplex(Complex&c1,Complex&c2);friend void outputcomplex(Complex&comp);};void inputcomplex(Complex&comp){cin>>comp.real>>comp.image;}Complex addcomplex(Complex&c1,Complex&c2){Complex c;c.real=c1.real+c2.real;c.image=c1.image+c2.image;return c;}Complex subcomplex(Complex&c1,Complex&c2){Complex c;c.real=c1.real-c2.real;c.image=c1.image-c2.image;return c;}Complex mulcomplex(Complex&c1,Complex&c2){Complex c;c.real=c1.real*c2.real-c1.image*c2.image;c.image=c1.real*c2.image+c1.image*c2.real;return c;}void outputcomplex(Complex&comp){cout<<"("<<comp.real<<","<<comp.image<<")"; }void main(){Complex c1,c2,result;cout<<"请输入第一个复数的实部和虚部:"<<endl;inputcomplex(c1);cout<<"请输入第二个复数的实部和虚部:"<<endl;inputcomplex(c2);result=addcomplex(c1,c2);outputcomplex(c1);cout<<"+";outputcomplex(c2);cout<<"=";outputcomplex(result);cout<<"\n-------------------------"<<endl;result=subcomplex(c1,c2);outputcomplex(c1);cout<<"-";outputcomplex(c2);cout<<"=";outputcomplex(result);cout<<"\n-------------------------"<<endl;result=mulcomplex(c1,c2);outputcomplex(c1);cout<<"*";outputcomplex(c2);cout<<"=";outputcomplex(result);cout<<endl;}第九题#include<iostream.h>class vehicle//基类vehicle类的定义{private://私有数据成员float weight;int wheels;public://公有函数成员vehicle(int in_wheels,float in_weight){wheels=in_wheels;weight=in_weight;}int get_wheels(){return wheels;}float get_weight(){return weight;}};class car:public vehicle//派生类car类的定义{private://新增私有数据成员int passenger_load;public://新增公有函数成员car(int in_wheel,float in_weight,int people=5):vehicle(in_wheel,in_weight){passenger_load=people;}int get_passengers(){return passenger_load;}};void main(){car bm(4,1000);//声明car类的对象cout<<"The message of bm(wheels,weight,passengers):"<<endl;cout<<bm.get_wheels()<<",";//访问派生类从基类继承来的公有函数cout<<bm.get_weight()<<",";//访问派生类从基类继承来的公有函数cout<<bm.get_passengers()<<endl;//访问派生类的公有函数}第十题#include<iostream.h>class vehicle//基类vehicle类的定义{private://私有数据成员int wheels;float weight;public://公有函数成员vehicle(int in_wheels,float in_weight){wheels=in_wheels;weight=in_weight;}int get_wheels(){return wheels;}float get_weight(){return weight;}};class car:private vehicle//定义派生类car类{private://新增私有数据成员int passenger_load;public://新增公有函数成员car(int in_wheels,float in_weight,int people=5):vehicle(in_wheels,in_weigh){passenger_load=people;}int get_wheels(){return vehicle::get_wheels();}//重新定义get_wheels()float get_weight(){return vehicle::get_weight();}//重新定义get_weight() int get_passengers(){return passenger_load;}};void main(){car bm(4,1000);//定义car类对象cout<<"The message of bm(wheels,weight,passengers):"<<endl;cout<<bm.get_wheels()<<","//输出小汽车的信息<<bm.get_weight()<<","<<bm.get_passengers()<<endl;}第十一题#include<iostream.h>class vehicle//定义基类vehicle{private://私有数据成员int wheels;protected://保护数据成员float weight;public://公有函数成员vehicle(int in_wheels,float in_weight){wheels=in_wheels;weight=in_weight;}int get_wheels(){return wheels;}float get_weight(){return weight;}};class car:protected vehicle//定义派生类car{private://新增私有数据成员int passenger_load;public://新增公有函数成员car(int in_wheels,float in_weight,int people=5):vehicle(in_wheels,in_weight){passenger_load=people;}int get_wheels()//重新定义get_wheels(){return vehicle::get_wheels();}float get_weight()//重新定义get_weight(){return weight;}int get_passengers(){return passenger_load;}};void main(){car bm(4,1000);//定义car类的对象cout<<"The message of bm(wheels,weight,passengers):"<<endl;cout<<bm.get_wheels()<<","//输出小汽车的信息<<bm.get_weight()<<","<<bm.get_passengers()<<endl;}第十二题#include<iostream.h>class data{int x;public:data(int x){data::x=x;cout<<"class data\n";}};class a{data d1;public:a(int x):d1(x){cout<<"class a\n";}};class b:public a{data d2;public:b(int x):a(x),d2(x){cout<<"class b\n";}};class c:public b{public:c(int x):b(x){cout<<"class c\n";}};void main(){c object(5);}第十三题#include<iostream.h>#include<string.h>class ST_COM{protected:char name[10];unsigned int num;float mat,eng,phy;public:ST_COM(char*na,unsigned int n,float ma,float en,float ph): num(n),mat(ma),eng(en),phy(ph){strcpy(name,na);}};class EL_DEP:public ST_COM{float pex,elnet,dst;public:EL_DEP(char*na,unsigned int n,float ma,float en,float ph,float pe,float el,float d):ST_COM(na,n,ma,en,ph),pex(pe),elnet(el),dst(d)void operator!(){cout<<"Name:"<<name<<"Number:"<<num<<endl;cout<<"Matchematics Scor:"<<mat<<endl;cout<<"English Scor:"<<eng<<endl;cout<<"Physics Scor:"<<phy<<endl;cout<<"Exchange Scor:"<<pex<<endl;cout<<"Elec_net Scor:"<<elnet<<endl;cout<<"Data_struct Scor:"<<dst<<endl;}};void main(){EL_DEP a("wang",1234,71,72,73,81,82,83);!a;}第十四题#include<iostream.h>#include<string.h>class ST_COM{protected:char name[10];unsigned int num;float mat,eng,phy,avg;public:ST_COM(char*na,unsigned int n,float ma,float en,float ph):num(n),mat(ma),eng(en),phy(ph){strcpy(name,na);avg=(mat+eng+phy)/3;}void operator!(){cout<<"Name:"<<name<<"Number:"<<num<<endl;cout<<"Matchematics Scor:"<<mat<<endl;cout<<"English Scor:"<<eng<<endl;cout<<"Physics Scor:"<<phy<<endl;}};class EL_DEP:public ST_COMfloat pex,elnet,dst,avg;public:EL_DEP(char*na,unsigned int n,float ma,float en,float ph,float pe,float el,float d):ST_COM(na,n,ma,en,ph),pex(pe),elnet(el),dst(d){avg=((pex+elnet+dst)/3+ST_COM::avg)/2;}void operator!(){ST_COM::operator!();cout<<"Exchange Scor:"<<pex<<endl;cout<<"Elec_net Scor:"<<elnet<<endl;cout<<"Data_struct Scor:"<<dst<<endl;cout<<"Average Scor:"<<avg<<endl;}};void main(){EL_DEP a("wang",1234,71,72,73,81,82,83);!a;}第十五题#include<iostream.h>class person{char*name;int age;char*add;public:person(){cout<<"the constructor of class person!\n";}~person(){cout<<"the destructor of class person!\n";}};class student:public person{char*department;int level;public:student(){cout<<"the constructor of class student!\n";}~student(){cout<<"the destructor of class student!\n";}};class teacher:public person{char*major;float salary;public:teacher(){cout<<"the constructor of class teacher!\n";}~teacher(){cout<<"the destructor of class teacher!\n";} };void main(){student d1;teacher d2;}第十六题#include<iostream.h>class Bed{public:Bed():weight(){}void Sleep(){cout<<"Sleeping...\n";}void SetWeight(int i){weight=i;}protected:int weight;};class Sofa{public:Sofa():weight(){}void WatchTV(){cout<<"Watching TV.\n";}void SetWeight(int i){weight=i;}protected:int weight;};class SleeperSofa:public Bed,public Sofa//多重继承{public:SleeperSofa(){}void FoldOut(){cout<<"Fold out the sofa.\n";}};void main(){SleeperSofa ss;ss.WatchTV();ss.FoldOut();ss.Sleep();ss.SetWeight(20);//出现二义性}第十七题#include<iostream.h>class Furniture//定义家具类{public:Furniture(){}void SetWeight(int i){weight=i;}int GetWeight(){return weight;}protected:int weight;};class Bed:virtual public Furniture//Furniture类作为Bed类的虚基类{public:Bed(){}void Sleep(){cout<<"Sleeping...\n";}};class Sofa:virtual public Furniture//Furniture类作为Sofa类的虚基类{public:Sofa(){}void WatchTV(){cout<<"Watching TV.\n";}};class SleeperSofa:public Bed,public Sofa{public:SleeperSofa():Sofa(),Bed(){}void FoldOut(){cout<<"Fold out the sofa.\n";}void main(){SleeperSofa ss;ss.SetWeight(20);cout<<ss.GetWeight()<<endl;}第十八题#include<iostream.h>class base{public:base(){cout<<"this is base class!\n";}};class base2{public:base2(){cout<<"this is base2class!\n";}};class level1:public base2,virtual public base {public:level1(){cout<<"this is level1class!\n";}};class level2:public base2,virtual public base {public:level2(){cout<<"this is level2class!\n";}};class toplevel:public level1,virtual public level2 {public:toplevel(){cout<<"this is toplevel class!\n";}};void main(){toplevel topobj;第十九题#include<iostream.h>class OBJ1{public:OBJ1(){cout<<"OBJ1\n";}~OBJ1(){cout<<"destructing OBJ1"<<endl;}};class OBJ2{public:OBJ2(){cout<<"OBJ2\n";}~OBJ2(){cout<<"destructing OBJ2"<<endl;}};class Base1{public:Base1(){cout<<"Base1\n";}~Base1(){cout<<"destructing Base1"<<endl;}};class Base2{public:Base2(){cout<<"Base2\n";}~Base2(){cout<<"destructing Base2"<<endl;}};class Base3{public:Base3(){cout<<"Base3\n";}~Base3(){cout<<"destructing Base3"<<endl;}};class Base4{public:Base4(){cout<<"Base4\n";}~Base4(){cout<<"destructing Base4"<<endl;}};class Derived:public Base1,virtual public Base2,public Base3,virtual public Base4 {public:Derived():Base4(),Base3(),Base2(),Base1(),obj2(),obj1()cout<<"Derived ok.\n";}~Derived(){cout<<"destructing Derived"<<endl;} protected:OBJ1obj1;OBJ2obj2;};void main(){Derived aa;}二十题#include<iostream.h>template<class T>class tem{T*data;int size;public:tem(int);~tem(){delete[]data;}T&operator[](int i){return data[i];}};template<class T>tem<T>::tem(int n){data=new T[n];size=n;}void main(){tem<int>x(5);int i;for(i=0;i<5;i++)x[i]=i;for(i=0;i<5;i++)cout<<x[i]<<'';cout<<'\n';。
C程序设计经典编程20题
1.求出10至1000之内能同时被2、3、7整除的数,并输出。
(提示:循环+判断)
2.从键盘输入两个正整数m和n,输出其最大公约数和最小公倍数。
(要求采用辗
转相除法,教程P140习题)
3.找出 100 到 999 之间的整数中,所有等于每位数字立方和的数(即水仙花数)。
例如 153=1^3+5^3+3^3(提示:教程P140习题)
4.求出下列数列的前20项之和。
(提示:教程P140习题)
2/1, 3/2, 5/3, 8/5, 13/8, 21/13, …
5.求100-200之间全部素数。
(参考教程例题P138,建议通过主函数调用素数函数
实现)
6.编写一判断m是否是素数的函数,在主函数中输出十对最小的孪生素数。
所谓孪
生素数是指两个相差为2的素数,如3和5,11和13。
素数函数原型为: int isprime(int m);
7.随机产生10个30~99的正整数,采用选择法或者起泡法按照从小到大排序后输
出。
(提示:产生一个20~99的正整数的表达式是:30+(rand()+90)%(99-30+1)), rand()函数在库函数math.h中,参考教程P168习题)
8.编写自定义函数fun实现字符串t的逆序存放功能,要求在主函数中输入字符串、
调用fun函数,然后输出逆序的字符串。
(参考教程P168习题)
9.数组a中有数据{21,5,58,9,3,66},再为x输入一个数据,在数组a中找
出第一个与x相等的元素并将其下标输出,若不存在这样的元素,则输出“Not found!”标志。
(参考教程P168习题)
10.打印输出如下图的杨辉三角。
(参考教程P168习题)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
11.编程计算一个3×3矩阵的两条对角线元素之和。
(要求:3×3矩阵由键盘输入,
教程P168习题)
12.编写一函数fun,实现矩阵A(3行3列)的转置(即行列互换),生成转置矩阵
B。
函数原型为: void fun(int a[3][3], int b[3][3]) (参考教程P218习题)
13.编写一个子定义函数实现十六进制数到十进制数的转换,要求在主函数中输入十
六进制数,并输出相应的十进制数。
(参考教程P219习题)
14.用递归法将一个整数n转换成字符串。
例如。
输入483,应输出字符串“483”。
n的位数不确定,可以是任意位数的整数。
(参考教程P219习题)
15.编一函数,功能为判断一字符串是否为回文(回文是指顺读和到读都是一样的字
符串,如“deed”和“level”是回文)。
在主函数中对输入的5个字符串统计其中回文的个数。
函数形式为: int huiwen(char s[]);
16.写一个函数,将两个字符串连接。
(参考教程P218习题)
17.用函数调用实现字符串的复制。
(参考教程P259例题,以及程序改进(5)P262
和(7)P263)
18.编写一个函数scmp实现对两个字符串的比较。
不能使用C语言提供的标准函数
strcmp。
要求在主函数中输入两个字符串,并输出比较的结果(相等的结果为0,不等时结果为第一个不相等字符的ASCII差值)。
(参考教程P292习题)
函数原型为:int scmp(char * s, char *t)
19.自定义一个函数slen,求一个字符串s的长度,在main函数中输入字符串,并输
出其长度。
(参考教程P291习题)
函数原型为: int slen(char *s)
20.综合第9章例9.9(建立动态链表的函数creat)、例9.10(输出链表的函数print)
和习题第7题(删除链表中的结点的函数del)、第8题(插入结点的函数insert),再编写一个主函数,调用这些函数。
调试实现链表的建立、输出、删除和插入操作。