|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct Student
{
char name[20];
char num[20];
float math;
float computer;
struct Student *next;
}People;
People *head;
void addstudent(People **buffer);
void delstudent();
void browsestudent();
void updatestudent();
void inputstudent();
void releasestudent();
void savestudent();
void loadstudent();
void meau();
void meau()
{
printf("\t-----------meau----------\n");
printf("\t| 1-add 2-del 3-update|\n");
printf("\t| 4-browse 5-save |\n");
printf("\t| 6-load 7-out|\n");
printf("\t-------------------------\n\n\n");
}
void addstudent(People **buffer) //新建节点,作用链接单个结构
{
People *temp; //定义头
if(head->next != NULL)
{
temp = head->next;
head->next = *buffer;
(*buffer)->next = temp;
}
else
{
head->next = *buffer;
(*buffer)->next = NULL;
}
}
void inputstudent()
{
People *buffer;
buffer = (People *)malloc(sizeof(People));
if(buffer == NULL)
{
printf("申请内存失败!\n");
exit(1);
}
printf("请输入学生姓名:");
scanf("%s", buffer->name);
printf("请输入学号:");
scanf("%s", buffer->num);
printf("请输入数学成绩:");
scanf("%f", &buffer->math);
printf("请输入计算机成绩:");
scanf("%f", &buffer->computer);
addstudent(&buffer);
}
void delstudent()
{
People *pb, *pf;
char str[20];
printf("请输入要删除的学生姓名:");
scanf("%s", str);
pb = head->next;
while(strcmp(str, pb->name) && pb->next != NULL) //循环退出条件是链表结束或者找到要删除的节点
{
pf = pb;
pb = pb->next;
}
if(strcmp(str, pb->name) == 0)
{
if(pb == head->next) //找到的节点是头结点
{
head->next = (head->next)->next; //指向第二个节点
}
else
{
pf->next = pb->next;
}
free(pb);
}
else
{
printf("查无此人!\n");
return ;
}
}
void updatestudent()
{
People* star = head->next;
char str[20];
int op;
float new_math;
float new_computer;
if(star == NULL)
{
printf("此链表为空!\n");
return ;
}
printf("请输入要更新信息的学生姓名:");
scanf("%s", str);
while(star != NULL)
{
if(strcmp(str, star->name) == 0)
{
printf("请输入要更新的内容(1-math,2-computer):");
scanf("%d", &op);
switch(op)
{
case 1: {printf("请输入新的数学成绩:");scanf("%f", &star->math);} break;
case 2: {printf("请输入新的计算机成绩:");scanf("%f", &star->computer);} break;
}
}
star = star->next;
}
}
void browsestudent()
{
People *star = head->next;
if(star == NULL)
{
printf("此链表为空!\n");
return ;
}
printf("姓名\t 学号\t\t数学\t计算机\n");
while(star != NULL)
{
printf("%s %s\t%.1f\t%.1f\n", star->name, star->num, star->math, star->computer);
star = star->next;
}
}
void savestudent()
{
People *star;
star = head->next;
FILE *fp;
if((fp = fopen("date.txt", "w")) == NULL)
{
printf("新建文件失败!\n");
}
while(star != NULL)
{
fprintf(fp, "%s %s %.1f %.1f\n", star->name, star->num, star->math, star->computer);
star = star->next;
}
fclose(fp);
}
void loadstudent()
{
FILE *fp;
People *buffer;
if((fp = fopen("date.txt", "r")) == NULL)
{
printf("打开文件错误!");
exit(1);
}
while(!feof(fp))
{
buffer = (People *)malloc(sizeof(People));
fscanf(fp, "%s %s %f %f", buffer->name, buffer->num, &buffer->math, &buffer->computer);
addstudent(&buffer);
}
printf("加载完成!\n");
fclose(fp);
}
void releasestudent()
{
People *temp, *star;
star = head->next;
while(star != NULL)
{
temp = star;
star = star->next;
free(temp);
}
}
int main(void)
{
head = (People *)malloc(sizeof(People));
head->next = NULL;
int op;
meau();
do
{
printf("请输入要执行的命令:");
scanf("%d", &op);
switch(op)
{
case 1: inputstudent(); break;
case 2: delstudent(); break;
case 3: updatestudent(); break;
case 4: browsestudent(); break;
case 5: savestudent(); break;
case 6: loadstudent(); break;
case 7: exit(1);
}
}while(1);
releasestudent();
return 0;
}
以下是打印结果,为什么会出现这一行乱码,该怎么解决呀,求指点
姓名 学号 数学 计算机
|
|