學單片機就必須會c語言,那些做個小游戲來檢驗一下你的c語言水平吧
C語言源程序如下:
- #include <stdio.h>
- void printf_cds(int (*array)[3]); //打印UI
- int input(int cds, int (*pcds)[3], int game); //獲取輸入
- int scan_arry(int (*pcds)[3], int gamer); //掃描數組
- int scan_arry_row(int (*pcds)[3], int gamer); //掃描行
- int scan_arry_column(int (*pcds)[3], int gamer); //掃描列
- int scan_arry_x(int (*pcds)[3], int gamer); //掃描對角線
- int scan_arry_zero(int (*pcds)[3]);//掃描剩余位置
- int main(int argc, char **argv)
- {
- int coords[3][3] = {{0, 0, 0},
- {0, 0, 0},
- {0, 0, 0}}; //第一個玩家用 1表示,第二個玩家用 2表示
- int gamer01 = 10, who = 1;
- char button;
- //開始界面
- printf("游戲開始\n");
- printf("操作輸入數字鍵盤對應的位置\n");
- printf(" 7 | 8 | 9 \n");
- printf("———————— ——————— ———————\n");
- printf(" 4 | 5 | 6 \n");
- printf("———————— ——————— ———————\n");
- printf(" 1 | 2 | 3 \n\n");
- printf_cds(coords);
- while (1)
- {
- if (scan_arry_zero(coords) == 0)
- return 0;
- printf("請第%d位玩家輸入\n", who);
- scanf("%d", &gamer01);
- if (gamer01 != 0)
- {
- if (who == 1)
- {
- if (input(gamer01, coords, 1))
- who = 2;
- printf_cds(coords);
- if (scan_arry(coords, 1) == 1)
- {
- printf("玩家%d勝利!", 1);
- return 1;
- }
- }
- else if (who == 2)
- {
- if (input(gamer01, coords, 2))
- who = 1;
- printf_cds(coords);
- if (scan_arry(coords, 2) == 2)
- {
- printf("玩家%d勝利!", 2);
- return 1;
- }
- }
- }
- else //結束游戲
- {
- printf("是否退出游戲?(y:是,n:否)\n");
- getchar();
- scanf("%c", &button);
- getchar();
- if (button == 'y')
- return 0;
- else if (button == 'n')
- printf_cds(coords);
- }
- }
- }
- //掃描零值
- int scan_arry_zero(int (*pcds)[3])
- {
- int i, j, count = 0;
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- if (pcds[i][j] == 0)
- count++;
- }
- }
- if (count == 0)
- {
- printf("游戲結束,平局!");
- return 0;
- }
- else
- return 1;
- }
- //掃描數組
- int scan_arry(int (*pcds)[3], int gamer)
- {
- if (scan_arry_row(pcds, gamer) == gamer)
- return gamer;
- else if (scan_arry_column(pcds, gamer) == gamer)
- return gamer;
- else if (scan_arry_x(pcds, gamer) == gamer)
- return gamer;
- else
- return 0;
- }
- //掃描行
- int scan_arry_row(int (*pcds)[3], int gamer)
- {
- int i, j, count_r = 0;
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- if (pcds[i][j] == gamer)
- {
- count_r += pcds[i][j]; //獲取行數據
- }
- }
- if (count_r / 3 == gamer)
- return gamer;
- else
- count_r = 0;
- }
- return 0;
- }
- //掃描列
- int scan_arry_column(int (*pcds)[3], int gamer)
- {
- int i, j, count_c = 0;
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- if (pcds[j][i] == gamer)
- count_c += pcds[j][i]; //獲取列數據
- }
- if (count_c / 3 == gamer)
- return gamer;
- else
- count_c = 0;
- }
- return 0;
- }
- //掃描對角線
- int scan_arry_x(int (*pcds)[3], int gamer)
- {
- int i, j, count_1 = 0, count_2 = 0;
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- if (pcds[i][j] == gamer)
- {
- if (i == j) //獲取斜邊
- count_1 += pcds[i][j];
- if (i + j == 2) //獲取斜邊2
- count_2 += pcds[i][j];
- }
- }
- }
- if (count_1 / 3 == gamer || count_2 / 3 == gamer)
- return gamer;
- else
- return 0;
- }
- //獲取玩家輸入
- int input(int cds, int (*pcds)[3], int gamer)
- {
- int arry[3][3] = {{7, 8, 9},
- {4, 5, 6},
- {1, 2, 3}};
- int i, j;
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- if (cds == arry[i][j])
- {
- if (pcds[i][j] == 0)
- {
- pcds[i][j] = gamer;
- return 1;
- }
- else
- {
- printf("此位置已有數據,請重新輸入\n");
- return 0;
- }
- }
- }
- }
- }
- //打印游戲界面
- void printf_cds(int (*array)[3])
- {
- int i, j;
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- printf("%d\t", array[i][j]);
- }
- printf("\n\n");
- }
- }
復制代碼
- /*做一個小項目:(簡單的學生信息管理系統--》可以管理10個學生的信息)
- 定義一個結構體數據類型,里面包括學生信息:
- 姓名、性別、學號、年齡、電話號碼、成績(語數英體),
- 可以自行選擇操作的增加、刪除、更改、查詢學生的信息等功能
- */
- #include <stdio.h>
- #include <strings.h>
- #include <string.h>
- int len = 10; //要管理的學生人數 ------------測試用,完成功能后改回項目要求
- //成績 的結構體數據類型定義
- struct score
- {
- int Chinese;
- int Math;
- int English;
- int PE;
- };
- //學生 的 結構體數據類型定義
- struct student
- {
- char name[20];
- char sex;
- int ID;
- unsigned char age;
- unsigned long long phone_num;
- struct score student_sc;
- };
- //函數聲明
- void printf_UI(void);
- void add_data(struct student *p_data);
- void printf_data(struct student *p_data);
- void delete_data(struct student *p_data);
- void change_data(struct student *p_data);
- int main(int argc, char **argv)
- {
- int cmd = 0;
- struct student yq[len];
- bzero(yq, sizeof(yq));
- while (1)
- {
- printf_UI();
- scanf("%d", &cmd);
- if (cmd == 1)
- add_data(yq);
- else if (cmd == 2)
- printf_data(yq);
- else if (cmd == 3)
- change_data(yq);
- else if (cmd == 4)
- delete_data(yq);
- }
- return 0;
- }
- //修改學生數據
- void change_data(struct student *p_data)
- {
- int i, num=0;
- for (i = 0; i < len; i++)
- {
- printf("學生%d姓名:%s\n", i + 1, p_data[i].name);
- }
- printf("\n請輸入需要修改的學生(1~10)\n");
- scanf("%d", &num);
- if(num<1||num>10)
- {
- printf("輸入有誤!\n");
- return;
- }
- num -= 1;
- printf("請輸入學生的姓名:");
- scanf("%s", p_data[num].name);
- getchar();
- printf("請輸入學生的性別(M->男, W-->女):");
- scanf("%c", &p_data[num].sex);
- getchar();
- printf("請輸入學生的學號:");
- scanf("%d", &p_data[num].ID);
- printf("請輸入學生的年齡(0~255):");
- scanf("%hhu", &p_data[num].age);
- printf("請輸入學生的電話號碼:");
- scanf("%llu", &p_data[num].phone_num);
- printf("請輸入學生的語文成績:");
- scanf("%d", &p_data[num].student_sc.Chinese);
- printf("請輸入學生的數學成績:");
- scanf("%d", &p_data[num].student_sc.Math);
- printf("請輸入學生的英語成績:");
- scanf("%d", &p_data[num].student_sc.English);
- printf("請輸入學生的體育成績:");
- scanf("%d", &p_data[num].student_sc.PE);
- printf("\n");
- printf("修改完成!\n\n");
- }
- //刪除數據
- void delete_data(struct student *p_data)
- {
- int i;
- for (i = 0; i < len; i++)
- bzero(&p_data[i], sizeof(*p_data));
- printf("數據已清除!\n\n");
- }
- //增加數據
- void add_data(struct student *p_data)
- {
- int i;
- for (i = 0; i < len; i++)
- {
- //姓名、性別、學號、年齡、電話號碼、成績(語數英體),
- printf("請輸入學生的姓名:");
- scanf("%s", p_data[i].name);
- getchar();
- printf("請輸入學生的性別(M->男, W-->女):");
- scanf("%c", &p_data[i].sex);
- getchar();
- printf("請輸入學生的學號:");
- scanf("%d", &p_data[i].ID);
- printf("請輸入學生的年齡(0~255):");
- scanf("%hhu", &p_data[i].age);
- printf("請輸入學生的電話號碼:");
- scanf("%llu", &p_data[i].phone_num);
- printf("請輸入學生的語文成績:");
- scanf("%d", &p_data[i].student_sc.Chinese);
- printf("請輸入學生的數學成績:");
- scanf("%d", &p_data[i].student_sc.Math);
- printf("請輸入學生的英語成績:");
- scanf("%d", &p_data[i].student_sc.English);
- printf("請輸入學生的體育成績:");
- scanf("%d", &p_data[i].student_sc.PE);
- printf("\n");
- }
- printf("數據輸入完成\n\n");
- }
- //打印數據
- void printf_data(struct student *p_data)
- {
- int i;
- for (i = 0; i < len; i++)
- {
- //姓名、性別、學號、年齡、電話號碼、成績(語數英體),
- printf("學生%d姓名:%s\n", i + 1, p_data[i].name);
- printf("學生%d性別:%c\n", i + 1, p_data[i].sex);
- printf("學生%d學號:%d\n", i + 1, p_data[i].ID);
- printf("學生%d年齡:%hhu\n", i + 1, p_data[i].age);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
51hei.png (3.76 KB, 下載次數: 76)
下載附件
2021-7-1 19:48 上傳
所有資料51hei提供下載:
三字棋游戲+學生簡易管理系統.7z
(9.64 KB, 下載次數: 7)
2021-7-1 19:49 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|