马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1024
void getlist(struct Dorm *dorm);
void printlist(struct Dorm *firstlist);
void freelist(struct Dorm *firstlist);
void addDorm(struct Dorm **firstlist);
struct Dorm *serachlist(struct Dorm *firstlist,char *target);
void printfserachlist(struct Dorm *firstlist);
typedef struct Dorm
{
char name[MAX];
int age;
struct Dorm *next;
}Dorm;
void getlist(Dorm *dorm)
{
printf("请输入名字:");
scanf("%s",dorm->name);
printf("请输入年龄:");
scanf("%d",dorm->age);
}
void printlist(Dorm *firstlist)
{
Dorm *dorm;
int count = 1;
dorm = firstlist;
while(dorm != NULL)
{
printf("\n名字为%s\n",dorm->name);
printf("\n年龄为%d\n",dorm->age);
dorm = dorm->next;
count ++;
}
}
void freelist(Dorm *firstlist)
{
while(firstlist != NULL)
{
firstlist = firstlist->next;
free(firstlist);
}
}
Dorm *searchlist(Dorm *firstlist,char *target)
{
Dorm *dorm;
dorm = firstlist;
while(dorm != NULL)
{
if(!strcmp(dorm->name,target))
{
break;
}
dorm = dorm->next;
}
return dorm;
}
void printfsearchlist(Dorm *firstlist)
{
printf("————查找完毕————");
printf(" 姓名:%s",firstlist->name);
printf(" 年龄:%d",firstlist->age);
}
void addDorm(Dorm **firstlist)
{
Dorm *dorm,*temp;
dorm = (Dorm *)malloc(sizeof(Dorm));
if(dorm == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getlist(dorm);
if(*firstlist != NULL)
{
temp = *firstlist;
*firstlist = dorm;
dorm->next = temp;
}
else
{
*firstlist = dorm;
dorm->next = NULL;
}
}
void list_file()
{
Dorm *dorm;
FILE *fp;
if((fp = fopen("C:\\Users\\帅气逼人的阿熊\\Desktop\\C语言\\文件\\成绩.txt","w")) == NULL)
{
printf("打开文件失败!");
exit(EXIT_FAILURE);
}
else
{
printf("保存文件成功!");
}
while(dorm != NULL)
{
fprintf(fp,"%s",(&dorm->age,1,sizeof(Dorm)));
dorm = dorm->next;
}
fclose(fp);
}
int main(void)
{
Dorm *firstlist = NULL;
Dorm *result;
char input[MAX];
char list[MAX];
int ch;
FILE *fp;
while(1)
{
system("color 3");
printf("\n\r\r#######学生考试信息系统#######\r\r\n\
\r\r########1录入学生信息#########\r\r\n\
\r\r#####2打印已录入学生信息######\r\r\n\
\r\r#####3查询已录入学生信息######\r\r\n\
\r\r#####4删除已录入学生信息######\r\r\n");
scanf("%d",&ch);
if(ch == 1)
{
addDorm(&firstlist);
system("pause");
system("cls");
}
else if(ch == 2)
{
printlist(firstlist);
system("pause");
system("cls");
}
else if(ch == 3)
{
printf("\n请输入需要查询的人的名字:");
scanf("%s",&input);
result = searchlist(firstlist,input);
if(result == NULL)
{
printf("\n未能找到数据...");
system("pause");
system("cls");
}
else
{
do
{
printf("\n已找到数据...\n");
printfsearchlist(result);
}while((result = searchlist(result->next,input)) != NULL);
system("cls");
}
}
else if(ch == 5)
{
list_file();
system("pause");
system("cls");
}
else
{
printf("请输入正确的序号!");
}
}
freelist(firstlist);
return 0;
}
问一下大佬们,为什么程序写入不了! |