用C语言结构体解决 “从键盘输入五个用户的数据,包括姓名和电话号码,要求按姓名排序后,输出用户数据

用C语言解决…… 定义一个二维数组,存储5个人的姓名和电话号码,从键盘输入姓名后,要求能输出电话号码~

#include
#include
void main()
{
int i;
char a[10][20],b[20];
printf("依次按对输入5个人的姓名和电话号码,如:张三 12345678 李四 87654321...
");
for(i=0;i<10;i++)
scanf("%s",a[i]);
printf("输入要查询的姓名(输入q结束):");
scanf("%s",b);
while(b[0]!='q')
{
for(i=0;i<5;i++)
{
if( !strcmp(b,a[2*i]) )
{
printf("电话号码是:%s

",a[2*i+1]);
break;
}
}
if(i==5)
printf("查无此人!

");
printf("输入要查询的姓名(输入q结束):");
scanf("%s",b);
}
}

#include
#define max 5 /*设定要输入成绩的学生个数*/

double zpj; /*总平均值*/
struct student /*结构体*/
{
int num;
char name[10];
int score1;
int score2;
int score3;
double pj;
};
struct student stu[max],temp;

void Input() /*输入函数*/
{ int i;
for(i=0;i<max;i++)
{printf("enter number");

scanf("%d",&stu[i].num);

printf("enter name");

scanf("%s",&stu[i].name);

printf("enter score1");

scanf("%d",&stu[i].score1);

printf("enter score2");

scanf("%d",&stu[i].score2);

printf("enter score3");

scanf("%d",&stu[i].score3);
}
}

average() /*求平均值函数*/

{int i;
for(i=0;i<5;i++)
{stu[i].pj=stu[i].score1+stu[i].score1+stu[i].score3;
zpj+=stu[i].pj;
}
for(i=0;i<5;i++)
stu[i].pj/=3;
zpj/=max;
}

MAX() /*找出最高平均值的学生的函数*/
{int i,j;
temp=stu[0];
for(i=0;i<max-1;i++)
for(j=i+1;j<max;j++)
if(stu[i].pj<stu[j].pj)
temp=stu[j];
}





output() /*输出函数*/

{int i;
for(i=0;i<max;i++)
printf("num=%d
name=%s
score1=%d
score2=%d
score3=%d
aver=%f
",
stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].pj);

printf("总平均分=%f

",zpj);

printf("平均分最高的学生
num=%d
name=%s
score1=%d
score2=%d
score3=%d
aver=%f
",
temp.num,temp.name,temp.score1,temp.score2,temp.score3,temp.pj);


}
void main() /*主函数*/
{

Input();
average();
MAX();
output();
}



自己写的 符合你的要求
不足之处还请指教

//希望我的回答对你的学习有帮助
#include <stdio.h>
#include <string.h>

struct Student
{
char Name[10]; //学生的姓名,使用字符数组存储,长度为 10
char PhoneNumber[15]; //学生的电话号,使用字符数组存储,长度为 15
}stu[5], temp; //结构体数组,长度为 5,用来存放 5 个学生的信息
//临时变量,用在冒泡排序的过程中交换数据

int main()
{
for (int i = 0; i < 5; i++) //输入 5 个学生的基本信息
{
scanf_s("%s %s", stu[i].Name, 10, &stu[i].PhoneNumber, 15); //输入字符串,使用 %s 标记。其中每个字符串使用“空格”分开
//如果输入的字符串包含“空格”,使用 gets 函数
}

for (int i = 0; i < 4; i++) //冒泡排序,外层循环只需要循环(长度 - 1)次
//因为剩下最后一个数据不需要比较
{
for (int j = 0; j < 4 - i; j++) //第一次循环,比较的次数为(长度 - 1)
//每次循环的次数都在缩小
//因为每次循环之后,最值已经放在了数组的尾部
//所以在下一次循环时,就可以排除这个最值,把比较的范围
{
if (strcmp(stu[j].Name, stu[j + 1].Name) > 0) //strcmp 由定义得到,它就是一个根据Ascii码,对字符串比较的函数
//如果strcmp 返回 0 ,证明两个字符串 相等
//如果strcmp 返回 - ,证明第一个字符串比第二个字符串小
//这个小,是指在两个字符串中第一个不相同的字符在 Ascii码表中的相对位置
//如果strcmp 返回 + ,证明第一个字符串比第二个字符串大
//这个大,是指在两个字符串中第一个不相同的字符在 Ascii码表中的相对位置
{
temp = stu[j];         //进行数据交换
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}

for (int i = 0; i < 5; i++) //输出 5 个学生的基本信息
{
printf("%s %s
", stu[i].Name,stu[i].PhoneNumber);
}

getchar(); getchar();

return 0;
}



网页链接

C语言集合



相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网