题目1
【问题描述】
试根据给出的main测试函数定义CString类,用以处理用户长度不定(使用动态内存处理)的字符串。提供的操作主要有字符串显示(单独占一行)、求字符串长度、在字符串末尾添加一个字符串、字符串拷贝等,其它功能自行分析并定义。本题不得用string类和cstring库函数。main函数不得修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| int main()
{
CString str1;
char s[80];
cin.getline(s,80);
str1.Cat(s);
CString str2(str1);
str2.Print();
cin.getline(s,80);
CString str3(s),str4;
str4.Copy(str3);
str4.Cat(str1);
cout<<str4.Len()<<endl;
str4.Print();
return 0;
}
|
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include<iostream> using namespace std; class CString { public: char String[80] = { 0 }; CString() {
} CString(const CString& ss) { for (int i = 0; i < 80; i++) { if (ss.String[i] != '\0')String[i] = ss.String[i]; if (ss.String[i] == '\0')String[i] = '\0'; } } CString(char mm[]) { for (int i = 0; i < 80; i++) { if (mm[i] != '\0')String[i] = mm[i]; if (mm[i] == '\0')String[i] = 0; }
} void Cat(char m[]) { int j = 0; for (int i = 0; i < 80; i++) { if (String[i] == '\0' && m[j] != '\0') { String[i] = m[j]; j++; String[i + 1] = '\0'; } } } void Cat(const CString mm) { int j = 0; for (int i = 0; i < 80; i++) { if (String[i] == '\0' && mm.String[j] != '\0') { String[i] = mm.String[j]; j++; String[i + 1] = '\0'; } } } void Print() { for (int i = 0; i < 80; i++) if (String[i] != '\0')cout << String[i]; cout << endl; } void Copy(const CString mm) { for (int i = 0; i < 80; i++) { if (mm.String[i] != '\0')String[i] = mm.String[i]; if (mm.String[i] == '\0')String[i] = '\0'; } } int Len() { int j = 0; for (int i = 0; i < 80; i++) { if (String[i] != 0)j++; } return j; } };
int main()
{
CString str1;
char s[80];
cin.getline(s,80);
str1.Cat(s);
CString str2(str1);
str2.Print();
cin.getline(s,80);
CString str3(s),str4;
str4.Copy(str3);
str4.Cat(str1);
cout<<str4.Len()<<endl;
str4.Print();
return 0;
}
|
题目2
【问题描述】
请根据已给出的部分代码,完善顺序栈类的设计以使程序正确运行。给出的代码部分修改无效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| class Stack
{
int top;
int* elements;
int maxSize;
public:
Stack(int=20);
~Stack();
Stack(const Stack&);
void Push(const int& data);
int Pop();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
};
int main()
{
int num,data;
cin>>num;
Stack stack1(num);
for(int i=0; i<num; i++)
{
cin>>data;
stack1.Push(data);
}
Stack stack2(stack1);
if(stack2.IsFull()) cout<<"Stack Full"<<endl;
while(!stack2.IsEmpty())
cout<<stack2.Pop()<<' ';
cout<<endl;
stack1.MakeEmpty();
cout<<stack1.IsEmpty()<<endl;
return 0;
}
|
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <iostream> using namespace std;
class Stack{ int top; int* elements; int maxSize; public: Stack(int size=20); ~Stack(); Stack(const Stack&); void Push(const int& data); int Pop(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; };
Stack::Stack(int size){ top = -1; maxSize = size; elements = new int[maxSize]; }
Stack::~Stack(){ delete []elements; }
Stack::Stack(const Stack& s){ top = s.top; maxSize = s.maxSize; elements = new int[maxSize]; for(int i = 0; i <= top; i++){ elements[i] = s.elements[i]; } }
void Stack::Push(const int& data){ if(!IsFull()){ top++; elements[top] = data; } }
int Stack::Pop(){ int data = 0; if(!IsEmpty()){ data = elements[top]; top--; } return data; }
void Stack::MakeEmpty(){ top = -1; }
bool Stack::IsEmpty() const{ return top == -1; }
bool Stack::IsFull() const{ return top == maxSize - 1; }
int main(){ int num,data; cin >> num; Stack stack1(num); for(int i = 0; i < num; i++){ cin >> data; stack1.Push(data); } Stack stack2(stack1); if(stack2.IsFull()) cout<<"Stack Full"<<endl; while(!stack2.IsEmpty()) cout << stack2.Pop() << ' '; cout << endl; stack1.MakeEmpty(); cout << stack1.IsEmpty() << endl; return 0; }
|
代码可能略有不足,请大佬多多指教