0.显示已建立的校园卡 1.建立校园卡 2.买饭 3.续钱 4.添加校园卡; 5.注销校园卡 6.设置与解除挂失 7.统计挂失人数 8.退出程序用c语言实现以上功能,要有链表存储和查找算法、排序算法
时间: 2024-05-01 08:22:39 浏览: 93
基于C语言实现(控制台)校园卡管理系统【100012316】
以下是使用链表实现的C语言代码,包括建立校园卡、买饭、续钱、添加校园卡、注销校园卡、设置与解除挂失、统计挂失人数等功能。代码中使用了选择排序算法对校园卡进行排序。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 校园卡结构体
typedef struct campus_card {
int num; // 卡号
char name[30]; // 姓名
float balance; // 余额
int lost; // 是否挂失,0表示未挂失,1表示已挂失
struct campus_card *next;
} CampusCard;
// 初始化链表
CampusCard* init() {
CampusCard *head = (CampusCard*)malloc(sizeof(CampusCard));
head->next = NULL;
return head;
}
// 建立校园卡
void create(CampusCard *head) {
printf("请输入卡号:");
int num;
scanf("%d", &num);
CampusCard *p = head->next;
while (p) {
if (p->num == num) {
printf("该卡号已存在,请重新输入卡号:");
scanf("%d", &num);
p = head->next;
} else {
p = p->next;
}
}
CampusCard *card = (CampusCard*)malloc(sizeof(CampusCard));
card->num = num;
printf("请输入姓名:");
scanf("%s", card->name);
printf("请输入余额:");
scanf("%f", &card->balance);
card->lost = 0;
card->next = NULL;
p = head;
while (p->next) {
p = p->next;
}
p->next = card;
printf("建立成功!\n");
}
// 买饭
void buy_food(CampusCard *head) {
printf("请输入卡号:");
int num;
scanf("%d", &num);
CampusCard *p = head->next;
while (p) {
if (p->num == num) {
if (p->lost == 1) {
printf("该卡已挂失,无法进行消费。\n");
} else {
printf("请输入消费金额:");
float cost;
scanf("%f", &cost);
if (cost > p->balance) {
printf("余额不足,无法进行消费。\n");
} else {
p->balance -= cost;
printf("消费成功!当前余额为%.2f元。\n", p->balance);
}
}
return;
} else {
p = p->next;
}
}
printf("卡号不存在,请重新输入。\n");
}
// 续钱
void add_money(CampusCard *head) {
printf("请输入卡号:");
int num;
scanf("%d", &num);
CampusCard *p = head->next;
while (p) {
if (p->num == num) {
if (p->lost == 1) {
printf("该卡已挂失,无法进行充值。\n");
} else {
printf("请输入充值金额:");
float money;
scanf("%f", &money);
p->balance += money;
printf("充值成功!当前余额为%.2f元。\n", p->balance);
}
return;
} else {
p = p->next;
}
}
printf("卡号不存在,请重新输入。\n");
}
// 添加校园卡
void add_card(CampusCard *head) {
CampusCard *p = head;
while (p->next) {
p = p->next;
}
CampusCard *card = (CampusCard*)malloc(sizeof(CampusCard));
printf("请输入卡号:");
scanf("%d", &card->num);
printf("请输入姓名:");
scanf("%s", card->name);
printf("请输入余额:");
scanf("%f", &card->balance);
card->lost = 0;
card->next = NULL;
p->next = card;
printf("添加成功!\n");
}
// 注销校园卡
void delete_card(CampusCard *head) {
printf("请输入卡号:");
int num;
scanf("%d", &num);
CampusCard *p = head->next;
CampusCard *pre = head;
while (p) {
if (p->num == num) {
pre->next = p->next;
free(p);
printf("注销成功!\n");
return;
} else {
pre = p;
p = p->next;
}
}
printf("卡号不存在,请重新输入。\n");
}
// 设置与解除挂失
void set_lost(CampusCard *head) {
printf("请输入卡号:");
int num;
scanf("%d", &num);
CampusCard *p = head->next;
while (p) {
if (p->num == num) {
if (p->lost == 0) {
p->lost = 1;
printf("挂失成功!\n");
} else {
p->lost = 0;
printf("解除挂失成功!\n");
}
return;
} else {
p = p->next;
}
}
printf("卡号不存在,请重新输入。\n");
}
// 统计挂失人数
void count_lost(CampusCard *head) {
int count = 0;
CampusCard *p = head->next;
while (p) {
if (p->lost == 1) {
count++;
}
p = p->next;
}
printf("当前挂失人数为%d人。\n", count);
}
// 选择排序算法,按卡号从小到大排序
void sort(CampusCard *head) {
CampusCard *p = head->next;
while (p) {
CampusCard *min = p;
CampusCard *q = p->next;
while (q) {
if (q->num < min->num) {
min = q;
}
q = q->next;
}
if (min != p) {
int temp_num = p->num;
p->num = min->num;
min->num = temp_num;
char temp_name[30];
strcpy(temp_name, p->name);
strcpy(p->name, min->name);
strcpy(min->name, temp_name);
float temp_balance = p->balance;
p->balance = min->balance;
min->balance = temp_balance;
int temp_lost = p->lost;
p->lost = min->lost;
min->lost = temp_lost;
}
p = p->next;
}
}
int main() {
CampusCard *head = init();
int choice = -1;
while (choice != 8) {
printf("请选择操作:\n");
printf("0.显示已建立的校园卡\n");
printf("1.建立校园卡\n");
printf("2.买饭\n");
printf("3.续钱\n");
printf("4.添加校园卡\n");
printf("5.注销校园卡\n");
printf("6.设置与解除挂失\n");
printf("7.统计挂失人数\n");
printf("8.退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 0: {
printf("卡号\t姓名\t余额\t是否挂失\n");
CampusCard *p = head->next;
while (p) {
printf("%d\t%s\t%.2f元\t%s\n", p->num, p->name, p->balance, p->lost == 0 ? "未挂失" : "已挂失");
p = p->next;
}
break;
}
case 1: {
create(head);
sort(head);
break;
}
case 2: {
buy_food(head);
break;
}
case 3: {
add_money(head);
break;
}
case 4: {
add_card(head);
sort(head);
break;
}
case 5: {
delete_card(head);
break;
}
case 6: {
set_lost(head);
sort(head);
break;
}
case 7: {
count_lost(head);
break;
}
case 8: {
break;
}
default: {
printf("请输入正确的操作编号!\n");
break;
}
}
printf("\n");
}
return 0;
}
```
阅读全文