C语言编程 歌手打分系统 高手进
发布网友
发布时间:2022-04-26 03:28
我来回答
共1个回答
热心网友
时间:2022-06-20 12:28
全力倾注打造:
#include <stdio.h>
#include <stdlib.h>
typedef struct _singer {
double score;
struct _singer* next;
} *node, singer;
node insert(node* head, node p, double score)
{
node tmp;
tmp = (node)malloc(sizeof(singer));
tmp->score = score;
tmp->next = p ? p->next : *head;
return (p ? p->next : *head) = tmp;
}
node create(double* beg, double* end)
{
node head, t;
head = t = NULL;
while(beg != end)
t = insert(&head, t, *beg++);
return head;
}
void remove(node* head, node p)
{
node r = *head;
if(p == *head) {
*head = r->next;
} else {
while(r->next != p) r = r->next;
r->next = p->next;
r = p;
}
free(r);
}
void show(node head)
{
while(head) {
printf("%.2f ", head->score);
head = head->next;
}
putchar('\n');
}
node sort(node head, int n)
{
int i, mid;
node l, r, t, v, p, *pp;
if(n < 2) return head;
mid = n/2;
t = l = r = head;
for(i = 0; i < mid; ++i)
t = r, r = r->next;
t->next = NULL;
l = sort(l, mid);
r = sort(r, n-mid);
v = p = NULL;
for(; l && r; (*pp) = (*pp)->next)
p = (v ? p->next : v) = *(l->score < r->score ? (pp = &l) : (pp = &r));
for(t = l ? l : r ? r : NULL; t; t = t->next)
p = (v ? p->next : v) = t;
return v;
}
#define FEN 1
#define PAI 0
void save(node head, int f)
{
FILE* fp;
node t = head;
char* file = f ? "D:\\aaa.txt" : "D:\\bbb.txt";
fp = fopen(file, "w");
while(t) {
fprintf(fp, "%.2f ", t->score);
t = t->next;
}
fclose(fp);
show(head);
printf("%s已保存!\n", f ? "分数" : "排名");
}
node input()
{
double s;
node head, t;
char c;
head = t = NULL;
printf("请输入歌手的分数(数量无*):");
while(scanf("%lf", &s)) {
t = insert(&head, t, s);
if((c = getchar()) == '\n')
break;
else
ungetc(c, stdin);
}
puts("输入完毕!");
return head;
}
void ins(node* head)
{
int n;
double s;
node t = *head;
printf("现在的分数序列是:");
show(*head);
printf("请输入你要插入到的位置(从1开始):");
scanf("%d", &n);
printf("请输入你要插入的分数:");
scanf("%lf", &s);
if(n == 1) t = NULL;
if(n >= 2) t = *head;
while(n-- > 2) t = t->next;
insert(head, t, s);
puts("插入完毕!");
}
void del(node* head)
{
int n;
node t = *head;
printf("现在的分数序列是:");
show(*head);
printf("请输入你要删除的分数所在的位置(从1开始):");
scanf("%d", &n);
while(--n) t = t->next;
remove(head, t);
puts("删除完毕!");
}
void pai(node* head)
{
int n;
node t = *head;
for(n = 0; t; ++n) t = t->next;
*head = sort(*head, n);
puts("排名完毕!");
}
int menu()
{
int n;
puts("* 1.输入歌手成绩 *");
puts("* 2.插入新歌手 *");
puts("* 3.删除退赛歌手 *");
puts("* 4.歌手成绩排名 *");
puts("* 5.输出歌手分数 *");
puts("* 6.输出歌手排名 *");
puts("* 7.退出系统 *");
scanf("%d", &n);
return n;
}
int main()
{
int done = 0;
node head;
while(!done)
switch (menu()) {
case 1: head = input(); break;
case 2: ins(&head); break;
case 3: del(&head); break;
case 4: pai(&head); break;
case 5: save(head, FEN); break;
case 6: save(head, PAI); break;
case 7: done = 1;
}
return 0;
}