写代码最蛋疼的事,比如这个!
c语言吧
全部回复
仅看楼主
level 6
老三挺2 楼主
题目:链表存放城市的坐标,名称,
问题1;输入名称求坐标
问题2;输入坐标和距离A,输出距离该坐标小于A的城市
代码下:
2012年10月16日 14点10分 1
level 6
老三挺2 楼主

# include<stdio.h>
# include<malloc.h>
# include<math.h>
# include<string.h>
# define n 20
# define length sizeof(struct city)
struct city
{
float x;
float y;
char name[n];
struct city *next;
};
void Fcity(float i,float j,float l,struct city *head)
{
float distance(float x,float y,float a,float b);
void caseif(float x,float y,struct city *head);
double d;
struct city *p;
int k;
p=head;
for(p=head;p!=NULL;)
{
d=distance(i,j,p->x,p->y);
if(d<=l&&d!=0)
caseif(p->x,p->y,head);
k++;
p=p->next;
}
if(k==0)
printf("没有"); }
float distance(float x,float y,float a,float b)
{
float i,j;
double c;
i=x-a;
j=y-b;
c=sqrt(j*j+i*i);
return c;
}
void put(char a[],struct city *head)
{
struct city *p;
char b[n];
int m;
for(m=0;m<n;m++)
b[m]=a[m];
p=head;
for(p=head;p!=NULL;p=p->next)
{
if(strcmp(b,p->name)==0)
printf("%f,%f",p->x,p->y);
}
if(p==NULL)
printf("error\n");
}
void caseif(float x,float y,struct city *head)
{
struct city *p;
p=head;
for(p=head;p!=NULL;)
{
if(x==p->x&&y==p->y)
printf("%s ",p->name);
p=p->next;
}
}
void main()
{
void Fcity(float i,float j,float l,struct city *head);
void put(char a[n],struct city *head);
char str1[n]="xi'an",str2[n]="shanghai";
char str3[n]="beijing",str4[n]="yulin";
char f[n];
float i,j,l;
int k;
struct city a,b,c,d,*p1,*p2,*head;
strcpy(a.name,str1); a.x=0; a.y=0;
strcpy(b.name,str2); b.x=10; b.y=10;
strcpy(c.name,str3); c.x=3; c.y=4;
strcpy(d.name,str4); d.x=-3; d.y=-4;
head=&a;
a.next=&b;
b.next=&c;
c.next=&d;
d.next=NULL;
printf("1、查询城市坐标\n2、查询距离小于要求的城市\n");
printf("enter operation:");
scanf("%d",&k);
if(k==1)
{
printf("输入城市名:");
for(k=0;k<n;k++)
scanf("%c",&f[k]);
put(f,head);
}
else if(k==2)
{
printf("输入坐标,距离:");
scanf("%d,%d,%d",&i,&j,&l);
Fcity(i,j,l,head);
}
else printf("error");
}

2012年10月16日 14点10分 2
level 6
老三挺2 楼主
蛋疼的事是,做了若干时间,本来第二个问题还可以求。为了完善一下,你吗输不出正确结果了………………!!!
2012年10月16日 14点10分 3
level 6
老三挺2 楼主
求大神帮忙看哪出错了?
put 为求1问题!
distance为求两坐标距离
Fcity为求第二个问题
caseif为求坐标对应的城市
2012年10月16日 14点10分 4
level 12
这时候你就需要一款版本控制软件了
2012年10月16日 14点10分 5
作用是干什么?电脑马上没电了!
2012年10月16日 14点10分
回复 老三挺2 :百度了一下,比较高级,没弄清楚是怎么回事
2012年10月16日 14点10分
level 6
老三挺2 楼主
麻烦诸位耐心看一看,谢谢了!
现在持续蛋疼中…………
2012年10月16日 14点10分 6
太长不看
2012年10月16日 14点10分
回复 Laconism :那就看看那个put函数吧!这个最需解决!谢谢了,随时都可以
2012年10月16日 14点10分
level 6
老三挺2 楼主
说是话,我更想知道自己哪错了!所以……
2012年10月16日 14点10分 7
level 9
fcity调用distance的时候貌似没保存返回值?导致distance白算。
2012年10月16日 23点10分 8
谢谢了!其实这个有的
2012年10月16日 23点10分
level 6
老三挺2 楼主
已解决,舒坦!
2012年10月16日 23点10分 9
level 10
最蛋疼的事是没有注释
2012年10月17日 00点10分 10
引用3楼: 蛋疼的事是,做了若干时间,本来第二个问题还可以求。为了完善一下,你吗输不出正确结果了………………!!!
2012年10月17日 00点10分
level 6
老三挺2 楼主
总结下错误,希望对大家有所帮助。
1、在gets()函数前若有其他输入函数,将导致gets()直接得到上一操作的"\0";
于是在执行中,相当于跳过了gets()。如果在gets()之前加上一个getchar()来吸 收'\0'即可。
2012年10月17日 00点10分 11
level 6
老三挺2 楼主
2、在调用Fcity时,输入的变量为int型;(scanf(%d));
导致函数执行错误;
2012年10月17日 00点10分 12
level 6
老三挺2 楼主
综上,C语言在很多细节方面都必须注意的,应仔细斟酌。另外,有时间应该学习下汇编原理还是好啊!
2012年10月17日 00点10分 13
1