作为大一的新生,疫情原因只能在家自学,大概自学了25天左右才写出了一个C的程序
大概功能就:搜索查询添加浏览之类的,因为都是自学的没有很系统的去学习,但是都是简单的代码,看看大家的讨论,很多和我一样的大一新生能一起来讨论一下更好,也希望各位大佬给我一个接下来的学习方向,因为大二的教材我不懂是啥 ,我信管专业的,希望大佬提点一下。
代码:
#include"stdio.h"
#include"malloc.h"
#include <stdlib.h>
#include"string.h"
struct student
{
char num[10];
char name[20];
char year[8];
char tel[13];
char address[50];
struct student *next;
};
void baocun(struct student * head)//保存文件
{ FILE *fp;
fp=fopen("student.bat","wb");
struct student *now;
now=head->next;
while(now!=NULL)
{
fwrite(now,sizeof(struct student),1,fp);
now=now->next ;
}
fclose(fp);
}
int duqu(struct student * head)//文件读取
{ int ch;
struct student *temp,*fn;
int n=0;
FILE *fp;
fp=fopen("student.bat","rb");
if(fp==NULL)
fp=fopen("student.bat","wb+");
getc(fp);
temp=head->next;
fn=head;
while(!feof(fp))
{
if(n==0)rewind(fp);
n++;
temp=(struct student *)malloc(sizeof(struct student));
fread(temp,sizeof(struct student),1,fp);
fn->next =temp;
fn=temp;
temp=temp->next;
if((ch=getc(fp))!=EOF)
fseek(fp,-1,1);
}
fn->next =NULL;
fclose(fp);
return n;
}
void delname(struct student *head,char name[20])//姓名删除
{
int i=0,c=0,n=0;
struct student *now,*stm[20],*fn,*stmn[20];
now=head->next;
while(now!=NULL)
{
i++;
if(!(strcmp(now->name ,name)))
{
if(i==1)
fn=head;
stm[c]=fn;
stmn[n]=now;
c++;
n++;
printf("%d.%s %s\t%s\t%s\t\t%s\n",c,now->num,now->name,now->year,now->tel,now->address);
}
fn=now;
now=now->next;
}
if(c==0)
{printf("\t未查询到信息\t\n\n");return;
}
printf("\n\t共计查询得到 %d 条数据",c);
printf("\n\t(输入0为取消删除)删除第:");
scanf("%d",&c);
if(c==0)
return;
c--;
n=c;
stm[c]->next=stmn[c]->next;
system("cls");
printf("\n\n\n删除成功!\n\n\n");
}
void delnum(struct student *head,char num[10])//学号删除
{
int i=0,c=0,n=0;
struct student *now,*stm[20],*fn,*stmn[20];
now=head->next;
while(now!=NULL)
{
i++;
if(!(strcmp(now->num ,num)))
{
if(i==1)
fn=head;
stm[c]=fn;
stmn[n]=now;
c++;
n++;
printf("%d.%s %s\t%s\t%s\t\t%s\n",c,now->num,now->name,now->year,now->tel,now->address);
}
fn=now;
now=now->next;
}
if(c==0)
{printf("\t未查询到信息\t\n\n");return;
}
printf("\n\t共计查询得到 %d 条数据",c);
printf("\n\t(输入0为取消删除)删除第:");
scanf("%d",&c);
if(c==0)
return;
c--;
n=c;
stm[c]->next=stmn[c]->next;
system("cls");
printf("\n\n\n删除成功!\n\n\n");
}
void del(struct student *head)//数据删除
{
char num[10];
char name[20];
struct student *tem;
tem=NULL;
int i;
printf("\n\n\t1.学号方式查询删除\n\t2.姓名方式查询删除");
scanf("%d",&i);
switch(i)
{
case 1:{printf("输入学号\n");scanf("%s",num);system("cls");delnum(head,num);
break; }
case 2:{printf("输入姓名\n");scanf("%s",name);system("cls");delname(head,name);break;}
default : {printf("输入错误");break;}
}}
void num_change(struct student *head,char num[10])//学号修改
{
int c=0;
struct student *now,*stm[20];
now=head->next;
while(now!=NULL)
{
if(!(strcmp(now->num ,num)))
{
stm[c]=now;
c++;
printf("%d.%s %s\t%s\t%s\t\t%s\n",c,now->num,now->name,now->year,now->tel,now->address);
}
now=now->next;
}
if(c==0)
{printf("\t未查询到信息\t\n\n");return;
}
printf("\n\t共计查询得到 %d 条数据",c);
printf("\n\t修改第:");
scanf("%d",&c);
c--;
printf("输入学号 姓名\t年级\t电话\t地址\t\n");
scanf("%s%s%s%s%s",stm[c]->num,stm[c]->name,&(stm[c]->year),stm[c]->tel,stm[c]->address);
system("cls");
printf("\n\n\n修改成功!\n\n\n");
}
void name_change(struct student *head,char name[20])//名字修改
{
int c=0;
struct student *now,*stm[20];
now=head->next;
while(now!=NULL)
{
if(!(strcmp(now->name ,name)))
{
stm[c]=now;
c++;
printf("%d.%s %s\t%s\t%s\t\t%s\n",c,now->num,now->name,now->year,now->tel,now->address);
}
now=now->next;
}
if(c==0)
{printf("\t未查询到信息\t\n\n");return;
}
printf("\n\t共计查询得到 %d 条数据",c);
printf("\n\t修改第:");
scanf("%d",&c);
c--;
printf("输入学号 姓名\t年级\t电话\t地址\t\n");
scanf("%s%s%d%s%s",stm[c]->num,stm[c]->name,&(stm[c]->year),stm[c]->tel,stm[c]->address);
system("cls");
printf("\n\n\n修改成功!\n\n\n");
}
void change(struct student *head)//数据修改
{
char num[10];
char name[20];
struct student *tem;
tem=NULL;
int i;
printf("\n\n\t1.学号方式查询修改\n\t2.姓名方式查询修改");
scanf("%d",&i);
switch(i)
{
case 1:{printf("输入学号\n");scanf("%s",num);system("cls");num_change(head,num);
break; }
case 2:{printf("输入姓名\n");scanf("%s",name);system("cls");name_change(head,name);break;}
default : {printf("输入错误");break;}
}
}
void num_seek(struct student *head,char num[10])//学号查询
{
int i=0;
struct student *now;
now=head->next;
while(now!=NULL)
{
if(!(strcmp(now->num ,num)))
{ i++;
printf("%s %s\t%s\t%s\t\t%s\n",now->num,now->name,now->year,now->tel,now->address);
}
now=now->next;
}
printf("\n\t共计查询得到 %d 条数据",i);
}
void name_seek(struct student *head,char name[20])//姓名查询
{
int i=0;
struct student *now;
now=head->next;
while(now!=NULL)
{
if(!(strcmp(now->name ,name)))
{ i++;
printf("%s %s\t%s\t%s\t\t%s\n",now->num,now->name,now->year,now->tel,now->address);
}
now=now->next;
}printf("\n\t共计查询得到 %d 条数据",i);
}
void seek(struct student *head)//数据查询
{
char num[10];
char name[20];
int i;
printf("\n\n\t1.学号方式查询\n\t2.姓名方式查询");
scanf("%d",&i);
switch(i)
{
case 1:{printf("输入学号\n");scanf("%s",num);system("cls");printf("\n\n学号 姓名\t年级\t电话\t\t\t地址\t\n\n");num_seek(head,num);;
break; }
case 2:{printf("输入姓名\n");scanf("%s",name);system("cls");printf("\n\n学号 姓名\t年级\t电话\t\t\t地址\t\n\n");name_seek(head,name);break;}
default : {printf("输入错误");break;}
}
}
void view(struct student * head)//预览数据
{
int i=0;
struct student *now;
now=head->next;
system("cls");
printf("\n\n学号 姓名\t年级\t电话\t\t\t地址\n\n");
while(now!=NULL)
{
i++;
printf("%s\t%s\t%s\t%s\t\t\t%s\n",now->num,now->name,now->year,now->tel,now->address);
now=now->next ;
}
printf("\n\t共计 %d 条数据",i);
}
void increat(struct student *head)//增加数据
{ system("cls");
struct student *now,*fn;
now=head->next;
fn=head;
while(now!=NULL)
{
fn=now;
now=now->next;
}
now=(struct student *)malloc(sizeof(struct student));
printf("输入学号 姓名 \t年级\t电话 \t\t地址\t\n");
scanf("%s\t%s\t%s\t%s\t%s",now->num,now->name,&now->year,now->tel,now->address);
fn->next=now;
now->next =NULL;
system("cls");
printf("\n\n\n增加成功!\n\n\n");
}
int menu()//菜单
{
int menu;
fflush(stdin);
printf("\n\n\n\t1.增加数据\t\n\t2.数据修改\t\n\t3.数据查询\t\n\t4.数据删除\t\n\t5.预览数据\t\n");
scanf("%d",&menu);
return menu;
}
struct student* increatlib()//增加链表
{
struct student *head;
head=(struct student *)malloc(sizeof(struct student));
head->next=NULL;
return head;
}
int main()
{ int n;
struct student *head;
head=increatlib();
printf("\n\n\t正在读取数据......");
n=duqu(head);
printf("\n\n\t共计读取数据%d条\n",n);
while(1)
{
switch(menu())
{
case 1:{increat(head);baocun(head);break;}
case 5:{view(head);break;}
case 3:{seek(head);break;}
case 2:{change(head);baocun(head);break;}
case 4:{del(head);baocun(head);break;}
default : {system("cls");printf("输入错误");break;}
}
}
return 0;
}