怎样记录为电子文件。。大神,求解。
题目:利用所学C语言知识及相关知识设计一套程序。例如模拟大学生活,设计两个参量一个体力,一个精神力,打一场篮球赛消耗体力为一个值,学习一个小时消耗体力和精神力各为多少,睡觉,吃饭又能补充多少等等最后得出这一天生活的结论,并且尽量能够记录为电子文件,当第二天打开该程序时,会接着前一天的继续。怎样记录为电子文件。。。。。
[此贴子已经被作者于2016-5-12 10:43编辑过]
#include <stdio.h> /* 以下体力精力的值根据实际情况修改 */ #define max_str 10 #define max_ene 10 #define eat_add_str 1 #define eat_add_ene 1 #define sleep_add_str 1 #define sleep_add_ene 1 #define study_del_str 1 #define study_del_ene 1 #define basketball_del_str 1 #define basketball_del_ene 1 typedef struct stman { int num; int strength; int energy; }man; enum eplay { nothing = 0, eat, sleep, study, basketball, butt }; enum eperson { NONE = 0, XIAOMING = 1, XIAOGANG, XIAOHONG, ALL }; int main() { int i = 0, act = 0; man person[ALL]; FILE *fp = NULL; char name[ALL][20] = {"", "xiaoming","xiaogang","xiaohong" }; fp = fopen("student.txt", "a+"); if (fp == NULL) { printf("操作文件失败!\n"); return 1; } memset(person, 0, sizeof(person)); if (fread(&person[NONE], 1, sizeof(person), fp) == 0) { printf("操作文件失败!\n"); return 1; } fclose(fp); while (1) { printf("======================================================\n"); printf("请输入要动作的人:\n"); printf("1-xiaoming\n"); printf("2-xiaogang\n"); printf("3-xiaohong\n"); printf("0-结束今天的活动\n"); printf("4-查询每个人的状态\n"); printf("======================================================\n"); scanf("%d",&i); if ((i < NONE) || (i > ALL)) { printf("输入错误!\n"); continue; } if (i == ALL) { for (i = NONE + 1; i < ALL; i++) { printf("%s 体力:%d, 精力:%d\n",name[i],person[i].strength, person[i].energy); } continue; } if (i == NONE) { fp = fopen("student.txt", "w+"); if (fp == NULL) { printf("操作文件失败!\n"); return 1; } if (fwrite(person, 1, sizeof(person), fp)) { printf("byebye!\n"); } else { printf("操作文件失败!\n"); } return 0; } printf("请输入要做的动作:1-eat,2-sleep,3-study,4-basketball\n"); scanf("%d", &act); switch (act) { case eat: if (person[i].strength > max_str) { printf("好饱,不想吃了!\n"); break; } if (person[i].energy > max_ene) { printf("神采奕奕,不想吃了!\n"); break; } person[i].strength += eat_add_str; person[i].energy += eat_add_ene; if (person[i].strength > max_str) { person[i].strength = max_str; } if (person[i].energy > max_ene) { person[i].energy = max_ene; } break; case sleep: if (person[i].strength > max_str) { printf("好饱,不想睡了!\n"); break; } if (person[i].energy > max_ene) { printf("神采奕奕,不想睡了!\n"); break; } person[i].strength += sleep_add_str; person[i].energy += sleep_add_ene; if (person[i].strength > max_str) { person[i].strength = max_str; } if (person[i].energy > max_ene) { person[i].energy = max_ene; } break; case study: if (person[i].strength < study_del_str) { printf("今天累了,不想学习了!\n"); break; } if (person[i].energy < study_del_ene) { printf("今天烦了,不想学习了!\n"); break; } person[i].strength -= study_del_str; person[i].energy -= study_del_ene; break; case basketball: if (person[i].strength < study_del_str) { printf("今天累了,不想打球了!\n"); break; } if (person[i].energy < study_del_ene) { printf("今天烦了,不想学习了!\n"); break; } person[i].strength -= basketball_del_str; person[i].energy -= basketball_del_ene; break; case nothing: case butt: default: printf("输入错误!\n"); break; } } return 0; }