箱子排序

链表用桶排序时间复杂度会较低

1.头文件虚基类linearlist.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
#ifndef linearlist_
#define linearlist_
#include <iostream>
#include<string>
using namespace std;
template<class T>
class linearlist
{
public:
virtual ~linearlist() {};

virtual bool empty() const = 0;

virtual int size() const = 0;

virtual T& get(int theindex) const = 0;

virtual int indexof(const T& theelement) const = 0;

virtual void erase(int theindex) = 0;

virtual void insert(int theindex, const T& theelement) = 0;

virtual void output(ostream& out) const = 0;

};
#endif

2.头文件类声明chain.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
#ifndef chain_H_
#define chain_H_
#include"linearlist.h"
template<class T>
struct chainnode
{
T element;
chainnode<T>* next;

chainnode() {}
chainnode(const T& element) { this->element = element; }
chainnode(const T& element, chainnode<T>* next)
{
this->element = element;
this->next = next;
}
};
template<class T>
class chain :public linearlist<T>
{
public:
chain(int initial = 10);
chain(const chain<T>&);//复制构造函数
~chain();
//
bool empty()const { return listsize == 0; }
int size()const { return listsize; }
T& get(int theindex)const;
int indexof(const T& theelement)const;
void erase(int theindex);
void insert(int theindex, const T& theelement);
void output(ostream& out)const;
void binsort(int range);

//迭代器
class iterator;
iterator begin() { return iterator(firstnode); }
iterator end() { return iterator(NULL); }

class iterator
{
public:
typedef forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;

// 构造函数
iterator(chainnode<T>* theNode = NULL)
{
node = theNode;
}

// 解引用操作符
T& operator*() const { return node->element; }
T* operator->() const { return &node->element; }


iterator& operator++() // 前加加
{
node = node->next; return *this;
}
iterator operator++(int) //后加加
{
iterator old = *this;
node = node->next;
return old;
}

bool operator!=(const iterator right) const
{
return node != right.node;
}
bool operator==(const iterator right) const
{
return node == right.node;
}
protected:
chainnode<T>* node;
};

protected:
chainnode<T>* firstnode;//指向链表第一个节点的指针
int listsize;
};


struct studentrecord
{
int score;
string* name;
int operator != (const studentrecord& x) const
{
return score != x.score;
}
operator int()const { return score; }
};

#endif

3.类实现chain.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "chain.h"
template<class T>
chain<T>::chain(int initial)//初始容积
{
if (initial < 1)
{
exit(0);
}
firstnode = nullptr;
listsize = 0;

}

template<class T>
chain<T>::chain(const chain<T>& thelist)
{
//复制构造函数 就是复制链表
listsize = thelist.listsize;
//如果链表thelist为空
if (listsize == 0)
{
firstnode = nullptr;
return;
}
//非空,先复制第一个
chainnode<T>* sourcenode = thelist.firstnode;//要复制链表thelist的节点
firstnode = new chainnode<T>(sourcenode->element);//复制链表thelist的首元素
sourcenode = sourcenode->next;
chainnode<T>* targetnode = firstnode;//当前链表*this的最后一个节点
while (sourcenode != nullptr)
{
//复制剩下的
targetnode->next = new chainnode<T>(sourcenode->element);
targetnode = targetnode->next;
sourcenode = sourcenode->next;
}
targetnode->next = nullptr;
}

template<class T>
chain<T>::~chain()
{
//析构,删除链表所有节点
while (firstnode)
{
chainnode<T>* nextnode = firstnode->next;
delete firstnode;
firstnode = nextnode;
}
}

template<class T>
T& chain<T>::get(int theindex)const
{
//返回索引为theindex的元素
//若元素不存在就抛出异常
/*checkindex(theindex);*/

//移向所需要的节点
chainnode<T>* currentnode = firstnode;
for (int i = 0; i < theindex; i++)
currentnode = currentnode->next;
return currentnode->element;
}

template<class T>
int chain<T>::indexof(const T& theelement)const
{
//返回元素首次出现时的索引,若不存在返回-1
chainnode<T>* currentnode = firstnode;
int index = 0;
while (currentnode && currentnode->element != theelement)
{
currentnode = currentnode->next;
index++;

}
if (currentnode == nullptr)
return -1;
else
return index;
}

template<class T>
void chain<T>::erase(int theindex)
{
/*checkindex(theindex);*/

chainnode<T>* deletenode;
if (theindex == 0)
{
deletenode = firstnode;
firstnode = firstnode->next;
}
else
{
chainnode<T>* p = firstnode;
for (int i = 0; i < theindex - 1; i++)
p = p->next;
deletenode = p->next;
p->next = p->next->next;

}
listsize--;
delete deletenode;
}

template<class T>
void chain<T>::insert(int theindex, const T& theelement)
{
if (theindex<0 || theindex>listsize)
{
exit(0);
}
if (theindex == 0)
firstnode = new chainnode<T>(theelement, firstnode);
else
{
chainnode<T>* p = firstnode;
for (int i = 0; i < theindex - 1; i++)
p = p->next;
p->next = new chainnode<T>(theelement, p->next);
}
listsize++;
}

template<class T>
void chain<T>::output(ostream& out)const
{
for (chainnode<T>* currentnode = firstnode;
currentnode;
currentnode = currentnode->next)
out << currentnode->element << " ";
}

template<class T>
ostream& operator<<(ostream& out, const chain<T>& x)
{
x.output(out);
return out;
}

//将限制排序作为链表chain的一个成员方法
template<class T>
void chain<T>::binsort(int range)
{
//创建并初始化箱子
chainnode<T>** bottom, ** top;
bottom = new chainnode<T>*[range + 1];
top = new chainnode<T>*[range + 1];
for (int b = 0; b <= range; b++)
bottom[b] = nullptr;


//把链表的节点分配到箱子
for (; firstnode; firstnode = firstnode->next)
{
int thebin = firstnode->element;
if (bottom[thebin] == nullptr)
bottom[thebin] = top[thebin] = firstnode;
else
{
//箱子不空
top[thebin]->next = firstnode;
top[thebin] = firstnode;
}
}

//把箱子中的节点收集到有序链表
chainnode<T>* y = nullptr;
for (int thebin = 0; thebin <= range; thebin++)
{
if (bottom[thebin] != nullptr)
{
//箱子不空
if (y == nullptr)
firstnode = bottom[thebin];
else
y->next = bottom[thebin];
y = top[thebin];
}
}
if (y != nullptr)
y->next = nullptr;
delete[]bottom;
delete[]top;
}

inline ostream& operator<<(ostream& out, const studentrecord& x)
{
out << x.score << ' ' << *x.name << endl;
return out;
}

inline void binsort(chain<studentrecord>& thechain, int range)
{
//对箱子初始化
chain<studentrecord>* bin;
bin = new chain<studentrecord>[range + 1];

//把学生记录从链表中取出然后分配到箱子里
int numberofelements = thechain.size();
for (int i = 1; i <= numberofelements; i++)
{
studentrecord x = thechain.get(0);
thechain.erase(0);
bin[x.score].insert(0, x);
}

//“稳定排序”,取两次正好还是原来的顺序


//从箱子中收集元素
for (int j = range; j >= 0; j--)
{
while (!bin[j].empty())
{
studentrecord x = bin[j].get(0);
bin[j].erase(0);
thechain.insert(0, x);
}
}
delete[]bin;
}

4.一个简短的测试程序测试.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
#include"chain.cpp"
//int main()
//{
// studentrecord i = { 99,"a" }, j = { 88,"b" }, k = { 77,"c" };
// chain<studentrecord> a, b, c;
// a.insert(0, i);
// a.insert(1, j);
// a.insert(2, k);
// binsort(a, 100);
// cout << a;
//
//}

int main(void)
{
studentrecord s;
chain<studentrecord> thechain;
for (int i = 1; i <= 20; i++)
{
s.score = i / 2;
s.name = new string(s.score, 'a');
thechain.insert(0, s);
}
cout << "The unsorted chain is" << endl;
cout << thechain << endl;
thechain.binsort(10);
cout << "The sorted chain is" << endl;
cout << thechain << endl;
}

八王后

百度找的,这个程序相当“好看”

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
#include<iostream>
using namespace std;
int main()
{
int a[9][9];
int x[9];
int n, b = 0;
n = 8;
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
a[i][j] = 0;
}
for (int c = 1;c <= n;c++)
{
x[1] = c;
a[1][c] = 1;
for (int c1 = 1;c1 <= n;c1++)
{
if (c1 != c)
a[1][c1] = 0;
}
for (int d = 1;d <= n;d++)
{
x[2] = d;
a[2][d] = 1;
for (int d1 = 1;d1 <= n;d1++)
{
if (d1 != d)
a[2][d1] = 0;
}
for (int e = 1;e <= n;e++)
{
x[3] = e;
a[3][e] = 1;
for (int e1 = 1;e1 <= n;e1++)
{
if (e1 != e)
a[3][e1] = 0;
}
for (int f = 1;f <= n;f++)
{
x[4] = f;
a[4][f] = 1;
for (int f1 = 1;f1 <= n;f1++)
{
if (f1 != f)
a[4][f1] = 0;
}
for (int g = 1;g <= n;g++)
{
x[5] = g;
a[5][g] = 1;
for (int g1 = 1;g1 <= n;g1++)
{
if (g1 != g)
a[5][g1] = 0;
}
for (int h = 1;h <= n;h++)
{
x[6] = h;
a[6][h] = 1;
for (int h1 = 1;h1 <= n;h1++)
{
if (h1 != h)
a[6][h1] = 0;
}
for (int i = 1;i <= n;i++)
{
x[7] = i;
a[7][i] = 1;
for (int i1 = 1;i1 <= n;i1++)
{
if (i1 != i)
a[7][i1] = 0;
}
for (int j = 1;j <= n;j++)
{
x[8] = j;
a[8][j] = 1;
for (int j1 = 1;j1 <= n;j1++)
{
if (j1 != j)
a[8][j1] = 0;
}
if (x[1] != x[2] && x[1] != x[3] && x[1] != x[4] && x[1] != x[5] && x[1] != x[6] && x[1] != x[7] && x[1] != x[8])
{
if (x[2] != x[3] && x[2] != x[4] && x[2] != x[5] && x[2] != x[6] && x[2] != x[7] && x[2] != x[8])
{
if (x[3] != x[4] && x[3] != x[5] && x[3] != x[6] && x[3] != x[7] && x[3] != x[8])
{
if (x[4] != x[5] && x[4] != x[6] && x[4] != x[7] && x[4] != x[8])
{
if (x[5] != x[6] && x[5] != x[7] && x[5] != x[8])
{
if (x[6] != x[7] && x[6] != x[8])
{
if (x[7] != x[8])
{
if (x[1] != x[2] + 1 && x[1] != x[3] + 2 && x[1] != x[4] + 3 && x[1] != x[5] + 4 && x[1] != x[6] + 5 && x[1] != x[7] + 6 && x[1] != x[8] + 7)
{
if (x[2] != x[3] + 1 && x[2] != x[4] + 2 && x[2] != x[5] + 3 && x[2] != x[6] + 4 && x[2] != x[7] + 5 && x[2] != x[8] + 6)
{
if (x[3] != x[4] + 1 && x[3] != x[5] + 2 && x[3] != x[6] + 3 && x[3] != x[7] + 4 && x[3] != x[8] + 5)
{
if (x[4] != x[5] + 1 && x[4] != x[6] + 2 && x[4] != x[7] + 3 && x[4] != x[8] + 4)
{
if (x[5] != x[6] + 1 && x[5] != x[7] + 2 && x[5] != x[8] + 3)
{
if (x[6] != x[7] + 1 && x[6] != x[8] + 2)
{
if (x[7] != x[8] + 1)
{
if (x[1] != x[2] - 1 && x[1] != x[3] - 2 && x[1] != x[4] - 3 && x[1] != x[5] - 4 && x[1] != x[6] - 5 && x[1] != x[7] - 6 && x[1] != x[8] - 7)
{
if (x[2] != x[3] - 1 && x[2] != x[4] - 2 && x[2] != x[5] - 3 && x[2] != x[6] - 4 && x[2] != x[7] - 5 && x[2] != x[8] - 6)
{
if (x[3] != x[4] - 1 && x[3] != x[5] - 2 && x[3] != x[6] - 3 && x[3] != x[7] - 4 && x[3] != x[8] - 5)
{
if (x[4] != x[5] - 1 && x[4] != x[6] - 2 && x[4] != x[7] - 3 && x[4] != x[8] - 4)
{
if (x[5] != x[6] - 1 && x[5] != x[7] - 2 && x[5] != x[8] - 3)
{
if (x[6] != x[7] - 1 && x[6] != x[8] - 2)
{
if (x[7] != x[8] - 1)
{
b++;
cout << "--------" << b << "----------" << endl;
for (int k = 1;k <= n;k++)
{
for (int l = 1;l <= n;l++)
cout << a[k][l] << " ";
cout << endl;
}
cout << endl << endl;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
cout << "共" << b << "种。";
system("pause");
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
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
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
typedef struct grade
{
string number;
string name;
int chinese;
int math;
int english;
struct grade* next;
}grade;
typedef struct point
{
grade* head;
grade* tail;
grade* target1;
grade* target2;
grade* tmp1;
grade* tmp2;
grade* now;
grade* tt;
int cnt;
}point;
void 读取文件(point* p);
void 打印成绩(point* p);
void 释放空间(point* p);
void 搜索目标(point* p);
void 链表排序(point* p);
int main()
{
point point;
读取文件(&point);
打印成绩(&point);
链表排序(&point);
打印成绩(&point);
释放空间(&point);
}
void 读取文件(point* p)
{
ifstream fin("111.txt");
p->head = nullptr;
p->tail = nullptr;
p->cnt = 0;
grade* now = nullptr;
if (fin.is_open())
{
while (!fin.eof())
{
now = new grade;
fin >> now->number;
fin >> now->name;
fin >> now->chinese;
fin >> now->english;
fin >> now->math;
now->next = nullptr;
if (p->head == nullptr)
{
p->head = now;
}
else
{
p->tail->next = now;
}
p->tail = now;
(p->cnt)++;
}
fin.close();
}
else
cout << "打不开文件!";
}
void 打印成绩(point* p)
{
grade* now = p->head;
while (now)
{
cout << now->number << "\t";
cout << now->name << "\t\t";
cout << now->chinese << "\t";
cout << now->math << "\t";
cout << now->english << endl;
now = now->next;
}
cout << "共有" << p->cnt << "名学生。\n";
}
void 释放空间(point* p)
{
grade* now = p->head, * tmp = nullptr;
while (now)
{
tmp = now;
now = now->next;
delete tmp;
}
}
void 链表排序(point* p)
{
p->target1 = p->target2 = p->head;
while (p->target1)
{
p->tmp2 = p->target1;
p->target2 = p->target1->next;
p->tt = p->target1;
搜索目标(p);
if (p->target2!=p->target1)
{
if (p->target1->next == p->target2)
{
if (p->target1 == p->head)
{
p->target1->next = p->target2->next;
p->target2->next = p->target1;
p->head = p->target2;
}
else
{
p->tmp1->next = p->target2;
p->target1->next = p->target2->next;
p->target2->next = p->target1;
}
}
else
{
if (p->target1 == p->head)
{
p->target2->next = p->target1->next;
p->target1->next = p->now;
p->tmp2->next = p->target1;
p->head = p->target2;
}
else
{
p->target2->next = p->target1->next;
p->tmp1->next = p->target2;
p->tmp2->next = p->target1;
p->target1->next = p->now;
}
}
p->now = p->target1;
p->target1 = p->target2;
p->target2 = p->now;
}
p->tmp1 = p->target1;
p->target1 = p->target1->next;
}

}
void 搜索目标(point* p)
{
while (p->target2)
{
if (p->tt->chinese < p->target2->chinese)
{
p->now = p->tmp2;
p->tt = p->target2;
}
p->tmp2 = p->tmp2->next;
p->target2 = p->target2->next;
}
p->target2 = p->tt;
p->tmp2 = p->now;
p->now = p->target2->next;
}

2048小游戏

基于easyx,这是我做的第一个游戏

1.2048字符版

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<graphics.h>
int main()
{
using namespace std;
int a[6][6], 备份[6][6];
int i, j, k;
srand((int)time(0));
//初始化数组a,并且随机产生两个2
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
a[i][j] = 0;
备份[i][j] = 0;
}
}
for (i = 0;i < 6;i++)
{
a[i][0] = 5;
a[i][5] = 5;
a[0][i] = 5;
a[5][i] = 5;
}
int i1, i2, j1, j2;
do {
i1 = rand() % 4 + 1;
i2 = rand() % 4 + 1;
j1 = rand() % 4 + 1;
j2 = rand() % 4 + 1;
} while (i1 == i2 && j1 == j2);
a[i1][j1] = 2;
a[i2][j2] = 2;
//打印初始数组
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
cout << a[i][j]<<"\t";
}
cout << endl;
}
cout << endl;
//键盘输入,后,随机一个格子生成一个数字
int ch, ch1, flag, tmp = 0, 为了符合规则[6][6];// 4 4 2 2 动过的不再动
while (1)
{
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
为了符合规则[i][j] = 0;
}
}
if (_kbhit)
{
ch1 = _getch();
ch = _getch();
//右移
if ((ch1 == 224 && ch == 77) || (ch == 228 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 3;j >= 1;j--)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j + 1;k <= 4;k++)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
//交换aij和aiflag;
if (a[i][flag + 1] == a[i][j]&&为了符合规则[i][flag+1]==0)
{
a[i][flag + 1] *= 2;
a[i][j] = 0;
为了符合规则[i][flag + 1] = 1;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
//下移
if ((ch1 == 224 && ch == 80) || (ch == 243 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 3;i >= 1;i--)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i + 1;k <=4;k++)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag + 1][j] == a[i][j] && 为了符合规则[flag + 1][j] == 0)
{
a[flag + 1][j] *= 2;
为了符合规则[flag + 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//上移
if ((ch1 == 224 && ch == 72) || (ch == 247 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 2;i <= 4;i++)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i - 1;k >= 1;k--)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag - 1][j] == a[i][j] && 为了符合规则[flag - 1][j] == 0)
{
a[flag - 1][j] *= 2;
为了符合规则[flag - 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//左移
if ((ch1 == 224 && ch == 75) || (ch == 225 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 2;j <= 4;j++)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j - 1;k >= 1;k--)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[i][flag - 1] == a[i][j] && 为了符合规则[i][flag - 1] == 0)
{
a[i][flag - 1] *= 2;
为了符合规则[i][flag - 1] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
}
//空余位置随机生成一个2或者4
int i3, j3, kk;
do
{
i3 = rand() % 4 + 1;
j3 = rand() % 4 + 1;
} while (a[i3][j3] != 0);
kk = rand() % 2;
if (kk == 1)
a[i3][j3] = 2;
else
a[i3][j3] = 4;

//打印a数组
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}
}

2.2048图像版

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<graphics.h>
void 初始化屏幕()
{
initgraph(1000, 1000);
setbkcolor(WHITE);
cleardevice();
int i, j;
setcolor(BLACK);
setfillcolor(YELLOW);
for (i = 100;i < 500;i+=100)
{
for (j = 100;j < 500;j+=100)
{
rectangle(i, j, 100+i, j+100);
fillrectangle(i, j, 100 + i, j + 100);
}
}
settextstyle(25, 25, _T("隶书"));
setbkmode(TRANSPARENT);
BeginBatchDraw();
}
int main()
{
using namespace std;
初始化屏幕();
int a[6][6], 备份[6][6];
int i, j, k;
srand((int)time(0));
//初始化数组a,并且随机产生两个2
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
a[i][j] = 0;
备份[i][j] = 0;
}
}
for (i = 0;i < 6;i++)
{
a[i][0] = 5;
a[i][5] = 5;
a[0][i] = 5;
a[5][i] = 5;
}
int i1, i2, j1, j2;
do {
i1 = rand() % 4 + 1;
i2 = rand() % 4 + 1;
j1 = rand() % 4 + 1;
j2 = rand() % 4 + 1;
} while (i1 == i2 && j1 == j2);
a[i1][j1] = 2;
a[i2][j2] = 2;
//打印初始数组
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
if (a[i][j] != 0)
{
TCHAR s[5];
_stprintf_s(s, _T("%d"), a[i][j]);
/*outtextxy(j * 100 + 25, i * 100 + 25, s);*/
RECT r = { 100 * j, 100 * i, 100 + 100 * j, 100 + 100 * i };
drawtext(s, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
}
FlushBatchDraw();
//键盘输入,后,随机一个格子生成一个数字
int ch, ch1, flag, tmp = 0, 为了符合规则[6][6],旗子;// 4 4 2 2 动过的不再动
while (1)
{
旗子 = 1;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
备份[i][j] = a[i][j];
}
}
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
为了符合规则[i][j] = 0;
}
}
if (_kbhit)
{
ch1 = _getch();
ch = _getch();
//右移
if ((ch1 == 224 && ch == 77) || (ch == 228 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 3;j >= 1;j--)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j + 1;k <= 4;k++)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
//交换aij和aiflag;
if (a[i][flag + 1] == a[i][j] && 为了符合规则[i][flag + 1] == 0)
{
a[i][flag + 1] *= 2;
a[i][j] = 0;
为了符合规则[i][flag + 1] = 1;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
//下移
if ((ch1 == 224 && ch == 80) || (ch == 243 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 3;i >= 1;i--)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i + 1;k <= 4;k++)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag + 1][j] == a[i][j] && 为了符合规则[flag + 1][j] == 0)
{
a[flag + 1][j] *= 2;
为了符合规则[flag + 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//上移
if ((ch1 == 224 && ch == 72) || (ch == 247 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 2;i <= 4;i++)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i - 1;k >= 1;k--)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag - 1][j] == a[i][j] && 为了符合规则[flag - 1][j] == 0)
{
a[flag - 1][j] *= 2;
为了符合规则[flag - 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//左移
if ((ch1 == 224 && ch == 75) || (ch == 225 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 2;j <= 4;j++)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j - 1;k >= 1;k--)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[i][flag - 1] == a[i][j] && 为了符合规则[i][flag - 1] == 0)
{
a[i][flag - 1] *= 2;
为了符合规则[i][flag - 1] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
}
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
if (备份[i][j] == a[i][j])
旗子 = 1;
else
{
旗子 = 0;
break;
}
}
if (旗子 == 0)
break;
}
//空余位置随机生成一个2或者4
int i3, j3, kk;
if (旗子 == 0)
{
do
{
i3 = rand() % 4 + 1;
j3 = rand() % 4 + 1;
} while (a[i3][j3] != 0);
kk = rand() % 2;
if (kk == 1)
a[i3][j3] = 2;
else
a[i3][j3] = 4;
}
cleardevice();
setcolor(BLACK);
setfillcolor(YELLOW);
for (i = 100;i < 500;i += 100)
{
for (j = 100;j < 500;j += 100)
{
rectangle(i, j, 100 + i, j + 100);
fillrectangle(i, j, 100 + i, j + 100);
}
}
//打印a数组
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
if (a[i][j] != 0)
{
TCHAR s[5];
_stprintf_s(s, _T("%d"), a[i][j]);
/*outtextxy(j * 100 + 25, i * 100 + 25, s);*/
RECT r = { 100 * j, 100 * i, 100 + 100 * j, 100 + 100 * i };
drawtext(s, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
}
FlushBatchDraw();
}
return 0;
}

3.2048最终版

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<graphics.h>
void 初始化屏幕()
{
initgraph(1000, 1000);
setbkcolor(WHITE);
cleardevice();
int i, j;
setcolor(BLACK);
setfillcolor(YELLOW);
for (i = 100;i < 500;i += 100)
{
for (j = 100;j < 500;j += 100)
{
rectangle(i, j, 100 + i, j + 100);
fillrectangle(i, j, 100 + i, j + 100);
}
}
settextstyle(30, 20, _T("隶书"));
setbkmode(TRANSPARENT);
/*settextcolor(BLUE);*/
BeginBatchDraw();
}
void 初始化数组a(int a[6][6],int 备份[6][6])
{
int i, j;
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
a[i][j] = 0;
备份[i][j] = 0;
}
}
for (i = 0;i < 6;i++)
{
a[i][0] = 5;
a[i][5] = 5;
a[0][i] = 5;
a[5][i] = 5;
}
int i1, i2, j1, j2;
do {
i1 = rand() % 4 + 1;
i2 = rand() % 4 + 1;
j1 = rand() % 4 + 1;
j2 = rand() % 4 + 1;
} while (i1 == i2 && j1 == j2);
a[i1][j1] = 2;
a[i2][j2] = 2;
}
void 打印a数组(int a[6][6])
{
int i, j;
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
if (a[i][j] != 0)
{
TCHAR s[5];
_stprintf_s(s, _T("%d"), a[i][j]);
/*outtextxy(j * 100 + 25, i * 100 + 25, s);*/
RECT r = { 100 * j, 100 * i, 100 + 100 * j, 100 + 100 * i };
drawtext(s, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
}
FlushBatchDraw();
}
void 备份并初始化计数器(int a[6][6],int 计数器[6][6], int 备份[6][6])
{
int i,j;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
备份[i][j] = a[i][j];
}
}
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
计数器[i][j] = 0;
}
}
}
void 键盘控制和游戏规则(int a[6][6],int 计数器[6][6])
{
int ch, ch1, flag, tmp = 0,i,j,k;
if (_kbhit)
{
ch1 = _getch();
ch = _getch();
//右移
if ((ch1 == 224 && ch == 77) || (ch == 228 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 3;j >= 1;j--)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j + 1;k <= 4;k++)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
//交换aij和aiflag;
if (a[i][flag + 1] == a[i][j] && 计数器[i][flag + 1] == 0)
{
a[i][flag + 1] *= 2;
a[i][j] = 0;
计数器[i][flag + 1] = 1;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
//下移
if ((ch1 == 224 && ch == 80) || (ch == 243 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 3;i >= 1;i--)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i + 1;k <= 4;k++)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag + 1][j] == a[i][j] && 计数器[flag + 1][j] == 0)
{
a[flag + 1][j] *= 2;
计数器[flag + 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//上移
if ((ch1 == 224 && ch == 72) || (ch == 247 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 2;i <= 4;i++)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i - 1;k >= 1;k--)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag - 1][j] == a[i][j] && 计数器[flag - 1][j] == 0)
{
a[flag - 1][j] *= 2;
计数器[flag - 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//左移
if ((ch1 == 224 && ch == 75) || (ch == 225 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 2;j <= 4;j++)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j - 1;k >= 1;k--)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[i][flag - 1] == a[i][j] && 计数器[i][flag - 1] == 0)
{
a[i][flag - 1] *= 2;
计数器[i][flag - 1] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
}
}
int 数据备份并判断有无变化(int a[6][6],int 备份[6][6],int 旗子)
{
int i, j;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
if (备份[i][j] == a[i][j])
旗子 = 1;
else
{
旗子 = 0;
break;
}
}
if (旗子 == 0)
break;
}
return 旗子;
}
void 空余位置随机生成一个2或者4(int a[6][6],int 旗子)
{
int i3, j3, kk;
if (旗子 == 0)
{
do
{
i3 = rand() % 4 + 1;
j3 = rand() % 4 + 1;
} while (a[i3][j3] != 0);
kk = rand() % 2;
if (kk == 1)
a[i3][j3] = 2;
else
a[i3][j3] = 4;
}
}
void 重新绘制屏幕()
{
int i, j;
cleardevice();
setcolor(BLACK);
setfillcolor(YELLOW);
for (i = 100;i < 500;i += 100)
{
for (j = 100;j < 500;j += 100)
{
rectangle(i, j, 100 + i, j + 100);
fillrectangle(i, j, 100 + i, j + 100);
}
}
FlushBatchDraw();
}
int main()
{
using namespace std;
初始化屏幕();
int a[6][6], 备份[6][6],计数器[6][6], 旗子;
srand((int)time(0));
初始化数组a(a, 备份);
打印a数组(a);
while (1)
{
旗子 = 1;
备份并初始化计数器(a, 计数器, 备份);
键盘控制和游戏规则(a, 计数器);
旗子 = 数据备份并判断有无变化(a, 备份, 旗子);
空余位置随机生成一个2或者4(a,旗子);
重新绘制屏幕();
打印a数组(a);
}
return 0;
}

4.2048优化版

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<graphics.h>
#include<cmath>
int 判断aij是2的几次方(int tt)
{
int i, sum = 1, cnt = 0;
if (tt == 0)
return 0;
for (i = 1;;i++)
{
sum *= 2;
cnt++;
if (sum == tt)
break;
}
return cnt;
}
void 初始化屏幕(int a[6][6])
{
initgraph(1000, 1000);
setbkcolor(WHITE);
cleardevice();
int i, j,tt,cnt;
setcolor(WHITE);
settextcolor(RED);
for (i = 1;i < 5;i ++)
{
for (j = 1;j < 5;j ++)
{
tt = a[i][j];
cnt=判断aij是2的几次方(tt);
setfillcolor(RGB(100, 255, 20 * cnt));
rectangle(j * 100, i * 100, 100 + j * 100, i * 100 + 100);
fillrectangle(j*100, i*100, 100 + j*100, i*100 + 100);
}
}
settextstyle(30, 20, _T("隶书"));
setbkmode(TRANSPARENT);
/*settextcolor(BLUE);*/
BeginBatchDraw();
}
void 初始化数组a(int a[6][6], int 备份[6][6])
{
int i, j;
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
a[i][j] = 0;
备份[i][j] = 0;
}
}
for (i = 0;i < 6;i++)
{
a[i][0] = 5;
a[i][5] = 5;
a[0][i] = 5;
a[5][i] = 5;
}
int i1, i2, j1, j2;
do {
i1 = rand() % 4 + 1;
i2 = rand() % 4 + 1;
j1 = rand() % 4 + 1;
j2 = rand() % 4 + 1;
} while (i1 == i2 && j1 == j2);
a[i1][j1] = 2;
a[i2][j2] = 2;
}
void 打印a数组(int a[6][6])
{
int i, j;
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
if (a[i][j] != 0)
{
TCHAR s[5];
_stprintf_s(s, _T("%d"), a[i][j]);
/*outtextxy(j * 100 + 25, i * 100 + 25, s);*/
RECT r = { 100 * j, 100 * i, 100 + 100 * j, 100 + 100 * i };
drawtext(s, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
}
FlushBatchDraw();
}
void 备份并初始化计数器(int a[6][6], int 计数器[6][6], int 备份[6][6])
{
int i, j;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
备份[i][j] = a[i][j];
}
}
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
计数器[i][j] = 0;
}
}
}
void 键盘控制和游戏规则(int a[6][6], int 计数器[6][6])
{
int ch, ch1, flag, tmp = 0, i, j, k;
if (_kbhit)
{
ch1 = _getch();
ch = _getch();
//右移
if ((ch1 == 224 && ch == 77) || (ch == 228 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 3;j >= 1;j--)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j + 1;k <= 4;k++)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
//交换aij和aiflag;
if (a[i][flag + 1] == a[i][j] && 计数器[i][flag + 1] == 0)
{
a[i][flag + 1] *= 2;
a[i][j] = 0;
计数器[i][flag + 1] = 1;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
//下移
if ((ch1 == 224 && ch == 80) || (ch == 243 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 3;i >= 1;i--)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i + 1;k <= 4;k++)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag + 1][j] == a[i][j] && 计数器[flag + 1][j] == 0)
{
a[flag + 1][j] *= 2;
计数器[flag + 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//上移
if ((ch1 == 224 && ch == 72) || (ch == 247 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 2;i <= 4;i++)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i - 1;k >= 1;k--)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag - 1][j] == a[i][j] && 计数器[flag - 1][j] == 0)
{
a[flag - 1][j] *= 2;
计数器[flag - 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//左移
if ((ch1 == 224 && ch == 75) || (ch == 225 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 2;j <= 4;j++)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j - 1;k >= 1;k--)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[i][flag - 1] == a[i][j] && 计数器[i][flag - 1] == 0)
{
a[i][flag - 1] *= 2;
计数器[i][flag - 1] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
}
}
int 数据备份并判断有无变化(int a[6][6], int 备份[6][6], int 旗子)
{
int i, j;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
if (备份[i][j] == a[i][j])
旗子 = 1;
else
{
旗子 = 0;
break;
}
}
if (旗子 == 0)
break;
}
return 旗子;
}
void 空余位置随机生成一个2或者4(int a[6][6], int 旗子)
{
int i3, j3, kk;
if (旗子 == 0)
{
do
{
i3 = rand() % 4 + 1;
j3 = rand() % 4 + 1;
} while (a[i3][j3] != 0);
kk = rand() % 2;
if (kk == 1)
a[i3][j3] = 2;
else
a[i3][j3] = 4;
}
}
void 重新绘制屏幕(int a[6][6])
{
int i, j,tt,cnt;
cleardevice();
setcolor(WHITE);
settextcolor(RED);
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
tt = a[i][j];
cnt = 判断aij是2的几次方(tt);
setfillcolor(RGB(100, 255, 20 * cnt));
rectangle(j * 100, i * 100, 100 + j * 100, i * 100 + 100);
fillrectangle(j * 100, i * 100, 100 + j * 100, i * 100 + 100);
}
}
FlushBatchDraw();
}
int main()
{
using namespace std;
int a[6][6], 备份[6][6], 计数器[6][6], 旗子;
srand((int)time(0));
初始化数组a(a, 备份);
初始化屏幕(a);
打印a数组(a);
while (1)
{
旗子 = 1;
备份并初始化计数器(a, 计数器, 备份);
键盘控制和游戏规则(a, 计数器);
旗子 = 数据备份并判断有无变化(a, 备份, 旗子);
空余位置随机生成一个2或者4(a, 旗子);
重新绘制屏幕(a);
打印a数组(a);
}
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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#include<cstdio>
#include<cstdlib>
#include<time.h>
#include<graphics.h>
#include<conio.h>
#include<fstream>
#include<string>
#include<iostream>
double** 生成矩阵(double** a, int* r, int* c);
double 计算行列式(double** a, int n);
void 打开菜单();
int 执行程序();
void 写入文件(double** a, int r1, int c1, double** d, int r2, int c2, std::string name);
void 写入逆矩阵(double** a, double** d, int r, std::string name);
void 写入行列式(double** a, int r, double ss);
void 写入秩(double** a, int r1, int c1, int 秩);
void 写入加减法(double** a, int r1, int c1, double** b, double** d, int r, int c, int sign);
void 写入乘法(double** a, int r1, int c1, double** b, int r2, int c2, double** d);
void 读取文件();
void 矩阵转置(double** a, int r, int c);
void 矩阵加减法(double** a, double** b, int r1, int c1, int r2, int c2, int sign);
void 矩阵数乘(double** a, int r, int c);
void 矩阵乘法(double** a, double** b, int r1, int c1, int r2, int c2);
int 矩阵化简(double** a, int r, int c, int sign);
void 矩阵求逆(double** a, int r);
void 打印d数组(double** d, int r, int c);
void 打印d数组(double** d, int r);
void 内存释放(double** d, int r);
int main()
{
int 开始; do { 打开菜单(); 开始 = 执行程序(); } while (开始 == 0);
}
void 矩阵转置(double** a, int r, int c)
{
double** d = NULL;
d = new double* [c];
for (int i = 0; i < c; i++) { d[i] = new double[r]; }
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
d[j][i] = a[i][j];
}
}
打印d数组(d, c, r);
写入文件(a, r, c, d, c, r, "的转置为:");
内存释放(d, c);
}
void 矩阵加减法(double** a, double** b, int r1, int c1, int r2, int c2, int sign)
{
if (r1 == r2 && c1 == c1)
{
double** d = NULL;
d = new double* [r1];
for (int i = 0; i < r1; i++) { d[i] = new double[c1]; }
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
d[i][j] = a[i][j] + b[i][j] * sign;
}
}
打印d数组(d, r1, c1);
写入加减法(a, r1, c1, b, d, r1, c1, sign);
内存释放(d, r1);
}
else
printf("这两个矩阵不可以这样运算!");
}
void 矩阵数乘(double** a, int r, int c)
{
double** d = NULL;
d = new double* [r];
for (int i = 0; i < r; i++) { d[i] = new double[c]; }
int k;
printf("请输入数值:");
scanf_s("%d", &k);
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
d[i][j] = k * a[i][j];
}
}
打印d数组(d, r, c);
std::to_string(k);
std::string name = "数乘" + std::to_string(k) + "的结果为:";
写入文件(a, r, c, d, r, c, name);
内存释放(d, r);
}
void 矩阵乘法(double** a, double** b, int r1, int c1, int r2, int c2)
{
if (r2 == c1)
{
double** d = NULL, sum;
d = new double* [r1];
for (int i = 0; i < r1; i++) { d[i] = new double[c2]; }
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
sum = 0;
for (int k = 0; k < c1; k++)
{
sum += a[i][k] * b[k][j];
}
d[i][j] = sum;
}
}
打印d数组(d, r1, c2);
写入乘法(a, r1, c1, b, r2, c2, d);
内存释放(d, r1);
}
else
printf("这两个矩阵不能相乘!");
}
int 矩阵化简(double** a, int r, int c, int sign)
{
double** d;
d = new double* [r];
for (int i = 0; i < r; i++) { d[i] = new double[c]; }
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
d[i][j] = a[i][j];
}
}
//起始行列
int cnt = 0;
int cct = 0;
int flag;
double t;
while (cnt <= r && cct <= c)
{
flag = -1;
for (int i = cnt; i < r; i++)
{
if (d[i][cct] != 0)
{
flag = i;
break;
}
}
if (flag == -1)
{
cct++;
continue;
}
//交换两行;
if (flag != cnt)
{
double tmp = 0;
for (int j = cct; j < c; j++)
{
tmp = d[flag][j];
d[flag][j] = d[cnt][j];
d[cnt][j] = tmp;
}
}
//化第一行首个为1
t = d[cnt][cct];
for (int j = cct; j < c; j++)
{
d[cnt][j] /= t;
}
for (int i = cnt + 1; i < r; i++)
{
if (d[i][cct] != 0)
{
t = d[i][cct];
for (int j = cct; j < c; j++)
{
d[i][j] = d[i][j] - d[cnt][j] * t;
}
}
}
cnt++;
cct++;
}
for (int i = r - 1; i > 0; i--)
{
flag = -1;
for (int j = 0; j < c; j++)
{
if (d[i][j] != 0)
{
flag = j;
break;
}
}
if (flag != -1)
{
for (int k = i - 1; k >= 0; k--)
{
t = d[k][flag];
for (int j = 0; j < c; j++)
{
d[k][j] = d[k][j] - t * d[i][j];
}
}
}
}
int number = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (d[i][j] != 0)
{
number++;
break;
}
}
}
if (sign == 0)
{

打印d数组(d, r, c);
写入文件(a, r, c, d, r, c, "的化简结果为:");
}
else
{
打印d数组(d, r);
写入逆矩阵(a, d,r, "的逆矩阵为: ");
}

内存释放(d, r);
return number;
}
void 矩阵求逆(double** a, int r)
{
if (计算行列式(a, r) != 0)
{
double** d;
d = new double* [r];
for (int i = 0; i < r; i++) { d[i] = new double[2 * r]; }
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
d[i][j] = a[i][j];
}
for (int j = r; j < 2 * r; j++)
{
if (i == j - r)
d[i][j] = 1;
else
d[i][j] = 0;
}
}
矩阵化简(d, r, 2 * r, 1);
内存释放(d, r);
}
else
printf("这个矩阵不可逆!");

}
void 打印d数组(double** d, int r, int c)
{
printf("结果如下:\n");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if ((int)(d[i][j] * 100) % 100 == 0)
printf("%d\t", (int)d[i][j]);
else
printf("%.2lf\t", d[i][j]);
}
printf("\n");
}
}
void 打印d数组(double** d, int r)
{
printf("结果如下:\n");
for (int i = 0; i < r; i++)
{
for (int j = r; j < 2 * r; j++)
{
if ((int)(d[i][j] * 100) % 100 == 0)
printf("%d\t", (int)d[i][j]);
else
printf("%.2lf\t", d[i][j]);
}
printf("\n");
}
}
double 计算行列式(double** a, int n)
{
double** d;
d = new double* [n];
for (int i = 0; i < n; i++) { d[i] = new double[n]; }
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
d[j][i] = a[i][j];
}
}
int flag = -1;
for (int i = 0; i < n; i++)
{
if (d[i][0] != 0)
{
flag = i;
break;
}
}
if (flag != 0)
{
double tmp = 0;
for (int j = 0; j < n; j++)
{
tmp = d[flag][j];
d[flag][j] = d[0][j];
d[0][j] = tmp;
}
}
double tmp;
for (int k = 1; k < n; k++)
{
for (int i = k; i < n; i++)
{
tmp = d[i][k - 1] / d[k - 1][k - 1];
for (int j = k - 1; j < n; j++)
{
d[i][j] = d[i][j] - tmp * d[k - 1][j];
}
}
}
double ss = 1;
for (int i = 0; i < n; i++)
{
ss *= d[i][i];
}
写入行列式(a, n, ss);
内存释放(d, n);
return ss;
}
double** 生成矩阵(double** a, int* r, int* c)
{
printf("请输入矩阵的行数,列数:");
scanf_s("%d%d", r, c);
printf("请输入矩阵:\n");
a = new double* [*r];
for (int i = 0; i < *r; i++) { a[i] = new double[*c]; }
for (int i = 0; i < *r; i++)
{
for (int j = 0; j < *c; j++)
{
scanf_s("%lf", &a[i][j]);
}
}
return a;
}
void 打开菜单()
{
printf("=======================================================================\n");
printf("| ----------1|我要求矩阵的加法 ----------- |\n");
printf("| ----------2|我要求矩阵的减法 ----------- |\n");
printf("| ----------3|我要求矩阵的转置 ----------- |\n");
printf("| ----------4|我要求矩阵的逆矩阵----------- |\n");
printf("| ----------5|我要求最简行矩阵 ----------- |\n");
printf("| ----------6|我要求矩阵的数乘 ----------- |\n");
printf("| ----------7|我要求矩阵的乘法 ----------- |\n");
printf("| ----------8|我要求矩阵的秩 ----------- |\n");
printf("| ----------9|我要计算行列式的值----------- |\n");
printf("| ----------0|读取计算结果文件 ----------- |\n");
printf("| ----------QAQ|输入字母可以退出----------- |\n");
printf("=======================================================================\n");
}
int 执行程序()
{
double** a = NULL, ** b = NULL;
int r1, c1, r2, c2;
char ch;
switch (ch = getchar())
{
case'1':
{
a = 生成矩阵(a, &r1, &c1);
b = 生成矩阵(b, &r2, &c2);
矩阵加减法(a, b, r1, c1, r2, c2, 1);
内存释放(a, r1);
内存释放(b, r2);
break;
}
case'2':
{
a = 生成矩阵(a, &r1, &c1);
b = 生成矩阵(b, &r2, &c2);
矩阵加减法(a, b, r1, c1, r2, c2, -1);
内存释放(a, r1);
内存释放(b, r2);
break;
}
case'3':
{
a = 生成矩阵(a, &r1, &c1);
矩阵转置(a, r1, c1);
内存释放(a, r1);
break;
}
case'4':
{
a = 生成矩阵(a, &r1, &c1);
矩阵求逆(a, r1);
内存释放(a, r1);
break;
}
case'5':
{
a = 生成矩阵(a, &r1, &c1);
矩阵化简(a, r1, c1, 0);
内存释放(a, r1);
break;
}
case'6':
{
a = 生成矩阵(a, &r1, &c1);
矩阵数乘(a, r1, c1);
内存释放(a, r1);
break;
}
case'7':
{
a = 生成矩阵(a, &r1, &c1);
b = 生成矩阵(b, &r2, &c2);
矩阵乘法(a, b, r1, c1, r2, c2);
内存释放(a, r1);
内存释放(b, r2);
break;
}
case'8':
{
a = 生成矩阵(a, &r1, &c1);
int 秩;
秩 = 矩阵化简(a, r1, c1, 0);
printf("矩阵的秩为:%d\n", 秩);
内存释放(a, r1);
break;
}
case'9':
{
a = 生成矩阵(a, &r1, &c1);
double ss = 计算行列式(a, r1);
if ((int)(ss * 100) % 100 == 0)
printf("%d\t", (int)ss);
else
printf("%.2lf\t", ss);
内存释放(a, r1);
break;
}
case '0':
{
读取文件();
break;
}
default:
{
return -1;
}
}
getchar();
return 0;
}
void 内存释放(double** d, int r)
{
for (int i = 0; i < r; i++) { delete[]d[i]; }
delete[]d;
}
void 写入文件(double** a, int r1, int c1, double** d, int r2, int c2,std::string name)
{
using namespace std;
string 文件名;
文件名 = "计算结果.txt";
ofstream fout(文件名.c_str(),ios_base::out|ios_base::app);
fout << "矩阵:" << endl;
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
if ((int)(a[i][j] * 100) % 100 == 0)
fout << (int)a[i][j] << "\t";
else
{
fout << a[i][j] << "\t";
}
}
fout << endl;
}
fout << name << endl;
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
if ((int)(d[i][j] * 100) % 100 == 0)
fout << (int)d[i][j] << "\t";
else
fout << d[i][j] << "\t";
}
fout << endl;
}

}
void 写入逆矩阵(double** a, double** d,int r, std::string name)
{
using namespace std;
string 文件名;
文件名 = "计算结果.txt";
ofstream fout(文件名.c_str(), ios_base::out | ios_base::app);
fout << "矩阵:" << endl;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
if ((int)(a[i][j] * 100) % 100 == 0)
fout << (int)a[i][j] << "\t";
else
{
fout << a[i][j] << "\t";
}
}
fout << endl;
}
fout << name << endl;
for (int i = 0; i < r; i++)
{
for (int j = r; j < 2*r; j++)
{
if ((int)(d[i][j] * 100) % 100 == 0)
fout << (int)d[i][j] << "\t";
else
{
fout << d[i][j] << "\t";
}
}
fout << endl;
}

}
void 写入行列式(double** a, int r,double ss)
{
using namespace std;
string 文件名;
文件名 = "计算结果.txt";
ofstream fout(文件名.c_str(), ios_base::out | ios_base::app);
fout << "矩阵:" << endl;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
if ((int)(a[i][j] * 100) % 100 == 0)
fout << (int)a[i][j] << "\t";
else
{
fout << a[i][j] << "\t";
}
}
fout << endl;
}
fout << "的行列式的值为:" << ss << endl;
}
void 写入秩(double** a, int r1, int c1, int 秩)
{
using namespace std;
string 文件名;
文件名 = "计算结果.txt";
ofstream fout(文件名.c_str(), ios_base::out | ios_base::app);
fout << "矩阵:" << endl;
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < 1; j++)
{
if ((int)(a[i][j] * 100) % 100 == 0)
fout << (int)a[i][j] << "\t";
else
{
fout << a[i][j] << "\t";
}
}
fout << endl;
}
fout << "的秩为:" << 秩 << endl;
}
void 写入加减法(double** a, int r1, int c1, double** b,double**d,int r,int c, int sign)
{
using namespace std;
string 文件名;
文件名 = "计算结果.txt";
ofstream fout(文件名.c_str(), ios_base::out | ios_base::app);
fout << "矩阵:" << endl;
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
if ((int)(a[i][j] * 100) % 100 == 0)
fout << (int)a[i][j] << "\t";
else
{
fout << a[i][j] << "\t";
}
}
fout << endl;
}
if (sign == 1)
{
fout << "加矩阵:" << endl;
}
else
{
fout << "减矩阵:" << endl;
}
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
if ((int)(b[i][j] * 100) % 100 == 0)
fout << (int)b[i][j] << "\t";
else
fout << b[i][j] << "\t";
}
fout << endl;
}
fout << "的结果为:" << endl;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if ((int)(d[i][j] * 100) % 100 == 0)
fout << (int)d[i][j] << "\t";
else
fout << d[i][j] << "\t";
}
fout << endl;
}
}
void 写入乘法(double**a,int r1,int c1,double**b,int r2,int c2,double**d)
{
using namespace std;
string 文件名;
文件名 = "计算结果.txt";
ofstream fout(文件名.c_str(), ios_base::out | ios_base::app);
fout << "矩阵:" << endl;
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
if ((int)(a[i][j] * 100) % 100 == 0)
fout << (int)a[i][j] << "\t";
else
{
fout << a[i][j] << "\t";
}
}
fout << endl;
}
fout << "乘矩阵:" << endl;
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
if ((int)(b[i][j] * 100) % 100 == 0)
fout << (int)b[i][j] << "\t";
else
fout << b[i][j] << "\t";
}
fout << endl;
}
fout << "的结果为:" << endl;
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
if ((int)(d[i][j] * 100) % 100 == 0)
fout << (int)d[i][j] << "\t";
else
fout << d[i][j] << "\t";
}
fout << endl;
}
}
void 读取文件()
{
using namespace std;
char ch;
string 文件名;
文件名 = "计算结果.txt";
ifstream fin(文件名);
if (fin.is_open())
{
while (fin.get(ch))
{
cout << ch;
}
fin.close();
}
}