题目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;//生成str1对象

char s[80];

cin.getline(s,80);

str1.Cat(s);//字符串s连接到str1中字符串的末尾

CString str2(str1);//生成str2对象

str2.Print();//输出str2中保存的字符串

cin.getline(s,80);

CString str3(s),str4;

str4.Copy(str3);//将str3中保存的字符串拷贝到str4中

str4.Cat(str1);

cout<<str4.Len()<<endl;//输出str4中字符串长度

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)//将对象复制到新的对象中去
//对应 CString str2(str1);//生成str2对象
{
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;//生成str1对象

char s[80];

cin.getline(s,80);

str1.Cat(s);//字符串s连接到str1中字符串的末尾

CString str2(str1);//生成str2对象

str2.Print();//输出str2中保存的字符串

cin.getline(s,80);

CString str3(s),str4;

str4.Copy(str3);//将str3中保存的字符串拷贝到str4中

str4.Cat(str1);

cout<<str4.Len()<<endl;//输出str4中字符串长度

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); //将data压栈

int Pop(); //弹出栈顶元素

void MakeEmpty(); //清空栈,数据重新从0下标存放

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); //将data压栈
int Pop(); //弹出栈顶元素
void MakeEmpty(); //清空栈,数据重新从0下标存放
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;
}

代码可能略有不足,请大佬多多指教