1.文件操作的基本方法:C语言将计算机的输入输出设备都看作是文件。例如,键盘文件、屏幕文件等.

2.从结构化程序和函数角度分析,按照功能,将不同的代码放到一起,用大括号括起来,然后给这段代码起个名字表示,然后还要设置上参数,一遍使用这个函数的时候向里传递实际的值,类似一个加法的函数,完成两个数的相加。

3.结构体的作用和应用,

do

{ system("cls");

printf("\n\n\n********学生信息管理系统********\n\n");

printf("1. 创建学生信息\n\n");

printf("2. 打印学生信息\n\n");

printf(" 3. 查询学生信息\n\n");

printf("4. 修改学生信息\n\n");

printf("5. 删除学生信息\n\n");

printf("6. 学生成绩信息排名\n\n");

printf("0. 退出系统\n\n");

printf("请选择(0-6):");

scanf("%d",&choice);

switch(choice)

{

case 1: k=input(stu); break;/*创建学生信息*/

case 2: output( stu) ; break;/*打印学生信息*/

case 3: inquire(stu); break;/*查询学生信息*/

case 4: change(stu); break;/*修改学生信息*/

case 5: deletel(stu); break;/*删除学生信息*/

case 6: sort(stu); break;/*学生成绩信息排名*/

case 0: break;

}

}while(choice!=0);

这个是显示一个提示的菜单,然后接收用户的按键,根据用户的按键(1-6)调用相应的函数执行相应的操作。一直不停循环,直到choice==0的作用是可以让用户在系统中不停执行各种操作,直到选择退出。

注意一下,STUDENTS stu[100];

是定义了一个自己结构的数组变量,变量名是stu

结构体typedef struct

{ intnum;/*学号*/

char name[20];/*姓名*/

charsex[5];/*性别*/

intage;/*年龄*/

charstudentclass[20];/*班级*/

intscore;/*成绩*/

}STUDENTS;

定义了一个结构体的类型STUDENTS,类似int型,这里用typedef的作用是把结构体struct直接定义成STUDENTS,免去后后面反复写struct的麻烦。

stu[100]相当于定义了100页的一个登记本,每一页上可以记录学号,姓名等信息,每一页用stu[0],stu[1],stu[2]…这样来访问,input(stu)的作用是把这个数组一块传给函数input,相当于把整本登记本一起传过去,在input函数中,

int input(STUDENTS stu[])

用stu[]来接收传过来的整个数组,数组大小为空的原因是可以处理不同大小的数组,类似这个函数可以处理任意厚度的登记本。



下面是修改之后的员工销售量信息管理系统软件

#include"stdio.h"

#include"stdlib.h"

#include"string.h"

#include"conio.h"

#define PAGE 3

#define MAX 1000

#define N 5

int k=0;

/*结构体类型*/

typedef struct

{ intnum;/*工号*/

charname[20];/*姓名*/

charsex[5];/*性别*/

intage;/*年龄*/

char studentclass[20];/*部门*/

intscore;/*销售量*/

}STUDENTS;

int read_file(STUDENTS stu[])

{ FILE *fp;

int i=0;

if((fp=fopen("stu.txt","rt"))==NULL)

{printf("\n\n*****库存文件不存在!请创");

return 0;

}

while(feof(fp)!=1)

{

fread(&stu[i],sizeof(STUDENTS),1,fp);

if(stu[i].num==0)

break;

else

i++;

}

fclose(fp);

return i;

}

void save_file(STUDENTS stu[],int sum)

{FILE*fp;

int i;

if((fp=fopen("stu.txt","wb"))==NULL)

{printf("写文件错误!\n");

return;

}

for(i=0;i<sum;i++)

if(fwrite(&stu[i],sizeof(STUDENTS),1,fp)!=1)

printf("写文件错误!\n");

fclose(fp);

}

/*创建员工信息*/

int input(STUDENTS stu[])

{ int i,x;

for(i=0;i<1000;i++)

{

system("cls");

printf("\n\n 录入员工信息 (最多%d个)\n",MAX);

printf("----------------------------\n");

printf("\n第%d个员工",k+1);

printf("\n 请输入员工的学号:");

scanf("%d",&stu[k].num);

printf("\n 请输入员工的姓名:");

scanf("%s",stu[k].name);

printf("\n请输入员工的性别:");

scanf("%s",stu[k].sex);

printf("\n 请输入员工的年龄:");

scanf("%d",&stu[k].age);

printf("\n 请输入员工的部门:");

scanf("%s",stu[k].studentclass);

printf("\n 请输入员工的销售量:");

scanf("%d",&stu[k++].score);

printf("\n 请按1键返回菜单或按0键继续创建");

scanf("%d",&x);

if(x)

break;

}

return k;

}

/*删除员工信息*/

void deletel(STUDENTS stu[])

{system("cls");

charStuname2[20];

int i,j;

printf("请输入员工姓名:");

scanf("%s",Stuname2);

printf("\n");

for(i=0;i<k;i++)

if(strcmp(stu[i].name,Stuname2)==0)

for(j=0;j<20;j++)

stu[i].name[j]=stu[i+1].name[j];

k--;

printf("删除成功\n");

printf("按任意键加回车返回主菜单!");

scanf("%d",&i);

getchar();

}

/*打印员工信息*/

void output(STUDENTS stu[])

{system("cls");

int i;

for(i=0;i<k;i++)

printf("工号:%d,姓名:%s,性别:%s,年龄:%d,部门:%s,销售量: %d\n",stu[i].num,stu[i].name,

stu[i].sex,stu[i].age,stu[i].studentclass,stu[i].score);

printf("按任意键加回车返回主菜单!");

scanf("%d",&i);

getchar();

}

/*查询员工信息*/

void inquire(STUDENTS stu[])

{ int i;

int num;

system("cls");

printf(" \n\n请输入您要查找的员工的学号");

scanf("%d",&num);

for(i=0;i<k;i++)

if(num==stu[i].num)

printf("\n\n\n工号:%d,姓名:%s,性别:%s,年龄:%d,部门:%s,销售量: %d\n",stu[i].num,stu[i].name,

stu[i].sex,stu[i].age,stu[i].studentclass,stu[i].score);

printf("按任意键加回车返回主菜单!");

scanf("%d",&i);

getchar();

}

/*修改员工信息*/

void change(STUDENTS stu[])

{ int num,i,choice;

system("cls");

printf("\n\n\n 请输入您要修改的员工的学号");

scanf("%d",&num);

for(i=0;i<k;i++)

{if(num==stu[i].num)

printf("\n工号:%d,姓名:%s,性别:%s,年龄:%d,部门:%s,销售量: %d\n",stu[i].num,stu[i].name,

stu[i].sex,stu[i].age,stu[i].studentclass,stu[i].score);

printf("\n\n\n ********请输入您想要修改的数据********\n\n");

printf(" 1. 工号\n\n");

printf(" 2. 姓名\n\n");

printf(" 3. 性别\n\n");

printf(" 4. 年龄\n\n");

printf(" 5. 部门\n\n");

printf(" 6. 销售量\n\n");

printf(" 请选择(1-6):");

scanf("%d",&choice);

switch(choice)

{case 1:{

printf("\n 请输入你改的新工号");

scanf("%d",&stu[i].num);

break;

}

case 2:{

printf("\n 请输入你改的新姓名");

scanf("%s",stu[i].name);

break;

}

case 3:{

printf("\n 请输入你改的新性别");

scanf("%s",stu[i].sex);

break;

}

case 4:{

printf("\n 请输入你改的新年龄");

scanf("%d",&stu[i].age);

break;

}

case 5:{

printf("\n 请输入你改的新部门");

scanf("%s",stu[i].studentclass);

break;

}

case 6:{

printf("\n 请输入你改的新销售量");

scanf("%d",&stu[i].score);

break;

}

}

printf("工号:%d,姓名:%s,性别:%s,年龄:%d,部门:%s,销售量: %d\n",stu[i].num,stu[i].name,

stu[i].sex,stu[i].age,stu[i].studentclass,stu[i].score);

printf("按任意键加回车返回主菜单!");

scanf("%d",&i);

break;

}

}

/*员工成绩信息排名*/

void sort(STUDENTS stu[])

{ int i,j,n=1,x;

system("cls");

int t;

for(i=0;i<k-1;i++)

for(j=i+1;j<k;j++)

if(stu[i].score<stu[j].score)

{t=stu[i].score;

stu[i].score=stu[j].score;

stu[j].score=t;

t=stu[i].num;

stu[i].num=stu[j].num;

stu[j].num=t;

}

for(i=0;i<k;i++)

printf("排名 工号 销售量\n %d %d%d\n",n++,stu[i].num,stu[i].score);

printf("按任意键加回车返回主菜单!");

scanf("%d",&x);

getchar();

}

void pagedis()

{

printf(" \n\n\n**********************************\n");

printf("**\n");

printf("**\n");

printf("**\n");

printf(" * 欢迎进入员工信息管理系统 *\n");

printf("**\n");

printf("**\n");

printf("**\n");

printf(" **********************************\n");

}

void check()

{

charuserName[5];/*用户名*/

charuserPWD[5];/*密码*/

inti,sum;

system("color 4E");

for(i =1; i < 4; i++)

{

/*用户名和密码均为12345;*/

printf(" 用户名和密码均为12345\n\n");

printf("\n 请输入您的用户名:");

gets(userName);

printf("\n 请输入您的密码:");

gets(userPWD);

if((strcmp(userName,"12345")==0) &&(strcmp(userPWD,"12345")==0))/*验证用户名和密码*/

{

printf("用户名和密码正确,显示主菜单");

return;

}

else

{

if (i < 3)

{

printf("用户名或密码错误,提示用户重新输入");

printf("用户名或密码错误,请重新输入!");

}

else

{

printf("连续3次输错用户名或密码,退出系统。");

printf("您已连续3次将用户名或密码输错,系统将退出!");

exit(1);

}

}

}

}

void menu()

{

STUDENTSstu[20];

intchoice,k,sum;

sum=read_file(stu);

if(sum==0)

{ printf("首先录入基本库存信息!按回车后进入*****\n");

getch();

sum=input(stu);

}

do

{ system("cls");

printf("\n\n\n********员工信息管理系统********\n\n");

printf("1. 创建员工信息\n\n");

printf("2. 打印员工信息\n\n");

printf(" 3. 查询员工信息\n\n");

printf("4. 修改员工信息\n\n");

printf("5. 删除员工信息\n\n");

printf("6. 员工销售量信息排名\n\n");

printf("0. 退出系统\n\n");

printf("请选择(0-6):");

scanf("%d",&choice);

switch(choice)

{

case1: k=input(stu); break;/*创建员工信息*/

case2: output( stu) ; break;/*打印员工信息*/

case3: inquire(stu); break;/*查询员工信息*/

case4: change(stu); break;/*修改员工信息*/

case5: deletel(stu); break;/*删除员工信息*/

case6: sort(stu); break;/*员工销售量信息排名*/

case0: break;

}

}while(choice!=0);

save_file(stu,sum);

}

int main()

{

int i,sum;

pagedis();

check();

menu();

}


下面是图书管理系统

#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>structbooks_list{charauthor[20];/*作者名*/charbookname[20];/*书名*/charpublisher[20];/*出版单位*/charpbtime[15];/*出版时间*/charloginnum[10];/*登陆号*/floatprice;/*价格*/charclassfy[10];/*分类号*/structbooks_list*next;/*链表的指针域*/};structbooks_list*Create_Books_Doc();/*新建链表*/voidInsertDoc(structbooks_list*head);/*插入*/voidDeleteDoc(structbooks_list*head,intnum);/*删除*/voidPrint_Book_Doc(structbooks_list*head);/*浏览*/voidsearch_book(structbooks_list*head);/*查询*/voidinfo_change(structbooks_list*head);/*修改*/voidsave(structbooks_list*head);/*保存数据至文件*//*新建链表头节点*/structbooks_list*Create_Books_Doc(){structbooks_list*head;head=(structbooks_list*)malloc(sizeof(structbooks_list));/*分配头节点空间*/head->next=NULL;/*头节点指针域初始化,定为空*/returnhead;}/*保存数据至文件*/voidsave(structbooks_list*head){structbooks_list*p;FILE*fp;p=head;fp=fopen("data.txt","w+");/*以写方式新建并打开data.txt文件*/fprintf(fp,"┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n");/*向文件输出表格*/fprintf(fp,"┃登录号┃书名┃作者┃出版单位┃出版时间┃分类号┃价格┃\n");fprintf(fp,"┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n");/*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/while(p->next!=NULL){p=p->next;fprintf(fp,"┃%-6.6s┃%-10.10s┃%-10.10s┃%-10.10s┃%-12.12s┃%-6.6s┃%.2f┃\n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price);}fprintf(fp,"┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n");fclose(fp);printf("已将图书数据保存到data.txt文件\n");}/*插入*/voidInsertDoc(structbooks_list*head){/*定义结构体指针变量s指向开辟的新结点首地址p为中间变量*/structbooks_list*s,*p;charflag='Y';/*定义flag,方便用户选择重复输入*/p=head;/*遍历到尾结点,p指向尾结点*/while(p->next!=NULL){p=p->next;}/*开辟新空间,存入数据,添加进链表*/while(flag=='Y'||flag=='y'){s=(structbooks_list*)malloc(sizeof(structbooks_list));printf("\n请输入图书登陆号:");fflush(stdin);scanf("%s",s->loginnum);printf("\n请输入图书书名:");fflush(stdin);scanf("%s",s->bookname);printf("\n请输入图书作者名:");fflush(stdin);scanf("%s",s->author);printf("\n请输入图书出版社:");fflush(stdin);scanf("%s",s->publisher);printf("\n请输入图书出版时间:");fflush(stdin);scanf("%s",s->pbtime);printf("\n请输入号:");fflush(stdin);scanf("%s",s->classfy);printf("\n请输入图书价格:");fflush(stdin);scanf("%f",&s->price);printf("\n");p->next=s;/*将新增加的节点添加进链表*/p=s;/*p指向尾节点,向后移*/s->next=NULL;printf("━━━━添加成功!━━━━");printf("\n继续添加?(Y/N):");fflush(stdin);scanf("%c",&flag);printf("\n");if(flag=='N'||flag=='n'){break;}elseif(flag=='Y'||flag=='y'){continue;}}save(head);/*保存数据至文件*/return;}/*查询操作*/voidsearch_book(structbooks_list*head){structbooks_list*p;chartemp[20];p=head;if(head==NULL||head->next==NULL)/*判断数据库是否为空*/{printf("━━━━图书库为空!━━━━\n");}else{printf("请输入您要查找的书名:");fflush(stdin);scanf("%s",temp);/*指针从头节点开始移动,遍历至尾结点,查找书目信息*/while(p->next!=NULL){p=p->next;if(strcmp(p->bookname,temp)==0){printf("\n图书已找到!\n");printf("\n");printf("登录号:%s\t\n",p->loginnum);printf("书名:%s\t\n",p->bookname);printf("作者名:%s\t\n",p->author);printf("出版单位:%s\t\n",p->publisher);printf("出版时间:%s\t\n",p->pbtime);printf("分类号:%s\t\n",p->classfy);printf("价格:%.2f\t\n",p->price);}if(p->next==NULL){printf("\n查询完毕!\n");}}}return;}/*浏览操作*/voidPrint_Book_Doc(structbooks_list*head){structbooks_list*p;if(head==NULL||head->next==NULL)/*判断数据库是否为空*/{printf("\n━━━━没有图书记录!━━━━\n\n");return;}p=head;printf("┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n");printf("┃登录号┃书名┃作者┃出版单位┃出版时间┃分类号┃价格┃\n");printf("┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n");/*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/while(p->next!=NULL){p=p->next;printf("┃%-6.6s┃%-10.10s┃%-10.10s┃%-10.10s┃%-12.12s┃%-6.6s┃%.2f┃\n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price);/*循环输出表格*/}printf("┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n");printf("\n");}/*修改操作*/voidinfo_change(structbooks_list*head){structbooks_list*p;intpanduan=0;/*此变量用于判断是否找到书目*/chartemp[20];p=head;printf("请输入要修改的书名:");scanf("%s",temp);while(p->next!=NULL){p=p->next;if(strcmp(p->bookname,temp)==0){printf("\n请输入图书登陆卡号:");fflush(stdin);scanf("%s",p->loginnum);printf("\n请输入图书书名:");fflush(stdin);scanf("%s",p->bookname);printf("\n请输入图书作者名:");fflush(stdin);scanf("%s",p->author);printf("\n请输入图书出版社:");fflush(stdin);scanf("%s",p->publisher);printf("\n请输入图书出版时间:");fflush(stdin);scanf("%s",p->pbtime);printf("\n请输入号:");fflush(stdin);scanf("%s",p->classfy);printf("\n请输入图书价格:");fflush(stdin);scanf("%f",&p->price);printf("\n");panduan=1;}}if(panduan==0){printf("\n━━━━没有图书记录!━━━━\n\n");}return;}/*删除操作*/voidDeleteDoc(structbooks_list*head){structbooks_list*s,*p;/*s为中间变量,p为遍历时使用的指针*/chartemp[20];intpanduan;/*此变量用于判断是否找到了书目*/panduan=0;p=s=head;printf("[请输入您要删除的书名]:");scanf("%s",temp);/*遍历到尾结点*/while(p!=NULL){if(strcmp(p->bookname,temp)==0){panduan++;break;}p=p->next;}if(panduan==1){for(;s->next!=p;)/*找到所需删除卡号结点的上一个结点*/{s=s->next;}s->next=p->next;/*将后一节点地址赋值给前一节点的指针域*/free(p);printf("\n━━━━删除成功!━━━━\n");}else/*未找到相应书目*/{printf("您输入的书目不存在,请确认后输入!\n");}return;}intmain(void){structbooks_list*head;charchoice;head=NULL;for(;;)/*实现反复输入选择*/{printf("┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n");printf("┃┃图书管理系统┃┃\n");printf("┃┗━━━━━━━━━━━━━━━━━━━┛┃\n");printf("┃●[1]图书信息录入┃\n");printf("┃┃\n");printf("┃●[2]图书信息浏览┃\n");printf("┃┃\n");printf("┃●[3]图书信息查询┃\n");printf("┃┃\n");printf("┃●[4]图书信息修改┃\n");printf("┃┃\n");printf("┃●[5]图书信息删除┃\n");printf("┃┃\n");printf("┃●[6]退出系统┃\n");printf("┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");printf("请选择:");fflush(stdin);scanf("%c",&choice);if(choice=='1'){if(head==NULL){head=Create_Books_Doc();}InsertDoc(head);}elseif(choice=='2'){Print_Book_Doc(head);}elseif(choice=='3'){search_book(head);}elseif(choice=='4'){info_change(head);}elseif(choice=='5'){DeleteDoc(head);}elseif(choice=='6'){printf("\n");printf("━━━━━━━━感谢使用图书管理系统━━━━━━━━\n");break;}else{printf("━━━━输入错误,请重新输入!━━━━");break;}}return0;}