工程实践作业报告

写这篇文章也就是对大一的一个总结吧,之前一直在准备期末考试和交各种报告什么的,也没时间搞这些,关于后期打算就是,学习数据结构,c++,java,了解web相关的知识,然后有空刷刷题,打打比赛吧。

工程实践报告

贪吃蛇

一个经典的c程序,在看了别人代码后自己实现了下,编译环境dev。

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

#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<time.h>
#include<conio.h>

void gotoxy(int x, int y);
void gotoxy_print(int x, int y);
void gotoxy_delete(int x, int y);
void gotoxy_food(int x, int y);
void hellow();
void end();
void creat_frame();
void create_food();
void check_way();
int check_end();
void snake_move();
void eat_food();
void end_to_head(int x,int y);

typedef struct Snakes
{
int x;
int y;
struct Snakes* next;
}snake;


struct Food
{
int x;
int y;
}food;

snake *head;

char name[20];
char way=' ';
int score=0;

int main()
{
system("color 03");
hellow();
creat_frame();
create_food();
check_way();
gotoxy(0,32);
printf(" ");

}


void gotoxy(int x, int y)
{
// 更新光标位置
COORD pos;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOutput, pos);
// 隐藏光标
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
SetConsoleCursorInfo(hOutput, &cursor);
}


void hellow()
{
gotoxy(10,5);
printf("/***************************************/");
gotoxy(17,6);
printf("welocme to this game");
gotoxy(10,8);
printf("/***************************************/");
gotoxy(17,7);
printf("please input your name:");
scanf("%s",name);
system("cls");
}


void creat_frame()
{
int i;
snake *p,*q;
for(i=0;i<28;i++)
{
gotoxy_print(0,i);
gotoxy_print(66,i);
}
for(i=0;i<66;i+=2)
{
gotoxy_print(i,0);
gotoxy_print(i,27);
}
head = (snake*)malloc(sizeof(snake));
p=(snake*)malloc(sizeof(snake));
q=(snake*)malloc(sizeof(snake));
head->x=14;
head->y=16;
p->x=14;
p->y=14;
q->x=14;
q->y=15;
head->next=p;
p->next=q;
q->next=NULL;
}

void create_food()
{
int x,y;
int flag=0;

while(!flag)
{
flag=1;
srand((unsigned)time(NULL));
x =rand()%64+1;
y=rand()%26+1;

if (x % 2 !=0)
{
x = x + 1;
}
snake *mid;
mid=head;
while(mid->next!=NULL)
{
if((x==mid->x)&&(y==mid->y))
{
flag=0;
}
mid=mid->next;
}
food.x=x;
food.y=y;
}
gotoxy_food(food.x,food.y);

}
void check_way()
{

while(1)
{
if(check_end()==0)
{
end();
}
char just;
just=way;
if(_kbhit())
{
way = _getch();
}
if(just=='w'&&way=='s')
{
way='w';
}
if(just=='s'&&way=='w')
{
way='s';
}
if(just=='a'&&way=='d')
{
way='a';
}
if(just=='d'&&way=='a')
{
way='d';
}
snake_move();
eat_food();
}
}

void snake_move()
{
int x,y;
snake *mid=head;

x=head->x;
y=head->y;

while(mid->next!=NULL)
{
mid=mid->next;
}
gotoxy_delete(mid->x,mid->y);


switch(way)
{
case 'w':
y-=1;
break;
case 's':
y+=1;
break;
case 'a':
x-=2;
break;
case 'd':
x+=2;
break;
default:
break;
}
if(x != head->x || y != head->y)
{
end_to_head(x,y);
}
gotoxy_print(head->x, head->y);
Sleep(150-score*4);
}

void end_to_head(int x,int y)
{
snake *new_head;
snake *p=head;
while(p->next->next!=NULL)
{
p=p->next;
}
free(p->next);
p->next=NULL;
new_head=(snake*)malloc(sizeof(snake));
new_head->x=x;
new_head->y=y;
new_head->next=head;
head=new_head;
}

void eat_food()
{
snake *p=head;
if(head->x==food.x&&head->y==food.y)
{
create_food();
snake *new_one;
new_one=(snake*)malloc(sizeof(snake));
while(p->next!=NULL)
{
p=p->next;
}
new_one->next=NULL;
p->next=new_one;
score ++;
}

}

int check_end()
{
snake *p=head;

if(head->x==0||head->x==66||head->y==0||head->y==27)
{
return 0;
}

while(p->next!=NULL)
{
p=p->next;
if(head->x==p->x&&head->y==p->y)
{
return 0;
}
}
return 1;
}

void end()
{
snake *p=head,*q;
system("cls");
printf("end");
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
system("pause");
}

void gotoxy_print(int x, int y)
{
gotoxy(x,y);
printf("⊙");
}

void gotoxy_delete(int x, int y)
{
gotoxy(x,y);
printf(" ");
}
void gotoxy_food(int x, int y)
{
gotoxy(x,y);
printf("⊙");
}

然后想了想可以不可以做成双蛇,然后就继续写了写。

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

#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<time.h>
#include<conio.h>

typedef struct Snakes
{
int x;
int y;
struct Snakes* next;
}snake;


struct Food
{
int x;
int y;
}food;

void gotoxy(int x, int y);
void gotoxy_print(int x, int y);
void gotoxy_delete(int x, int y);
void gotoxy_food(int x, int y);
void gotoxy_snake1(int x,int y);
void gotoxy_snake2(int x,int y);
void hellow();
void end();
void no_back(char tmp);
void creat_frame();
void create_food();
void check_way();
int check_end();
void snake1_move();
void snake2_move();
void eat_food();
void end_to_head1(int x,int y);
void end_to_head2(int x,int y);



snake *head1;
snake *head2;

char name[20];
char way=' ';
char way1;
char way2;
int score1=0;
int score2=0;

int main()
{
int i;
system("color 03");
hellow();

gotoxy_delete(17,6);
creat_frame();
create_food();

check_way();

}


void gotoxy(int x, int y)
{
// 更新光标位置
COORD pos;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOutput, pos);
// 隐藏光标
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
SetConsoleCursorInfo(hOutput, &cursor);
}


void hellow()
{
gotoxy(10,5);
printf("/***************************************/");
gotoxy(17,6);
printf("welocme to this game");
gotoxy(10,10);
printf("/***************************************/");
gotoxy(17,7);
printf("snake1: up(w) down(s) left(a) right(d)");
gotoxy(17,8);
printf("snake1: up(i) down(k) left(j) right(l)");
gotoxy(17,9);
printf("please input your name:");
scanf("%s",name);
system("cls");
}


void creat_frame()
{
int i;
snake *p,*q;
snake *m,*n;
for(i=0;i<28;i++)
{
gotoxy_print(0,i);
gotoxy_print(66,i);
}
for(i=0;i<66;i+=2)
{
gotoxy_print(i,0);
gotoxy_print(i,27);
}
//snake1
head1 = (snake*)malloc(sizeof(snake));
p=(snake*)malloc(sizeof(snake));
q=(snake*)malloc(sizeof(snake));
head1->x=14;
head1->y=13;
p->x=14;
p->y=14;
q->x=14;
q->y=15;
head1->next=p;
p->next=q;
q->next=NULL;
gotoxy_snake1(14, 13);
gotoxy_snake1(14, 14);
gotoxy_snake1(14, 15);

//snake2
head2=(snake*)malloc(sizeof(snake));
m=(snake*)malloc(sizeof(snake));
n=(snake*)malloc(sizeof(snake));
head2->x=30;
head2->y=10;
m->x=30;
m->y=11;
n->x=30;
n->y=12;
head2->next=m;
m->next=n;
n->next=NULL;
gotoxy_snake2(30, 10);
gotoxy_snake2(30, 11);
gotoxy_snake2(30, 12);

}

void create_food()
{
int x,y;
int flag=0;

while(!flag)
{
flag=1;
srand((unsigned)time(NULL));
x =rand()%64+1;
y=rand()%26+1;

if (x % 2 !=0)
{
x = x + 1;
}
snake *p;
snake *q;

p=head1;
while(p->next!=NULL)
{
if((x==p->x)&&(y==p->y))
{
flag=0;
}
p=p->next;
}
q=head2;
while(q->next!=NULL)
{
if((x==q->x)&&(y==q->y))
{
flag=0;
}
q=q->next;
}
}
food.x=x;
food.y=y;
gotoxy_food(food.x,food.y);
}
void check_way()
{
int object;
while(1)
{
if(check_end()==0)
{
end();
}
char just1;
char just2;
if(way=='w'||way=='s'||way=='a'||way=='d')
{
just1=way;
}
if(way=='i'||way=='j'||way=='l'||way=='k')
{
just2=way;
}

if(_kbhit())
{
way = _getch();
}


if(way=='w'||way=='s'||way=='a'||way=='d')
{
no_back(just1);
way1=way;
}

if(way=='i'||way=='j'||way=='l'||way=='k')
{
no_back(just2);
way2=way;
}
snake1_move();
snake2_move();
eat_food();
}
}

void snake1_move()
{
int x1,y1;
snake *mid=head1;

x1=head1->x;
y1=head1->y;

while(mid->next!=NULL)
{
mid=mid->next;
}
gotoxy_delete(mid->x,mid->y);



switch(way1)
{
case 'w':
y1-=1;
break;
case 's':
y1+=1;
break;
case 'a':
x1-=2;
break;
case 'd':
x1+=2;
break;
default:
break;
}

if(x1 != head1->x || y1 != head1->y)
{
end_to_head1(x1,y1);
}
gotoxy_snake1(head1->x, head1->y);
Sleep(70-score1);
}

void snake2_move()
{
int x2,y2;
snake *p=head2;

x2=head2->x;
y2=head2->y;

while(p->next!=NULL)
{
p=p->next;
}
gotoxy_delete(p->x,p->y);

switch(way2)
{
case 'i':
y2-=1;
break;
case 'k':
y2+=1;
break;
case 'j':
x2-=2;
break;
case 'l':
x2+=2;
break;
default:
break;
}

if(x2 != head2->x || y2 != head2->y)
{
end_to_head2(x2,y2);
}
gotoxy_snake2(head2->x, head2->y);
Sleep(70-score2);
}

void end_to_head1(int x,int y)
{
snake *new_head1;
snake *p=head1;

while(p->next->next!=NULL)
{
p=p->next;
}
free(p->next);
p->next=NULL;
new_head1=(snake*)malloc(sizeof(snake));
new_head1->x=x;
new_head1->y=y;
new_head1->next=head1;
head1=new_head1;

}

void end_to_head2(int x,int y)
{
snake *new_head2;
snake *q=head2;

while(q->next->next!=NULL)
{
q=q->next;
}
free(q->next);
q->next=NULL;
new_head2=(snake*)malloc(sizeof(snake));
new_head2->x=x;
new_head2->y=y;
new_head2->next=head2;
head2=new_head2;
}

void eat_food()
{
snake *p=head1;
snake *q=head2;

if(head1->x==food.x&&head1->y==food.y)
{
create_food();
snake *new_one1;
new_one1=(snake*)malloc(sizeof(snake));
while(p->next!=NULL)
{
p=p->next;
}
new_one1->next=NULL;
p->next=new_one1;
score1 ++;
gotoxy(75,15);
printf("snked1 : %d",score1);
}

if(head2->x==food.x&&head2->y==food.y)
{
create_food();
snake *new_one2;
new_one2=(snake*)malloc(sizeof(snake));
while(q->next!=NULL)
{
q=q->next;
}
new_one2->next=NULL;
q->next=new_one2;
score2 ++;
gotoxy(75,16);
printf("snked1 : %d",score2);
}

}

int check_end()
{
snake *p=head1;
snake *q=head2;

if(head1->x==0||head1->x==66||head1->y==0||head1->y==27)
{
return 0;
}

while(p->next!=NULL)
{
p=p->next;
if(head1->x==p->x&&head1->y==p->y)
{
return 0;
}
}

if(head2->x==0||head2->x==66||head2->y==0||head2->y==27)
{
return 0;
}

while(q->next!=NULL)
{
q=q->next;
if(head2->x==q->x&&head2->y==q->y)
{
return 0;
}
}
p=head1;
q=head2;

while(q->next!=NULL)
{
q=q->next;
if(head1->x==q->x&&head1->y==q->y)
{
return 0;
}
}

while(p->next!=NULL)
{
p=p->next;
if(head2->x==p->x&&head2->y==p->y)
{
return 0;
}
}

return 1;
}

void end()
{
snake *p=head1,*q;
snake *m=head2,*n;
system("cls");
if(score1>score2)
{
gotoxy(10,10);
printf("snake1 win");
}
else
{
gotoxy(10,10);
printf("snake2 win");
}
printf("\n\n\n\n\n\n\n\n\n\n\n\n");
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
while(m!=NULL)
{
n=m->next;
free(p);
m=n;
}
system("pause");
}


void no_back(char tmp)
{
if(tmp=='w'&&way=='s')
{
way='w';
}
if(tmp=='s'&&way=='w')
{
way='s';
}
if(tmp=='a'&&way=='d')
{
way='a';
}
if(tmp=='d'&&way=='a')
{
way='d';
}

if(tmp=='i'&&way=='k')
{
way='i';
}
if(tmp=='k'&&way=='i')
{
way='k';
}
if(tmp=='j'&&way=='l')
{
way='j';
}
if(tmp=='l'&&way=='j')
{
way='l';
}
}

void gotoxy_print(int x, int y)
{
gotoxy(x,y);
printf("卐");
}

void gotoxy_snake1(int x,int y)
{
gotoxy(x,y);
printf("■");
}

void gotoxy_snake2(int x,int y)
{
gotoxy(x,y);
printf(" ");
}

void gotoxy_delete(int x, int y)
{
gotoxy(x,y);
printf(" ");
}
void gotoxy_food(int x, int y)
{
gotoxy(x,y);
printf("⊙");
}

简易计算器

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

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
#include<conio.h>
#include <conio.h>
#include <windows.h>
#include<dos.h>
double add(double a, double b) // 加法
{
return a+b;
}

double sub(double a, double b) // 减法
{
return a-b;
}

double mul(double a, double b) // 乘法
{
return a*b;
}

double chu(double a, double b) // 除法
{
return a/b;
}

void hellow();
void gotoxy(int x, int y) ;
int data_handling(char ch[], char num[100][100]);
double calculating_data(char num[100][100], int num_count);
void calculate();

int main()
{
system("color 03");
char out;
while(1)
{
system("cls");
hellow();
calculate();
printf("按任意键继续,按Q退出:");
scanf("%c", &out);
if(out=='Q')
{
break;
}

}
return 0;
}

void gotoxy(int x, int y)
{
// 更新光标位置
COORD pos;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
pos.X = x;
pos.Y = y;1
SetConsoleCursorPosition(hOutput, pos);
// 隐藏光标
// CONSOLE_CURSOR_INFO cursor;
// cursor.bVisible = FALSE;
// cursor.dwSize = sizeof(cursor);
// SetConsoleCursorInfo(hOutput, &cursor);
}


double calculating_data(char num[100][100], int num_count) // 数据计算
{
double sum = atof(num[0]);
int i, j;

for(j=0; i<num_count; i++)
{
if( strcmp(num[i], "**")==0 )//判断幂运算
{
sum = pow(atof(num[i-1]), atof(num[i+1]));
gcvt(sum, 50, num[i-1]);
j = i;
while( (j+2)<num_count )
{
strcpy(num[j], num[j+2]);
j++;
}
i--;
num_count -= 2;
}
}


for(i=0; i<num_count; i++)
{

if( (strcmp(num[i], "*")==0)||(strcmp(num[i], "/")==0) )
{
if( num[i][0]=='*' )
{
sum = mul(atof(num[i-1]), atof(num[i+1]));
gcvt(sum, 50, num[i-1]);
}
else
{
sum = chu(atof(num[i-1]), atof(num[i+1]));
gcvt(sum, 50, num[i-1]);
}
j = i;
while( (j+2)<num_count )
{
strcpy(num[j], num[j+2]);
j++;
}
i--;
num_count -= 2;
}
}

for(i=0; i<num_count; i++)
{
if( strcmp(num[i],"+")==0 )
{
sum = add(atof(num[i-1]), atof(num[i+1]));
gcvt(sum, 50, num[i-1]); // 将浮点数转换为字符串
gcvt(sum, 50, num[i+1]);
}

else if( strcmp(num[i],"-")==0 )
{
sum = sub(atof(num[i-1]), atof(num[i+1]));
gcvt(sum, 50, num[i-1]);
gcvt(sum, 50, num[i+1]);
}
}

return sum;
}

int data_handling(char ch[], char num[100][100]) // 数据处理
{
int i, num_count = 0, j = 0, key = 0, kuohao_count = 0, flag1 = 0, flag2 = 0;
char *sin_num, x[100], *tmp, num_brackets[100], arr[100][100];
double sum = 0;


for(i=0; ch[i]!='\0'; i++)
{
// 判断是否有中括号
if( ch[i]=='[' )
{
if( (ch[i-1]!='s')&&(ch[i-1]!='c')&&(ch[i-1]!='t')&&(ch[i-1]!='l'))
{
tmp = ch+i+1;
for(j=i; ch[j]!='\0'; j++)
{
if( ch[j]==']' )
{
ch[j] = '\0';
strcpy(num_brackets, tmp);
kuohao_count = data_handling(num_brackets, arr);
sum = calculating_data(arr, kuohao_count);
gcvt(sum, 100, x);
strcpy(num[key], x);
key++;
break;
}
}
i = j+1;
}
}


// 判断是否小有括号
if( ch[i]=='(' )
{
if( (ch[i-1]!='s')&&(ch[i-1]!='c')&&(ch[i-1]!='t')&&(ch[i-1]!='l') )
{
tmp = ch+i+1;
for(j=i; ch[j]!='\0'; j++)
{
if( ch[j]==')' )
{
ch[j] = '\0';
strcpy(num_brackets, tmp);
kuohao_count = data_handling(num_brackets, arr);
sum = calculating_data(arr, kuohao_count);
gcvt(sum, 100, x);
strcpy(num[key], x);
key++;
break;
}
}
i = j+1;
}
}

// 判断三角运算标识字符
if( ch[i]=='s'||ch[i]=='c'||ch[i]=='l'||ch[i]=='t' )
{
if( ch[i+1]=='i'&&ch[i+2]=='n'&&ch[i+3]=='(' )
{
sin_num = ch+i+4;
for(j=i+4; ch[j]!='\0'; j++)
{
if( ch[j]==')' )
{
ch[j] = '\0';
gcvt(sin(atof(sin_num)), 100, x);
strcpy(num[key], x);
key++;
break;
}
}
}

else if( ch[i+1]=='o'&&ch[i+2]=='s'&&ch[i+3]=='(' )
{
sin_num = ch+i+4;
for(j=i+4; ch[j]!='\0'; j++)
{
if( ch[j]==')' )
{
ch[j] = '\0';
gcvt(cos(atof(sin_num)), 100, x);
strcpy(num[key], x);
key++;
break;
}
}
}

else if( ch[i+1]=='n'&&ch[i+2]=='(' )
{
sin_num = ch+i+3;
for(j=i+3; ch[j]!='\0'; j++)
{
if( ch[j]==')' )
{
ch[j] = '\0';
gcvt(log(atof(sin_num)), 100, x);
strcpy(num[key], x);
key++;
break;
}
}
}

else if( ch[i+1]=='a'&&ch[i+2]=='n'&&ch[i+3]=='(' )
{
sin_num = ch+i+4;
for(j=i+4; ch[j]!='\0'; j++)
{
if( ch[j]==')' )
{
ch[j] = '\0';
gcvt(tan(atof(sin_num)), 100, x);
strcpy(num[key], x);
key++;
break;
}
}
}

i = j+1;
}

// 判断 数字, 符号:+ - * /
if( (ch[i]<='9'&&ch[i]>='0')&&flag1==0 )
{
sin_num = ch+i;
flag1 = 1;
}
if( !(ch[i]<='9'&&ch[i]>='0')&&ch[i]!='.'&&flag1==1 )
{
if( ch[i]=='+'||ch[i]=='-'||ch[i]=='*'||ch[i]=='/' )
{
if( ch[i+1]=='*')
{
num[key+1][0] = ch[i];
num[key+1][1] = ch[i+1];
num[key+1][2] = '\0';
ch[i] = '\0';
i++;
}
else
{
num[key+1][0] = ch[i];
num[key+1][1] = '\0';
}
flag2 = 1;
}
ch[i] = '\0';
strcpy(num[key], sin_num);
flag1 = 0;
if( flag2==1 )
{
key += 2;
flag2 = 0;
}
else
key++;
}
if( ch[i]=='+'||ch[i]=='-'||ch[i]=='*'||ch[i]=='/' )
{
num[key][0] = ch[i];
num[key][1] = '\0';
key++;
}
}

ch[i] = '\0';
strcpy(num[key], sin_num);
return key+1;
}

void calculate()
{
char ch[1000],num[100][100];
int i, num_count, flag = 0;
double sum = 0;


while( flag==0 )
{
fflush(stdin);
gotoxy(3,3);
gets(ch);

for(i=0; ch[i]!='\0'; i++)
{
if( (ch[i]<='9')&&(ch[i]>='0') )
{
flag = 1;
break;
}
}
if( flag==0 )
{
gotoxy(0,14);
printf("输入数据有误!\n");
break;
}

}

num_count = data_handling(ch,num);
sum = calculating_data(num, num_count);
gotoxy(0,14);
printf(">>>%.2f\n", sum);

}


void hellow()
{
gotoxy(4,1);
printf("This is a simple calculator!");

gotoxy(1,2);
printf("┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┐ \n");
printf(" │ │ \n");
printf(" │ │ \n");
printf(" ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┤ \n");
printf(" │ 1 2 3 + - │ \n");
printf(" │ │ \n");
printf(" │ 4 5 6 * / │ \n");
printf(" │ │ \n");
printf(" │ 7 8 9 cos sin│ \n");
printf(" │ │ \n");
printf(" │ 0 . = End │ \n");
printf(" └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┘ \n");

}

五子棋

一个朋友网上嫖的,看了看代码实现过程,发现学到的东西还挺多。编译环境vc6++,jpg要自己下载。

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

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <string.h>

/**************************************************************/
void init_system(); //初始化系统
void close_system(); //关闭系统
void init_game(); //初始化游戏界面

void m_white(); //白棋
void m_black(); //黑棋

void m_play(); //下棋
int m_win(); //判断胜负

void m_replay(); //重玩

/**************************************************************/
int m_bine[22][22] ; //定义棋子数组
int m_w = 15,m_h = 15,m_wh = 25; //定义行列数,每格像素数
int m_x0 = 25,m_y0 = 25; //初始棋子位置
int m_x9 = 375,m_y9 = 375; //最终棋子位置
int flag;

/**************************************************************/
void main()
{
init_system();

m_play();

close_system();
}

/*************************************************************/
void init_system() //初始化系统
{
init_game();
}

void init_game() //初始化游戏界面
{
IMAGE img;
loadimage(&img, "test.jpg");//导入test图片用作棋盘
int w, h;
w = img.getwidth();
h = img.getheight();
initgraph(w+150,h);
putimage( 0, 0, &img);

setlinecolor(WHITE);
setfillcolor(BLACK);
fillrectangle(w, 0, w+130, 60);
settextstyle(20,0,"楷体");
outtextxy(w+10,5 ,"黑方:玩家一");
outtextxy(w+10,30 ,"白方:玩家二");
}

void close_system() //关闭系统
{
closegraph();
}

/*********************************************/
void m_white() //将画笔变为白色
{
setlinecolor(WHITE);
setfillcolor(WHITE);
}

void m_black() //将画笔变为黑色
{
setlinecolor(BLACK);
setfillcolor(BLACK);
}


/**********************************************/
void m_play() //下棋
{
MOUSEMSG m_mouse;
int i,j;
while(1)
{
flag = 0;
m_mouse = GetMouseMsg();
switch(m_mouse.uMsg)
{
int a,b;
case WM_LBUTTONDOWN: //左键下黑棋
{
if(m_mouse.x > m_x0-m_wh/2 && m_mouse.x < m_x9+m_wh/2&&
m_mouse.y > m_y0-m_wh/2 && m_mouse.y < m_y9+m_wh/2) //判断是否在棋盘内
{
for(i = 0 ; i <= 15 ; i++)
{
for(j = 0 ; j <= 15 ; j++)
{
if(m_mouse.x >= m_x0-m_wh/2 +i*m_wh &&
m_mouse.x <= m_x0+m_wh/2 +i*m_wh &&
m_mouse.y >= m_y0-m_wh/2 +j*m_wh &&
m_mouse.y <= m_y0+m_wh/2 +j*m_wh) //纠正棋子位置,使其落在两线交叉点
{
a = i + 1;
b = j + 1;
m_mouse.x = a*m_wh;
m_mouse.y = b*m_wh;
break;
}
}
}
if(m_bine[a][b] != 0) //如果此处有棋子则不能下棋
continue;
m_bine[a][b] = 1;
m_black();
fillcircle(m_mouse.x,m_mouse.y,10);
}
flag = m_win();
if(flag == 1) //判断是否胜利
{
outtextxy(400,250 ,"按任意键");
outtextxy(400,270 ,"再来一盘");
getch();
m_replay();
}
else
m_win();
}
break;
case WM_RBUTTONDOWN : //右键下白棋
{
if(m_mouse.x > m_x0-m_wh/2 && m_mouse.x < m_x9+m_wh/2&&
m_mouse.y > m_y0-m_wh/2 && m_mouse.y < m_y9+m_wh/2) //判断是否在棋盘内
{
for(i = 0 ; i <= 15 ; i++)
{
for(j = 0 ; j <= 15 ; j++)
{
if(m_mouse.x >= m_x0-m_wh/2 +i*m_wh &&
m_mouse.x <= m_x0+m_wh/2 +i*m_wh &&
m_mouse.y >= m_y0-m_wh/2 +j*m_wh &&
m_mouse.y <= m_y0+m_wh/2 +j*m_wh) //纠正棋子位置,使其落在两线交叉点
{
a = i + 1;
b = j + 1;
m_mouse.x = a*m_wh;
m_mouse.y = b*m_wh;
break;
}
}
}
if(m_bine[a][b] != 0) //如果此处有棋子则不能下棋
continue;
m_bine[a][b] = 2;
m_white();
fillcircle(m_mouse.x,m_mouse.y,10);
}
flag = m_win();
if(m_win() == 1) //判断是否胜利
{
outtextxy(400,250 ,"按任意键");
outtextxy(400,270 ,"再来一盘");
getch();
m_replay();
}
else
m_win();
}
break;
}
}
}

void m_replay()//重新游戏
{
memset(m_bine,0,sizeof(m_bine));//初始化数组
init_game();
}

int m_win() //判断是否胜利
{
int i,j;
for(i = 1 ; i <= 15 ; i++)
{
for(j = 1 ; j <= 15 ; j++)
{
if(m_bine[i][j] == 1 && m_bine[i+1][j] == 1 && m_bine[i+2][j] == 1 &&
m_bine[i+3][j] == 1 && m_bine[i+4][j] == 1)
{
settextcolor(WHITE);
outtextxy(400,200 ,"the black win");
return 1;
}
else if(m_bine[i][j] == 1 && m_bine[i+1][j+1] == 1 && m_bine[i+2][j+2] == 1 &&
m_bine[i+3][j+3] == 1 && m_bine[i+4][j+4] == 1)
{
settextcolor(WHITE);
outtextxy(400 ,200 ,"the black win");
return 1;
}
else if(m_bine[i][j+1] == 1 && m_bine[i][j+2] == 1 && m_bine[i][j+3] == 1 &&
m_bine[i][j] == 1 && m_bine[i][j+4] == 1)
{
settextcolor(WHITE);
outtextxy(400 ,200,"the black win");
return 1;
}
else if(i >= 3 && m_bine[i][j] == 1 && m_bine[i+1][j-1] == 1 && m_bine[i+2][j-2] == 1 &&
m_bine[i+3][j-3] == 1 && m_bine[i+4][j-4] == 1)
{
settextcolor(WHITE);
outtextxy(400 ,200 ,"the black win");
return 1;
}
else if(j >= 3 && m_bine[i][j] == 1 && m_bine[i+1][j-1] == 1 && m_bine[i+2][j-2] == 1 &&
m_bine[i+3][j-3] == 1 && m_bine[i+4][j-4] == 1)
{
settextcolor(WHITE);
outtextxy(400 ,200 ,"the black win");
return 1;
}
else if(m_bine[i][j] == 2 && m_bine[i+1][j] == 2 && m_bine[i+2][j] == 2 &&
m_bine[i+3][j] == 2 && m_bine[i+4][j] == 2)
{
settextcolor(WHITE);
outtextxy(400,200 ,"the white win");
return 1;
}
else if(m_bine[i][j] == 2 && m_bine[i+1][j+1] == 2 && m_bine[i+2][j+2] == 2 &&
m_bine[i+3][j+3] == 2 && m_bine[i+4][j+4] == 2)
{
settextcolor(WHITE);
outtextxy(400 ,200 ,"the white win");
return 1;
}
else if(m_bine[i][j+1] == 2 && m_bine[i][j+2] == 2 && m_bine[i][j+3] == 2 &&
m_bine[i][j] == 2 && m_bine[i][j+4] == 2)
{
settextcolor(WHITE);
outtextxy(400 ,200,"the white win");
return 1;
}
else if(i >= 3 && j > 3 && m_bine[i][j] == 2 && m_bine[i+1][j-1] == 2 && m_bine[i+2][j-2] == 2 &&
m_bine[i+3][j-3] == 2 && m_bine[i+4][j-4] == 2)
{
settextcolor(WHITE);
outtextxy(400 ,200 ,"the white win");
return 1;
}
else if(j >= 3 && m_bine[i][j] == 2 && m_bine[i+1][j-1] == 2 && m_bine[i+2][j-2] == 2 &&
m_bine[i+3][j-3] == 2 && m_bine[i+4][j-4] == 2)
{
settextcolor(WHITE);
outtextxy(400 ,200 ,"the white win");
return 1;
}
}
}
return 0;
}