[C#] 纯文本查看 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct a{
char name[100]; //电影名
int grade; //序号
int number; //一天场次
int time; //电影时长
int hall; //展厅号
double price; //票价
char timeline[100][100]; //时间段
struct a* next;
}Cinema,*List;
List head=NULL;
int Login(); // 登陆
void Menu(); // 主菜单
int Choose(); // 选择功能
void Creathead(); // 创建头结点
void Reload(); // 载入本地文件
void Search(); // 查找电影时间 进行购买
void DisplayAll(); // 显示所有电影进行购买
void Save(); // 保存
int main()
{
if(Login()==0) return 0;
Creathead();
// 加载文件中的数据
Reload();
while (1)
{
Menu();
switch (Choose())
{
case 1:
Search(); break;
case 2:
DisplayAll(); break;
case 3:
Save(); break;
case 0:
return 0;
}
}
}
// 登录页面
int Login()
{
while (1)
{
printf("----------------------------------------------------------\n");
printf("* *\n");
printf("* 1:用户登录 *\n");
printf("* 2:用户注册 *\n");
printf("* *\n");
printf("----------------------------------------------------------\n");
int choose;
char fusername[100], fpassword[100];
char username[100], password[100];
scanf("%d", &choose);
if (choose == 1)
{
FILE* f1 = fopen("user.txt", "r");
printf("请输入用户名:");
scanf("%s", username);
getchar();
printf("请输入密码:");
char c;
int i = 0;
while ((c = getch()) != '\r')
{
if (c == '\b')
{
i--;
putchar('\b');
putchar(' ');
putchar('\b');
}
else
{
password[i] = c;
i++;
putchar('*');
}
}
printf("\n");
password[i] = '\0';
fscanf(f1, "%s %s", fusername, fpassword);
if ((strcmp(fusername, username) == 0) && (strcmp(fpassword, password)) == 0)
{
printf("登陆成功\n");
getch();
system("cls");
return 1;
}
else
{
printf("用户名或密码错误");
getch();
return 0;
}
}
else if (choose == 2)
{
FILE* f2 = fopen("user.txt", "w");
printf("请输入用户名:");
scanf("%s", username);
printf("请输入密码:");
scanf("%s", password);
fprintf(f2, "%s %s", username, password);
fclose(f2);
system("cls");
printf("注册成功!\n");
}
}
}
//主菜单
void Menu()
{
printf("\n\n");
printf("-----------------------------影院售票管理--------------------------------\n");
printf("\t1.查找某个电影时间段购买 2.显示所有电影信息进行购买 3.保存 0.退出\n");
printf("\n-------------------------------------------------------------------------\n");
}
//选择
int Choose(){
int n;
printf("请输入序号:");
while (scanf("%d", &n) != 1 || n < 0 || n>8)
{
printf("请重新输入:");
scanf("%d", &n);
}
return n;
}
//加载 data.txt文件的数据
void Reload()
{
int number,time,hall,count=0,grade;
double price;
char name[100];
char timeline[100][100];
List b=head;
FILE* p = fopen("data.txt", "a+");
while (fscanf(p, "%s%d%d%d%d%lf",name,&grade,&number,&time,&hall,&price) == 6)
{
List a = (List)malloc(sizeof(Cinema));
b->next=a;
a->grade = grade;
a->price = price;
a->time = time;
a->hall = hall;
a->number = number;
strcpy(a->name, name);
int i;
for(i=0;i<number;i++)
{
fscanf(p,"%s",timeline[i]);
strcpy(a->timeline[i], timeline[i]);
}
b = b->next;
count++;
}
if(count!=0)
b->next = NULL;
printf("成功载入%d条信息\n\n", count);
fclose(p);
}
//创造头节点
void Creathead()
{
List a=(List)malloc(sizeof(Cinema));
a->price = 0;
a->time = 0;
a->hall = 0;
a->number = 0;
a->grade = 0;
strcpy(a->name, "0");
// strcpy(a->timeline, "0");
a->next=NULL;
head=a;
}
//查找某个时间的
void Search()
{
int flag = 1;
List a,b;
a=b = head->next;
char name[100];
printf("输入要查找的电影时间段:");
scanf("%s", name);
printf("\n");
while(a->time==name)
{
b=a;
a=b->next;
if (a== NULL)
{
printf("查找失败\n\n");
flag = 0;
break;
}
}
if(flag)
{
printf("电影名\t\t序号\t\t场次\t\t时长\t\t展厅号\t\t票价\t\t上映时间段\n");
printf("---------------------------------------------------------");
printf("---------------------------------------------------\n");
printf("%*s\t%d\t\t%d\t\t%d\t\t%d\t\t%.2lf\t\t[1]%s\n",-10,a->name,a->grade,a->number,a->time,a->hall,a->price,a->timeline[0]);
int i;
for(i=1;i<a->number;i++)
printf("\t\t\t\t\t\t\t\t\t\t\t\t[%d]%s\n",i+1,a->timeline[i]);
printf("\n");
}
printf("\n\n");
printf("选择购买的序号");
char n ;
scanf("%s", &n);
printf("购买成功");
getchar();
getchar();
system("cls");
}
// 显示所有时间 进行购买
void DisplayAll()
{
system("cls");
List a, b;
a = b = head->next;
if (head->next == NULL)
{
printf("无数据");
getchar();
getchar();
system("cls");
return;
}
printf("电影名\t\t序号\t\t场次\t\t时长\t\t展厅号\t\t票价\t\t上映时间段\n");
printf("---------------------------------------------------------");
printf("---------------------------------------------------\n");
while (a!=NULL)
{
int i;
int x=1 ;
printf("%d\n",x);
x=x+1;
printf("%*s\t%d\t\t%d\t\t%d\t\t%d\t\t%.2lf\t\t[1]%s\n",-10,a->name,a->grade,a->number,a->time,a->hall,a->price,a->timeline[0]);
for(i=1;i<a->number;i++)
printf("\t\t\t\t\t\t\t\t\t\t\t\t[%d]%s\n",i+1,a->timeline[i]);
printf("\n");
b = a;
a = b->next;
}
printf("选择购买的序号");
char n ;
scanf("%s", &n);
printf("购买成功");
getchar();
getchar();
system("cls");
}
//保存数据
void Save()
{
if (head->next == NULL)
{
printf("未录入信息\n\n");
return;
}
List a=head->next,b=head;
FILE *p=fopen("data.txt","w");
while(a!=NULL)
{
fprintf(p, "%s %d %d %d %d %.2lf ", a->name,a->grade, a->number, a->time, a->hall, a->price);
int i;
for(i=0;i< a->number;i++)
fprintf(p,"%s ",a->timeline[i]);
fprintf(p, "\n");
b=a;
a=a->next;
}
fclose(p);
printf("保存成功");
getch();
system("cls");
}