1.输出的结果是什么?p82
#include <iostream> using namespace std; void f2(int a[],int k){ int n,t,j; for (j=0;j<k-1;j++) for(n=0;n<k-j;n++) if (a[n]<a[n+1]) {t=a[n];a[n]=a[n+1];a[n+1]=t;} } int main(){ int i; int x[]={50,30,60,10,80}; f2(x,5); for(i=0;i<5;i++) {cout<<x[i]<<"\t";} cout<<endl; return 0; }
2.
#include <iostream> using namespace std; class strCyton{ public: char *str; strCyton(char p[50]={'h','e','l','l','o','\0'}){ str = p; } friend char* operator+=(strCyton &str,strCyton str1); friend char* operator=(strCyton str,strCyton str1); bool operator==(strCyton str1); void show(){ cout<<str<<endl; } }; char* operator+=(strCyton &str,strCyton str1){ str.str = new char [strlen(str.str)+strlen(str1.str)+1]; char *temp = str.str; while(str.str++); while(str.str=str1.str++); str.*str='\0'; str.str=temp; return str.str; } char* operator=(strCyton str,strCyton str1){ str.str=str1.str; return str.str; } bool strCyton::operator==(strCyton str1) { if (strcmp(str,str1.str)==0){ return true; }else{ return false; } } int main(){ strCyton st,st1("world"); st+=st1; st.show(); st=st1; st.show(); cout<<(st==st1)<<endl; return 0; }
3.
#include <iostream> #include <string> using namespace std; class myStringCpp{ char *str; public: myStringCpp(){ str = new char [1]; } myStringCpp(char *p){ str=p; } friend myStringCpp& operator+=(myStringCpp& s1,myStringCpp& s2); friend ostream& operator<<(ostream& os,myStringCpp &s1); }; myStringCpp& operator+=(myStringCpp& s1,myStringCpp& s2){ myStringCpp s3; s3.str = new char [strlen(s1.str)+strlen(s2.str)+1]; strcpy(s3.str,s1.str); strcat(s3.str,s2.str); return s3; } ostream& operator<<(ostream &os,myStringCpp &s1){ return os<<s1.str; } using namespace std; int main(){ myStringCpp ss1("hello"),ss2("world"); ss1+=ss2; cout<<ss1<<endl; return 0; }