|
发表于 2020-6-5 18:38:03
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define ERROR 1
#define LONG 1024
#define WILD 798
#define K 30
typedef struct point
{
int x,y;
struct point *next;
}point;
int n=K;
void print(struct point *b)
{
struct point *a;
a=b;
while(a->next!=NULL)
{
printf("%d,%d\n",a->x,a->y);
a=a->next;
}
printf("\n有%d个坐标\n",n);
}
int distance(struct point *m)
{
int i;
i=sqrt((m->x-512)*(m->x-512)+(m->y-399)*(m->y-399));
return i;
}
struct point *make()
{
n=0;
srand((unsigned int)time(NULL));
struct point *head,*p;
head=p=(struct point *)malloc(sizeof(struct point));
head->x=rand()%LONG+0;
head->y=rand()%WILD+0;
head->next=NULL;
p=head;
for(int i=0;i<K;i++)
{
p=head;
head=(struct point *)malloc(sizeof(struct point));
head->x=rand()%LONG+0;
head->y=rand()%WILD+0;
head->next=p;
n++;
}
return (head);
}
struct point *delete(struct point *m)
{
int x;
struct point *p,*p1;
p1=p=m;
while(p1->next!=NULL)
{
x=distance(p1);
p=p1->next;
if(x<500)
{
p1->next=p->next;
n--;
}
p1=p;
}
return m;
}
struct point *insert(struct point *m)
{
int j,k,r,s,z=0;
struct point *a,*b,*c;
a=b=m;
printf("插入坐标:\t");
scanf("%d,%d",&j,&k);
printf("放在哪个坐标前面: \n");
scanf("%d,%d",&r,&s);
c=(struct point *)malloc(sizeof(struct point));
c->x=j;
c->y=k;
while(b->next!=NULL)
{
a=b;
b=b->next;
if(r==b->x&&s==b->y)
{
a->next=c;
c->next=b;
n++;
z=1;
break;
}
}
if(z==0)
{
printf("errror!!\n");
}
if(z==1)
print(m);
return m;
}
void queue(struct point *m)
{
struct point *a,*b,*temp;
a=b=m;
int c,d;
c=distance(a);
d=distance(b);
printf("这里写个循环,让两个数换位置");
while(b->next!=NULL)
{
b=a->next;
if(c>d)
{
printf("按大小交换位置你应该是会的吧“);
}
b=a;
}
}
void clean(struct point *b)
{
while(b)
{
b=b->next;
free(b);
}
b=NULL;
}
int main()
{
int a;
struct point *p;
p=make();
print(p);
while(1)
{
printf ("你需要输入的操作:\n1-重新创建链表\n2-删除距离(512,399)500以内的对坐标\n3-插入数据\n4-排序\n");
scanf("%d",&a);
if(0<a&&5>a)
switch(a)
{
case 1:clean(p);p=make();print(p);break;
case 2: p=delete(p);print(p);break;
case 3: p=insert(p);break;
case 4: queue(p);print(p);break;
}
else
{
printf("the operation is wrong!");
}
}
return 0;
}
准备要吃饭了,排序的函数你就自己写吧,你要的功能都有实现,插入功能是按照插入到某个坐标前来设计的,要是要按个数的话,你稍微自己改一改,大概的写完了,没有那么的简洁 |
|