請寫出 mystring類,使得下面程式的輸出結果符合下面的要求。
#include #include #include #include using namespace std;
// 在此處補充你的**
int comparestring( const void * e1, const void * e2)
int main()
; cout << "1. " << s1 << s2 << s3<< s4<< endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'a' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(sarray,4,sizeof(mystring),comparestring);
for( int i = 0;i < 4;i ++ )
cout << sarray[i] << endl;
//s1的從下標0開始長度為4的子串
cout << s1(0,4) << endl;
//s1的從下標5開始長度為10的子串
cout << s1(5,10) << endl;
return 0;
}
無1. abcd-efgh-abcd-
2. abcd-
3. 4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijal-
9. ijal-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
bigme
take
abcd
qrst-abcd-
樣例輸出
1. abcd-efgh-abcd-
2. abcd-
3.4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijal-
9. ijal-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
bigme
take
abcd
qrst-abcd-
我的**:不知道為什麼到11行結束就執行出錯,被迫中止了,求大神解答
#include #include #include #include using namespace std;
// 在此處補充你的**
class mystring
//mystring(const char* str) :str(str){}//本來就沒想寫這句,ideonline上說陣列的哪一行110,有錯,char[4]不能轉為myatring
//但是寫了反而多了個error:125行s4=等號出錯,對應多個,不懂
mystring(string s) :str(s){}
mystring(const mystring & s) :str(s.str){}
//1.過載<、==、>
friend bool operator <(const mystring & a, const mystring &b)
return false;
} friend bool operator ==(const mystring & a, const mystring &b)
return false;
} friend bool operator >(const mystring & a, const mystring &b)
return false;
} //3.<<
friend ostream & operator << (ostream & out,const mystring & s)
//4.
char & operator (int i)//6.()
string operator()(int a, int b)
//7.+
string operator+(const mystring &s)
void operator+=(string s)
friend string operator+(string s, const mystring &b)
//8.=
void operator=(string s)
};int comparestring(const void * e1, const void * e2)
int main()
; cout << "1. " << s1 << s2 << s3 << s4 << endl;
s4 = s3;
s3 = s1 + s3;//5.+
cout << "2. " << s1 << endl;//3.過載<<
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;//4.過載
s2 = s1;
s1 = "ijkl-";//5.=
s1[2] = 'a';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";//+
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(sarray, 4, sizeof(mystring), comparestring);
for (int i = 0; i < 4; i++)
cout << sarray[i] << endl;
//s1的從下標0開始長度為4的子串
cout << s1(0, 4) << endl;//6.()
//s1的從下標5開始長度為10的子串
cout << s1(5, 10) << endl;
return 0;
}
C程式設計題 6
6.矩陣中填數 當給出 n n 的矩陣,要求用程式填入下列形式的數 倒填,例如 n 5 蛇形填數 迴轉填數 25 24 23 22 21 1 3 4 10 11 1 16 15 14 13 20 19 18 17 16 2 5 9 12 19 2 17 24 23 12 15 14 13 12 11...
c語言程式設計題 求車速
一輛以固定速度行駛的汽車,司機在上午10點看到里程表 里程表有6位 上的讀數是乙個對稱數95859 即這個數從左向右讀和從右向左讀是完全一樣的 兩小時後里程表上出現了乙個新的對稱數。問該車的速度是多少?新的對稱數是多少?include stdio.h int main if a 0 a 4 a 1 ...
筆試程式設計題彙總 6
給乙個長度為n的整數陣列,將這個陣列分成m段,每一段的和為這一段的權重,所有段的權重的最大值為這次分割的權重,求所有分割中最小的權重 輸入描述 第一行是兩個整數,表示n,m m n 第二行數n個空格分割的整數 輸出 輸出所有分割中最小的權重 樣例 input 5 31 4 2 3 5 output ...