按照题目要求:
1、实现文件写入功能,一次写入一个职工信息。
2、实现查询功能(包含4种条件查询)。
3、实现信息删除和修改并写入文件。
根据题意,数据用结构数组存储,且职工编号唯一性,因此单独两个函数计算信息长度和编号。
除条件查询函数和读取文件的函数,不会自行free释放内存。如你写代码调用,注意自行写free语句释放。
下面是代码:
#include <stdioh>
#include <malloch>
#include <stringh>
#include <windowsh>
#include <conioh>
#define PATH "C:\\workerdata"
typedef struct worker//职员
{
int id;
char name[20];
int sex;//0:女。1:男
int byy;
int bmm;
int bdd;
}WR;
int write2File(char path[]);//写入文件,成功返回1,失败返回-1
int getLen(char path[]);//重新计算信息长度,成功返回1,失败返回-1
int getMaxId(char path[]);//获取最大的id,避免ID重复,成功返回1,失败返回-1
WR readFormFile(char path[]);//读取文件内容到结构体数组,成功返回数组,失败返回NULL
void showWRS();//打印输出文件所有内容
WR selectBy(WR wrs,int n);//根据条件查询,查询成功返回查到的第一个节点(对于唯一的编号而言是精准查询),失败返回NULL。
//注意:该函数不会自动释放wrs内存,单独调用后需单独释放。单独调用n传0,在修改模块中调用n传1
int init();//初始化参数
int updateWr();//更新,成功返回1,失败返回-1
int showMenu();//菜单
int len=0;
int id=0;
int main()
{
init();
if(showMenu()==-1) return 1;
return 0;
}
int showMenu()
{
int n;
WR wrs=NULL;;
while(1)
{
n=0;
printf("职工档案管理系统\n1、输入一个职工信息\n2、查询所有职工信息\n3、条件查询职工信息\n4、修改删除职工信息\n");
while(n<1 || n>4) scanf("%d",&n);
switch(n)
{
case 1:if(write2File(PATH)==-1) return -1; break;
case 2:showWRS(); break;
case 3:
wrs=readFormFile(PATH);
selectBy(wrs,0);
free(wrs);
break;
case 4:if(updateWr()==-1) return -1;break;
}
fflush(stdin);
printf("按任意键继续。。。。。"),getch();
system("cls");
}
}
int init()
{
if(getLen(PATH)!=-1 && getMaxId(PATH)!=-1) return 1;
return -1;
}
int updateWr()
{
int i,del=-1;
WR wr=NULL,wrs=NULL;
FILE fp=NULL;
wrs=readFormFile(PATH);
if(!wrs) return -1;
wr=selectBy(wrs,1);
printf("请输入 *** 作(1、删除,2、修改):");
while(del!=2 && del!=1)scanf("%d",&del);
if(del==2)
{
printf("请输入修改后职工信息:\n");
printf("请输入职工姓名:"),scanf("%s",wr->name);
printf("请选择职工性别(0:女。1:男):"),scanf("%d",&wr->sex);
printf("请输入职工生日(年月日用空格分隔):"),scanf("%d%d%d",&wr->byy,&wr->bmm,&wr->bdd);
}
if(remove(PATH)==-1) return -1;
if(!(fp=fopen(PATH,"w"))) return -1;
for(i=0;i<len;i++)
if(del==2 ||(del==1 && wr!=&wrs[i]))
fprintf(fp,"%d %s %d %d %d %d%s",wrs[i]id,wrs[i]name,wrs[i]sex,wrs[i]byy,wrs[i]bmm,wrs[i]bdd,"\n");
printf("更新成功!\n");
fclose(fp);
free(wrs);
return 1;
}
WR selectBy(WR wrs,int n)
{
int i;
WR swr,sp=NULL;
if(!wrs) return NULL;
while(n<1 || n>4)printf("请输入筛选条件(1、编号,2、姓名,3、性别,4、出生日期):"),scanf("%d",&n);
switch(n)
{
case 1:printf("请输入要查找的编号:"),scanf("%d",&swrid);break;
case 2:printf("请输入要查找的姓名:"),scanf("%s",swrname);break;
case 3:printf("请输入要查找的性别(1、男,0、女):"),scanf("%d",&swrsex);break;
case 4:printf("请输入职工出生日期(年月日用空格分隔):"),scanf("%d%d%d",&swrbyy,&swrbmm,&swrbdd);break;
}
printf("%8s %8s %8s %10s\n","职工编号","职工姓名","职工性别","职工年龄");
for(i=0;i<len;i++)
if((n==1 && wrs[i]id==swrid) ||(n==2 && strcmp(wrs[i]name,swrname)==0) ||(n==3 && wrs[i]sex==swrsex) ||(n==4 && wrs[i]byy==swrbyy && wrs[i]bmm==swrbmm && wrs[i]bdd==swrbdd))
{
if(sp==NULL) sp=&wrs[i];
printf("%8d %8s %8s %04d-%02d-%02d\n",wrs[i]id,wrs[i]name,wrs[i]sex"男":"女",wrs[i]byy,wrs[i]bmm,wrs[i]bdd);
}
return sp;
}
void showWRS()
{
int i;
WR wrs=NULL;
wrs=readFormFile(PATH);
if(wrs)
{
printf("%8s %8s %8s %10s\n","职工编号","职工姓名","职工性别","职工年龄");
for(i=0;i<len;i++)
printf("%8d %8s %8s %04d-%02d-%02d\n",wrs[i]id,wrs[i]name,wrs[i]sex"男":"女",wrs[i]byy,wrs[i]bmm,wrs[i]bdd);
}
free(wrs);
}
int getLen(char path[])
{
FILE fp=NULL;
WR wr;
if(!(fp=fopen(path,"r"))) return -1;
len=0;
while((fscanf(fp,"%d%s%d%d%d%d",&wrid,wrname,&wrsex,&wrbyy,&wrbmm,&wrbdd))!=-1)
len++;
fclose(fp);
return 1;
}
int getMaxId(char path[])
{
FILE fp=NULL;
WR wr;
if(!(fp=fopen(path,"r"))) return -1;
len=0;
while((fscanf(fp,"%d%s%d%d%d%d",&wrid,wrname,&wrsex,&wrbyy,&wrbmm,&wrbdd))!=-1)
if(id<wrid) id=wrid;
fclose(fp);
return 1;
}
WR readFormFile(char path[])
{
int i;
FILE fp=NULL;
WR wrs=NULL;
getLen(path);
if(!(fp=fopen(path,"r"))) return NULL;
if(len==0) return NULL;
wrs=(WR )malloc(sizeof(WR)len);
if(!wrs) return NULL;
i=0;
while((fscanf(fp,"%d%s%d%d%d%d",&wrs[i]id,wrs[i]name,&wrs[i]sex,&wrs[i]byy,&wrs[i]bmm,&wrs[i]bdd))!=-1)i++;
fclose(fp);
return wrs;
}
int write2File(char path[])
{
FILE fp=NULL;
WR wr;
if(!(fp=fopen(path,"a"))) return -1;
wrid=id++;
printf("-----------------------------\n");
printf("请输入职工姓名:"),scanf("%s",wrname);
printf("请选择职工性别(0:女。1:男):"),scanf("%d",&wrsex);
printf("请输入职工生日(年月日用空格分隔):"),scanf("%d%d%d",&wrbyy,&wrbmm,&wrbdd);
printf("-----------------------------\n\n");
fprintf(fp,"%d %s %d %d %d %d%s",wrid,wrname,wrsex,wrbyy,wrbmm,wrbdd,"\n");
fclose(fp);
len++;
return len;
}
你好
网上找的,希望你能用
#include "stdioh"
#include "stdlibh"
#include "stringh"
typedef struct subjects
{
int num;
char name[20];
char kind[10];
int stime;
int ttime;
int etime;
int score;
int term;
struct subjects next;
}SUB;
SUB create_form()
{
SUB head,tail,p;
int num,stime,ttime;
int etime,score,term;
char name[20],kind[10];
int size=sizeof(SUB);
head=tail=NULL;
printf("输入选修课程信息:\n");
scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);
while(num!=0){
p=(SUB )malloc(size);
p->num=num;
strcpy(p->name,name);
strcpy(p->kind,kind);
p->stime=stime;
p->ttime=ttime;
p->etime=etime;
p->score=score;
p->term=term;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);
}
tail->next=NULL;
return head;
}
void savefile(SUB head)
{
SUB p;
FILE fp;
fp=fopen("subjectstxt","w");
fprintf(fp,"课程编号 课程名称 课程性质 总学时 授课学时 实验或上机学时 学分 开课学期\n");
for(p=head;p;p=p->next)
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
fclose(fp);
}
void savefileadd(SUB head)
{
SUB p;
FILE fp;
fp=fopen("subjectsaddtxt","w");
fprintf(fp,"课程编号 课程名称 课程性质 总学时 授课学时 实验或上机学时 学分 开课学期\n");
for(p=head;p;p=p->next)
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
fclose(fp);
}
void savefiledel(SUB head)
{
SUB p;
FILE fp;
fp=fopen("subjectsdeltxt","w");
fprintf(fp,"课程编号 课程名称 课程性质 总学时 授课学时 实验或上机学时 学分 开课学期\n");
for(p=head;p;p=p->next)
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
fclose(fp);
}
void prin(SUB head)
{
SUB ptr;
if(head==NULL){
printf("NO RECORDS!\n");
return;
}
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学期\n");
for(ptr=head;ptr;ptr=ptr->next)
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
}
void search(SUB head)
{
int a,num;
int t=1;
char type[10];
char ch='a',ch1;
SUB ptr;
while(ch!=' '){
printf("若要按课程性质查找请输入1,若要按学分查找请输入2:\n");
scanf("%d",&a);
switch(a){
case 1:printf("请输入要查找的课程的性质:\n");
scanf("%s",type);
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学期\n");
for(ptr=head;ptr;ptr=ptr->next)
if(strcmp(type,ptr->kind)==0){
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
t=0;
}
if(t) printf("未找到!\n");
t=1;
break;
case 2:printf("输入要查找的课程的学分\n");
scanf("%d",&num);
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学期\n");
for(ptr=head;ptr;ptr=ptr->next)
if(ptr->score==num){
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
t=0;
}
if(t) printf("未找到!\n");
t=1;
}
printf("继续查找请按回车键,结束请按空格键:\n");
ch1=getchar();
ch=getchar();
}
}
SUB insert(SUB head)
{
SUB ptr,ptr2,subj;
int size=sizeof(SUB);
char ch='a',ch1;
while(ch!=' '){
subj=(SUB )malloc(size);
ptr=subj;
printf("输入要插入的课程信息:\n");
scanf("%d%s%s%d%d%d%d%d",&subj->num,subj->name,subj->kind,&subj->stime,&subj->ttime,&subj->etime,&subj->score,&subj->term);
if(head==NULL){
head=ptr;
head->next=NULL;
}
else{
for(ptr2=head;ptr2;ptr2=ptr2->next)
if(ptr2->next==NULL){
ptr2->next=subj;
subj->next=NULL;
break;
}
}
printf("继续插入请按回车,结束请按空格:\n");
ch1=getchar();
ch=getchar();
}
return head;
}
SUB del(SUB head)
{
SUB p1,p2;
char ch='a',ch1;
int num;
while(ch!=' '){
printf("输入想要删除的课程编号:\n");
scanf("%d",&num);
if(head->num==num){
p2=head;
head=head->next;
free(p2);
}
if(head==NULL)
return NULL;
p1=head;
p2=head->next;
while(p2){
if(p2->num==num){
p1->next=p2->next;
free(p2);
}
else p1=p2;
p2=p1->next;
}
printf("继续删除请按回车,结束请按空格:\n");
ch1=getchar();
ch=getchar();
}
return head;
}
void choose(SUB head)
{
SUB p,q;
int a[5];
int num,total=0,i=0,j;
printf("输入要选修的课程的编号,编号之间以空格分开,输完后以0结束\n");
scanf("%d",&num);
while(num!=0){
for(p=head;p;p=p->next)
if(p->num==num){
total=total+p->score;
a[i]=num;
i++;
}
scanf("%d",&num);
}
if(total<60) printf("选修总学分未达到60,选修失败!\n");
else {
printf("选修成功!\n");
printf("您选修的课程为:\n");
for(j=0;j<i;j++)
for(q=head;q;q=q->next)
if(q->num==a[j])
printf("%s ",q->name);
printf("\n");
}
}
void main()
{
SUB head=create_form();
savefile(head);
prin(head);
search(head);
head=insert(head);
savefileadd(head);
printf("修改后的信息为:\n");
prin(head);
head=del(head);
savefiledel(head);
printf("修改后的课程信息为:\n");
prin(head);
choose(head);
}
以上就是关于c语言程序大神帮忙设计一下全部的内容,包括:c语言程序大神帮忙设计一下、c语言程序设计题,高人来指点一下、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)