C语言影院管理系统
D_W? 人气:0**影院管理系统基本运行图**
下面是实现全部功能的函数与解析与思路
**结构体与预定义**
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #include<windows.h> typedef struct accout { char pass[20]; char name[20]; struct accout* next; }mm, * MM; typedef struct movie { char number[20];//电影序号 char name[20];//电影名称 int price;//电影价格 int grade;//电影评分 int time;//电影时长 struct movie* next; } Linklist;
下面基本分为三大部分
一.登录与注册
1.注册函数
void Register(MM head)//注册 { system("cls");//清屏 char username[20] = ""; char password[13] = ""; char pass[13] = ""; char q; int i; FILE* fp; printf("\n\n\n"); printf("\t\t\t\t账号:"); scanf("%s", username); getchar(); printf("\t\t\t\t密码:"); i = 0; while (1) { q = getch(); if (q == '\r') { password[i] = '\0'; break; } else if (q == '\b') { printf("\b \b"); password[i++] = q; } else { password[i++] = q; printf("*"); } } printf("\n"); printf("\t\t\t\t请确认密码"); i = 0; while (1) { q = getch(); if (q != 13) { printf("*"); pass[i++] = q; } else { pass[i] = '\0'; printf("\n"); break; } } if (strcmp(pass, password) == 0) { fp = fopen("d:\\课设\\accout.txt", "ab+"); fprintf(fp, "%s %s", username, password);//把内存中的文件输入到硬盘之中 fclose(fp); system("cls"); printf("\n\n\n注册成功\n\n"); system("pause"); } }
2.判断函数
MM judge(mm * head)//判断密码是否输入正确 { char name1[20] = "", pass1[20] = ""; char q; MM p1; int i = 0, j = 0; system("cls"); printf("\n\n\n\n"); while (1) { j++; p1 = head->next; printf("\t\t\t\t账号"); scanf("%s", name1); getchar(); printf("\t\t\t\t密码"); i = 0; while (1) { j++; q = getch(); if (q == '\r') { pass1[i] = '\0'; break; } else if (q == '\b') { printf("\b \b"); pass1[i++] = q; } else { pass1[i++] = q; printf("*"); } } while (p1 != NULL) { if (strcmp(name1, p1->name) == 0) { if (strcmp(pass1, p1->pass) == 0) { printf("\n\n\t\t登录成功"); system("pause"); return head; } else { printf("登陆失败"); system("pause"); } } else { p1 = p1->next; } } if (p1 == NULL) { printf("\n\n\n\n\t\t\t\t输入有误"); system("cls"); break; } } return head; }
3./打开读取文件之前注册的账号和密码
放到链表里面操作
MM AccountLibrary(MM head)//打开读取注册的账号和密码,放到链表里面 { //用户数据库 FILE* fp1 = fopen("d:\\课设\\accout.txt", "r"); while (!feof(fp1)) //feof()是检测流上的文件结束符的函数,如果文件结束,则返回非0值,否则返回0 { MM q = (MM)malloc(sizeof(mm)); fscanf(fp1, "\n%s%s\n", q->name, q->pass); q->next = head; head = q; } fclose(fp1); return head; }
4.用户或者管理员进行注册或者登录操作
以下代码为具体部分操作函数
int E=judge(head);//将E作为判断judge函数返回值的标志 if (!E) guanliyuan();/user()
5.登录主函数
void login() { system("cls"); int bk = 0; int bb = 0; MM head = 0, p1 = 0; // char a[10]={0}; head = (mm*)malloc(sizeof(mm)); head->next = NULL; while (1) { // char a[200]; // char b[200]; int a, b; printf_d(); printf("请输入 "); scanf("%d", &a); switch (a) { case 1: {//登录 printf1(); printf("请输入 "); scanf("%d", &b); switch (b) { case 1: { AccountLibrary(head);//在文件中查找注册过的账号和密码 int E=judge(head); if (!E) guanliyuan(); } case 2: { //judge(head);//用户登录 AccountLibrary(head); int E=judge(head); if(!E) user(); break; } case 0: { printf("再见"); exit(0); break; } default:printf("输入错误"); } case 2: {//注册 Register(head); break; } case 0: {//退出 printf("再见"); break; } } } } }
二.管理员系统操作
管理员系统登录之后函数
void guanliyuan()//管理员系统 { Linklist* head; head = (Linklist*)malloc(sizeof(Linklist)); head->next = NULL;//创建头结点 //录入信息 int a = 0, b = 0; while (1) { system("cls"); fflush(stdin); printf2(); printf("请输入指令"); scanf("%d", &a); fflush(stdin); switch (a) { case 1://录入信息 { head = Input(); Foutput(head); system("cls"); break; } case 2://修改 { head = read(); change(head); Foutput(head); system("cls"); break; } case 3://删除 { head = read(); delete_1(head); Foutput(head); system("cls"); break; } case 4://排序 { head = read(); sort(head); output(head); //Foutput(head); system("cls"); break; } case 5://添加 { head = read(); insert(head); Foutput(head); system("cls"); break; } case 6://查找 { head = read(); fflush(stdin); search(head); system("cls"); break; } case 7://预览 preview(); break; /*case 8://修改密码//砍掉的功能 { MM head; head = NULL; printf("\t\t\t欢迎修改密码\n"); //head=duqu_cipher(head); head = AccountLibrary(head); change_cipher(head); luru_cipher(head); break; }*/ case 0://退出系统 { printf("感谢使用\n"); exit(1); } default: { printf("输入错误!请重新输入"); system("pause"); break; } } } free(head); }
1.录入信息
录入信息分为两部分
void Input_1(Linklist* pNew)//输入结点的值 { int i = 0, j = 0, num = 0, sum = 0; char ch = 0; fflush(stdin); //pNew = (Linklist*)malloc(sizeof(Linklist)); printf("请输入电影序号\n"); scanf("%s", pNew->number); fflush(stdin);//清除缓存区数据 if (pNew->number[0] == '#') return; printf("请输入电影名称\n"); scanf("%s", pNew->name); fflush(stdin); printf("请输入电影价格\n"); scanf("%d", &pNew->price); fflush(stdin); printf("请输入电影评分\n"); scanf("%d", &pNew->grade); fflush(stdin); printf("请输入电影时长\n"); scanf("%d", &pNew->time); fflush(stdin); printf("\n"); } Linklist* Input()//手动输入,录入信息链表 { Linklist* head = NULL, * pEnd, * pNew; pEnd = head = (Linklist*)malloc(sizeof(Linklist)); printf("电影序号输入#时停止输入"); pNew = (Linklist*)malloc(sizeof(Linklist)); Input_1(pNew); while (pNew->number[0] != '#')//尾插法 { pEnd->next = pNew; pNew->next = NULL; pEnd = pNew; pNew = (Linklist*)malloc(sizeof(Linklist)); Input_1(pNew); printf("\n"); } Foutput(head); free(pNew);//最后一个pNew是没有数据所以释放掉 return head; }
2.修改
void change(Linklist* head)//修改 { read(); int n, j, k, l, y; char m[20] = ""; char i = 0; Linklist* p = head; printf("1.名称2.评分3.价格4.时长5.编号"); printf("请输入要修改的项目"); scanf("%d", &n); switch (n) { case 1: { printf("请输入要修改的电影名称"); scanf("%s", m); while (p != NULL) { if (strcmp(p->number, m) == 0) { printf("输入修改之后的名称"); scanf("%s", &p->name); //strcpy() printf("修改完成"); } p = p->next; } } case 2: { printf("请输入要修改的的电影评分"); scanf("%d", &j); while (p != NULL) { if (p->grade == j) { printf("请输入修改之后的评分"); scanf("%d", &p->grade); printf("修改成功"); } } } case 3: { printf("请输入要修改的电影价格"); scanf("%d", &l); while (p != NULL) { if (p->price == l) { printf("请输入修改之后的价格"); scanf("%d", &p->price); printf("修改成功"); } } } case 4: { printf("请输入要修改电影时长"); scanf("%d", &k); while (p != NULL) { if (p->time == k) { printf("请输入修改之后的电影时长"); scanf("%d", &p->time); printf("修改成功"); } } } case 5: { printf("请输入要修改电影编号"); scanf("%d", &y); while (p != NULL) { if (p->time == y) { printf("请输入修改之后的电影时长"); scanf("%d", &p->time); printf("修改成功"); } } } } }
3.删除
void delete_1(Linklist* head)//删除 { Linklist* p, * p1 = head; p = p1->next; int n, i; char a[20]; printf("请输入1确认删除"); scanf("%d", &i); switch (i) { case 1: { printf("请输入要删除的电影编号\n"); scanf("%s", a); while (p != NULL) { if ((strcmp(p->number, a)) == 0) { printf("\t\t以下是要删除的电影信息\n\n"); printf("%s\t%s\t%d\t%d\t%d\n\n", p->number, p->name, p->price, p->grade, p->time); p1->next = p->next; free(p); printf("删除完毕"); } p1 = p; p = p->next; } } default: { printf("输入错误....."); break; } } }
4.排序
这里用得是链表冒泡排序
有点难理解,附图
void sort(Linklist* head)//单链表冒泡排序 { int n; Linklist* p, * q, * p1; p1 = NULL; printf("1.按票价从低到高排序2.按评分从低到高排序3.按时长从短到长排序\n"); printf("请选择排序方式"); scanf("%d", &n); switch (n) { case 1: { while ((head->next->next) != p1)//相当于数组第一层循环 { p = head;//一个数据 q = head->next;//下一个数据 while (q->next != p1) { if (q->price > q->next->price) { p->next = q->next; //第一步 q->next = q->next->next;//第二步 p->next->next = q;//第三步 q = p->next;//第四补3 } q = q->next;//接着往后走 p = p->next; } p1 = q;//相当于数组比较的下一个外层 } printf("排序完成"); output(head); system("pause"); break; } case 2: { while ((head->next->next) != p1)//相当于数组第一层循环 { p = head;//一个数据 q = head->next;//下一个数据 while (q->next != p1) { if (q->grade > q->next->grade) { p->next = q->next; //第一步 q->next = q->next->next;//第二步 p->next->next = q;//第三步 q = p->next;//第四补3 } q = q->next;//接着往后走 p = p->next; } p1 = q;//相当于数组比较的下一个外层 } printf("排序完成"); output(head); break; } case 3: { while ((head->next->next) != p1)//相当于数组第一层循环 { p = head;//一个数据 q = head->next;//下一个数据 while (q->next != p1) { if (q->time > q->next->time) { p->next = q->next; //第一步 q->next = q->next->next;//第二步 p->next->next = q;//第三步 q = p->next;//第四补3 } q = q->next;//接着往后走 p = p->next; } p1 = q;//相当于数组比较的下一个外层 } printf("排序完成"); output(head); break; } } }
5.添加
void insert(Linklist* head)//添加新的电影信息 { Linklist* p = head, * p1; //p=p1->next; int n, i; printf("请输入1进行添加信息操作"); scanf("%d", &n); switch (n) { case 1: { printf("请输入要插入信息前面的电影编号"); scanf("%d", &i); while (p != NULL) { if (atoi(p->number) == i) { p1 = (Linklist*)malloc(sizeof(Linklist));//开辟新的空间 printf("请输入电影编号"); scanf("%s", p->number); printf("请输入电影名称"); scanf("%s", p->name); printf("请输入电影价格"); scanf("%d", &p->price); printf("请输入电影评分"); scanf("%d", &p->grade); printf("请输入电影时长"); scanf("%d", &p->time); printf("电影信息添加完毕"); p1->next = p->next;// p->next = p; } p = p->next;//遍历 } } default: { printf("输入错误。。。。"); } } Foutput(head); }
*6.查找
void search(Linklist* head) //查询 { Linklist* p = head; //int n; char a[20] = ""; printf("请输入要查询电影编号"); scanf("%s", a); while (p != NULL) { if ((strcmp(p->number, a)) == 0) { printf("编号\t名称\t价格\t评分\t时长\n"); printf("%s\t%s\t%d\t%d\t%d\t\t", p->number, p->name, p->price, p->grade, p->time); break; } p = p->next; } system("pause"); }
三.用户操作系统
用户登录主函数
void user() { fflush("stdin"); Linklist* head; head = (Linklist*)malloc(sizeof(Linklist)); head->next = NULL; int n; printf3(); printf("请输入要选择的操作"); scanf("%d", &n); system("pause"); char c[20] = ""; // printf("") switch (n) { case 1: { printf("\t\t\t欢迎预览\n"); preview(); system("pause"); break; } case 2: { printf("\t\t欢迎来到查询系统\n"); head = read(); fflush(stdin); search(head); system("cls"); break; } case 3: { printf("\t\t\t欢迎来到购票系统\n"); head = read(); fflush(stdin); Linklist* p = head; //p = (Linklist*)malloc(sizeof(Linklist)); printf("\t\t\t请输入要购买电影的编号\n"); scanf("%s", c); //printf("ggdggd"); while (p) { if ((strcmp(p->number, c)) == 0) { printf("*****************************"); printf("|--------电影名字%s----------|\n", p->name); printf("|--------电影价格%d----------|\n", p->price); printf("|--------电影时间%d----------|\n", p->time); printf("|--------恭喜购票成功--------|\n"); printf("|----------------------------|\n"); printf("*****************************"); system("pause"); break; } p = p->next; } break; } case 4: { printf("感谢使用"); exit(1); } } }
预览
void preview() { printf("\t\t***********************************************\n"); printf("\t\t| 电影名称:Captain America 价格: 35 |\n"); printf("\t\t| 评分:9 时长: 135 |\n"); printf("\t\t***********************************************\n"); printf("\t\t***********************************************\n"); printf("\t\t| 电影名称:The Shawshank Redemption 价格:36 |\n"); printf("\t\t| 评分:8 时长:126 |\n"); printf("\t\t***********************************************\n"); printf("\t\t***********************************************\n"); printf("\t\t| 电影名称:Citizen Kane Redemption 价格:34 \n"); printf("\t\t| 评分:10 时长:123 |\n"); printf("\t\t***********************************************\n"); printf("\t\t***********************************************\n"); printf("\t\t| 电影名称:Casablanca 价格:32 |\n"); printf("\t\t| 评分:6 时长:157 |\n"); printf("\t\t***********************************************\n"); printf("\t\t***********************************************\n"); printf("\t\t| 电影名称:The Godfather Part II 价格:31 |\n"); printf("\t\t| 评分:5 时长:134 |\n"); printf("\t\t***********************************************\n"); }
四.文件操作
void Foutput(Linklist* head)//电影信息录入文件 { Linklist* pt = head; FILE* fp; pt = pt->next; fp = fopen("d:\\课设\\dgw.txt", "wt+"); if (fp == NULL) { printf("文件打开失败"); exit(1); } while (pt) { fprintf(fp, "%s %s %d %d %d", pt->number, pt->name, pt->price, pt->grade, pt->time); pt = pt->next; } fclose(fp); }
五.打印函数实现
void printf2() { system("cls"); printf("\t\t\t|*******************************|\n"); printf("\t\t\t| 欢迎来到影院管理系统(管理员) |\n"); printf("\t\t\t|*******************************|\n"); printf("\t\t\t| 1.录入信息 |\n"); printf("\t\t\t| 2.修改 |\n"); printf("\t\t\t| 3.删除 |\n"); printf("\t\t\t| 4.排序 |\n"); printf("\t\t\t| 5.添加 |\n"); printf("\t\t\t| 6.查询 |\n"); printf("\t\t\t| 7.预览 |\n"); printf("\t\t\t|-------------------------------|\n"); printf("\t\t\t|-------------------------------|\n"); printf("\t\t\t| 0.退出程序 |\n"); printf("\t\t\t|===============================|\n"); printf("\t\t\t请选择你需要的操作\t\t"); }//管理员系统菜单 void printf3()//用户登录界面 { system("cls"); printf("\t\t\t|*******************************|\n"); printf("\t\t\t| 欢迎来到影院管理系统(个人用户)|\n"); printf("\t\t\t|*******************************|\n"); printf("\t\t\t| 1.预览 |\n"); printf("\t\t\t| 2.查询 |\n"); printf("\t\t\t| 3.购票并打印报表 |\n"); printf("\t\t\t| 0.退出程序 |\n"); printf("\t\t\t|===============================|\n"); } void printf_d() { system("cls"); printf("\n\n\n\n"); printf("\t\t\t★☆★☆★☆★☆★☆★☆★☆★☆\n"); printf("\t\t\t☆ ★\n"); printf("\t\t\t☆ 1.登录 ★\n"); printf("\t\t\t☆ 2.注册 ★\n"); printf("\t\t\t★ 0.退出 ☆\n"); printf("\t\t\t☆ ★\n"); printf("\t\t\t★☆★☆★☆★☆★☆★☆★☆★☆\n"); }//登录 void printf1()//不同用户登录 { system("cls"); printf("\n\n\n\n"); printf("\t\t\t********************************\n"); printf("\t\t\t|| ||\n"); printf("\t\t\t|| 1.管理员登录 ||\n"); printf("\t\t\t|| 2.用户登录 ||\n"); printf("\t\t\t|| 0.退出 ||\n"); printf("\t\t\t|| ||\n"); printf("\t\t\t********************************\n"); }
加载全部内容