/*输入一组数据,包括某人的学号,姓名,性别,年龄和收入,
可以由用户决定按某种顺序排序 */
/*2005-4-22 梁见斌*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define OVERFLOW 1
typedef struct l{
char name[15];
char sex[8];
int number;
int age;
float income;
struct l *next;
}Lk;
Lk *creat(Lk stu[]);
void print(Lk *stu);
int sort_menu(void);
void sort(Lk **stu, int n) ;
void insert(Lk **stu);
void del_menu(Lk *stu);
void del_1 (Lk **stu);
void del_2 (Lk **stu);
void del_3 (Lk **stu);
void del_4 (Lk **stu);
void del_5 (Lk **stu);
void find_menu(Lk *stu);
void find_1 (Lk **stu);
void find_2 (Lk **stu);
void find_3 (Lk **stu);
void find_4 (Lk **stu);
void find_5 (Lk **stu);
int main(void)
{
Lk *student;
int flag, n;
student = creat(student);
a: do{
puts("您需要什么服务?");
puts("排序请按1;查找请按2;插入新名单请按3;");
puts("删除名单请按4;显示当前结果请按5;离开请按0");
printf("请输入一个选择: ");
scanf("%d", &flag);
fflush(stdin);
} while(flag < 0 || flag > 5) ;
switch(flag)
{
case 1: n=sort_menu();
sort(&student, n);
break;
case 2: find_menu(student);
break;
case 3: insert(&student);
break;
case 4: del_menu(student);
break;
case 5: print(student);
break;
default : puts("谢谢使用,再见!");
break;
}
do{
puts("您还需要其他服务吗?");
puts("继续请按1;离开请按0");
printf("请输入一个选择: ");
scanf("%d", &flag);
fflush(stdin);
} while(flag < 0 || flag > 1) ;
if(flag == 1)
goto a;
else
puts("谢谢使用,再见!");
system("pause");
return 0;
}
Lk *creat(Lk *stu)
{
int i=0, num;
Lk *head, *p, *s;
head = (Lk *)malloc(sizeof(Lk));
if(!head)
{
puts("出错了!");
getchar();
exit(OVERFLOW);
}
p = head;
printf("\n请输入第%d个人的序号,输入0表示输入结束\t", i+1);
scanf("%d", &num);
fflush(stdin);
while (num > 0)
{
s = (Lk *)malloc(sizeof(Lk));
if(!s)
{
puts("出错了!!");
getchar();
exit(OVERFLOW);
}
s->next = NULL;
p->next = s;
p = s;
s->number = num;
printf("姓名\t");
scanf("%s", &s->name);
fflush(stdin);
printf("性别\t");
scanf("%s", &s->sex);
fflush(stdin);
printf("年龄\t");
scanf("%d", &s->age);
fflush(stdin);
printf("薪水\t");
scanf("%f", &s->income);
fflush(stdin);
i++;
printf("\n请输入第%d个人的序号,输入0表示输入结束\t", i+1);
scanf("%d", &num);
fflush(stdin);
}
return head;
}
void print(Lk *stu)
{
Lk *p;
printf("%5s%10s%12s%10s%20s\n", "序号", "姓名", "性别", "年龄", "薪水");
for( p = stu->next; p; p=p->next)
printf("%3d%12s%12s%10d%20f\n",
p->number, p->name, p->sex, p->age, p->income);
}
int sort_menu(void)
{
int n;
do{
printf("您希望按照哪种顺序排列---按序号排列请按1\n");
printf("\t---按姓氏排列请按2\n");
printf("\t---按性别排列请按3\n");
printf("\t---按年龄排列请按4\n");
printf("\t---按薪水排列请按5\n");
scanf("%d", &n);
fflush(stdin);
} while(n < 1 || n > 5);
return n;
}
void sort(Lk **stu, int n)
{
Lk *head, *p, *q, *s, *min;
p = head = *stu;
while(p != NULL)
{
min = s = p;
switch(n)
{
case 1:
{
while(s->next != NULL)
{
if( s->next->number < min->next->number)
min = s;
s = s->next;
}
break;
}
case 2:
{
while(s->next != NULL)
{
if( strcmp(s->next->name , min->next->name) < 0)
min = s;
s = s->next;
}
break;
}
case 3:
{
while(s->next != NULL)
{
if( strcmp(s->next->sex , min->next->sex) < 0)
min = s;
s = s->next;
}
break;
}
case 4:
{
while(s->next != NULL)
{
if( s->next->age < min->next->age)
min = s;
s = s->next;
}
break;
}
case 5:
{
while(s->next != NULL)
{
if( s->next->income < min->next->income)
min = s;
s = s->next;
}
break;
}
default:
{
printf("出错了!");
getchar();
exit(1);
}
}
if(p != min)
{
q = min->next;
min->next = q->next;
q->next = p->next;
p->next = q;
}
p = p->next;
}
*stu = head;
}
void insert(Lk **stu)
{
Lk *p, *s;
int num, i=0;
printf("\n请输入第%d个人的序号,输入0表示输入结束\t", i+1);
scanf("%d", &num);
fflush(stdin);
while (num > 0)
{
s = (Lk *)malloc(sizeof(Lk));
if(!s)
{
puts("出错了!");
getchar();
exit(OVERFLOW);
}
p = *stu;
s->next = p->next;
p->next = s;
s->number = num;
printf("姓名\t");
scanf("%s", &s->name);
fflush(stdin);
printf("性别\t");
scanf("%s", &s->sex);
fflush(stdin);
printf("年龄\t");
scanf("%d", &s->age);
fflush(stdin);
printf("薪水\t");
scanf("%f", &s->income);
fflush(stdin);
i++;
printf("\n请输入第%d个人的序号,输入0表示输入结束\t", i+1);
scanf("%d", &num);
fflush(stdin);
}
}
void del_menu(Lk *stu)
{
int n;
do{
printf("请提供被删除对象的有关信息---提供序号请按1\n");
printf("\t---提供姓名请按 2\n");
printf("\t---提供性别请按 3\n");
printf("\t---提供年龄请按 4\n");
printf("\t---提供薪水请按 5\n");
scanf("%d", &n);
fflush(stdin);
} while(n < 1 || n > 5);
switch(n)
{
case 1: del_1 (&stu);
break;
case 2: del_2 (&stu);
break;
case 3: del_3 (&stu);
break;
case 4: del_4 (&stu);
break;
case 5: del_5 (&stu);
break;
default:
{
printf("出错了!");
getchar();
exit(1);
}
}
}
void del_1 (Lk **stu)
{
Lk *p, *s;
int num;
printf("\n请输入所对应的序号: ");
scanf("%d", &num);
fflush(stdin);
p = *stu;
s = p->next;
while(s != NULL)
{
if( s->number == num)
{
p->next = s->next;
free(s);
s = p->next;
}
else
{
p = s;
s = s->next;
}
}
}
void del_2 (Lk **stu)
{
Lk *p, *s;
char ch[15];
printf("\n请输入所对应的姓名: ");
scanf("%s", ch);
fflush(stdin);
p = *stu;
s = p->next;
while(s != NULL)
{
if( strcmp(s->name , ch) == 0)
{
p->next = s->next;
free(s);
s = p->next;
}
else
{
p = s;
s = s->next;
}
}
}
void del_3 (Lk **stu)
{
Lk *p, *s;
char ch[8];
printf("\n请输入所对应的性别: ");
scanf("%s", ch);
fflush(stdin);
p = *stu;
s = p->next;
while(s != NULL)
{
if(strcmp(s->sex , ch) == 0)
{
p->next = s->next;
free(s);
s = p->next;
}
else
{
p = s;
s = s->next;
}
}
}
void del_4 (Lk **stu)
{
Lk *p, *s;
int num;
printf("\n请输入所对应的年龄: ");
scanf("%d", &num);
fflush(stdin);
p = *stu;
s = p->next;
while(s != NULL)
{
if( s->age == num)
{
p->next = s->next;
free(s);
s = p->next;
}
else
{
p = s;
s = s->next;
}
}
}
void del_5 (Lk **stu)
{
Lk *p, *s;
float num;
printf("\n请输入所对应的薪水: ");
scanf("%f", &num);
fflush(stdin);
p = *stu;
s = p->next;
while(s != NULL)
{
if( s->income == num)
{
p->next = s->next;
free(s);
s = p->next;
}
else
{
p = s;
s = s->next;
}
}
}
void find_menu(Lk *stu)
{
int n;
do{
printf("请提供被删除对象的有关信息---提供序号请按1\n");
printf("\t---提供姓名请按 2\n");
printf("\t---提供性别请按 3\n");
printf("\t---提供年龄请按 4\n");
printf("\t---提供薪水请按 5\n");
scanf("%d", &n);
fflush(stdin);
} while(n < 1 || n > 5);
switch(n)
{
case 1: find_1 (&stu);
break;
case 2: find_2 (&stu);
break;
case 3: find_3 (&stu);
break;
case 4: find_4 (&stu);
break;
case 5: find_5 (&stu);
break;
default:
{
printf("出错了!");
getchar();
exit(1);
}
}
}
void find_1 (Lk **stu)
{
Lk *s;
int num;
printf("\n请输入所对应的序号: ");
scanf("%d", &num);
fflush(stdin);
printf("%5s%10s%12s%10s%20s\n", "序号", "姓名", "性别", "年龄", "薪水");
s = (*stu)->next;
while(s != NULL)
{
if( s->number == num)
printf("%3d%12s%12s%10d%20f\n",s->number, s->name, s->sex, s->age, s->income);
s = s->next;
}
}
void find_2 (Lk **stu)
{
Lk *s;
char ch[15];
printf("\n请输入所对应的姓名: ");
scanf("%s", ch);
fflush(stdin);
printf("%5s%10s%12s%10s%20s\n", "序号", "姓名", "性别", "年龄", "薪水");
s = (*stu)->next;
while(s != NULL)
{
if( strcmp(s->name , ch) == 0)
printf("%3d%12s%12s%10d%20f\n",s->number, s->name, s->sex, s->age, s->income);
s = s->next;
}
}
void find_3 (Lk **stu)
{
Lk *s;
char ch[8];
printf("\n请输入所对应的性别: ");
scanf("%s", ch);
fflush(stdin);
printf("%5s%10s%12s%10s%20s\n", "序号", "姓名", "性别", "年龄", "薪水");
s = (*stu)->next;
while(s != NULL)
{
if( strcmp(s->sex , ch) == 0)
printf("%3d%12s%12s%10d%20f\n",s->number, s->name, s->sex, s->age, s->income);
s = s->next;
}
}
void find_4 (Lk **stu)
{
Lk *s;
int num;
printf("\n请输入所对应的年龄: ");
scanf("%d", &num);
fflush(stdin);
printf("%5s%10s%12s%10s%20s\n", "序号", "姓名", "性别", "年龄", "薪水");
s = (*stu)->next;
while(s != NULL)
{
if( s->age == num)
printf("%3d%12s%12s%10d%20f\n",s->number, s->name, s->sex, s->age, s->income);
s = s->next;
}
}
void find_5 (Lk **stu)
{
Lk *s;
int num;
printf("\n请输入所对应的薪水: ");
scanf("%d", &num);
fflush(stdin);
printf("%5s%10s%12s%10s%20s\n", "序号", "姓名", "性别", "年龄", "薪水");
s = (*stu)->next;
while(s != NULL)
{
if( s->income == num)
printf("%3d%12s%12s%10d%20f\n",s->number, s->name, s->sex, s->age, s->income);
s = s->next;
}
}
正文
教师管理程序2005-04-23 07:33:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/goal00001111/692.html
阅读(3185) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论