
正文
c语言load()函数 c语言中load函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
c语言,数据能写入文件,但是从文件读取数据的时候,出现了乱码,如下代码,求解答
在写文件部分的for循环里做如下修改:(已编译运行,成功)
for(i = 0; i SIZE; i++)
{
printf("请输入name:\n");
scanf("%s",stu[i].name);
printf("请输入num:\n");
scanf("%d",(stu[i].num));
printf("请输入age:\n");
scanf("%d",(stu[i].age));
printf("请输入addr:\n");
scanf("%s",stu[i].addr);
//scanf("%s,%d,%d,%s", (stu[i].name),(stu[i].num),(stu[i].age), (stu[i].addr));
printf("\n");
}
相关问答
Q1: c语言 取出文件中的数据
void load()
{FILE *fp;
struct student come;
fp=fopen("D:\\data.txt","rb");
if(fp!=NULL)
{
fread(come,sizeof(struct student),1,fp);
}
fclose(fp);
}
这样c语言load()函数,就把保存在文件里c语言load()函数的第一个数据取出来c语言load()函数了。如果想取出多条数据的话c语言load()函数,改成循环就可以。如果想随机读取的话,请用fseek函数来定位。
另外,如果用fwrite来写入的话,打开文件需要加入b,表明是以二进制读写的。请楼主自己改成“wb”。fwrite和fread读取文本和二进制文件都合适。fgets,fprintf,fputs,fscanf等可以读文本,但读取二进制效果将会非常糟糕。
Q2: 用C语言写一个模拟火车票管理系统。
#includestdio.h
#includestring.h
#includestdlib.h
#includetime.h
#includeconio.h
#define N 1000
typedef struct TICKET
{
char num[10];
char hour[3];
char min[3];
char from[10];
char to[10];
float hours;
int max;
int now;
}CLASS;
int class_num=0;
CLASS records[N];
int system_time();
void NewMessage();
void ShowTable1();
void ShowTable2(int i);
void Display();
void add();
void save();
void load();
void search();
void change();
void quit();
void Ticketorder();
void Ticketdelete();
int menu_select();
int whether(int);
void find(char s1[],char s2[]);
void deletemessage();
int findnum(char s1[]);
void get(int,int);
char *menu[]={"*****************欢迎使用车票管理系统*****************",
"\n*******************MENU功能菜单***********************",
"\n 1. 录入班次 ",
"\n 2. 显示所有班次 ",
"\n 3. 查询班次 ",
"\n 4. 增加班次 ",
"\n 5. 售票 ",
"\n 6. 退票 ",
"\n 7. 修改班次 ",
"\n 8. 删除班次 ",
"\n 9. 退出 "};
/**主函数**/
void main()
{
system("cls");
while(1)
{
switch(menu_select())
{
case 1: NewMessage();break;
case 2: Display();break;
case 3: search();break;
case 4: add();break;
case 5: Ticketorder();break;
case 6: Ticketdelete();break;
case 7: change();break;
case 8: deletemessage();break;
case 9: quit();break;
}
}
}
/**菜单函数**/
int menu_select()
{
char s[5];
int c,i;
system("cls");
for(i=0;i11;i++)
{
printf("%s",menu[i])) ;
}
i=0;
printf("\n");
printf("******************************************************");
printf("\n请选择(1-9):");
scanf("%s",s);
c=atoi(s);
while(i0||i9)
{
printf("\n");
printf("******************************************************");
printf("\n请选择(1-9):");
scanf("%s",s);
c=atoi(s);
}
return c;
}
void NewMessage()
{
int i=0,j=5,h;
char s[5];
FILE *fp;
system("cls");
if((fp=fopen("d:number.dat","rb"))!=NULL)
{
printf("车票信息已经存在请选择增加功能!\n");
printf("任意输入则返回菜单\n");
scanf("%s",s);
i=1; //通过是1
}
if(i==0)
{
system("cls");
printf("请输入要录入班次总数:\n");
scanf("%d",class_num);
system("cls");
for(i=0;iclass_num;i++)
{
system("cls");
printf("请输入第%d个班次信息:\n",i+1);
h=-1;
for(;h!=i;)
{
printf("请输入班次:");
scanf("%s",records[i].num);
for(h=0;hi;h++)
if(strcmp(records[h].num,records[i].num)==0)
//判断字符串比较是否相等,, 待比较的字符串
{
printf("输入错误!该班次已存在!\n");
break;
}
}
get(i,j);
j=5;
}
save();
}
}
void Display() //显示所有函数
{
int i,j;
system("cls"); //刷屏
load(); // 调用按班次查询函数
ShowTable1(); //线框调用1,,,不用每次输
for(i=0,j=0;iclass_num;i++,j+=2) /////按班次顺序输出
{
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
if(whether(i))
printf("|%10s|%5s:%-4s|%10s|%10s|%8.1f|%8d|%8d|",records[i].num,records[i].hour,
records[i].min, records[i].from,records[i].to,records[i].hours,
records[i].max,records[i].now);
else
printf("|%10s| 已发车 |%10s|%10s|%8.1f|%8d|%8d|",records[i].num,
records[i].from,records[i].to,records[i].hours,records[i].max,records[i].now);
}
ShowTable2(j); //线框调用2
printf("\n按任意键继续....\n");
getch(); /////从控制台读取一个字符,但不显示在屏幕上
}
/**打印表头**/
void ShowTable1()
{
int i=2;
system("cls");
printf("**************************车票信息系统**********************************\n");
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
printf("\n| 班次 | 发车时间 | 起点站 | 终点站 |行车时间|额定载量| 已售票 |\n");
}
void ShowTable2(int i)
{
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
}
void search()
{
int i;
char s1[10]={'\0'},s2[10]={'\0'};
system("cls");
printf("1. 按班次查询\n")
printf("2. 按终点站查询\n");
printf("3. 退出\n");
printf("请选择(1-3):\n");
scanf("%d",i);
load();
switch(i)
{
case 1: printf("请输入要查询的班次:");
scanf("%s",s1);
find(s1,s2);
break;
case 2: printf("请输入要查询终点站:");
scanf("%s",s2);
find(s1,s2);
break;
case 3: break;
default : printf("输入错误!\n");
break;
}
printf("按任意键继续....\n");
getch();
}
void find(char s1[],char s2[])
{
int i,h=0,m;
ShowTable1();
if(s2[0]=='\0')
m=1;
else m=0;
for(i=0;iclass_num;i++)
if(strcmp(s1,records[i].num)==0||strcmp(s2,records[i].to)==0)
{
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
printf("|%10s|%5s:%-4s|%10s|%10s|%8.1f|%8d|%8d|",records[i].num,records[i].hour,records[i].min,
records[i].from,records[i].to,records[i].hours,records[i].max,records[i].now);
h+=1;
if(m==1)
break;
}
ShowTable2(h);
if(h==0)
printf("要查找的班次不存在!\n");
}
/**增加函数**/
void add()
{
int i,j=5;
load();
system("cls");
printf("1. 增加班次");
printf("2. 返回\n");
printf("请选择(1-2)\n");
scanf("%d",i);
if(i==1)
{
system("cls"); //刷屏
printf("1. 请输入要增加的班次:");
scanf("%s",records[class_num].num);
for(i=0;iclass_num;i++)
if(strcmp(records[class_num].num,records[i].num)==0)
//判断字符串比较是否相等,待比较的字符串,判断车次没重复
{
printf("输入错误!\n");
getch(); /////从控制台读取一个字符,但不显示在屏幕上
break;
}
if(i==class_num)
{
get(i,j); ////修改及增加班次*
class_num++; ///使班次数加1
save(); //调用保存函数
}
}
}
/**售票函数**/
void Ticketorder() //*售票函数
{
int i;
char num[10];
system("cls"); //刷屏
printf("1. 售票\n");
printf("2. 返回\n");
printf("请选择(1-2):\n");
scanf("%d",i);
if(i==1)
{
load(); // 加载函数
search(); //查找函数
printf("请输入要订票的班次(若无请输入0):");
scanf("%s",num);
for(i=0;iclass_num;i++)
if(strcmp(num,records[i].num)==0)//判断字符串比较是否相等,, 待比较的字符串
if(records[i].maxrecords[i].nowwhether(i)==1)
//判断时间是否超出函数并且没超出最大客量
{
records[i].now++; ///使已售票加1
printf("通向%s班次为%s的票订票成功!\n",records[i].to,records[i].num);
save(); //调用保存函数
getch(); /////从控制台读取一个字符,但不显示在屏幕上
break;
}
else
{
printf("该班次已满或已发出!\n");
getch(); /////从控制台读取一个字符,但不显示在屏幕上
}
}
}
/**退票函数**/
void Ticketdelete() //删除函数*
{
int i;
char num[10];
system("cls"); //刷屏
printf("1. 退票\n");
printf("2. 返回\n");
printf("请选择(1-2)\n:");
scanf("%d",i);
if(i==1)
{
system("cls"); //刷屏
load(); // 调用按班次查询函数
printf("请输入要退票的班次:\n");
scanf("%s",num);
i=findnum(num); //调用班次查询函数
if(strcmp(num,records[i].num)==0) //判断字符串比较是否相等 待比较的字符串
if(whether(i)) //判断时间是否超出函数***
{
printf("确定(Y/N)?");
scanf("%s",num);
if(num[0]=='y'||num[0]=='Y')
{
records[i].now--; //使已售票减1
printf("退票成功!\n");
save(); //调用保存函数
getch(); //从控制台读取一个字符,但不显示在屏幕上
}
}
else
{
printf("该班车已发出,无法退票!\n");
getch(); /////从控制台读取一个字符,但不显示在屏幕上
}
if(i==class_num)
{ printf("输入错误!\n");
getch(); /////从控制台读取一个字符,但不显示在屏幕上
}
}
}
字数超了。。。。
Q3: 谁来帮我改改这个C语言课程设计提目..谢谢了 高分悬赏 好的话+66分.
已知学校可用的教室都存放在文件file25_1.txt中;每天的上课时间段存放在文件file25_2.txt中;教学周1~16周;每周仅允许在周1~周5上课。
某位任课教师想通过你编写的软件申请上课教室。教师申请时,将提供以下信息:教学周,星期,课节,教室。例如:第3周,周4,1~2节,B楼112教室。
如果此教室的此时间段已经被占用,则提示教师,另作其它申请;否则,批准申请,并及时将新的占用信息写入file25_1.txt。
说明:file25_1.txt文件中每行的内容按照顺序为:教学楼号,教室号,若干占用信息。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#define SIZE 8
struct class
{char bulding;
int week;
int day;
int class;
}cla[SIZE];
void write();
void load();
void complet();
int save();
int menu();
int j;
main()
{ clrscr();
for(;;)
{
switch(menu())
{
case 1:load();break;
case 2:write();break;
case 3:complet(); break;
case 4:exit(0);
}
}
}
int menu()/*menu*/
{int c;
printf("******************welcome******************\n");
printf("* 1)read *\n");
printf("* 2)input *\n");
printf("* 3) response apply *\n");
printf("* 4).exit *\n");
printf("*******************************************\n");
printf("qing shu ru");
scanf("%d",c);
if(c4c1)
printf("wrong");
return (c);
}
void load()/**/
{
FILE*fp;
int i;
if((fp=fopen("d:\\file25_1.txt","rb"))==NULL)
{
printf("cannot open infile\n");
return;
}
printf("bulding classroom week day class\n");
for(i=0;iSIZE;i++)
{
fscanf(fp,"%c%d%d%d\n",cla[i].bulding,cla[i].week,cla[i].day,cla[i].class);
if(feof(fp)!=0)
continue;
printf("%-5c%4d%4d%4d\n",cla[i].bulding,cla[i].week,cla[i].day,cla[i].class);
}
fclose(fp);
}
void write()/**/
{FILE*fp;
int i,j=10000;
printf("bulding");
scanf("%s",cla[j].bulding);
printf(".week");
scanf("%d",cla[j].week);
printf("day");
scanf("%d",cla[j].day);
printf("class");
scanf("%d",cla[j].class);
if((fp=fopen("d:\\file25_1.txt","rb"))==NULL)
{
printf("cannot open infile\n");
return;
}
for(i=0;iSIZE;i++)
{
fscanf(fp,"%c%d%d%d%d\n",cla[i].bulding,cla[i].week,cla[i].day,cla[i].class);
if((strcmp(cla[j].bulding,cla[i].bulding)==0)(strcmp(cla[j].week,cla[i].week)==0)(strcmp(cla[j].day,cla[i].day)==0)(strcmp(cla[j].class,cla[i].class)==0))
{printf("sorry,this classroom have choose!\n");break;}
if(feof(fp)!=0)
continue;}
printf("you can use this room\n");
}
void complet()
{
int i;
printf("be sure? 1)yes 2)no\n");
scanf("%d",i);
if(i==2)
return;
if(i==1)
{FILE*fp;
if((fp=fopen("d:\\file25_1.txt","a"))==NULL)
{printf("can not open\n");
return;
}
fprintf(fp,"%c%d%d%d\n",cla[10000].bulding,cla[10000].week,cla[10000].day,cla[10000].class);
fclose(fp);}
return;
}
问题补充:最好在多加些注释..因为看不明白哦.. 我还要答辩 .闷.
提问者:咘叮ㄨ鉎⒎ - 助理 二级
答复共 1 条
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#define SIZE 8
struct class //定义一个结构用来存储教室的信息
{char bulding; //所在楼
int week; //上课时间
int day; //不知道为啥用
int class; //教室
}cla[SIZE];//定义一个结构数组,最大为8个此结构,也可改变SIZE的值来改变结构数组的大小。
void write(); //声明函数
void load(); //声明函数
void complet(); //声明函数
int save(); //声明函数
int menu(); //声明函数
int j; //定义全局变量
main() //主函数
{ clrscr(); //清除屏幕
for(;;) //循环调用功能函数
{
switch(menu()) //调用菜单函数,并利用其返回值确定操作项目,switch()多分支语句运用。
{
case 1:load();break;//如返回1,运行初始化函数
case 2:write();break; //如返回2,运行功能函数
case 3:complet(); break; //如返回3,运行功能函数
case 4:exit(0); //如返回4,退出程序
}
}
}
int menu()/*menu*/ //子函数
{int c; //定义输入菜单选项
printf("******************welcome******************\n");
printf("* 1)read *\n");
printf("* 2)input *\n");
printf("* 3) response apply *\n");
printf("* 4).exit *\n");
printf("*******************************************\n");
printf("qing shu ru");
scanf("%d",c);
if(c4c1)
printf("wrong");
return (c); //返回输入的操作项
}
void load()/**/ //初始化函数
{
FILE*fp; //定义文件指针
int i;
if((fp=fopen("d:\\file25_1.txt","rb"))==NULL)//判断是否能够以二进制只读形式打开d:\file25_1.txt文件
{
printf("cannot open infile\n");
return; //异常中止,建议采用exit(1)函数返回操作系统
}
printf("bulding classroom week day class\n"); //按格式输出file25_1.txt的内容
for(i=0;iSIZE;i++) //最多只读取SIZE组数据,
{
fscanf(fp,"%c%d%d%d\n",cla[i].bulding,cla[i].week,cla[i].day,cla[i].class); //从文件中读取数据到结构数组中,其中bulding只能接受单个字符,不知是否合乎你的要求,如要接受字符串应在结构中定义数组
if(feof(fp)!=0) //判断是否到了文件末尾,此处用feof()函数判断文件是否到了末尾,但对舆空文件,其判断结果可能会出现错误,建议使用fget(c)=="\0"
continue; //如果不是末尾,跳过for中continue后面的语句,继续从for语句开头,开始执行(也就是跳过本次循环)
printf("%-5c%4d%4d%4d\n",cla[i].bulding,cla[i].week,cla[i].day,cla[i].class); //此句只能在到了文件末尾后运行,但是此for中最多能从文件中读取SIZE组数据,是否已经到文件末尾不可知,所以此句是否运行不可知。 即使此句运行了,也只能输出读出的最后一组数据,故建议你将取出数据,舆显示数据分开进行。
}
fclose(fp);
}
void write()/**/
{FILE*fp;
int i,j=10000;
printf("bulding"); //此处你应该是要输入你想查询的教室的信息,但是存储到结构cla[i]中肯定出错,因为你的j=10000,超过了开头定义的SIZE的大小范围,应该改的小於SIZE,或者把 SIZE改到大於j.
scanf("%s",cla[j].bulding);
printf(".week");
scanf("%d",cla[j].week);
printf("day");
scanf("%d",cla[j].day);
printf("class");
scanf("%d",cla[j].class);
if((fp=fopen("d:\\file25_1.txt","rb"))==NULL)
{
printf("cannot open infile\n");
return;
}
for(i=0;iSIZE;i++) //load()函数中已经存储了订阅的教室信息,此处为什麼还要将这些信息存储到结构中?不是重复吗?
{
fscanf(fp,"%c%d%d%d%d\n",cla[i].bulding,cla[i].week,cla[i].day,cla[i].class);
if((strcmp(cla[j].bulding,cla[i].bulding)==0)(strcmp(cla[j].week,cla[i].week)==0)(strcmp(cla[j].day,cla[i].day)==0)(strcmp(cla[j].class,cla[i].class)==0))//此处比较输入的信息舆存储的信息是否有重复,有则表示教室占用,strcmp(str1,str2),为字符串比较函数,而你的上述结构中的全部为整型数据,建议全部用字符型,不过此处应该不会出现错误,因为整型和字符型可相互转换;如果你改为字符型输入,建议使用stricmp(str1,str2)函数比较字符串,因为它可以忽略大小写的。
{printf("sorry,this classroom have choose!\n");break;}
if(feof(fp)!=0)//如果不用再次读文件,这里的部分也可以不用了
continue;}
printf("you can use this room\n");
}
void complet() //如果找到教室,确定是否预定函数
{
int i;
printf("be sure? 1)yes 2)no\n"); //输出选择项
scanf("%d",i);
if(i==2)
return; //选择2则退出
if(i==1)
{FILE*fp; //选择1则存储信息到文件中
if((fp=fopen("d:\\file25_1.txt","a"))==NULL)//此处为追加方式打开文件
{printf("can not open\n");
return; //再次建议使用exit(1)退出程序
}
fprintf(fp,"%c%d%d%d\n",cla[10000].bulding,cla[10000].week,cla[10000].day,cla[10000].class);
fclose(fp);} //写入刚此输入的教室信息
return; //ok返回
}
完了,以上的问题自己改改吧,好多啊。
Q4: C语言 如何将链表中删除的字符返回(函数已给)
tdio.h
#includedos.h
#includestdlib.h /*其它说明*/
#includestring.h /*字符串函数*/
#includemem.h /*内存操作函数*/
#includectype.h /*字符操作函数*/
#includealloc.h /*动态地址分配函数*/
#define LEN sizeof(STUDENT)
typedef struct stu /*定义结构体数组用于缓存数据*/
{char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;
/*函数原型*/
STUDENT *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
STUDENT *create(); /*创建链表*/
void print(STUDENT *head); /* 显示全部记录*/
void search(STUDENT *head); /*查找记录*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*排序*/
STUDENT *insert(STUDENT *head,STUDENT *new); /*插入记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/
/*主函数界面*/
main()
{STUDENT *head,new;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,new);break; /*new表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜单返回值为9则程序结束*/
}
}
}
/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}
/*菜单选择函数*/
menu_select()
{int n;
struct date d; /*定义时间结构体*/
getdate(d); /*读取系统日期并把它放到结构体d中*/
printf("\n按任一键进入主菜单...... \npress any key to enter the menu......"); /*按任一键进入主菜单*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t 欢迎 Welcome to\n");
printf("\n\t\t\t 使用学生管理系统1.0\n\n\t\t\t\t\t-----------景炎中学计算机组WJQ\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. 输入学生成绩记录 Enter the record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. 显示 Print the record\n"); /*显示*/
printf("\t\t\t3. 寻找 Search record on name\n"); /*寻找*/
printf("\t\t\t4. 删除 Delete a record\n"); /*删除*/
printf("\t\t\t5. 排序 Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. 插入 Insert record to list\n"); /*插入*/
printf("\t\t\t7. 保存 Save the file\n"); /*保存*/
printf("\t\t\t8. 读取 Load the file\n"); /*读取*/
printf("\t\t\t9. 退出 Quit\n"); /*退出*/
printf("\n\t\t 制作吴俊遒WJQ Made by Wu Junqiu.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t当前系统日期:%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/
do{
printf("\n\t\t\t输入你的选择Enter your choice(1~9):");
scanf("%d",n);
}while(n1||n9); /*如果选择项不在1~9之间则重输*/
return(n); /*返回选择项c语言load()函数,主函数根据该数调用相应的函数*/
}
/*输入函数*/
STUDENT *create()
{int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\n输出内存溢出. Out of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("输入学号Enter the num(0:list end):");
scanf("%s",p-num);
if(p-num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("输入名字Enter the name:");
scanf("%s",p-name);
printf("请输入3门成绩Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分c语言load()函数,初值为0*/
for(i=0;i3;i++) /*3门课程循环3次*/
{
do{
printf("成绩score%d:",i+1);
scanf("%d",p-score[i]);
if(p-score[i]0 || p-score[i]100) /*确保成绩在0~100之间*/
printf("数据错误,请重新输入 Data error,please enter again.\n");
}while(p-score[i]0 || p-score[i]100);
s=s+p-score[i]; /*累加各门成绩*/
}
p-sum=s; /*将总分保存*/
p-average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p-order=0; /*未排序前此值为0*/
p-next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}
/* 显示全部记录函数*/
void print(STUDENT *head)
{int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p-num,p-name,p-score[0],p-score[1],p-score[2],p-sum,p-average,p-order);
p=p-next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}
/*查找记录函数*/
void search(STUDENT *head)
{STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("请输入个姓名来查找. Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p-name,s) p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p-next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p-num,p-name,p-score[0],p-score[1],p-score[2],p-sum,p-average,p-order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\n没有该学生 There is no num %s student on the list.\n",s); /*显示没有该学生*/
}
/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("请输入要删除的学号 Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1-num,s) p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1-next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1-num,s)==0) /*学号找到c语言load()函数了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1-num,p1-name,p1-score[0],p1-score[1],p1-score[2],p1-sum,p1-average,p1-order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("\n是否要删除,输入Y删除,N则退出\nAre you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1-next; /*把第二个结点地址赋予head*/
else
p2-next=p1-next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\n学号为(Num): %s 学生以被删除(student have been deleted.)\n",s);
printf("别忘c语言load()函数了保存. Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\n没有这个学生在表上\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}
/*排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head-next; /*将原表的头指针所指的下一个结点作头指针*/
head-next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp-next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t-averagep1-averagep1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1-next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t-next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t-next=p1; /*t的后继是p1*/
p2-next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1-order=i; /*将结点序号赋值给名次*/
p1=p1-next; /*指针后移*/
}
printf("排序成功 Sorting is sucessful.\n"); /*排序成功*/
return (head);
}
/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *new)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=new; /*p0指向要插入的结点*/
printf("\nPlease enter a new record.\n"); /*提示输入记录信息*/
printf("输入学号Enter the num:");
scanf("%s",new-num);
printf("输入名字Enter the name:");
scanf("%s",new-name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i3;i++)
{
do{
printf("成绩score%d:",i+1);
scanf("%d",new-score[i]);
if(new-score[i]100||new-score[i]0)
printf("数据错误Data error,please enter again.\n");
}while(new-score[i]100||new-score[i]0);
sum1=sum1+new-score[i]; /*累加各门成绩*/
}
new-sum=sum1; /*将总分存入新记录中*/
new-average=(float)sum1/3;
new-order=0;
if(head==NULL) /*原来的链表是空表*/
/*使p0指向的结点作为头结点*/
else
{while((p0-averagep1-average)(p1-next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1-next; /*p1后移一个结点*/
}
if(p0-average=p1-average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2-next=p0; /*插到p2指向的结点之后*/
p0-next=p1;}
else
/*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\n学生Student %s 已被更新have been inserted.\n",new-name);
printf("不要忘c语言load()函数了保存Don't forget to save the new file.\n");
return(head);
}
/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("输出文件例如:c:\\score Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"w"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("打不开文件Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\n保存中...Saving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p-next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("保存成功....Save the file successfully!\n");
}
/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("倒入文件例如:c:\\score Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"r"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("打不开文件Can not open the file.\n");
return(head);
}
printf("\n寻找文件...Loading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("内存溢出!Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1-next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1-next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1-next; /*指针后移,新读入数据链到当前表尾*/
}
p2-next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("\n你成功的从文件中读取了数据!\nYou have success to read data from the file!\n");
return (head);
}
关于c语言load()函数和c语言中load函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







