PTA习题9.5-通讯录排序
//6223
/*
3
zhang 19850403 13912345678
wang 19821020 +86-0571-88018448
qian 19840619 13609876543
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tel_d {//telephone directory电话簿
char name[20];
long int birth;
char tel[20];
}tele[20], temp;//不超过10个朋友
int main(int argc, char const *argv[])
{
int i, n, j;
scanf("%d", &n);
for(i=0; i<n; i++)//*****gets()读入空格时不会结束,操蛋
{
scanf("%s %d %s", &tele[i].name, &tele[i].birth, &tele[i].tel);
// scanf("%s", tele[i].name);
// scanf("%d", &tele[i].birth);
// scanf("%s", tele[i].tel);
}
// int num[20]={0};//记录顺序
/*选择排序*/
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(tele[i].birth>tele[j].birth)
{
//结构体可以相互传值,temp应该定义为结构体
temp = tele[i];
tele[i] = tele[j];
tele[j] = temp;
// num[i] = j;//记录与i有关的函数 *****************//
}
}
}
for(i=0; i<n; i++)
{
printf("%s %d %s\n", tele[i].name, tele[i].birth, tele[i].tel);
}
return 0;
}
结构体
结构体可以传值,数组不能相互传值!
字符串
字符串和数值可以混合输入,有空格应用scanf()函数.scanf()函数以空格,换行,tab结束