用C语言结构体解决 “从键盘输入五个用户的数据,包括姓名和电话号码,要求按姓名排序后,输出用户数据 c语言 结构体和共用体 1. 有5个学生,每个学生的数据包括...
\u7528C\u8bed\u8a00\u89e3\u51b3\u2026\u2026 \u5b9a\u4e49\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4,\u5b58\u50a85\u4e2a\u4eba\u7684\u59d3\u540d\u548c\u7535\u8bdd\u53f7\u7801,\u4ece\u952e\u76d8\u8f93\u5165\u59d3\u540d\u540e,\u8981\u6c42\u80fd\u8f93\u51fa\u7535\u8bdd\u53f7\u7801#include
#include
void main()
{
int i;
char a[10][20],b[20];
printf("\u4f9d\u6b21\u6309\u5bf9\u8f93\u51655\u4e2a\u4eba\u7684\u59d3\u540d\u548c\u7535\u8bdd\u53f7\u7801\uff0c\u5982\uff1a\u5f20\u4e09 12345678 \u674e\u56db 87654321...\n");
for(i=0;i<10;i++)
scanf("%s",a[i]);
printf("\u8f93\u5165\u8981\u67e5\u8be2\u7684\u59d3\u540d\uff08\u8f93\u5165q\u7ed3\u675f\uff09\uff1a");
scanf("%s",b);
while(b[0]!='q')
{
for(i=0;i<5;i++)
{
if( !strcmp(b,a[2*i]) )
{
printf("\u7535\u8bdd\u53f7\u7801\u662f\uff1a%s\n\n",a[2*i+1]);
break;
}
}
if(i==5)
printf("\u67e5\u65e0\u6b64\u4eba\uff01\n\n");
printf("\u8f93\u5165\u8981\u67e5\u8be2\u7684\u59d3\u540d\uff08\u8f93\u5165q\u7ed3\u675f\uff09\uff1a");
scanf("%s",b);
}
}
#include
#define max 5 /*\u8bbe\u5b9a\u8981\u8f93\u5165\u6210\u7ee9\u7684\u5b66\u751f\u4e2a\u6570*/
double zpj; /*\u603b\u5e73\u5747\u503c*/
struct student /*\u7ed3\u6784\u4f53*/
{
int num;
char name[10];
int score1;
int score2;
int score3;
double pj;
};
struct student stu[max],temp;
void Input() /*\u8f93\u5165\u51fd\u6570*/
{ 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() /*\u6c42\u5e73\u5747\u503c\u51fd\u6570*/
{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() /*\u627e\u51fa\u6700\u9ad8\u5e73\u5747\u503c\u7684\u5b66\u751f\u7684\u51fd\u6570*/
{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() /*\u8f93\u51fa\u51fd\u6570*/
{int i;
for(i=0;i<max;i++)
printf("num=%d\n name=%s\n score1=%d\n score2=%d\n score3=%d\n aver=%f\n",
stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].pj);
printf("\u603b\u5e73\u5747\u5206=%f\n\n",zpj);
printf("\u5e73\u5747\u5206\u6700\u9ad8\u7684\u5b66\u751f\nnum=%d\n name=%s\n score1=%d\n score2=%d\n score3=%d\n aver=%f\n",
temp.num,temp.name,temp.score1,temp.score2,temp.score3,temp.pj);
}
void main() /*\u4e3b\u51fd\u6570*/
{
Input();
average();
MAX();
output();
}
\u81ea\u5df1\u5199\u7684 \u7b26\u5408\u4f60\u7684\u8981\u6c42
\u4e0d\u8db3\u4e4b\u5904\u8fd8\u8bf7\u6307\u6559
//希望我的回答对你的学习有帮助
#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++调用c# ... c#视频教程 ... c++教程 ... c语言结构体个人总结 ... 为什么电脑不能用ctrl c了 ... c++编辑 ... ctrl c不能用了怎么办 ... c语言的结构体有哪些 ... c需要结构体 ...