#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define USER "1234"
#define PASSWORD "1234"
struct APP{
int no;
char name[20];
char version[20];
char time[20];
};
struct List
{
struct APP data;
struct List* next;
};
struct List* load();
void print(struct List* head);
void save(struct List* head);
struct List* add(struct List* head);
void init();
int menu();
void search(struct List* head);
void change(struct List* head);
struct List* del(struct List* head);
void sort(struct List* head);
int main()
{
int choice;
struct List* head;
init();
head=load();
do
{
choice=menu();
switch(choice)
{
case 1:
head=add(head);
case 2:
print(head);
break;
case 3:
search(head);
break;
case 4:
change(head);
break;
case 5:
head=del(head);
break;
case 6:
sort(head);
print(head);
break;
}
}while(choice!=0);
save(head);
return 0;
}
int menu()
{
int choice;
system("cls");
printf("\n\n\n\n\n\n\n");
printf(" ********** 欢迎光临 **********\n\n");
printf(" ************************ APP管理系统 ************************\n\n\n");
printf("\n\n");
printf(" ***********************************\n");
printf(" *1:新增APP信息 2:显示APP信息*\n");
printf(" *3:查询APP信息 4:修改APP信息*\n");
printf(" *5:删除APP信息 6:排序APP信息*\n");
printf(" *0:退出 *\n");
printf(" ***********************************\n");
printf(" 请输入选择:");
scanf("%d",&choice);
return choice;
}
void search(struct List* head)
{
int no;
struct List* p=head;
printf("请输入要查询的APP编号:");
scanf("%d",&no);
while(p)
{
if(p->data.no==no)
{
printf("%d %s %s %s\n",p->data.no,p->data.name,p->data.version,p->data.time);
break;
}
p=p->next;
}
if(p==NULL)
{
printf("无该APP信息!\n");
}
system("pause");
}
void change(struct List* head)
{
struct APP t;
struct List* p=head;
system("cls");
printf("请输入要修改的APP编号:");
scanf("%d",&t.no);
while(p)
{
if(p->data.no==t.no)
{
printf("%d %s %s %s\n",p->data.no,p->data.name,p->data.version,p->data.time);
break;
}
p=p->next;
}
if(p==NULL)
{
printf("无该APP信息!\n");
}
else
{
printf("请修改APP名称:");
scanf("%s",t.name);
printf("请修改APP版本:");
scanf("%s",t.version);
printf("请修改APP发布时间:");
scanf("%s",t.time);
p->data=t;
printf("修改成功!\n");
}
}
struct List* del(struct List* head)
{
int no;
struct List* p=head,*q;
printf("请输入要删除的APP编号:");
scanf("%d",&no);
while(p)
{
if(p->data.no==no)
{
printf("%d %s %s %s\n",p->data.no,p->data.name,p->data.version,p->data.time);
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
printf("无该APP信息!\n");
}
else
{
if(p==head)
{
head=head->next;
}
else
{
q->next=p->next;
}
free(p);
printf("删除成功!\n");
}
return head;
}
void sort(struct List* head)
{
struct List* i,*j;
struct APP temp;
for(i=head;i!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(i->data.no>j->data.no)
{
temp=i->data;
i->data=j->data;
j->data=temp;
}
}
}
}
void init()
{
int n,y=1,i=0;
char a,d,b[10];
char code[100];
struct hotelinfo *head=NULL;
while(y)
{
system("cls");
printf("\n\n\n\n\n\n\n");
printf(" ********** 欢迎光临 **********\n\n");
printf(" ************************ APP管理系统 ************************\n\n\n");
printf("\n\n");
printf(" ***********************************\n");
printf(" ************1-用户登录***********\n");
printf(" ************0-退出系统***********\n");
printf(" ***********************************");
printf("\n");
printf(" 请输入您的选择:");
scanf("%d",&n);
printf("\n");
getchar();
switch(n){
case 0:
exit(0);
break;
case 1:
printf(" 请输入您的用户名(1234):");
gets(b);
printf("\n");
printf(" 请输入您的密码(1234):");
while((a=getch())!='\r')
{
if(a=='\b')
{
if(i>0)
{
printf("\b \b");
i--;
}
}
else
{
code[i++]=a;
printf("*");
}
}
code[i]='\0';
printf("\n");
if(strcmp(b,USER)!=0||strcmp(code,PASSWORD)!=0)
{
printf(" 验证失败,请重新输入!\n");
scanf("%c",&d);
getchar();
system("cls");
}
else
{
return;
}
break;
default:
printf(" 您的输入有误! 请重新输入!\n");
printf(" 按任意键继续!\n");
getchar();
break;
}
}
}
struct List* add(struct List* head)
{
struct APP newapp;
struct List* p;
printf("请输入APP编号:");
scanf("%d",&newapp.no);
printf("请输入APP名称:");
scanf("%s",newapp.name);
printf("请输入APP版本:");
scanf("%s",newapp.version);
printf("请输入APP发布时间:");
scanf("%s",newapp.time);
p=(struct List*)malloc(sizeof(struct List));
p->data=newapp;
p->next=head;
return p;
}
void save(struct List* head)
{
struct List *p=head;
FILE* fp=fopen("f1.txt","w");
if(fp==NULL)
{
printf("写入文件时,文件无法打开!");
exit(0);
}
while(p)
{
fprintf(fp,"%d %s %s %s\n",p->data.no,p->data.name,p->data.version,p->data.time);
p=p->next;
}
fclose(fp);
}
struct List* load()
{
FILE* fp;
struct List *head,*tail,*p1;
head=tail=NULL;
if((fp=fopen("f1.txt","r"))==NULL)
{
printf("不存在此文件,无法打开!");
fp=fopen("f1.txt","w");
fclose(fp);
exit(0);
}
if(fgetc(fp)!=EOF)
{
fp=fopen("f1.txt","r");
while(!feof(fp))
{
p1=(struct List*)malloc(sizeof(struct List));
fscanf(fp,"%d %s %s %s\n",&p1->data.no,p1->data.name,p1->data.version,p1->data.time);
if(head==NULL)
{
head=p1;
tail=head;
}
else
{
tail->next=p1;
}
tail=p1;
}
tail->next=NULL;
fclose(fp);
}
return head;
}
void print(struct List* head)
{
struct List* temp=head;
while(temp!=NULL)
{
printf("%d %s %s %s\n",temp->data.no,temp->data.name,temp->data.version,temp->data.time);
temp=temp->next;
}
system("pause");
}