智障一样的数构实验-u3

3-2

listQueue.h

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


#include <iostream>

#include "simpleList.h"

#ifndef LISTQUEUE_H
#define LISTQUEUE_H

class : public sList
{
public:
void enqueue(T& data)
{
insertAtBack(data);
}
void dequeue(T& data)
{
removeFromFront(data);
}
bool isQueueEmpty()
{
return isEmpty();
}
void printQueue()
{
print();
}
};

#endif

simpleList.h

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205


#include <iostream>
#include <fstream>
using namespace std;

typedef int T;

#ifndef LISTNODE_H
#define LISTNODE_H

class sList;

class ListNode
{
friend class sList;
private:
T data;
ListNode* nextPtr;
public:
ListNode(const T& info)
{
data = info;
nextPtr = NULL;
}
T getData()
{
return data;
}
};

#endif


#ifndef SIMPLELIST_H
#define SIMPLELIST_H

class sList
{
private:
ListNode* firstPtr;
ListNode* lastPtr;
int lengthList;
ListNode* getNewNode(T& value)
{
return new ListNode(value);
}
public:
sList();
~sList();
void insertAtFront(T&);
void insertAtBack(T&);
bool removeFromFront(T&);
bool removeFromBack(T&);
bool isEmpty()
{
return firstPtr == NULL;
}
void print();
void createList(fstream&);
};

#endif

sList::sList() :firstPtr(NULL), lastPtr(NULL)
{
lengthList = 0;
}

sList::~sList()
{
if (!isEmpty())
{
ListNode* currentPtr = firstPtr;
ListNode* tmpPtr;
while (currentPtr != NULL)
{
tmpPtr = currentPtr;

currentPtr = currentPtr->nextPtr;
lengthList--;
delete tmpPtr;
}
}
//cout << "当前链表所有节点已释放n" << endl;
}


void sList::insertAtFront(T& value)
{
ListNode* newPtr = getNewNode(value);
if (isEmpty())
{
firstPtr = lastPtr = newPtr;
}
else
{
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
lengthList++;
}
}

void sList::insertAtBack(T& value)
{
ListNode* newPtr = getNewNode(value);
if (isEmpty())
{
firstPtr = lastPtr = newPtr;
}
else
{
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
lengthList++;
}
}

bool sList::removeFromFront(T& value)
{
if (isEmpty())
{
return false;
}
else
{
ListNode* tmpPtr = firstPtr;
if (firstPtr == lastPtr)
{
firstPtr = lastPtr = NULL;
}
else
{
firstPtr = firstPtr->nextPtr;
}
value = tmpPtr->data;
lengthList--;
delete tmpPtr;

return true;
}
}


bool sList::removeFromBack(T& value)
{
if (isEmpty())
{
return false;
}
else
{
ListNode* tmpPtr = lastPtr;
if (firstPtr == lastPtr)
{
firstPtr = lastPtr = NULL;
}
else
{
ListNode* currentPtr = firstPtr;
while (currentPtr->nextPtr != lastPtr)
{
currentPtr = currentPtr->nextPtr;
}
lastPtr = currentPtr;
currentPtr->nextPtr = NULL;
}
value = tmpPtr->data;
lengthList--;
delete tmpPtr;
return true;
}
}

void sList::print()
{
if (isEmpty())
{
cout << "n空链表";
return;
}
ListNode* currentPtr = firstPtr;
cout << "n当前链表:n";
while (currentPtr)
{
cout << currentPtr->data << " ";
currentPtr = currentPtr->nextPtr;
}
cout << endl;
}

void sList::createList(fstream& inF)
{
T value;
int len;
inF >> len;
while (len > 0)
{
inF >> value;
cout << "nvalue: " << value << endl;
insertAtBack(value);
len--;
}
cout << endl;
}

main.cpp

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
#include <iostream>
#include <fstream>
#include <cstdlib>

#include "listQueue.h"

using namespace std;

void YangHITriangle(int m, ofstream& outF)
{
listQueue q;
T k = 0;
T u = 1, s = 0, t;
q.enqueue(u);
q.enqueue(u);

//q.printQueue();
//cout << "--------------------------" << endl;
//system("pause");

for (int i = 1; i <= m; i++)
{
//cout << endl;
q.enqueue(k);
//q.printQueue();
//cout << "--------------------------" << endl;
//system("pause");
for (int j = 1; j <= i + 2; j++)
{
q.dequeue(t);
//cout << t << " " << endl;
//cout << "--------------" << endl;
//system("pause");
u = s + t;
q.enqueue(u);
s = t;
if (j != i + 2)
{
outF << s << " ";
}
//q.printQueue();
//cout << "--------------------------" << endl;
//system("pause");
}
outF << endl;
}
}

void execCh3_2()
{
int n;
ofstream outf("triangle.txt", ios::in);
if (!outf)
{
cerr << "Unable to open the file.n";
exit(1);
}
n = 5 + rand() % 10;
YangHITriangle(n, outf);
}

int main()
{
execCh3_2();
return 0;
}

3-3

listStack.h

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


#include "simpleList.h"

#ifndef LISTSTACK_H
#define LISTSTACK_H

class listStack : public sList
{
public:
void push(T& data)
{
insertAtFront(data);
}
void pop(T& data)
{
removeFromFront(data);
}
bool isStackEmpty()
{
return isEmpty();
}
void printStack()
{
print();
}
};

#endif

simpleList.h

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205


#include <iostream>
#include <fstream>
using namespace std;

typedef int T;

#ifndef LISTNODE_H
#define LISTNODE_H

class sList;

class ListNode
{
friend class sList;
private:
T data;
ListNode* nextPtr;
public:
ListNode(const T& info)
{
data = info;
nextPtr = NULL;
}
T getData()
{
return data;
}
};

#endif


#ifndef SIMPLELIST_H
#define SIMPLELIST_H

class sList
{
private:
ListNode* firstPtr;
ListNode* lastPtr;
int lengthList;
ListNode* getNewNode(T& value)
{
return new ListNode(value);
}
public:
sList();
~sList();
void insertAtFront(T&);
void insertAtBack(T&);
bool removeFromFront(T&);
bool removeFromBack(T&);
bool isEmpty()
{
return firstPtr == NULL;
}
void print();
void createList(fstream&);
};

#endif

sList::sList() :firstPtr(NULL), lastPtr(NULL)
{
lengthList = 0;
}

sList::~sList()
{
if (!isEmpty())
{
ListNode* currentPtr = firstPtr;
ListNode* tmpPtr;
while (currentPtr != NULL)
{
tmpPtr = currentPtr;

currentPtr = currentPtr->nextPtr;
lengthList--;
delete tmpPtr;
}
}
//cout << "当前链表所有节点已释放n" << endl;
}


void sList::insertAtFront(T& value)
{
ListNode* newPtr = getNewNode(value);
if (isEmpty())
{
firstPtr = lastPtr = newPtr;
}
else
{
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
lengthList++;
}
}

void sList::insertAtBack(T& value)
{
ListNode* newPtr = getNewNode(value);
if (isEmpty())
{
firstPtr = lastPtr = newPtr;
}
else
{
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
lengthList++;
}
}

bool sList::removeFromFront(T& value)
{
if (isEmpty())
{
return false;
}
else
{
ListNode* tmpPtr = firstPtr;
if (firstPtr == lastPtr)
{
firstPtr = lastPtr = NULL;
}
else
{
firstPtr = firstPtr->nextPtr;
}
value = tmpPtr->data;
lengthList--;
delete tmpPtr;

return true;
}
}


bool sList::removeFromBack(T& value)
{
if (isEmpty())
{
return false;
}
else
{
ListNode* tmpPtr = lastPtr;
if (firstPtr == lastPtr)
{
firstPtr = lastPtr = NULL;
}
else
{
ListNode* currentPtr = firstPtr;
while (currentPtr->nextPtr != lastPtr)
{
currentPtr = currentPtr->nextPtr;
}
lastPtr = currentPtr;
currentPtr->nextPtr = NULL;
}
value = tmpPtr->data;
lengthList--;
delete tmpPtr;
return true;
}
}

void sList::print()
{
if (isEmpty())
{
cout << "n空链表";
return;
}
ListNode* currentPtr = firstPtr;
cout << "n当前链表:n";
while (currentPtr)
{
cout << currentPtr->data << " ";
currentPtr = currentPtr->nextPtr;
}
cout << endl;
}

void sList::createList(fstream& inF)
{
T value;
int len;
inF >> len;
while (len > 0)
{
inF >> value;
cout << "nvalue: " << value << endl;
insertAtBack(value);
len--;
}
cout << endl;
}

main.cpp

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>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;

const int maxLength = 100;

#include "listStack.h"

void computerExpression(char* expr)
{
listStack val;
T x, y, v;
char p;
int op[] = { 32,14,20,5,70,2 };
int i, len;

len = strlen(expr);

for (i = 1; i <= len; i++)
{
if (expr[i - 1] >= 'A' && expr[i - 1] <= 'Z')
{
v = op[expr[i - 1] - 'A'];
cout << expr[i - 1] << '=' << v << " ";
val.push(v);
}
else if (expr[i - 1] == '+' || expr[i - 1] == '-' || expr[i - 1] == '*' || expr[i - 1] == '/')
{
p = expr[i - 1];
val.pop(x);
val.pop(y);
if (p == '+')
{
v = x + y;
val.push(v);
}
else if (p == '-')
{
v = y - x;
val.push(v);
}
else if (p == '*')
{
v = y * x;
val.push(v);
}
else if (p == '/')
{
if (x == 0)
{
cout << "n除数不能为零" << endl;
exit(1);
}
else
{
v = y / x;
val.push(v);
}
}
}
else
{
break;
}
}
val.pop(x);
if (val.isStackEmpty())
{
cout << "n计算结果:" << x << endl;
}
else
{
cout << "n表达式中缺少操作数" << endl;
}
}

void postfixExpression()
{
char* strExpre;
int len;

fstream inf("postfixFile.txt", ios::in);
if (!inf)
{
cerr << "无法打开文件" << endl;
exit(1);
}
strExpre = new char[maxLength];
inf >> strExpre;

len = strlen(strExpre);
strExpre[len] = '#';
strExpre[len + 1] = '