#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#include"windows.h"
#define MAX 50
typedef struct LNode {
char name[MAX];
int id;
int VIP_level;
int length;
int expense ;
struct LNode* next; //嵌套
}LNode, * LinkList;
int t = 0;
char reward[MAX][2*MAX]{
"Billbill年度大会员",
"网易云会员一年",
"QQ音乐会员一年",
"特等奖!程序设计大礼包!(包含35RMB的数据结构教材)",
};
int products[MAX]{//产品
{1000},{2000} ,{3000}, {5000},{10000}
};
void init(LinkList& L) {
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
//LinkList* L = new LinkList;
L->length = 0;
L->expense = 0;
}
void Creat(LinkList& L) {
init(L);
LNode* p, * r = L;
int n, i = 0; L->length = 0;
printf("录入信息人数: ");
scanf("%d", &n);
printf("\n");
printf("客户信息客户姓名 ID VIP等级: \n");
for (; i < n; i++, L->length++) {
p = (LNode*)malloc(sizeof(LNode));
p->next = r->next;
r->next = p;
r = p;
scanf("%s %d %d", &p->name, &p->id, &p->VIP_level);
printf("\n");
}
}
void insert(LinkList& L) {
LNode* p, * r = L;
int n;
printf("输入新增客户信息(姓名 ID VIP等级): ");
printf("\n");
p = (LNode*)malloc(sizeof(LNode));
p->next = r->next;
r->next = p;
r = p;
scanf("%s %d %d", &p->name, &p->id, &p->VIP_level);
L->length++;
printf("\n");
}
void delete_(LinkList& L) {
LNode* p= L,*q = p;
int id;
printf("输入删除客户信息(ID): ");
scanf("%d", &id);
printf("\n");
//p = (LNode*)malloc(sizeof(LNode));
for (;p->id !=id&&p!=NULL;) {
q = p;
p = p->next;
}
q->next = p->next;
delete(p);
/*
q = q->next;
printf("被删除客户信息:\n ");
printf("%s %d %d", q->name, q->id, q->VIP_level);
*/
L->length--;
printf("\n");
}
/*
void sort_id(LinkList L) {
LNode* q, * p = L;
q = (LNode*)malloc(sizeof(LNode));
for (; p != NULL; ) {
printf(" %s %d %d\n\n", p->name, p->id, p->VIP_level);
p = p->next;
}
}*/
void lottery(LinkList &L) {//抽奖
srand(time(NULL));//每次随机不同
int n = ((int)rand() % L->length);
LNode* p = L->next;
for (int i = 0; i< n &&p != NULL; p = p->next) {
if (p->VIP_level < 6) {
srand(time(NULL));
n = (rand() % L->length);
}
else break;
}
int m = (rand() % 4);
printf("恭喜姓名为%s的幸运客户,中奖了!\n", p->name);
printf("您获得的奖品是 %s\n",reward[m] );//程序设计大礼包!
printf("\n");
}
void buy_products(LinkList& L) {
char quire[MAX];
int id;
int n;
double payment;
printf("输入本人信息(ID)\n");
scanf("%ld", &id);
printf("请输入购买产品编号(0-4)\n");
scanf("%d", &n);
printf("确认阅读注意事项,承诺遵守以上条约(yes / no)\n");
scanf("%s", quire);//数组名
if (strcmp(quire ,"yes")== 0 ) {
printf("购买成功!\n");
LNode* p = L->next;
for (; p != NULL; ) {
if (p->id == id)
{
p->expense = 0;
p->expense += products[n];
p->VIP_level += ((p->expense) / 1000 + 1);
payment = products[n] * (1 - 0.001 * p->VIP_level); //% products[n] //小折扣不亏
printf("尊敬的客户%s,您现在的VIP等级是: %d\n", p->name, p->VIP_level);
printf("本次您需要付款的金额为:%lf\n", payment);
}
p = p->next;
}
if (strcmp(quire, "no") == 0){ printf("无法购买!\n"); return; }
}
}
void show(LinkList &L) {
LNode* p = L->next;
printf("客户姓名 ID VIP等级 \n ");
for (; p != NULL; ) {
printf(" %s \t %d\t %d\t\n\n", p->name, p->id, p->VIP_level);
p = p->next;
}
}
void search_name(LinkList &L) {
LNode* p = L->next;
char name[MAX] ;//= "小李"
printf("请输入客户姓名\n");
scanf(" %s", &name);
for (;p != NULL; ) {
if (strcmp(p->name, name)==0)
{
//printf(" 查找成功!\n");
printf("客户信息:\n");
printf(" %s %d %d\n\n", p->name, p->id, p->VIP_level);
}
p = p->next;
//if (p = NULL) { printf(" 查找失败!\n"); }
}
}
void search_id(LinkList &L) {
LNode* p = L->next;
int id;
printf("请输入查找ID:\n");
scanf("%d", &id);
for (;p != NULL;p = p->next) {
if (p->id == id) {
printf(" %s %d %d\n\n", p->name, p->id, p->VIP_level);
}
}
}
void sort_VIP_level_max(LinkList &L) {
int i, j, temp; char t[MAX];
printf("客户等级排行:\n");
LNode* q = L->next, * p = L->next;
for (int i = 0; i < L->length - 1; i++){
for (j = 0; j < L->length - i - 1; j++) {
if (p->VIP_level < p->next->VIP_level) {
temp = p->VIP_level;
p->VIP_level = p->next->VIP_level;
p->next->VIP_level = temp;
temp = p->id;
p->id = p->next->id;
p->next->id = temp;
strcpy(t, p->name);
strcpy(p->name, p->next->name);
strcpy(p->next->name, t);
}
p = p->next;
}
q = q->next;
p = q;
}
show(L);//改变L展现排序后结果,引用型
printf("\n");
}
void discount(LinkList& L) {
srand(time(NULL));
int n = (rand() %11);
}
void menu() {
printf("******①录入客户信息***************\n\n");
printf("******②展示客户信息****************\n\n");
printf("******③删除客户信息***************\n\n");
printf("******④添加客户信息**************\n\n");
printf("******⑤查找客户信息**************\n\n");
printf("******⑥保存客户信息**************\n\n");
printf("******⑦购买产品**************\n\n");
printf("******⑧抽奖活动*****************\n\n");
printf("******⑨VIP排名***********\n\n");
printf("******⑩退出系统*****************\n\n");
}
void Function() {
menu();
LinkList L;
int n;
for (;;)
{
printf("请选择功能:\n");
scanf("%d", &n);
switch (n)
{
case 1:Creat(L); break;
case 2:show(L); break;
case 3:delete_(L); break;
case 4:insert(L); break;
case 5:search_name(L);break;
case 6:system("cls"); break;
case 7:buy_products(L); break;
case 8:lottery(L); break;
case 9:sort_VIP_level_max(L); break;
case 10:printf("已退出...\n"); exit(0); break;
default:
break;
}
if (n == 10)break;
}
}
int main() {
system("color 02");
Function();
/*
int n; scanf("%d", &n);//调试会不会异常终止退出
*/
return 0;
}
/*
test:
6
大李 3549 1
小魏 1426 1
小陈 7359 1
小李 2100 1
小傅 7531 1
大陈 2847 1
②show(L)
④insert(L)
大白 7777 1
②show(L)
③delete_(L)
1426
②show(L)
⑦buy_products(L)
3549
0
no
⑦
7777
4
yes
⑦
7359
3
yes
⑦
7359
0
yes
⑦
7359
0
yes
⑨VIP排行
⑧抽奖活动
⑤search_name(L)
小李
*/
/*
void swap(char** p1, char** p2)
{
char* p;
p = *p1;
*p1 = *p2;
*p2 = p;
}
*/
/*
void sort_VIP_level(LinkList& L) {
LNode* q, * p = L->next;
q = (LNode*)malloc(sizeof(LNode));
int i, j; string s;
printf("按阅读书籍数量排序(冒泡从小到大):\n ");
for (i = 0; i < L->length; i++)
for (j = 0; j < L->length - i; j++) {
if (p->VIP_level > p->next->VIP_level) {
q->VIP_level = p->VIP_level; q->id = p->id;
p->VIP_level = p->next->VIP_level; p->id = p->next->id;
p->next->VIP_level = q->VIP_level; p->next->id = q->id;
swap(p->name, p->next->name);
}
}
for (; p != NULL; ) {
printf(" %s %d %d\n\n", p->name, p->id, p->VIP_level);
p = p->next;
}
}
*/
/*
double show_count_expense(LinkList &L) {
double count = 0;
LNode* p = L->next;
for (; p != NULL; ) {
if (p->VIP_level != 1&&p->expense > 0) {
count += p->expense;
}
p = p->next;
//printf("业绩:%lf\n", count);
}
t = count;
printf("季度总销售业绩:%lf\n", count);
return count;
}
void salary(LinkList &L) {
double n ;
n = 3500 + t/20; //show_count_expense(L)
printf("本季度工资为%d", n);
printf("\n");
}
*/