c语言结构体 建立一份同学通讯录,包含姓名、电话、通信地址、邮编、生日等信息,要求按姓名的字母顺序排
#include<stdio.h>#include<string.h>structdata{intmonth;intday;intyear;};structstud{ch...
#include<stdio.h>
#include<string.h>
struct data
{
int month;
int day;
int year;
};
struct stud
{
char name[20];
char tele[12];
char zip[7];
struct data birthday;
char addre[30];
};
struct
stud1[30]={"Liming""123456789","216546",3,14,2001,"beijing","Xavi","789456123","216549",4,5,2009,"tianjing","Messi","894564613","121487",12,1,1993,"shanghai"};
main()
{
int k,i,j,n=3;
struct stud temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(stdu1[j].name,stud1[k].name)<0)
k=j;
temp=stud1[i];
stud1[i]=stud1[k];
stud1[k]=temp;
}
printf("姓名 电话 邮编 生日 地址");
for(i=0;i<3;i++)
printf("%10s%12s%8s %2d-%2d-%4d %15s\n",stud1[i].name,stud[i].zip,stud[i].birthday.month,stud1[i].birthday.day,stud[i].birthday.year,stud[i].addre);
}
在codeblock运行总错误什么before'['token什么的 怎么在原来基础上改 谢谢 展开
#include<string.h>
struct data
{
int month;
int day;
int year;
};
struct stud
{
char name[20];
char tele[12];
char zip[7];
struct data birthday;
char addre[30];
};
struct
stud1[30]={"Liming""123456789","216546",3,14,2001,"beijing","Xavi","789456123","216549",4,5,2009,"tianjing","Messi","894564613","121487",12,1,1993,"shanghai"};
main()
{
int k,i,j,n=3;
struct stud temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(stdu1[j].name,stud1[k].name)<0)
k=j;
temp=stud1[i];
stud1[i]=stud1[k];
stud1[k]=temp;
}
printf("姓名 电话 邮编 生日 地址");
for(i=0;i<3;i++)
printf("%10s%12s%8s %2d-%2d-%4d %15s\n",stud1[i].name,stud[i].zip,stud[i].birthday.month,stud1[i].birthday.day,stud[i].birthday.year,stud[i].addre);
}
在codeblock运行总错误什么before'['token什么的 怎么在原来基础上改 谢谢 展开
1个回答
展开全部
你对结构体中的子结构体的赋值也是不对的……
等我看看怎么改比较好……
改成这样:
#include<stdio.h>
#include<string.h>
struct data
{
int month;
int day;
int year;
};
struct stud
{
char name[20];
char tele[12];
char zip[7];
struct data birthday;
char addre[30];
};
/*{*/
int main()
{
//birthday是个整体,要加{}给birthday赋值
struct stud stu[30]={
"Liming","123456789","216546",{3,14,2001},"beijing",
"Xavi","789456123","216549",{4,5,2009},"tianjing",
"Messi","894564613","121487",{12,1,1993},"shanghai"};
int k,i,j,n=3;
struct stud temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(stu[j].name,stu[k].name)<0)
k=j;
temp=stu[i];
stu[i]=stu[k];
stu[k]=temp;
}
printf("姓名 电话 邮编 生日 地址\n");
for(i=0;i<3;i++)
printf("%10s%12s%8s %2d-%2d-%4d %15s\n",stu[i].name,stu[i].tele,stu[i].zip,stu[i].birthday.month,stu[i].birthday.day,stu[i].birthday.year,stu[i].addre);//少了电话……
return 0;
}
仔细调试啊,少年……
自己调试才能提高自己的编程能力。
为你解决疑问其实就是调试的过程,也是我学习的过程。
傍晚好,少年!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询