学单片机就必须会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提供下载:
三字棋游戏+学生简易管理系统.7z
(9.64 KB, 下载次数: 7)
|