C语言期末作业15个(下)
9、C语言货物管理系统
建议点赞收藏
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> /*屏幕操作函数库*/
/*主管权限数据格式化*/
#define HEADER1_zg "-----------------------------货物管理系统(主管)--------------------------------\n"
#define HEADER2_zg "| 编号 | 名称 | 进价 | 售价 | 产地 | 生产日期 |保质期| 库存 | 销量 | 收入 |\n"
#define HEADER3_zg "|------|--------|------|------|------|----------|------|------|------|--------|\n"
#define FORMAT_zg "|%-6s|%-8s|%.2f |%.2f |%6s|%-4d%3d%3d| %4d | %4d | %4d |%.2f |\n"
#define DATA_zg p->data.numbs,p->data.name,p->data.purchase,p->data.sales,p->data.place,p->data.Date.year,p->data.Date.mouth,p->data.Date.day,p->data.days,p->data.stock,p->data.salesnumb,p->data.income
#define END_zg "-------------------------------------------------------------------------------\n"
/*普通权限数据格式化*/
#define HEADER1_pt "----------------货物管理系统(普通)--------------------\n"
#define HEADER2_pt "| 编号 | 名称 | 售价 | 产地 | 生产日期 |保质期| 销量 |\n"
#define HEADER3_pt "|------|--------|------|------|----------|------|------|\n"
#define FORMAT_pt "|%-6s|%-8s| %.2f|%6s|%-4d%3d%3d| %4d | %4d |\n"
#define DATA_pt p->data.numbs,p->data.name,p->data.sales,p->data.place,p->data.Date.year,p->data.Date.mouth,p->data.Date.day,p->data.days,p->data.salesnumb
#define END_pt "--------------------------------------------------------\n"
int yhflag=0; /*是否优惠*/
int incomflag=0; /*是否计算收入*/
int dateflag=0; /*是否输入日期*/
int saveflag=0; /*是否需要保存*/
char zg[]="zg";
char pt[]="pt";
struct date
{
int year;
int mouth;
int day;
};
/*货物基本信息*/
struct goods
{
char numbs[6]; /*编号*/
char name[10]; /*名称*/
float purchase; /*进价*/
float sales; /*售价*/
char place[10]; /*产地*/
struct date Date; /*生产日期*/
int days; /*保质期*/
int datecha; /*保质期截止日期差*/
int stock; /*库存量*/
int salesnumb; /*销量*/
float income; /*收入*/
};
typedef struct node
{
struct goods data;
struct node *next;
}Node,*Link;
/*格式化输出主管表头*/
void printheader_zg()
{
printf(HEADER1_zg);
printf(HEADER2_zg);
printf(HEADER3_zg);
}
/*格式化输出普通表头*/
void printheader_pt()
{
printf(HEADER1_pt);
printf(HEADER2_pt);
printf(HEADER3_pt);
}
/*格式化输出主管表中数据*/
void printdata_zg(Node *pp)
{
Node *p;
p=pp;
printf(FORMAT_zg,DATA_zg);
}
/*格式化输出普通表中数据*/
void printdata_pt(Node *pp)
{
Node *p;
p=pp;
printf(FORMAT_pt,DATA_pt);
}
/*输出案件错误信息*/
void wrong()
{
printf("\n\n\n\n*******错误:输入错误,按任意键继续*******\n");
getchar();
}
/*输出为查找到货物的信息*/
void Nofind()
{
printf("\n*****无此货物信息!*****\n");
getchar();
}
/*主管显示菜单*/
void Disp(Link L,char s[])
{
Node *p;
p=L->next;
if (!p) /*空,则*/
{
printf("\n没有录入货物信息\n");
getchar();
return ;
}
printf("\n\n");
if (strcmp(zg,s)==0)
{
printheader_zg();
while (p)
{
printdata_zg(p);
p=p->next;
printf(HEADER3_zg);
}
}
else
{
printheader_pt();
while (p)
{
printdata_pt(p);
p=p->next;
printf(HEADER3_pt);
}
}
getchar();
}
/*返回链表中符合要求的节点的指针,s[]保存用户查找内容,NaorNum[]保存按什么查找”name,num“*/
Node *Locate(Link L,char s[],char NaorNum[] )
{
Node *p;
if (strcmp(NaorNum,"num")==0)
{
p=L->next;
while (p)
{
if (strcmp(p->data.numbs,s)==0)
return p;
p=p->next;
}
}
else if (strcmp(NaorNum,"name")==0)
{
p=L->next;
while (p)
{
if (strcmp(p->data.name,s)==0)
return p;
p=p->next;
}
}
return 0; /*未找到,返回空指针*/
}
/*平润年判断*/
int Year_pd(int year)
{
int flag;
if ((year%4==0&&year%100!=0)||year%400==0)
{/*闰年*/
flag=0;
}
else
{/*平年*/
flag=1;
}
return flag;
}
/*月份对应的天数*/
int Day_pd(int mouth,int flag)
{
int day;
switch(mouth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day=31;break;
case 4:
case 6:
case 9:
case 11:day=30;break;
case 2:if (flag) day=28;
else day=29;break;
}
return day;
}
/*输入字符串,并进行验证,用户的输入通过*t输出*/
void S_input(char *t,int lens,char *notice)
{
char str[255];
do
{
printf(notice);
scanf("%s",str);
if(strlen(str)>lens)
printf("\n超过了要求长度,重新输入\n");
} while (strlen(str)>lens);
strcpy(t,str);
}
int Num_input(char *notice)
{
int a;
do
{
printf(notice);
scanf("%d",&a);
if(a<0) printf("\n数字必须大于零\n");
} while (a<0);
return a;
}
/*输入月份进行验证*/
int Mou_input(char *notice)
{
int mouth=0;
do
{
printf(notice);
scanf("%d",&mouth);
if (mouth>12||mouth<0)
printf("\n月份必须在(1~12)之间!\n");
} while (mouth>12||mouth<0);
return mouth;
}
/*输入天数进行验证*/
int Day_input(char *notice,int year,int mouth)
{
int day=0;
int nlimit,flag;
flag=Year_pd(year);
do
{
printf(notice);
scanf("%d",&day);
nlimit=Day_pd(mouth,flag);
if (day>nlimit||day<0)
printf("\n日期必须在(1~%d)之间!\n",nlimit);
} while (day>nlimit||mouth<0);
return day;
}
/*增加货物记录*/
void Add_goods(Link L)
{
Node *p,*r,*s;
char ch,flag=0,num[10];
r=L;
s=L->next;
system("cls");
Disp(L,zg);
while (1)
{
while (1) /*输入编号,保证没有被使用,若输入为0,则推出*/
{
S_input(num,6,"输入货物编号(按‘0'返回菜单界面):");
flag=0;
if (strcmp(num,"0")==0) return;
s=L->next;
while (s) /*查询货物编号是否存在,若存在,要去输入新编号*/
{
if (strcmp(s->data.numbs,num)==0)
{
flag=1;
break;
}
s=s->next;
}
if (flag)
{
getchar();
printf("货物编号:%s已存在,是否要重新输入?(y/n)",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return;
}
else
break;
}
p=(Node *)malloc(sizeof(Node));
if (!p)
{
printf("\n没有申请到");
return; /*返回到主界面*/
}
strcpy(p->data.numbs,num);
S_input(p->data.name,8,"名称:");
p->data.purchase=Num_input("进价:");
p->data.sales=Num_input("售价:");
S_input(p->data.place,6,"产地:");
p->data.Date.year=Num_input("成产日期:年:");
p->data.Date.mouth=Mou_input(" 月:");
p->data.Date.day=Day_input(" 日:",p->data.Date.year,p->data.Date.mouth);
p->data.days=Num_input("保质期(天):");
p->data.stock=Num_input("库存:");
p->data.salesnumb=Num_input("销量:");
p->data.income=00;
p->next=r->next;
r->next=p; /*前插*/
}
return ;
}
int select(int a,int b) /*菜单选择*/
{
int n;
do
{
printf("\n\n");
printf("%15s"," ");
printf("请输入一个数(%d~%d): [ ]\b\b",a,b);
scanf("%d",&n);
} while (n<a||n>b);
return n;
}
/*实时计算实际收益*/
void Sum_Income(Link L)
{
Node *p;
p=L->next;
while (p)
{
p->data.income=p->data.salesnumb*(p->data.sales-p->data.purchase);
p=p->next;
}
incomflag=1;
return ;
}
/*查询的通用方法*/
void Sea_by_ways(Link L,Node *p,char s[])
{
if (p)
{
if (strcmp(zg,s)==0)
{
printheader_zg();
printdata_zg(p);
printf(END_zg);
printf("按任意键返回...");
getchar();
}
else
{
printheader_pt();
printdata_pt(p);
printf(END_pt);
printf("按任意键返回...");
getchar();
}
}
else
{
Nofind();
getchar();
}
}
/*按编号或货物名称查询*/
void Sea_by_name(Link L,char s[])
{
Node *p;
char ss[20];
system("cls");
S_input(ss,10,"请输入要查找的货物名称:");
p=Locate(L,ss,"name");
Sea_by_ways(L,p,s);
}
void Sea_by_num(Link L,char s[])
{
Node *p;
char ss[20];
system("cls");
S_input(ss,10,"请输入要查找的货物编号:");
p=Locate(L,ss,"num");
Sea_by_ways(L,p,s);
}
/*日期相对于本年的天数*/
int Sum_day(int year,int mouth,int day)
{
int i,flag,days,sum=0;
flag=Year_pd(year);
for (i=1;i<mouth;i++)
{
days=Day_pd(i,flag);
sum+=days;
}
return sum+day;
}
/*两个日期差值*/
void Datecha(Link L)
{
int year,mouth,day;
Node *p;
p=L->next;
year=Num_input("今天日期:年:");
mouth=Mou_input("月:");
day=Day_input("日:",year,mouth);
dateflag=1;
if (!p)
{
printf("***无货物信息,不用输入日期***\n");
getchar();
return ;
}
else
{
while (p)
{
p->data.datecha=Sum_day(year,mouth,day)-Sum_day(p->data.Date.year,p->data.Date.mouth,p->data.Date.day);
p=p->next;
}
}
}
void Dz_by_data( Link L)
{
Node *p,*r;
Link l;
int year,mouth,day,count=0;
p=L->next;
l=(Node *)malloc(sizeof(Node));
l->next=NULL;
system("cls");
if (!dateflag)
{
Datecha(L);
}
printf("| 编号 | 名称 |过期差值|\n");
while (p)
{
printf("|%-6s|%-8s| %4d |",p->data.numbs,p->data.name,p->data.datecha);
if (p->data.days>=p->data.datecha)
{
/*优惠的标示优惠*/
if (p->data.days-p->data.datecha<=10)
{
count++;
p->data.sales=p->data.sales/2;
printf("已优惠\n");
/* Ltol(l,p,p->next);*/
}
else
{
printf("\n");
}
}
else
{
printf("该商品过期,请删除\n");
}
p=p->next;
}
if(!count)
{
printf("/n***没有差10天过期的货物***/n");
getchar();
return;
}
else
{
printf("共有%d中商品打折",count);
yhflag=1;
saveflag=1;
Disp(L,zg);
}
}
/*删除*/
void Del_by_ways(Link L,Node *p)
{
Node *q;
char ch;
if (p)
{
q=L;
while(q->next!=p)
q=q->next;
printheader_zg();
printdata_zg(p);
printf(END_zg);
getchar();
printf("请确定是否要删除该货物信息(y/n):");
scanf("%c",&ch);
if (ch=='y'||ch=='Y')
{
q->next=p->next;
free(p);
printf("\n成功删除该货物信息\n");
saveflag=1;
getchar();
return;
}
else
{
printf("\n没有删除该货物信息\n");
getchar();
return;
}
}
else
{
Nofind();
getchar();
}
}
/*通过名称删除*/
void Del_by_name(Link L)
{
Node *p;
char s[20];
system("cls");
S_input(s,10,"请输入要查删除的货物名称:");
p=Locate(L,s,"name");
Del_by_ways(L,p);
}
/*通过编号删除*/
void Del_by_num(Link L)
{
Node *p;
char s[20];
system("cls");
S_input(s,10,"请输入要查删除的货物的编号:");
p=Locate(L,s,"num");
Del_by_ways(L,p);
}
/*补货信息*/
void Buhuo(Link L)
{
Node *p,*q;
int i=0;
p=L->next;
system("cls");
if (p)
{
q=L->next;
printf("| 编号 | 名称 |库存|销量|剩余| \n");
while (q)
{
/*统计库存,销量,剩余*/
printf("|%-8s|%-8s|%-4d|%4d|%4d|\n",q->data.numbs,q->data.name,q->data.stock,q->data.salesnumb,q->data.stock-q->data.salesnumb);
q=q->next;
}
while (p)
{
if ((p->data.stock-p->data.salesnumb)<100)
{
if (i==0)
{
printf("需要补货的货物分别是\n");
printf("| 编号 |商品名称|\n");
}
printf("|%-8s|%8s|\n",p->data.numbs,p->data.name);
i++;
}
p=p->next;
}
if (i)
{
printf("共%d种货物\n",i);
getchar();
return;
}
else
{
printf("没有需要补充的货物种类\n");
getchar();
return;
}
}
else
{
printf("\n***没有货物信息***\n");
printf("\n按任意键返回...\n");
getchar();
return ;
}
}
void Sort_by_ways(Link L,char s[],int n)
{
int flag=0,a;
Node *p,*q,*lq,*fq; /*fq记下节点前驱,lq记下节点的后驱,q每次冒泡遍历,p控制遍历,qq用来交换地址*/
p=L->next;
system("cls");
while (p)
{
fq=L;
q=fq->next;
while (lq=q->next)
{
if (strcmp(zg,s)==0)
{
switch (n)
{
case 1:a=strcmp(q->data.name,lq->data.name);break;
case 2:a=strcmp(q->data.numbs,lq->data.numbs);break;
case 3:a=(q->data.salesnumb>lq->data.salesnumb);break;
case 4:if(!incomflag) Sum_Income(L); a=(q->data.income>lq->data.income);break;
case 5:if (!dateflag) Datecha(L);a=(q->data.datecha>lq->data.datecha);break;
}
}
else
{
switch (n)
{
case 1:a=strcmp(q->data.name,lq->data.name);break;
case 2:a=strcmp(q->data.numbs,lq->data.numbs);break;
case 3:a=(q->data.sales>lq->data.sales);break;
case 4:a=(q->data.salesnumb>lq->data.salesnumb);break;
case 5:if (!dateflag) Datecha(L);a=(q->data.datecha>lq->data.datecha);break;
}
}
/*if ((n==1)?(strcmp(q->data.name,lq->data.name)):(strcmp(q->data.numbs,q->data.numbs)))*/
if(a>0)
{
flag=1;
q->next=lq->next;
lq->next=q;
fq->next=lq;
}
fq=fq->next;
q=fq->next;
}
if(flag==0) break;
p=p->next;
}
if (strcmp(zg,s)==0)
Disp(L,zg);
else
Disp(L,pt);
}
void Wb_Save(Link L)
{
FILE *out;
Node *p;
int count=0;
if ((out=fopen("d:\\Goods.txt","w"))==NULL)
{
printf("打开文件失败!\n");
getchar();
return;
}
p=L->next;
fprintf(out,HEADER1_zg);
fprintf(out,HEADER2_zg);
fprintf(out,HEADER3_zg);
while (p)
{
fprintf(out,FORMAT_zg,DATA_zg);
p=p->next;
fprintf(out,HEADER3_zg);
count++;
}
if (count>0)
{
getchar();
printf("\n\n\n***文本文件保存完成,共%d条货物信息保存***\n",count);
getchar();
saveflag=0;
}
else
{
system("cls");
printf("没有货物信息,没有货物信息保存\n");
getchar();
}
fclose(out); /*关闭此文件*/
}
void Er_Save(Link L)
{
FILE *out;
Node *p;
int count=0;
if ((out=fopen("d:\\Goods","wb"))==NULL)
{
printf("打开文件失败!\n");
getchar();
return;
}
p=L->next;
while (p)
{
if (fwrite(p,sizeof(Node),1,out))
{
p=p->next;
count++;
}
else
{
break;
}
}
if (count>0)
{
getchar();
printf("\n\n\n***二进制文件保存完成,共%d条货物信息保存***\n",count);
getchar();
saveflag=0;
}
else
{
system("cls");
printf("没有货物信息,没有货物信息保存\n");
getchar();
}
fclose(out); /*关闭此文件*/
}
void Menu_sort_zg()
{
system("cls");
printf("\n\n\n");
printf("%30s%s"," "," 排序货物信息 \n\n");
printf("%30s%s"," ","********功能*******\n\n");
printf("%30s%s"," ","* 1.按名称 *\n\n");
printf("%30s%s"," ","* 2.按编号 *\n\n");
printf("%30s%s"," ","* 3.按销量*\n\n");
printf("%30s%s"," ","* 4.按利润*\n\n");
printf("%30s%s"," ","* 5.按保期 *\n\n");
printf("%30s%s"," ","* 6.返回 *\n\n");
printf("%30s%s"," ","* 7.退出 *\n\n");
printf("%30s%s"," ","********************\n");
}
void Menu_sort_pt()
{
system("cls");
printf("\n\n\n");
printf("%30s%s"," "," 排序货物信息 \n\n");
printf("%30s%s"," ","********功能*******\n\n");
printf("%30s%s"," ","* 1.按名称 *\n\n");
printf("%30s%s"," ","* 2.按编号 *\n\n");
printf("%30s%s"," ","* 3.按售价 *\n\n");
printf("%30s%s"," ","* 4.按销量 *\n\n");
printf("%30s%s"," ","* 5.按保期 *\n\n");
printf("%30s%s"," ","* 6.返回 *\n\n");
printf("%30s%s"," ","* 7.退出 *\n\n");
printf("%30s%s"," ","********************\n");
}
void Menu_del()
{
system("cls");
printf("\n\n\n");
printf("%30s%s"," "," 删除货物信息 \n\n");
printf("%30s%s"," ","********功能*******\n\n");
printf("%30s%s"," ","* 1.按名称 *\n\n");
printf("%30s%s"," ","* 2.按编号 *\n\n");
printf("%30s%s"," ","* 3.返回 *\n\n");
printf("%30s%s"," ","* 4.退出 *\n\n");
printf("%30s%s"," ","********************\n");
}
void Menu_Sea()
{
system("cls");
printf("\n\n\n");
printf("%30s%s"," "," 查询货物信息 \n\n");
printf("%30s%s"," ","********功能*******\n\n");
printf("%30s%s"," ","* 1.按名称 *\n\n");
printf("%30s%s"," ","* 2.按编号 *\n\n");
printf("%30s%s"," ","* 3.返回 *\n\n");
printf("%30s%s"," ","* 4.退出 *\n\n");
printf("%30s%s"," ","********************\n");
}
void Menu_Qx() /*权限菜单*/
{
system("cls");
printf("\n\n\n");
printf("%25s%s"," "," 欢迎进入 \n");
printf("%25s%s"," "," 货物管理体统 \n\n\n");
printf("%25s%s"," ","**********功能*********\n\n");
printf("%25s%s"," ","* 1.主管 *\n\n");
printf("%25s%s"," ","* 2.普通用户 *\n\n");
printf("%25s%s"," ","* 3.退出 *\n\n");
printf("%25s%s"," ","***********************\n");
}
void Menu_zg() /*主管菜单*/
{
system("cls");
printf("\n\n\n");
printf("%15s%s"," "," 货物管理体统 \n\n\n");
printf("%15s%s"," ","*****************主管功能******************\n\n");
printf("%15s%s"," ","* 1.录入货物信息 *\n");
printf("%15s%s"," ","* 2.删除货物信息 *\n");
printf("%15s%s"," ","* 3.查找货物信息 *\n");
printf("%15s%s"," ","* 4.显示货物信息 *\n");
printf("%15s%s"," ","* 5.差10天过物信息(促销) *\n");
printf("%15s%s"," ","* 6.统计货物数量(补货) *\n");
printf("%15s%s"," ","* 7.实时收益 *\n");
printf("%15s%s"," ","* 9.排序日期差 *\n");
printf("%15s%s"," ","* 10.保存文件 *\n");
printf("%15s%s"," ","* 11.返回 *\n");
printf("%15s%s"," ","* 12.退出 *\n");
printf("%15s%s"," ","*******************************************\n");
}
void Menu_pt() /*普通菜单*/
{
printf("%15s%s"," "," 货物管理体统 \n\n\n");
printf("%15s%s"," ","*****************普通用户功能***************\n\n");
printf("%15s%s"," ","* 1.查找货物信息 *\n");
printf("%15s%s"," ","* 2.显示货物信息 *\n");
printf("%15s%s"," ","* 3.排序货物销量 *\n");
printf("%15s%s"," ","* 4.返回 *\n");
printf("%15s%s"," ","* 5.退出 *\n");
printf("%15s%s"," ","*************************************************\n");
}
void Isaveflag(Link L)
{
char ch;
if (saveflag)
{
printf("您已经修改数据了,是否要保存退出(y/n):");
getchar();
scanf("%c",&ch);
if (ch=='y'||ch=='Y')
{
Wb_Save(L);
Er_Save(L);
exit(0);
}
else
exit(0);
}
else
exit(0);
}
int IsPassword() /*密码是否通过*/
{
char password[7];
char mypassword[]="123456";
static int n=3;
int i;
system("cls");
for (;n>0;n--)
{
switch(n)
{
case 1:
case 2: printf("密码错误,您还有%d次机会,请再输入:",n);break;
case 3: printf("请输入6位进入主管功能的密码:");getchar();break;
}
for ( i=0;i<6;i++)
{
password[i]=getch();
printf("*");
}
getchar();
password[6]='\0';
if (strcmp(mypassword,password)==0)
{
printf("***密码正确***");
getchar();
return 1;
}
}
printf("恭喜你,三次都输错了,无法再输入密码,按任意键返回...");
getchar();
return 0;
}
void Sort_goods(Link L,char s[])
{
if (!L->next)
{
system("cls");
printf("\n没有货物信息,无法使用排序功能\n");
getchar();
return;
}
else
{
if (strcmp(zg,s)==0)
{
system("cls");
for (;;)
{
Menu_sort_zg();
switch(select(1,7))
{
case 1:Sort_by_ways(L,zg,1);break; /*名称*/
case 2:Sort_by_ways(L,zg,2);break; /*编号*/
case 3:Sort_by_ways(L,zg,3);break; /*销量*/
case 4:Sort_by_ways(L,zg,4);break; /*利润*/
case 5:Sort_by_ways(L,zg,5);break; /*保期*/
case 6:return;break;
case 7:Isaveflag(L);break;
}
getchar();
}
}
else
{
system("cls");
for (;;)
{
Menu_sort_pt();
switch(select(1,7))
{
case 1:Sort_by_ways(L,pt,1);break; /*名称*/
case 2:Sort_by_ways(L,pt,2);break; /*编号*/
case 3:Sort_by_ways(L,pt,3);break; /*售价*/
case 4:Sort_by_ways(L,pt,4);break; /*销量*/
case 5:Sort_by_ways(L,pt,5);break; /*销量*/
case 6:return;break;
case 7:Isaveflag(L);break;
}
getchar();
}
}
}
}
void Sea_goods(Link L,char s[])
{
if (!L->next)
{
system("cls");
printf("\n***没有货物记录,不能进入查询功能***\n");
return ;
}
else
{
system("cls");
for (;;)
{
Menu_Sea();
switch(select(1,4))
{
case 1:Sea_by_name(L,s);break;
case 2:Sea_by_num(L,s);break;
case 3:return;break;
case 4:Isaveflag(L);break;
}
getchar();
}
}
}
void Del_goods(Link L)
{
if (!L->next)
{
system("cls");
printf("\n***没有货物记录,不能使用删除功能***\n");
return ;
}
else
{
system("cls");
for (;;)
{
Menu_del();
switch (select(1,4))
{
case 1:Del_by_name(L);break;
case 2:Del_by_num(L);break;
case 3:return;break;
case 4:Isaveflag(L);break;
}
getchar();
}
}
}
void main_zg(Link L) /*主管功能*/
{
system("cls");
for (;;)
{
Menu_zg();
switch (select(1,12))
{
case 1:Add_goods(L);saveflag=1;break; /*增加货物信息*/
case 2:Del_goods(L);break; /*删除学生信息*/
case 3:Sea_goods(L,zg);break; /*按编号名字查询*/
case 4:system("cls");Disp(L,zg);break;
case 5:Dz_by_data(L);break;
case 6:Buhuo(L);break;
case 7:Disp(L,zg);Sum_Income(L);Disp(L,zg);break;
case 9:Sort_goods(L,zg);break;
case 10:Wb_Save(L);Er_Save(L);break;
case 11:return;break;
case 12:Isaveflag(L);break;
}
getchar();
}
}
void main_pt(Link L) /*普通功能*/
{
system("cls");
for (;;)
{
Menu_pt();
switch (select(1,5))
{
case 1:Sea_goods(L,pt);break; /*按编号名字查询*/
case 2:system("cls");Disp(L,pt);break;
case 3:Sort_goods(L,pt);break;
case 4:return;break;
case 5:Isaveflag(L);break;
}
getchar();
system("cls");
}
}
int main()
{
Link L;
FILE *in;
Node *p,*r;
int count=0;
L=(Node *)malloc(sizeof(Node));
L->next=NULL;
r=L;
if ((in=fopen("d:\\Goods","ab+"))==NULL) /*以追加方式打开二进制文件*/
{
printf("\n***打开文件失败***\n");
exit(0);
}
while (!feof(in))
{
p=(Node *)malloc(sizeof(Node));
if (fread(p,sizeof(Node),1,in))
{
p->next=r->next;
r->next=p;
count++;
}
}
fclose(in);
for (;;)
{
Menu_Qx();
switch (select(1,3))
{
case 1:if(IsPassword())
{
main_zg(L);
}
break;
case 2:main_pt(L);break;
case 3:Isaveflag(L);break;
}
getchar();
}
return 0;
}
10、C语言歌曲信息管理系统
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//歌曲信息包括:歌名、演唱者、作词、作曲、所属专辑、出版时间、出版公司
typedef struct music
{
char name[20]; //歌名
char singer[20]; //演唱者
char authors[20]; //作词
char compose[30]; //作曲
char album[20]; //所属专辑
char time[15]; //出版时间
char company[30]; //出版公司
struct music *next;
}music;
music *head=NULL;
int length; //链表的长度
void create()
{
music *p1,*p2;
length=0;
p1=(music *)malloc(sizeof(music));
strcpy(p1->name,"-1");
if(head==NULL)
head=p1;
printf("请输入音乐的歌名、演唱者、作词、作曲、所属专辑、出版时间、出版公司:/n");
while(1) //歌名为0的时候退出
{
p2=(music *)malloc(sizeof(music));
//输入歌曲信息
scanf("%s %s %s %s %s %s %s",p2->name,p2->singer,p2->authors,p2->compose,p2->album,p2->time,p2->company);
if(strcmp(p2->name,"0")==0)
{
printf("链表创建完成!/n");
break;
}
length++; //链表的长度
p1->next=p2;
p2->next=NULL;
p1=p1->next;
}
return ;
}
void ModifymusicInfo()
{
music *p=head->next;
char name[20];
printf("请输入要修改的歌曲的歌名:");
getchar();
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
printf("修改前,歌名为%s的歌曲的信息如下:/n",name);
printf("音乐的歌名、演唱者、作词、作曲、所属专辑、出版时间、出版公司:/n");
printf("%s %s %s %s %s %s %s/n",p->name,p->singer,p->authors,p->compose,p->album,p->time,p->company);
printf("请输入歌曲的新的所属专辑:");
getchar();
scanf("%s",p->album);
printf("请输入歌曲的新出版公司:");
getchar();
scanf("%s",p->company);
printf("修改后,歌名为%s的歌曲的信息如下:/n",name);
printf("音乐的歌名、演唱者、作词、作曲、所属专辑、出版时间、出版公司:/n");
printf("%s %s %s %s %s %s %s/n",p->name,p->singer,p->authors,p->compose,p->album,p->time,p->company);
return ;
}
p=p->next;
}
if(p==NULL)
{
printf("该歌曲不存在!/n");
return ;
}
}
void display()
{
music *p=head->next;
printf("链表中所有的歌曲信息如下:/n");
printf("音乐的歌名、演唱者、作词、作曲、所属专辑、出版时间、出版公司:/n");
while(p!=NULL)
{
printf("%s %s %s %s %s %s %s/n",p->name,p->singer,p->authors,p->compose,p->album,p->time,p->company);
p=p->next;
}
return ;
}
void search()
{
int num,x,flag;
char name[20];
music *p=head->next;
printf("请选择查询的方式:/n");
printf("1、按歌名查询/t 2、按演唱者查询/n");
scanf("%d",&x);
if(x==1)
{
printf("需要查找的歌曲歌名为:");
getchar();
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
printf("歌名为%s的歌曲的信息如下:/n",name);
printf("音乐的歌名、演唱者、作词、作曲、所属专辑、出版时间、出版公司:/n");
printf("%s %s %s %s %s %s %s/n",p->name,p->singer,p->authors,p->compose,p->album,p->time,p->company);
return ;
}
p=p->next;
}
if(p==NULL)
printf("没有这首歌曲的记录!/n");
}
else if(x==2)
{
flag=0;
printf("需要查找的演唱者为:");
getchar();
scanf("%s",name);
p=head->next;
while(p!=NULL)
{
if(strcmp(p->singer,name)==0)
{
if(flag==0)
{
printf("演唱者为%s的歌曲的信息如下:/n",name);
printf("音乐的歌名、演唱者、作词、作曲、所属专辑、出版时间、出版公司:/n");
flag=1;
}
printf("%s %s %s %s %s %s %s/n",p->name,p->singer,p->authors,p->compose,p->album,p->time,p->company);
}
p=p->next;
}
if(p==NULL && flag==0)
{
printf("没有该演唱者的歌曲记录!/n");
return;
}
}
return ;
}
void insert()
{
int num,i;
music *p,*q;
p=head;
printf("请输入你要插入位置: ");
scanf("%d",&num);
if(num>length)
{
printf("找不到要插入的位置/n");
return ;
}
else
{
printf("请输入你要插入的音乐的歌名、演唱者、作词、作曲、所属专辑、出版时间、出版公司:/n");
q=(music *)malloc(sizeof(music));
//输入歌曲信息
scanf("%s %s %s %s %s %s %s",q->name,q->singer,q->authors,q->compose,q->album,q->time,q->company);
while(p!=NULL)
{
if(strcmp(p->name,q->name)==0)
{
printf("该歌曲已经存在,无法插入!/n");
return ;
}
p=p->next;
}
p=head;
for(i=0;i<num;i++)
p=p->next;
q->next=p->next;
p->next=q;
length++;
printf("插入成功!/n");
return ;
}
}
void Delete()
{
char name[20];
music *p,*q;
q=head,p=head->next;
printf("请输入要删除的歌曲的歌名:/n");
getchar();
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
q->next=p->next;
free(p);
length--;
printf("删除成功!/n");
return ;
}
p=p->next;
q=q->next;
}
if(p==NULL)
{
printf("找不到要删除的歌曲!/n");
return ;
}
}
void menu()
{
printf("________________________________________________________________/n");
printf("| 歌厅歌曲管理系统 |/n");
printf("| 0、 退出系统 |/n");
printf("| 1、 录入歌曲信息 |/n");
printf("| 2、 显示歌曲信息 |/n");
printf("| 3、 查找链表中的某一首歌曲 |/n");
printf("| 4、 删除链表中指定歌曲 |/n");
printf("| 5、 指定的位置上插入一个新结点 |/n");
printf("| 6、 修改歌曲信息 |/n");
printf("________________________________________________________________/n");
return ;
}
int main(void)
{
int a;
menu();
while(1)
{
printf("请选择相应的功能:");
scanf("%d",&a);
switch(a)
{
case 0:
return 0;
case 1:
create();
menu();
break;
case 2:
if(head)
{
display();
menu();
}
else
{
printf("链表为空,请先建立链表!/n");
menu();
}
break;
case 3:
if(head)
{
search();
menu();
}
else
{
printf("链表为空,请先建立链表!/n");
menu();
}
break;
case 4:
if(head)
{
Delete();
menu();
}
else
{
printf("链表为空,请先建立链表!/n");
menu();
}
break;
case 5:
if(head)
{
insert();
menu();
}
else
{
printf("链表为空,请先建立链表!/n");
menu();
}
break;
case 6:
if(head)
{
ModifymusicInfo();
menu();
}
else
{
printf("链表为空,请先建立链表!/n");
menu();
}
break;
default:
break;
}
}
system("pause");
return 0;
}
11、C语言职工信息管理系统源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct worker
{
char ID[20];//工号
char name[20];//姓名
char sex[5];//性别
char born[20];//生日
char edu[20];//学历
char position[20];//职务
char wage[10];//工资
char address[100];//住址
char tel[15];//电话
}work[1000];
int n ;
//文件读写模块
void read()//读入数据
{
FILE *fp;
int i = 0;
if( (fp = fopen("d:\\date.txt","r")) == NULL )
{
printf("请在D盘根目录下新建一个文本文件,文件名为date\n\n\n");
system("pause");
system("cls");
return ;
}
fscanf(fp,"%d\n",&n);
while(i < n)
{
fscanf(fp,"%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",work[i].ID,work[i].name,work[i].sex,work[i].born,work[i].edu,work[i].position,work[i].wage,work[i].address,work[i].tel);
i ++;
}
fclose(fp);
}
void write()//写入数据
{
FILE *fp;
int i = 0;
if( (fp = fopen("d:\\date.txt","w")) == NULL )
{
printf("无法打开文件!\n");
return ;
}
fprintf(fp,"%d\n",n);
while(i < n)
{
fprintf(fp,"%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",work[i].ID,work[i].name,work[i].sex,work[i].born,work[i].edu,work[i].position,work[i].wage,work[i].address,work[i].tel);
i ++;
}
fclose(fp);
}
//输入模块
void input()// 从键盘输入
{
int i;
int flag;//标记工号是否已存在
char c;//指令Y?N
read();//输入数据前先将文件中原有的数据读入
do
{
flag = 0;
printf("\t\t请输入职工的工号: \n\t\t");
scanf("%s",work[n].ID);
for(i = 0 ; i < n ; i ++)
{
if(strcmp(work[i].ID,work[n].ID) == 0)
{
flag = 1;
break;
}
}
if(flag)
{
printf("该工号已存在!\n\n");
printf("\t\t是否继续输入?(Y/N): ");
scanf("%*c%c",&c);
if(c == 'N' || c == 'n') break;
continue;
}
printf("\t\t请输入职工的姓名: \n\t\t");
scanf("%s",work[n].name);
printf("\t\t请输入职工的性别: \n\t\t");
scanf("%s",work[n].sex);
printf("\t\t请输入职工的出生年月: \n\t\t");
scanf("%s",work[n].born);
printf("\t\t请输入职工的学历: \n\t\t");
scanf("%s",work[n].edu);
printf("\t\t请输入职工的职务: \n\t\t");
scanf("%s",work[n].position);
printf("\t\t请输入职工的工资: \n\t\t");
scanf("%s",work[n].wage);
printf("\t\t请输入职工的住址: \n\t\t");
scanf("%s",work[n].address);
printf("\t\t请输入职工的电话: \n\t\t");
scanf("%s",work[n].tel);
printf("\t\t是否继续输入?(Y/N): ");
scanf("%*c%c",&c);
n ++;
}while( c != 'N' && c != 'n');
write();
printf("\n\n数据已保存!\n\n");
}
//显示模块
void output_inf(int id)
{
printf("\t\t 工号: %s\n",work[id].ID);
printf("\t\t 姓名: %s\n",work[id].name);
printf("\t\t 性别: %s\n",work[id].sex);
printf("\t\t出生年月: %s\n",work[id].born);
printf("\t\t 学历: %s\n",work[id].edu);
printf("\t\t 职务: %s\n",work[id].position);
printf("\t\t 工资: %s\n",work[id].wage);
printf("\t\t 住址: %s\n",work[id].address);
printf("\t\t 电话: %s\n",work[id].tel);
printf("\n\n\n");
}
void show()//显示所有职工信息
{
int i;
read();
if(n == 0) printf("无记录!\n\n");
else
for(i = 0 ; i < n ; i ++)
{
output_inf(i);
}
}
//查询模块
void que()//查询信息
{
char num;
int flag,i;
char q_id[20] ;//查询工号
char q_name[20] ;//查询姓名
char q_edu[20] ; //查询学历
char q_position[20] ;//查询职位
char c;//指令Y?N
read();//打开程序后,要查询以前的数据,要先读入
while(1)
{
system("cls");
printf("\t\t******************查询********************\n");
printf("\t\t* *\n");
printf("\t\t* 1.按工号查询 *\n");
printf("\t\t* *\n");
printf("\t\t* 2.按姓名查询 *\n");
printf("\t\t* *\n");
printf("\t\t* 3.按学历查询 *\n");
printf("\t\t* *\n");
printf("\t\t* 4.按职务查询 *\n");
printf("\t\t* *\n");
printf("\t\t* 输入其他任意键退出本模块 *\n");
printf("\t\t******************************************\n");
printf("\t\t请输入指令(1-4):");
scanf("%*c%c",&num);
if(num < '1' || num > '4')
{
printf("错误指令!\n\n");
system("pause");
continue;
}
system("cls");
flag = 0;//用来标记职工信息是否存在
if(num == '1')//按工号查询
{
printf("\t\t请输入您要查询职工的工号:");
scanf("%s",q_id);
for(i = 0 ; i < n ;i ++)
{
if( strcmp(work[i].ID,q_id) == 0)
{
output_inf(i);
flag = 1;//标记
break;
}
}
}
else if(num == '2')//按姓名查询
{
printf("\t\t请输入您要查询职工的姓名:");
scanf("%s",q_name);
for(i = 0 ; i < n ;i ++)
{
if( strcmp(work[i].name,q_name) == 0)
{
output_inf(i);
flag = 1;//标记
}
}
}
else if(num == '3')//按学历查询
{
printf("\t\t请输入您要查询职工的学历:");
scanf("%s",q_edu);
for(i = 0 ; i < n ;i ++)
{
if( strcmp(work[i].edu,q_edu) == 0)
{
output_inf(i);
flag = 1;//标记
}
}
}
else if(num == '4')//按职务查询
{
printf("\t\t请输入您要查询职工的职务:");
scanf("%s",q_position);
for(i = 0 ; i < n ;i ++)
{
if( strcmp(work[i].position,q_position) == 0)
{
output_inf(i);
flag = 1;//标记
}
}
}
if(!flag)//判断是否有该职工信息
{
printf("\n\n无该职工信息!\n\n");
}
printf("\n\n是否继续查询(Y/N):");
scanf("%*c%c",&c);
if(c == 'N' || c == 'n') break;
}
}
//修改信息模块
void mod()//修改职工信息
{
char change_ID[20];//需修改的职工的工号
int number;//职工信息在数据中的位置
char num;//指令
char message[100];//修改的信息
char c;//指令Y?N
int flag;
int i;
read();
while(1)
{
flag = 0;
system("cls");
printf("请输入需修改的职工的工号:");
scanf("%s",change_ID);
for(i = 0 ; i < n ; i ++)
{
if( strcmp(work[i].ID,change_ID) == 0 )
{
number = i;
flag = 1;
break;
}
}
if(!flag)
{
printf("\n\n无此员工信息!!\n\n");
printf("\n\n是否继续修改(Y/N):");
scanf("%*c%c",&c);
if(c == 'N' || c == 'n') break;
continue;
}
printf("\n\n\t\t******************修改********************\n");
printf("\t\t* *\n");
printf("\t\t* 1.修改姓名 *\n");
printf("\t\t* *\n");
printf("\t\t* 2.修改学历 *\n");
printf("\t\t* *\n");
printf("\t\t* 3.修改职务 *\n");
printf("\t\t* *\n");
printf("\t\t* 4.修改工资 *\n");
printf("\t\t* *\n");
printf("\t\t* 5.修改住址 *\n");
printf("\t\t* *\n");
printf("\t\t* 6.修改电话 *\n");
printf("\t\t* *\n");
printf("\t\t* 输入其他任意键退出本模块 *\n");
printf("\t\t******************************************\n");
printf("\t\t请输入指令(1-6):");
scanf("%*c%c",&num);
if(num < '1' || num > '6')
{
printf("错误指令!\n\n");
system("pause");
continue;
}
system("cls");
printf("请输入修改后的信息:");
scanf("%s",message);
switch(num)
{
case '1' : strcpy(work[number].name,message); break;
case '2' : strcpy(work[number].edu,message); break;
case '3' : strcpy(work[number].position,message); break;
case '4' : strcpy(work[number].wage,message); break;
case '5' : strcpy(work[number].address,message); break;
case '6' : strcpy(work[number].tel,message); break;
}
system("cls");
printf("\n\n是否继续修改(Y/N):");
scanf("%*c%c",&c);
if(c == 'N' || c == 'n') break;
}
write();
}
//删除信息模块
void del()//删除职工信息
{
char delete_ID[20];//删除职工工号
int i,j;
int flag;//标记是否有该职工信息
char c;
read();
while(1)
{
system("cls");
flag = 0;
printf("请输入需删除职工信息的工号:");
scanf("%s",delete_ID);
for(i = 0 ; i < n ; i ++)
{
if( strcmp(work[i].ID,delete_ID) == 0)
{
n --;
flag = 1;
for(j = i ; j < n ; j ++)
{
work[j] =work[j + 1];
}
break;
}
}
if(!flag) printf("\n\n无此员工信息!\n\n");
else printf("\n\n删除成功!\n");
printf("\n\n是否继续删除(Y/N):");
scanf("%*c%c",&c);
if(c == 'N' || c == 'n') break;
}
write();
}
//菜单模块
void menu()//菜单
{
printf("\t\t************职工信息管理系统菜单**********\n");
printf("\t\t* *\n");
printf("\t\t* 1.录入职工信息 *\n");
printf("\t\t* *\n");
printf("\t\t* 2.显示职工信息 *\n");
printf("\t\t* *\n");
printf("\t\t* 3.查询职工信息 *\n");
printf("\t\t* *\n");
printf("\t\t* 4.修改职工信息 *\n");
printf("\t\t* *\n");
printf("\t\t* 5.删除职工信息 *\n");
printf("\t\t* *\n");
printf("\t\t* 0.退出 *\n");
printf("\t\t* *\n");
printf("\t\t******************************************\n");
printf("\t\t请输入指令(0-5): ");
}
int main()
{
int num;
n = 0;
system("color 0B");
while(1)
{
menu();
scanf("%d",&num);
system("cls");
switch(num)
{
case 1: input(); break;
case 2: show(); break;
case 3: que(); break;
case 4: mod(); break;
case 5: del(); break;
case 0: printf("谢谢使用!\n\n"); return 0;
default :printf("\n--->无效的指令!\n\n\n");
}
system("pause");
system("cls");
}
}
12、C语言学生打卡系统
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
FILE *fp; //定义文件
char a; //返回选择变量
float kq = 10; //应到打卡次数
struct student
{
char name[10]; //姓名
char number[11]; //学号
char sex; //性别
int year; //出生日期--年
int month; //出生日期--月
int day; //出生日期--日
int k; //打卡次数
}stu[100];
void input();
void change();
void search();
void times();
void evaluate();
int main();
int main()
{
int it = 0;
printf("\t\t★★★★★★★★★★★★★★★★★★★★★★★★\n");
printf("\t\t★ ※欢迎进入学生考勤系统 ★\n");
printf("\t\t★ ★\n");
printf("\t\t★ 1.学生信息及打卡情况录入 ★\n");
printf("\t\t★ ★\n");
printf("\t\t★ 2.修改某个学生的打卡情况 ★\n");
printf("\t\t★ ★\n");
printf("\t\t★ 3.查询某个学生的打卡记录 ★\n");
printf("\t\t★ ★\n");
printf("\t\t★ 4.旷课学生的姓名及次数 ★\n");
printf("\t\t★ ★\n");
printf("\t\t★ 5.学生打卡的总评分数计算 ★\n");
printf("\t\t★ ★\n");
printf("\t\t★ 6.退出系统 ★\n");
printf("\t\t★ ★\n");
printf("\t\t★★★★★★★★★★★★★★★★★★★★★★★★\n");
printf("\t *******请选择您需要的操作******* ");
scanf("%d", &it);
switch (it)
{
case 1: input(); break;
case 2:change(); break;
case 3:search(); break;
case 4:times(); break;
case 5:evaluate(); break;
case 6: break;
default: printf("请重新输入!\n");
}
if (it == 6)
printf("\t\t\t退出考勤系统,欢迎下次使用!");
system("pause");
return 0;
}
void input()
{
int i = 0;
int j = 0;
int x = 1;
printf("************1.学生信息及打卡情况录入************\n");
if ((fp = fopen("data.txt", "w")) == NULL)
{
printf("文件打开失败! \n");
main();
}
for (i = 0; x == 1; i++)
{
for (; j == 0;)
{
getchar();
printf("请输入第%d个学生的姓名:", i + 1);
gets(stu[i].name);
printf("请输入第%d个学生的学号:", i + 1);
gets(stu[i].number);
printf("请输入第%d个学生的性别(男-m 女-f):", i + 1);
stu[i].sex = getchar();
printf("请输入第%d个学生的出生日期(格式:年-月-日):", i + 1);
scanf("%d%d%d", &stu[i].year, &stu[i].month, &stu[i].day);
printf("请输入第%d个学生当前的打卡次数:", i + 1);
scanf("%d", &stu[i].k);
printf("您输入的信息为: 姓名%s 学号%s 性别%c 出生日期 %d年%d月%d日 打卡次数%d \n ",
stu[i].name, stu[i].number, stu[i].sex,stu[i].year, stu[i].month, stu[i].day, stu[i].k);
printf("请确定信息 (重输-0 确定-1): ");
scanf("%d", &j); //输入确认
}
j = 0;
if (fwrite(&stu[i], sizeof(struct student), 1, fp) != 1)
printf("文件写入错误! \n"); // 写入文件
printf("是否继续输入下一个学生的信息 终止-0 继续-1: ");
scanf("%d", &x); //继续输入的选择
}
fclose(fp);
printf("返回主菜单-0 :");
getchar();
a = getchar();
getchar();
if (a == '0')
main(); //返回主菜单
} //录入数据函数结束
void change()
{
printf("************2.修改某个学生的打卡情况************\n");
int i = 0;
int j = 0;
int t = 10;
int it = 10;
int x = 10;;
char name[10] = { 0 };
char number[10] = { 0 };
if ((fp = fopen("data.txt", "r")) == NULL) //将文件内容读入内存
{
printf("文件打开失败! \n");
main();
}
for (i = 0; feof(fp) == 0; ++i)
{
fread(&stu[i], sizeof(struct student), 1, fp); //读取文件内容
}
printf("选择修改对象:按姓名查找-1 按学号查找-2: ");
scanf("%d", &it);
if (it == 1) //按姓名查找开始
{
printf("请输入需要修改学生的姓名: ");
scanf("%s", &name);
for (j = 0; t != 0; j++)
t = strcmp(stu[j].name, name);
if (j == i)
{
t = 0;
printf("未找到该学生信息,请重新输入! \n");
fclose(fp);
}
if (j < i) //查询成功
{
printf("您查询的信息为: 姓名%s 学号%s 性别%c 打卡次数%d \n ",
stu[j - 1].name, stu[j - 1].number, stu[j - 1].sex, stu[j - 1].k);
}
printf("是否确认修改该生打卡信息 否-0 是-1: ");
scanf("%d", &x);
if (x == 1)
{
printf("请输入新的打卡次数: ");
scanf("%d", &stu[j-1].k); //输入新的打卡次数
if ((fp = fopen("data.txt", "w")) == NULL) //以只写方式打开
{
printf("文件打开失败! \n");
main();
}
if (fwrite(&stu[j], sizeof(struct student), 1, fp) != 1)
printf("文件写入错误! \n"); //写入文件
}
fclose(fp);
}//按姓名查找结束
if (it == 2) //按学号查找开始
{
printf("请输入需要修改学生的学号:");
scanf("%s", &number);
for (j = 0; t != 0; j++)
t = strcmp(stu[j].number, number);
if (j == i)
{
t = 0;
printf("未找到该学生信息,请重新输入! \n");
}
if (j < i) //查询成功
{
printf("您查询的信息为: 姓名%s 学号%s 性别%c 打卡次数%d \n ",
stu[j - 1].name, stu[j - 1].number, stu[j - 1].sex, stu[j - 1].k);
}
printf("是否确认修改该生打卡信息 否-0 是-1:");
scanf("%d", &x);
if (x == 1)
{
printf("请输入新的打卡次数: ");
scanf("%d", &stu[j-1].k); //输入新的打卡次数
if ((fp = fopen("data.txt", "w")) == NULL) //以只写方式打开
{
printf("文件打开失败! \n");
main();
}
if (fwrite(&stu[j], sizeof(struct student), 1, fp) != 1)
printf("文件写入错误! \n"); //写入文件
}
fclose(fp);
}//按学号查询结束
printf("是否继续修改其他学生打卡信息 返回主菜单-0 继续-1: ");
scanf("%d", &x);
if (x == 1) change();
else main(); //返回主菜单
} //修改数据函数内容结束
void search()
{
printf("************3.查询某个学生的打卡记录************\n");
int i = 0;
int j = 0;
int it = 10;
int t = 10;
int x = 10;
char name[10] = { 0 };
char number[10] = { 0 };
if ((fp = fopen("data.txt", "r")) == NULL) //将文件内容读入内存
{
printf("文件打开失败! \n");
main();
}
for (i = 0; feof(fp) == 0; ++i)
{
fread(&stu[i], sizeof(struct student), 1, fp); //读取文件内容
}
printf("选择查找方式:按姓名查找-1 按学号查找-2: ");
scanf("%d", &it);
if (it == 1) //按姓名查找开始
{
printf("请输入需要查询学生的姓名: ");
scanf("%s", &name);
for (j = 0; t != 0; j++)
t = strcmp(stu[j].name, name);
if (j == i)
{
t = 0;
printf("未找到该学生信息,请重新输入! \n");
}
if (j < i) //查询成功
{
printf("您查询的信息为: 姓名%s 学号%s 性别%c 出生日期 %d年%d月%d日 打卡次数%d \n ",
stu[j - 1].name, stu[j - 1].number, stu[j - 1].sex, stu[j - 1].year, stu[j - 1].month, stu[j - 1].day, stu[j - 1].k);
}
fclose(fp);
}//按姓名查询结束
if (it == 2) //按学号查找开始
{
printf("请输入需要查询学生的学号:");
scanf("%s", &number);
for (j = 0; t != 0; j++)
t = strcmp(stu[j].number, number);
if (j == i)
{
t = 0;
printf("未找到该学生信息,请重新输入! \n");
}
if (j < i) //查询成功
{
printf("您查询的信息为: 姓名%s 学号%s 性别%c 出生日期 %d年%d月%d日 打卡次数%d \n ",
stu[j - 1].name, stu[j - 1].number, stu[j - 1].sex, stu[j - 1].year, stu[j - 1].month, stu[j - 1].day, stu[j - 1].k);
}
fclose(fp);
}
printf("是否继续查询其他学生打卡信息 返回主菜单-0 继续-1: ");
scanf("%d", &x);
if (x == 1) search();
else main(); //返回主菜单
}
void times()
{
printf("************4.旷课学生的姓名及次数 ************\n");
int i = 0;
int j = 0;
int it = 0;
if ((fp = fopen("data.txt", "r")) == NULL) //将文件内容读入内存
{
printf("文件打开失败! \n");
main();
}
for (i = 0; feof(fp) == 0; i++)
{
fread(&stu[i], sizeof(struct student), 1, fp); //读取文件内容
}
printf("旷课学生的姓名及次数如下:\n ");
for (j = 0; j < i-1;j++ )
{
if (stu[j].k < kq)
printf("姓名%s 学号%s 缺课次数%f \n ",stu[j].name, stu[j].number, (kq - stu[j].k));
}
printf("是否返回主菜单 0否 1是: ");
scanf("%d", &it);
if (it == 0) return;
else main();
}
void evaluate()
{
printf("************5.学生打卡的总评分数计算************\n");
int i = 0;
int j = 0;
int t = 10;
int it = 10;
int x = 10;
float score=0;
char name[10] = { 0 };
char number[10] = { 0 };
if ((fp = fopen("data.txt", "r")) == NULL) //将文件读入内存
{
printf("文件打开失败! \n");
main();
}
for (i = 0; feof(fp) == 0; ++i)
fread(&stu[i], sizeof(struct student), 1, fp); //读取文件内容
printf("选择计算对象:1 按姓名查找 2 按学号查找: ");
scanf("%d", &it);
if (it == 1) //按姓名查找开始
{
printf("请输入需要计算分数的学生姓名: ");
scanf("%s", &name);
for (j = 0; t != 0; ++j)
t = strcmp(stu[j].name, name);
if (j == i)
{
t = 0;
printf("未找到该学生信息,请重新输入! \n");
fclose(fp);
}
if (j < i) //查询成功
{
printf("您查询的信息为: 姓名%s 学号%s 打卡次数%d \n ",
stu[j - 1].name, stu[j - 1].number, stu[j - 1].k);
score = stu[j-1].k / kq * 100;
printf("该生当前打卡总评分为 %5.2f \n", score);
}
fclose(fp);
printf("是否继续计算其他学生打卡分数 0否 1是");
scanf("%d", &x);
if (x == 1) evaluate();
else main();
} //按姓名查找结束
if (it == 2) //按学号查找开始
{
printf("请输入需要计算分数的学生学号:");
scanf("%s", &number);
for (j = 0; t != 0; j++)
t = strcmp(stu[j].number, number);
if (j == i)
{
t = 0;
printf("未找到该学生信息,请重新输入! \n");
}
if (j < i) //查询成功
{
printf("您查询的信息为: 姓名%s 学号%s 性别%c 打卡日期 %d年%d月%d日 打卡次数%d \n ",
stu[j - 1].name, stu[j - 1].number, stu[j - 1].sex, stu[j - 1].year, stu[j - 1].month, stu[j - 1].day, stu[j - 1].k);
score = (float)(stu[j-1].k) / kq * 100;
printf("该生当前打卡总评分为 %5.2f \n", score);
}
fclose(fp);
printf("是否继续计算其他学生打卡分数 0否 1是");
scanf("%d", &x);
if (x == 1) evaluate();
else main();
} //按学号查找结束
printf("0 返回主菜单\n");
getchar();
a = getchar();
getchar();
if (a == '0')
main(); //返回主菜单
}
13、C语言小学生计算机辅助教学系统
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int Calculate(int x,char op,int y);/*声明函数Calculate(),计算(x,y)的四则运算(由op定)*/
int CreateRandomNumber(); /*声明函数CreateRandomNumber(),生成一个1-10的随机整数*/
char CreateRandomOperator(); /*声明函数CreateRandomOperator(),随机生成一个运算符号(+、-、*、/)*/
void PrintRandomRightEvaluation(); /*声明函数PrintRandomRightEvaluation(),生成一个题目做对的随机提示*/
void PrintRandomWrongEvaluation(); /*声明函数PrintRandomWrongEvaluation(),生成一个题目做错的随机提示*/
int main()
{
int a,b,userAnswer,i,rightNumber=0,flag=0;
/*a,b随机整数,userAnswer是用户答案,i是控制循环的变量,rightNumber是正确答案数,flag成绩标志,当成绩<75分,其值为1*/
char opChar; /*opChar是运算符*/
srand(time(NULL)); /*time(NULL)的返回值作为产生随机数的种子*/
do{
for(i=0;i<10;i++) /*循环10次,做10道题*/
{
a=CreateRandomNumber(); /*产生一个随机数赋值给整型变量a*/
b=CreateRandomNumber(); /*产生一个随机数赋值给整型变量b*/
opChar=CreateRandomOperator(); /*产生一个随机运算符赋值给字符变量opChar*/
printf("%d%c%d=?\n",a,opChar,b); /*输出一个四则运算表达式=?*/
scanf("%d",&userAnswer); /*用户输入答案*/
if(userAnswer==Calculate(a,opChar,b))/*如果答案正确*/
{
PrintRandomRightEvaluation(); /*输出一个题目做对的随机提示*/
rightNumber++; /*回答正确题数加1*/
}
else /*如果回答不正确*/
{
PrintRandomWrongEvaluation(); /*输出一个题目做错的随机提示*/
}
}
printf("Total score is %d\n",rightNumber*10);
printf("Rate of correctness is %d%%\n",rightNumber*10);
if(rightNumber*10<75) /*如果成绩低于75分*/
{
printf("Once Again!\n");
rightNumber=0; /*正确答案数清零*/
flag=1;
}
}while(flag);
return 0;
}
/*函数功能:计算两个数(x,y)的四则运算(运算符由op确定?),返回计算结果值*/
int Calculate(int x,char op,int y)
{
switch(op)
{
case '+': return x+y;
case '-': return x-y;
case '*': return x*y;
case '/': return x/y;
default:printf("Operator error!\n");return 0;
}
}
/*函数功能:生成一个1-10的随机整数*/
int CreateRandomNumber()
{
return rand()%10+1;
}
/*函数功能:随机生成一个运算符号(+,-,*,/)*/
char CreateRandomOperator()
{
int op;
op=rand()%4+1;
switch(op)
{
case 1:return '+';
case 2:return '-';
case 3:return '*';
case 4:return '/';
}
return 0;
}
/*函数功能:生成一个题目做对的随机提示*/
void PrintRandomRightEvaluation()
{
int i;
i=rand()%4+1;
switch(i)
{
case 1:printf("Very good!\n");
break;
case 2:printf("Excellent!\n");
break;
case 3:printf("Nice work!\n");
break;
case 4:printf("Keep up the good work!\n");
break;
default:printf("Wrong type!");
}
}
/*函数功能:生成一个题目做错的随机提示*/
void PrintRandomWrongEvaluation()
{
int i;
i=rand()%4+1;
switch(i)
{
case 1:printf("No.Please try again.\n");
break;
case 2:printf("Wrong.Try once more.\n");
break;
case 3:printf("Don't give up!\n");
break;
case 4:printf("Not correct.Keep trying.\n");
break;
default:printf("Wrong type!");
}
}
14、C语言门禁系统
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
struct information
{
char name[10];
char password[10];
char profession[20];
};
struct Thelog
{
char name[10];
char times[24];
char num[1];
};
FILE *fp; //文件指针
information *head;//链表头指针
void add_student(information a); //函数[1]管理员添加学生账号,使用门禁系统
void delete_student(char names[10]);//函数[2]管理员员删除学生账号,解除使用门禁系统权限
void administrator_login(); //函数[3]管理员登陆
void administrator_management(); //函数[4]管理员操作界面,添加学生账号,删除学生账号,查看学生使用门禁情况
void student_login(); //函数[5]学生登陆,用于打开门禁
void time (); //函数[6]时间函数,用于显示登陆时间
void All_output(); //函数[7]全部学生信息
void the_log(information a,char num[1]); //函数[8]录入学生使用门禁情况
void log_output(); //函数[9]输出学生使用门禁情况
void replace_massage(char names[10]); //函数[10]更改学生信息,只有管理员有权限
int main()
{
int n;
printf(" *********************************************\n");
printf(" * 欢迎使用四海门禁系统!!! *\n");
printf(" *********************************************\n");
printf("\n");
printf(" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
printf(" *输入0:退出系统; 输入1:管理员登陆; 输入2:学生登陆*\n");
printf(" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
printf(" ");
scanf("%d",&n);
while(n!=0)
{
if(n==1)
administrator_login();
else if(n==2)
student_login();
else if(n>10||n<0)
{
printf(" *********************************************\n");
printf(" * 欢迎使用四海门禁系统!!! *\n");
printf(" *********************************************\n");break;
}
printf(" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
printf(" *输入0:退出系统; 输入1:管理员登陆; 输入2:学生登陆*\n");
printf(" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
printf(" ");
scanf("%d",&n);
}
printf(" *********************************************\n");
printf(" * 欢迎使用四海门禁系统!!! *\n");
printf(" *********************************************\n");
return 0;
}
void administrator_login() //管理员登录
{
information a;
printf(" 请输入管理员账号:");
scanf("%s",a.name);
printf(" 请输入管理员密码:");
scanf("%s",a.password);
if(strcmp(a.name,"管理员")==0&&strcmp(a.password,"123456789")==0)
{
printf("\n");
printf(" *********管理员登陆成功!!!*********\n\n");
administrator_management();
}
else
{
printf( " 管理员登陆失败!!!请检查仔细核对账号、密码\n");
}
}
void administrator_management() //管理员操作台
{
int num;
printf(" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
printf(" *输入1:注册学生账号 输入2:删除账号 输入3:显示所有学生信息*\n");
printf(" *输入4;查看学生使用门禁情况 输入5:修改学生信息 输入6:刷新屏幕 *\n");
printf(" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
printf(" ");
scanf("%d",&num);
while(num!=-1)
{
if(num==0||num>10||num<0)
{
printf(" 管理员身份已注销\n");
break;
}
if(num==1)
{
fp=fopen("student_Account.txt","a");
information a;
printf(" 请输入账号:");
scanf("%s",a.name);
printf(" 请输入密码:");
scanf("%s",a.password);
printf(" 请输入专业班级:");
scanf("%s",a.profession);
add_student(a);
fclose(fp);
}
if(num==2)
{
char names[10];
printf(" 请输入要删除的账号:");
scanf("%s",names);
delete_student(names);
}
if(num==3)
{
fp=fopen("student_Account.txt","r");
All_output();
fclose(fp);
}
if(num==4)
{
fp=fopen("Thelog.txt","r");
log_output();
fclose(fp);
}
if(num==5)
{
char names[10];
fp=fopen("student_Account.txt","r+");
printf(" 请输入需要修改的账号:");
scanf("%s",names);
replace_massage(names);
fclose(fp);
}
if(num==6)
{
system("cls");
}
printf(" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
printf(" *输入1:注册学生账号 输入2:删除账号 输入3:显示所有学生信息*\n");
printf(" *输入4;查看学生使用门禁情况 输入5:修改学生信息 输入6:刷新屏幕 *\n");
printf(" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
printf(" ");
scanf("%d",&num);
}
}
void student_login() //学生登录
{
information a;
information b;
fp=fopen("student_Account.txt","r");
printf(" 请输入账号:");
scanf("%s",a.name);
printf(" 请输入密码:");
scanf("%s",a.password);
fread(&b,sizeof(struct information),1,fp);
int i=0;
while(feof(fp)==0)
{
if(strcmp(a.name,b.name)==0&&strcmp(a.password,b.password)==0)
{
char num[1];
printf(" 请选择要打开几号门\n");
printf(" ");
scanf("%s",num);
time();
printf(" 亲爱的%s童鞋,你成功登陆本系统,已开启%s号门\n",a.name,num);
i++;
the_log(a,num);
}
fread(&b,sizeof(struct information),1,fp);
}
if(i==0)
printf(" 登陆账号不存在或密码错误!!!若无账号请联系管理员分配账号\n");
fclose(fp);
}
void delete_student(char names[10]) //管理员删除学生账号,解除使用门禁权限
{
FILE *p;
information a;
int i=0;
fp=fopen("student_Account.txt","r");
p=fopen("student.txt","w");
fread(&a,sizeof(struct information),1,fp);
while(feof(fp)==0)
{
if(strcmp(a.name,names)==0)
{fread(&a,sizeof(struct information),1,fp);i=1;}
else
{
fwrite(&a,sizeof(struct information),1,p);
}
fread(&a,sizeof(struct information),1,fp);
}
if(strcmp(a.name,names)!=0)
fwrite(&a,sizeof(struct information),1,p);
else
i=1;
if(i==0)
printf(" 账号%s不在系统中\n",names);
else
printf(" 账号%s已成删除\n",names);
fclose(fp);
fclose(p);
remove("student_Account.txt");
rename("student.txt","student_Account.txt");
}
void add_student(information a) //管理员添加学生信息,分配账号,授权使用门禁系统
{
fseek(fp,-sizeof(struct information),1);
fwrite(&a,sizeof(struct information),1,fp);
printf(" 添加学生%s成功!!!\n\n",a.name);
}
void time() //显示本地时间
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime (&rawtime );
printf(" %s",asctime (timeinfo));
}
void All_output() //输出所有学生信息
{
int i=0;
information a;
fread(&a,sizeof(struct information),1,fp);
while(feof(fp)==0)
{
printf(" * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" * 学生账号:%-20s*\n",a.name);
printf(" * 学生密码:%-20s*\n",a.password);
printf(" * 专业班级:%-20s*\n",a.profession);
printf(" * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\n");
i++;
fread(&a,sizeof(struct information),1,fp);
}
if(i==0)
printf(" 系统中无学生账号!!!\n\n");
}
void the_log(information a,char num[1]) //实时保存学生使用门禁系统情况
{
FILE *p;
Thelog c;
strcpy(c.name,a.name);
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo =localtime ( &rawtime );
strcpy(c.times,asctime (timeinfo));
strcpy(c.num,num);
p=fopen("Thelog.txt","a");
fwrite(&c,sizeof(struct Thelog),1,p);
fclose(p);
}
void log_output() //输出学生使用门禁系统情况
{
Thelog a;
fread(&a,sizeof(struct Thelog),1,fp);
while(feof(fp)==0)
{
printf("%s",a.times);
printf("学生%s登录了门禁系统,并开启了%s号门\n",a.name,a.num);
fread(&a,sizeof(struct Thelog),1,fp);
}
}
void replace_massage(char names[10]) //更改学生信息
{
information a;
information b;
fread(&a,sizeof(struct information),1,fp);
if(strcmp(names,a.name)==0)
{
printf(" 请输入更改后的账号:");
scanf("%s",b.name);
printf(" 请输入修改后的密码:");
scanf("%s",b.password);
printf(" 请输入修改后的专业班级:");
scanf("%s",b.profession);
fseek(fp,-sizeof(struct information),1);//文件指针退回原结构体头
fwrite(&b,sizeof(struct information),1,fp);//更新联系人信息
printf(" 信息更改成功!!!\n\n");
}
else if(feof(fp)==0)
replace_massage(names);//递归调用直到找到需要更改的姓名,并完成更改
else
printf("%s不在系统中!!!\n\n",names);
}
15、C语言银行管理系统(文件操作)
#include<stdio.h> /*I/O函数*/
#include<stdlib.h> /*其它说明*/
#include<string.h> /*字符串函数*/
#define max 100
#define LEN 15 /* 账号和密码最大字符数,实际请更改*/
#define N 100 /* 最大账户数,实际请更改*/
int k=1,n=0, m=0;/* n代表当前记录的学生人数*/
//账户信息结构体
struct banker
{
char accountNo[LEN];
char accountName[LEN];
char Password[LEN];
float Balance;
char VIPoNormal[LEN];
}bank[N];
void fileWrite();
//存钱
void Save()
{
int item,i;
char s1[LEN+1]; //用来输账号的
printf("请输入需要存钱的账号\n");
scanf("%s",&s1);
for(i=0; i<n; i++) //n代表当前记录数
{
if(strcmp(s1,bank[i].accountNo)==0)
{
item=i;
}
}
printf("请输入需要存钱的金额\n");
float wd =0.0;
scanf("%f",&wd);
bank[item].Balance+=wd;
fileWrite();
return;
}
//查询银行账户信息
void seek()
{
//先把账户信息读进来 必须输入该账号和对应的密码,输入错误则给出警告
int j=0;
FILE * fp;
if((fp=fopen("account.txt","r"))==NULL)
return ;
while(fscanf(fp,"%s %s %s %f %s\n",&bank[j].accountNo,&bank[j].accountName,&bank[j].Password,&bank[j].Balance,&bank[j].VIPoNormal)==5)
{
j++;
}
n=j;
int i=0,item=0,flag=0;
char s1[LEN+1]; /* 以账号和密码最长长度+1为准*/ //用来输账号的
char s2[LEN+1]; /* 以账号和密码最长长度+1为准*/ //用来输密码的
printf("请输入需要查询的账号\n");
scanf("%s",&s1);
for(i=0; i<n; i++) //n代表当前记录数
{
if(strcmp(s1,bank[i].accountNo)==0)
{
flag=1;
item=i;
}
}
if(0==flag) //输入的账号和存好的账号不一致
printf("该账号不存在!\n");
else{
int flag1=0;//判断密码输入是否正确
printf("请输入需要查询的账号对应的密码\n");
scanf("%s",&s2);
if(strcmp(s2,bank[item].Password)==0)
{
flag1=1;
}
if(flag1==0){
printf("密码输入错误\n");//这块可以改,重新输入,初步打算while循环
return;
}else{
printf("密码输入正确\n");
printf("您余额为:%10.1f\n",bank[item].Balance);
}
}
return;
}
//取款模块
// vip一次最多3000 普通的2000
// 提取的钱超过余额则失败 ,用户可以选择重新输或者返回主菜单
// 取钱后进行相应的更新
void Withdraw()
{
//首先肯定需要查询一下
//先把账户信息读进来 必须输入该账号和对应的密码,输入错误则给出警告
int j=0;
FILE * fp;
if((fp=fopen("account.txt","r"))==NULL)
return ;
while(fscanf(fp,"%s %s %s %f %s\n",&bank[j].accountNo,&bank[j].accountName,&bank[j].Password,&bank[j].Balance,&bank[j].VIPoNormal)==5)
{
j++;
}
n=j;
int i=0,item=0,flag=0;
char s1[LEN+1]; /* 以账号和密码最长长度+1为准*/ //用来输账号的
char s2[LEN+1]; /* 以账号和密码最长长度+1为准*/ //用来输密码的
printf("请输入账号\n");
scanf("%s",&s1);
for(i=0; i<n; i++) //n代表当前记录数
{
if(strcmp(s1,bank[i].accountNo)==0)
{
flag=1;
item=i;
}
}
if(0==flag) //输入的账号和存好的账号不一致
printf("该账号不存在!\n");
else{
int flag1=0;//判断密码输入是否正确
printf("请输入需要查询的账号对应的密码\n");
scanf("%s",&s2);
if(strcmp(s2,bank[item].Password)==0)
{
flag1=1;
}
if(flag1==0){
printf("密码输入错误\n");//这块可以改,重新输入,初步打算while循环
return;
}else{
printf("密码输入正确\n");
printf("您余额为:%10.1f\n",bank[item].Balance);
printf("请输入需要取款的金额");
printf("您的账户类型是%s\n",bank[item].VIPoNormal);
printf("*****注意事项:*****\n");
printf("取款金额不能超过限额\n");
printf("V类型用户单次取款不得超过3000元\n");
printf("N类型用户单次取款不得超过2000元\n");
//判断类型
float jud=0.0;
if(strcmp(bank[item].VIPoNormal,"V")==0)
{
jud=3000;
}
else jud=2000;
float wd =0.0;
scanf("%f",&wd);
while(bank[item].Balance<wd || wd>jud)
{
printf("输入不符合规范,请按照注意事项进行输入");
scanf("%f",&wd);
}
//取款之后的更新操作
bank[item].Balance-=wd;
fileWrite();
}
}
return;
}
//转账
void Transfer()
{
//首先肯定需要查询一下
//先把账户信息读进来 必须输入该账号和对应的密码,输入错误则给出警告
int j=0;
FILE * fp;
if((fp=fopen("account.txt","r"))==NULL)
return ;
while(fscanf(fp,"%s %s %s %f %s\n",&bank[j].accountNo,&bank[j].accountName,&bank[j].Password,&bank[j].Balance,&bank[j].VIPoNormal)==5)
{
j++;
}
n=j;
int i=0,item=0,flag=0;
char s1[LEN+1]; /* 以账号和密码最长长度+1为准*/ //用来输账号的
char s2[LEN+1]; /* 以账号和密码最长长度+1为准*/ //用来输密码的
printf("请输入账号\n");
scanf("%s",&s1);
for(i=0; i<n; i++) //n代表当前记录数
{
if(strcmp(s1,bank[i].accountNo)==0)
{
flag=1;
item=i;
}
}
if(0==flag)
printf("该账号不存在!\n");
else{
int flag1=0;//判断密码输入是否正确
printf("请输入账号对应的密码\n");
scanf("%s",&s2);
if(strcmp(s2,bank[item].Password)==0)
{
flag1=1;
}
if(flag1==0){
printf("密码输入错误\n");//这块可以改,重新输入,初步打算while循环
return;
}else{
printf("密码输入正确\n");
printf("您余额为:%10.1f\n",bank[item].Balance);
printf("您的账户类型是%s\n",bank[item].VIPoNormal);
printf("请输入需要转账的账户");
int flag2=0;
char s3[LEN+1];
int item2=0;//标记目的账户
scanf("%s",&s3);
for(i=0; i<n; i++)
{
if(strcmp(s3,bank[i].accountNo)==0)
{
flag2=1;
item2=i;
}
}
while(flag2==0){
printf("账户输入有误请重新输入");
scanf("%s",&s3);
for(i=0; i<n; i++)
{
if(strcmp(s3,bank[i].accountNo)==0)
{
flag2=1;
item2=i;
printf("accountNo accountName Password Balance VIPoNormal \n");
printf("--------------------------------------------------------------------\n");
printf("%s %16s %10s %10.1f %s\n",bank[i].accountNo,bank[i].accountName,bank[i].Password,bank[i].Balance,bank[i].VIPoNormal);
}
}
}
printf("*****注意事项:*****\n");
printf("转账金额不能超过限额\n");
printf("V类型用户单次转账不得超过20000元\n");
printf("N类型用户单次转账不得超过10000元\n");
printf("请输入需要转账的金额\n");
float jud=0.0;
if(strcmp(bank[item].VIPoNormal,"V")==0)
{
jud=20000;
}
else jud=10000;
float money;
scanf("%f",&money);
while(money>jud || money>bank[item].Balance)
{
printf("输入不符合规范,请按照注意事项进行输入");
scanf("%f",&money);
}
if(strcmp(bank[item].VIPoNormal,"V")==0)
{
bank[item].Balance-=money*0.005;
}else{
bank[item].Balance-=money*0.01;
}
bank[item].Balance-=money;
bank[item2].Balance+=money;
fileWrite();
printf("转账成功\n");
}
}
return;
}
//插入学生信息函数
void insert()
{
int i;
printf("请输入待增加的账户数:");
scanf("%d",&n);
FILE * fp;
if((fp=fopen("account.txt","at"))==NULL)
printf("文件为空!\n");
for(i=0; i<n; i++)
{
printf("\n请输入第%d个账户的账号:",i+1);
scanf("\n%s",&bank[i].accountNo);
printf("\n请输入第%d个账户的姓名:",i+1);
scanf("%s",&bank[i].accountName);
printf("\n请输入第%d个账户的密码:",i+1);
scanf("%s",&bank[i].Password);
printf("\n请输入第%d个账户的余额:",i+1);
scanf("%f",&bank[i].Balance);
printf("\n请输入第%d个账户的类别:",i+1);
scanf("%s",&bank[i].VIPoNormal);
// sort(); //调用按学号排序函数
//写入文件
fprintf(fp,"%s %16s %10s %10.1f %s\n",&bank[i].accountNo,&bank[i].accountName,&bank[i].Password,&bank[i].Balance,&bank[i].VIPoNormal);
}
printf("\n添加成功!!!\n");
fclose(fp);
}
//显示当前信息
void display()
{
int i=0;
FILE * fp;
if((fp=fopen("account.txt","r"))==NULL)
printf("文件为空!\n");
while(fscanf(fp,"%s %s %s %f %s\n",&bank[i].accountNo,&bank[i].accountName,&bank[i].Password,&bank[i].Balance,&bank[i].VIPoNormal)==5)
{
i++;
}
n=i; //记录文件中数据的行数
printf("accountNo accountName Password Balance VIPoNormal \n");
printf("--------------------------------------------------------------------\n");
for(i=0;i<n;i++) //依次输出学生信息
{
printf("%s %16s %10s %10.1f %s\n",bank[i].accountNo,bank[i].accountName,bank[i].Password,bank[i].Balance,bank[i].VIPoNormal);
}
fclose(fp);
}
//保存当前账户信息
void fileWrite()
{
FILE *fp;
int i;
if((fp=fopen("account.txt","w"))==NULL)
printf("文件为空!\n");
for(i=0; i<n; i++)
{
fprintf(fp,"%s %16s %10s %10.1f %s\n",bank[i].accountNo,bank[i].accountName,bank[i].Password,bank[i].Balance,bank[i].VIPoNormal);
}
fclose(fp);
printf("保存成功!!!");
}
//界面
void menu()
{
int num;
printf(" \n\n \n\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" * 银行管理系统 *\n");
printf(" * *\n");
printf(" * * * * * * * *系统功能菜单* * * * * * * *\n");
printf(" * -------------------------------------------- *\n");
printf(" * 1. 查询账户信息 *\n");
printf(" * 2. 存钱 *\n");
printf(" * 3. 取款 *\n");
printf(" * 4. 转账 *\n");
printf(" * 5. 退出系统 *\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n");
printf("请选择菜单编号:");
scanf("%d",&num);
switch(num) {
case 0:
// help();
break;
case 1:
seek();
break;
case 2:
Save();
break;
case 3:
Withdraw();
break;
case 4:
Transfer();
break;
case 5:
k=0;
break;
default:
printf("请在0-8之间选择\n");
}
}
//主函数
int main()
{
while(k)
{
menu();
}
system("pause");
return 0;
}