1 贪吃蛇练习
/*
贪吃蛇练习
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 4
enum {UP, DOWN, LEFT, RIGHT};
typedef struct {
int x;
int y;
} position;
void show_snake(position *p_position, int size) {
int pos = 0;
for (pos = 0;pos <= size - 1;pos++) {
printf("(%d,%d) ", p_position[pos].x, p_position[pos].y);
}
printf("\n");
}
int main() {
int pos = 0, direction = 0;
position snake[SIZE], head;
srand(time(0));
snake[0].x = rand() % 10;
snake[0].y = rand() % 10;
for (pos = 1;pos <= SIZE - 1;pos++) {
snake[pos] = snake[pos - 1];
snake[pos].x++;
}
show_snake(snake, SIZE);
while (1) {
printf("请输入方向(%d代表上,%d代表下,%d代表左,%d代表右):", UP, DOWN, LEFT, RIGHT);
scanf("%d", &direction);
head = snake[0];
switch(direction) {
case UP:
head.y--;
break;
case DOWN:
head.y++;
break;
case LEFT:
head.x--;
break;
case RIGHT:
head.x++;
break;
}
for (pos = 0;pos <= SIZE - 1;pos++) {
if (head.x == snake[pos].x &&
head.y == snake[pos].y) {
break;
}
}
if (pos <= SIZE - 1) {
printf("方向错误\n");
}
else {
for (pos = SIZE - 2;pos >= 0;pos--) {
snake[pos + 1] = snake[pos];
}
snake[0] = head;
show_snake(snake, SIZE);
}
}
return 0;
}
2 --1.从键盘得到日程信息并最终输出。
每个日程包含小时数(0--23)和内容两部分
不要求排序
--2.对结果进行排序
/*
结构体练习
*/
#include <stdio.h>
#include <string.h>
struct month {
char name[10];
int days;
};
int main() {
struct month months[12];
char * names[] = {"Jan", "Feb", "Mar",
"Apr", "May", "Jun",
"Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"};
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int loop = 0;
for (loop = 0;loop <= 11;loop++) {
strcpy(months[loop].name, names[loop]);
months[loop].days = days[loop];
}
for (loop = 0;loop <= 11;loop++) {
printf("名称是%s,天数是%d\n", months[loop].name, months[loop].days);
}
return 0;
}
/*
结构体初始化练习
*/
#include <stdio.h>
/*struct month {
char name[10];
int days;
};
typedef struct month month;*/
typedef struct /*month*/ {
char name[10];
int days;
} month;
int main() {
month months[3] = {{"Jan", 31},
{"Feb", 28},
{"Mar", 31}};
int loop = 0;
for (loop = 0;loop <= 2;loop++) {
printf("名称是%s,天数是%d\n", months[loop].name, months[loop].days);
}
return 0;
}
3 编写一个函数录入多个人员信息。每次录入之前都向用户询问。
/*
人员信息管理系统练习
*/
#include <stdio.h>
#include <string.h>
int save() {
int num = 0, ids[10] = {}, id = 0, pos = 0;
char name[20] = {}, buf[20] = {};
float salary = 0.0f;
FILE *p_file = fopen("a.bin", "ab");
if (!p_file) {
printf("文件打开失败\n");
return 0;
}
while (1) {
printf("请输入人员id:");
scanf("%d", &id);
scanf("%*[^\n]");
scanf("%*c");
for (pos = 0;pos <= num - 1;pos++) {
if (id == ids[pos]) {
break;
}
}
if (pos <= num - 1) {
continue;
}
printf("请输入人员姓名:");
fgets(name, 20, stdin);
if (strlen(name) == 19 && name[18] != '\n') {
scanf("%*[^\n]");
scanf("%*c");
}
printf("请输入人员工资:");
scanf("%g", &salary);
scanf("%*[^\n]");
scanf("%*c");
fwrite(&id, sizeof(int), 1, p_file);
fwrite(name, sizeof(char), 20, p_file);
fwrite(&salary, sizeof(float), 1, p_file);
ids[num] = id;
num++;
printf("是否要录入下一个员工信息:");
fgets(buf, 20, stdin);
if (strlen(buf) == 19 && buf[18] != '\n') {
scanf("%*[^\n]");
scanf("%*c");
}
if (buf[0] != 'y' && buf[0] != 'Y') {
break;
}
}
fclose(p_file);
return num;
}
int main() {
printf("共录入了%d个用户的信息\n", save());
return 0;
}
4 编写函数完成基于id的查询功能
/*
id比较练习
*/
#include <stdio.h>
int main() {
int id = 0, id1 = 0, length = 0;
FILE *p_file = NULL;
printf("请输入一个id:");
scanf("%d", &id);
p_file = fopen("a.bin", "rb");
if (p_file) {
fseek(p_file, 0, SEEK_END);
length = ftell(p_file);
rewind(p_file);
while(ftell(p_file) < length) {
fread(&id1, sizeof(int), 1, p_file);
if (id == id1) {
printf("id已存在\n");
break;
}
fseek(p_file, 24, SEEK_CUR);
}
if (ftell(p_file) >= length) {
printf("id不存在\n");
}
fclose(p_file);
p_file = NULL;
}
return 0;
}