程序填空题:C Fill in the blanks
Run the following program, Enter: 1, the output is:
S1 == S2
HfLLO
HFLLO
```
#include
using namespace std;
class ERROR{};
class STRING
{
char *m_pStr;
int m_len;
public:
STRING(char *str=NULL){
if (str != NULL) {
m_len = strlen(str);
m_pStr = @@[new char[m_len+1]](1);
strcpy(@@[m_pStr,str](1));
}
else {
m_len = 0;
m_pStr = NULL;
}
}
@@[STRING&](1) operator=(char *str)
{
@@[delete[]](1) m_pStr ;
m_len = strlen(str)+1;
m_pStr = new char[m_len];
strcpy(@@[m_pStr,str](1));
return @@[*this](1);
}
bool operator==(STRING str) @@[const](1)
{
return (@@[strcmp](1)(m_pStr, str.m_pStr)== 0);
}
char operator [] (int i) @@[const](1)
{
if (i=0) return m_pStr[i];
throw @@[ERROR()](1);
}
char& operator[](int i) @@[](1)
{
if (i=0) return m_pStr[i];
ERROR e;
@@[throw e](1);
}
@@[friend](1) ostream& operator<<(ostream& out ,STRING s);
};
ostream& operator<<(ostream& out ,STRING s)
{
out << s.m_pStr;
return out;
}
int main()
{
STRING s1,s2("HeLLO");
int i;
cin >> i;
s1 = s2;
if (s1 == s2) cout << "S1 == S2\n";
s1[1] = s1[1] + 1;
cout << s1 << endl;;
@@[try](1){
if(s1[i]>='a' && s1[i]<='z') s1[i] = s1[i] - 32;
cout << s1 << endl;
}
@@[catch](1)( ERROR& e)
{
cout << "upperbound overflow";
}
return 0;
}
```
答案:
第1空:new char[m_len+1]
第2空:m_pStr,str
第3空:STRING&
第4空:delete[]
第5空:m_pStr,str
第6空:*this
第7空:const
第8空:strcmp
第9空:const
第10空:ERROR()
第11空:
第12空:throw e
第13空:friend
第14空:try
第15空:catch
S1 == S2
HfLLO
HFLLO
```
#include
using namespace std;
class ERROR{};
class STRING
{
char *m_pStr;
int m_len;
public:
STRING(char *str=NULL){
if (str != NULL) {
m_len = strlen(str);
m_pStr = @@[new char[m_len+1]](1);
strcpy(@@[m_pStr,str](1));
}
else {
m_len = 0;
m_pStr = NULL;
}
}
@@[STRING&](1) operator=(char *str)
{
@@[delete[]](1) m_pStr ;
m_len = strlen(str)+1;
m_pStr = new char[m_len];
strcpy(@@[m_pStr,str](1));
return @@[*this](1);
}
bool operator==(STRING str) @@[const](1)
{
return (@@[strcmp](1)(m_pStr, str.m_pStr)== 0);
}
char operator [] (int i) @@[const](1)
{
if (i
throw @@[ERROR()](1);
}
char& operator[](int i) @@[](1)
{
if (i
ERROR e;
@@[throw e](1);
}
@@[friend](1) ostream& operator<<(ostream& out ,STRING s);
};
ostream& operator<<(ostream& out ,STRING s)
{
out << s.m_pStr;
return out;
}
int main()
{
STRING s1,s2("HeLLO");
int i;
cin >> i;
s1 = s2;
if (s1 == s2) cout << "S1 == S2\n";
s1[1] = s1[1] + 1;
cout << s1 << endl;;
@@[try](1){
if(s1[i]>='a' && s1[i]<='z') s1[i] = s1[i] - 32;
cout << s1 << endl;
}
@@[catch](1)( ERROR& e)
{
cout << "upperbound overflow";
}
return 0;
}
```
答案:
第1空:new char[m_len+1]
第2空:m_pStr,str
第3空:STRING&
第4空:delete[]
第5空:m_pStr,str
第6空:*this
第7空:const
第8空:strcmp
第9空:const
第10空:ERROR()
第11空:
第12空:throw e
第13空:friend
第14空:try
第15空:catch