关于C语言的结构指针

我写了一个按照生日来排列朋友目录的程序,但貌似是用指针的时候,在VOID RANKING那个函数那里出了点问题,得不到结果,哪位大大帮忙看一下~
#include<stdio.h>
#include<string.h>
struct frineds_list{
char name[20],telephone[13],address[40],birthday[10];
};
void read(struct frineds_list *,int);
void ranking(struct frineds_list *,int);
void swap(char *,char *);
void main()
{
int n;
struct frineds_list friends[50],*p;
p=friends;
printf("how many friends:");
scanf("%d",&n);
read(p,n);
ranking(p,n);
}
void read(struct frineds_list *p,int n)
{
int i;
for(i=0;i<n;i++,p++){
printf("enter friend's name:");
gets(p->name);
getchar();
printf("enter friend's telephone:");
gets(p->telephone);
getchar();
printf("enter friend's address:");
gets(p->address);
getchar();
printf("enter friend's birthday:");
gets(p->birthday);
getchar();
}
}
void ranking(struct frineds_list *p,int n)
{
int i,j;
for(i=1;i<n;i++,p++)
for(j=0;j<n-i;j++,p++)
if(strcmp(p->birthday,(p+1)->birthday)){
swap(p->name,(p+1)->name);
swap(p->telephone,(p+1)->telephone);
swap(p->address,(p+1)->address);
}
for(i=0;i<n;i++,p++)
printf("friend's information:%s %s %s %s\n",p->name,p->birthday,p->telephone,p->address);
}
void swap(char *px,char *py)
{
char t;
t=*px;
*px=*py;
*py=t;
}
感谢2楼的答案,再请教一下,这个生日排序问题是像这样用字符串来排列好还是用将日期转化为整型来排列好?

你用swap子函数是希望如果前一个人的生日比后一个的大,就把两个人的信息位置换一下吧,但是你写的这个子函数有问题,你只是把姓名等数组的第一个字符对换了,并没有把整个数组对换,

我觉得用字符数组存放比较方便 整型的确知范围太小 你有想定义也只能定义成unsigned long型,而且在输出的时候没法在年月日之间输出空格,会不好看,用字符数组输入时虽然不能输出空格,但是你可以将空格换成“-”,这样输出很方便也很好看,在生日比较的时候用strcmp和用整型的比较一样方便的,在比较生日的时候你用的是起泡法吧,你可以试试选择排序法
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-05-18
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct frineds_list{
char name[20],telephone[13],address[40],birthday[10];
};
void read(struct frineds_list *,int);
void ranking(struct frineds_list *,int);
void swap(struct frineds_list *px,struct frineds_list *py);
void main()
{
int n;
struct frineds_list friends[50],*p;
p=friends;
printf("how many friends:");
scanf("%d",&n);
read(p,n);
ranking(p,n);
}
void read(struct frineds_list *p,int n)
{
int i;
for(i=0;i<n;i++){
printf("enter friend's name:");
scanf("%s",(p+i)->name);
//getchar();
printf("enter friend's telephone:");
scanf("%s",(p+i)->telephone);
//getchar();
printf("enter friend's address:");
scanf("%s",(p+i)->address);
//getchar();
printf("enter friend's birthday:");
scanf("%s",(p+i)->birthday);
//getchar();
}
}
void ranking(struct frineds_list *p,int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
if(strcmp((p+j)->birthday,(p+j+1)->birthday)>0){//比较字符串是看返回值 >0 ==0 <0
swap(p+j,p+j+1);
}
for(i=0;i<n;i++)
printf("friend's information:%s %s %s %s\n",(p+i)->name,(p+i)->birthday,(p+i)->telephone,(p+i)->address);
}
void swap(struct frineds_list *px,struct frineds_list *py) //交换函数是这样的
{
struct frineds_list t;
memcpy(&t,px,sizeof(struct frineds_list));
memcpy(px,py,sizeof(struct frineds_list));
memcpy(py,&t,sizeof(struct frineds_list));
}本回答被提问者采纳
第2个回答  2009-05-19
#include <stdio.h>
#include <string.h>
#include <memory.h>

struct frineds_list{
char name[20],telephone[13],address[40],birthday[10];
};
void read(struct frineds_list *,int);
void ranking(struct frineds_list *,int);
void swap(frineds_list *,frineds_list *);
void main()
{
int n;
struct frineds_list friends[50],*p;
p=friends;
printf("how many friends:");
scanf("%d",&n);
read(p,n);
ranking(p,n);
}
void read(struct frineds_list *p,int n)
{
int i;
for(i=0;i<n;i++,p++){
printf("enter friend's name:");
scanf("%s", p->name);
printf("enter friend's telephone:");
scanf("%s", p->telephone);
printf("enter friend's address:");
scanf("%s", p->address);
printf("enter friend's birthday:");
scanf("%s", p->birthday);
}
}
void ranking(struct frineds_list *p,int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
t = i;

for(j=i;j<n;j++)
{
if(strcmp(p[t].birthday,p[j].birthday))
t = j;
}

if (t != i)
swap(p + t, p + i);

}

for(i=0;i<n;i++,p++)
printf("friend's information:%s %s %s %s\n",p->name,p->birthday,p->telephone,p->address);
}
void swap(struct frineds_list *f1, struct frineds_list *f2)
{
struct frineds_list f;
int s = sizeof(struct frineds_list);
memcpy(&f, f1, s);
memcpy(f1, f2, s);
memcpy(f2, &f, s);
}
第3个回答  2009-05-19
#include<stdio.h>
#include<string.h>
struct frineds_list{
char name[20],telephone[13],address[40],birthday[10];
};
void read(struct frineds_list *,int);
void ranking(struct frineds_list *,int);
void swap(char *,char *);
void main()
{
int n;
struct frineds_list friends[50],*p;
p=friends;
printf("how many friends:");
scanf("%d",&n);
read(p,n);
ranking(p,n);
}
void read(struct frineds_list *p,int n)
{
int i;
for(i=0;i<n;i++,p++){
printf("enter friend's name:");
gets(p->name);
getchar();
printf("enter friend's telephone:");
gets(p->telephone);
getchar();
printf("enter friend's address:");
gets(p->address);
getchar();
printf("enter friend's birthday:");
gets(p->birthday);
getchar();
}
}
void ranking(struct frineds_list *p,int n)
{
int i,j;
for(i=0;i<=n;i++)
for(j=0;j<=n-i;j++)
if(strcmp((p+j)->birthday,(p+j+1)->birthday)){
swap((p+j)->name,(p+j+1)->name);
swap((p+j)->telephone,(p+j+1)->telephone);
swap((p+j)->address,(p+j+1)->address);
}
for(i=0;i<n;i++,p++)
printf("friend's information:%s %s %s %s\n",p->name,p->birthday,p->telephone,p->address);
}
void swap(char *px,char *py)
{
char t;
t=*px;
*px=*py;
*py=t;
}
相似回答