
读程序写
结果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 3cout <<i1<<' '<<i2<<endl} 3. # include <iomanip.h>void main(){ int a[9]={2,4,6,8,10,12,14,16,18} for (int i=0i<9i++) { cout <<setw(5)<<*(a+i) if ((i+1)%3==0) cout <冲闭<endl }} 结果: 2 4 68 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,bpublic : A() { a=b=0} A(int aa,int bb) { a=aab=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,a3public : FF(TT b1,TT b2,TT b3) { a1=b1a2=b2a3=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 numfloat fl public:test( )int getint( ){return num} float getfloat( ){return fl} ~test( ) } test::test( ) {cout <<"Initalizing default" <<endlnum=0fl=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=0i<5i++) { 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=5void main() {int a=10,b=20 cout <<a<<' '<<b<<endl { int a=0,b=0 for (int i=1i<6i++) { a+=ib+=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=0i<ni++) 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=38float f=493 strcpy(x.name,t) x.age=dx.pay=f cout <<x.name<<' '<<x.age<<' '<<x.pay<<endl} 13.# include <iostream.h>class A {int apublic:A(int aa=0) { a=aa}~A() { cout <<"Destructor A!"<<a<<endl }}class B:public A {int bpublic: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 apublic:A(int aa=0) { a=aa}~A() { cout <<"Destructor A!"<<a<<endl }}class B:public A {int bpublic: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移位 *** 作是按二进制算的, 右移 一下相当于除2,左移乘2,说以10进制的姿枣不能通过移位实现取位,2,8,16进迹森拆制的都可以哦。
不过还春慎是有办法,就是用16进制变量存你的10进制变量,然后按16进制移位取位,最后按16进制输出,可达到你想要的效果哦~
我要上课去了,如代码还是写不出来,追问一下,我回来帮你写个
评论列表(0条)