求C语言经典题目

要求经典,有解答过程... 要求经典,有解答过程 展开
 我来答
匿名用户
2013-12-23
展开全部
读程序写结果1. # include <iostream.h>void main(){ int s=0; for (int i=1; ; i++) { if (s>50) break; if (i%2==0) s+=i; } cout <<"i,s="<<i<<","<<s<<endl;}结果:i,s=15,56 2. # include <iostream.h>void main(){ char a[]="abcdabcabfgacd"; int i1=0,i2=0,i=0; while (a[i]) { if (a[i]=='a') i1++; if (a[i]=='b') i2++; i++;}结果:4 3 cout <<i1<<' '<<i2<<endl;} 3. # include <iomanip.h>void main(){ int a[9]={2,4,6,8,10,12,14,16,18}; for (int i=0; i<9; i++) { cout <<setw(5)<<*(a+i); if ((i+1)%3==0) cout <<endl; }} 结果: 2 4 6 8 10 12 14 16 18 4. # include <iomanip.h>void LE(int * a,int * b) { int x=*a; *a=*b; *b=x; cout <<*a<<' '<<*b<<endl;}void main(){ int x=10,y=25; LE(&x,&y); cout <<x<<' '<<y<<endl;}结果: 25 10 25 10 5. # include <iostream.h>class A { int a,b;public : A() { a=b=0; } A(int aa,int bb) { a=aa; b=bb; cout <<a<<' '<<b<<endl; }};void main(){ A x,y(2,3),z(4,5);}结果: 2 3 4 5 6. # include <iostream.h>template <class TT>class FF { TT a1,a2,a3;public : FF(TT b1,TT b2,TT b3) { a1=b1; a2=b2; a3=b3; } TT Sum() { return a1+a2+a3; }};void main(){ FF <int> x(2,3,4),y(5,7,9); cout <<x.Sum()<<' '<<y.Sum()<<endl;}结果:9 21 7. #include "iostream.h"  class test {  private:  int num;   float fl;  public:  test( );   int getint( ){return num;}   float getfloat( ){return fl;}   ~test( );  };  test::test( )  {  cout << "Initalizing default" << endl;   num=0;fl=0.0;  }  test::~test( ) {  cout << "Desdtructor is active" << endl; }  void main( )  {  test array[2];   cout << array[1].getint( )<< " " << array[1].getfloat( ) <<endl;  }Initalizing defaultInitalizing defaultDesdtructor is activeDesdtructor is active8.# include <iostream.h>void SB(char ch) { switch(ch){ case 'A': case 'a': cout <<"well!"; break; case 'B': case 'b': cout <<"good!"; break; case 'C': case 'c': cout <<"pass!"; break; default: cout <<"nad!"; break; }} good! pass! nad! well!void main() { char a1='b',a2='C',a3='f'; SB(a1);SB(a2);SB(a3);SB('A'); cout <<endl;} 9.# include <iostream.h># include <string.h> void main() { char *a[5]={"student","worker","cadre","soldier","peasant"}; char *p1,*p2; p1=p2=a[0]; for (int i=0; i<5; i++) { if (strcmp(a[i],p1)>0) p1=a[i]; if (strcmp(a[i],p2)<0) p2=a[i]; } cout <<p1<<' '<<p2<<endl;} worker cadre 10.# include <iostream.h>int a=5;void main() { int a=10,b=20; cout <<a<<' '<<b<<endl; { int a=0,b=0; for (int i=1; i<6; i++) { a+=i; b+=a; } cout <<a<<' '<<b<<' '<<::a<<endl; } cout <<a<<' '<<b<<endl;}10 2015 35 510 20 11.# include <iomanip.h>int LB(int *a,int n) { int s=1; for (int i=0; i<n; i++) s*=*a++; return s;}void main() { int a[]={1,2,3,4,5,6,7,8}; int b=LB(a,5)+LB(&a[3],3); cout <<"b="<<b<<endl;}b=24012.# include <iostream.h># include <string.h>struct Worker{ char name[15]; // 姓名 int age; // 年龄 float pay; // 工资};void main() { Worker x; char *t="liouting"; int d=38; float f=493; strcpy(x.name,t); x.age=d; x.pay=f; cout <<x.name<<' '<<x.age<<' '<<x.pay<<endl;} 13.# include <iostream.h>class A { int a;public: A(int aa=0) { a=aa; } ~A() { cout <<"Destructor A!"<<a<<endl; }};class B:public A { int b;public: B(int aa=0,int bb=0):A(aa) { b=bb; } ~B() { cout <<"Destructor B!"<<b<<endl; }};void main() { B x(5),y(6,7); // 后定义的变量将先被释放} 14. # include <iostream.h>class A { int a;public: A(int aa=0) { a=aa; } ~A() { cout <<"Destructor A!"<<a<<endl; }};class B:public A { int b;public: B(int aa=0,int bb=0):A(aa) { b=bb; } ~B() { cout <<"Destructor B!"<<b<<endl; }};void main() { B x(5),y(6,7); }Destructor B! 7 Destructor A! 6 Destructor B! 0 Destructor A! 5
匿名用户
2013-12-23
展开全部
C语言名题精选百则技巧篇 很不错的一本书,你去看看
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式