###### Frii_430

2020-04-10 20:47 阅读 74

# 基础的结构体排序问题，前来求助大佬们QaQ

#include
#include
#include
#include
struct student //定义结构体 typedef
{
char name[20]; //姓名
char num[15]; //学号
char phone[15]; //手机号
char wechat[15]; //微信号
struct student next; //结构体指针
};
struct student temp; struct student *p1; struct student stu_1[10];
int K = 1;
void shuru(struct student * p1) //输入信息
{
printf("姓名:");
scanf("%s", &p1->name);
printf("学号:");
scanf("%s", &p1->num);
printf("手机号:");
scanf("%s", &p1->phone);
printf("微信号:");
scanf("%s", &p1->wechat);
}
struct student * shuru2() //数据输入函数
{
int i = 1,m=1; struct student *p1, *p2;
p1 = (struct student *)malloc(sizeof(struct student)); //初始化
if (p1 != NULL)
{
printf("\n\t\t\t
******输入数据*******\n");
printf("------------------------------------------------------------------\n");
printf("在姓名处输入“N”代表输入数据结束\n");
while (i)
{
printf("姓名:");
scanf("%s", &(*p1).name);
if (strcmp(p1->name, "N") == 0) //判断p1是否等于0
{
printf("\n输入完毕!\n");
printf("========================================================================\n");
i = 0;
p2->next = NULL;
free(p1); //防止野针
p1 = p2;
m = m + 1;
}
else
{
printf("学号:");
scanf("%s", &(*p1).num);
printf("手机号:");
scanf("%s", &(*p1).phone);
printf("微信号:");
scanf("%s", &(*p1).wechat);
K = K + 1;
printf("=====================================\n");
p2 = p1;
p1 = (struct student )malloc(sizeof(struct student));
stu_1[m] = (*p1);
if (p1 != NULL)
{
p2->next = p1;
}
}
}
}
return(p1->next);
}
struct student * shuchu1(struct student*p1) //输出数据的函数
{
printf("\n\t\t\t
******显示数据*******\n");
while (p1 != NULL)
{
printf("--------------------------------------------------------------------\n");
printf("姓名:%s\n", p1->name);
printf("学号:%s\t", p1->num);
printf("手机号:%s\t", p1->phone);
printf("微信号:%s\n", p1->wechat);
p1 = p1->next;
}
printf("--------------------------------------------------------------------\n");
return p1;
}
void paixuput()
{
int j, i, l;
for (i = 0; i <= K; i++)
{
l = i;
for (j = i + 1; j < K; j++)
if (stu_1[j].num > stu_1[l].num)
l = j;
temp = stu_1[l];
stu_1[l] = stu_1[i];
stu_1[i] = temp;
}
for (p1=stu_1;p1<=stu_1+K-1;p1++)
{
printf("姓名:%s\n", p1->name);
printf("学号:%s\t", p1->num);
printf("手机号:%s\t", p1->phone);
printf("微信号:%s\n", p1->wechat);
}
}
void caidan() //主菜单
{
system("cls");
printf("\n\n\n");
printf(" ***********************************************************\n");
printf(" \t\t\t班级通讯录管理系统\n");
printf("\n");
printf(" \t\t1. 输入数据");
printf("\t\t2. 显示数据\n");
printf(" \t\t3. 插入数据");
printf("\t\t4. 删除数据\n");
printf("\t\t5. 排序数据\n");
printf("\t\t6. 修改数据\n");
printf("\t\t8. 返回主菜单\n");
printf(" ***********************************************************\n");
printf(" ~~~~~退~~~~~~~~~~出~~~~~~~~~~请~~~~~~~~~~按~~~~~~~~~~9~~~~~\n");
printf(" -----------------------------------------------------------\n\n");
}
int main()
{
struct student *p1 = NULL;
int i;
caidan();
while (1)
{
printf("请输入你的选择(1~9):");
loop:scanf("%d", &i);
if (i9)
{
caidan();
printf("输入有误,请在1~9中进行选择:");
goto loop;
}
switch (i)
{
case 1:
shuru2();
caidan();
break;
case 2:
break;
case 3:

``````        break;
case 4:

break;
case 5:
paixuput();
break;
case 6:

break;
case 7:

break;
case 8:
caidan();
break;
case 9:
exit(1);
break;
}
}
``````

}

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享