📄 city.c
字号:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#define MAX 1000#define BUFF 5typedef struct point_struct { int x; int y; }point;typedef struct city_struct{ point location; char *name; struct city_struct *next; }city, *linkcity;void getpoint(linkcity L){ int flag1=1; int flag2; char yn[BUFF]; char name[MAX]; linkcity headptr; /* moving head point */ headptr = (linkcity)malloc(sizeof(city)); headptr->next=L->next; /* get the head point */ while (flag1){ /* get the city's location */ flag2=0; printf("want to get the city's location please input its name :"); scanf("%s",name); while (headptr->next!=NULL){ if (strcmp(headptr->next->name,name)==0){ printf("your city is at (%d,%d) :)\n", headptr->next->location.x, headptr->next->location.y); flag2=1; } headptr->next=headptr->next->next; } if (flag2==0) printf("Sorry cannot find it, because you city isn't in the list.\n"); printf("Do you want to contitue [yes/no]:"); scanf("%s",yn); if(strcmp(yn,"yes")==0) headptr->next=L->next; else flag1=0; }}linkcity Creatfromhead(){ char name[MAX]; city *newcity; int n=0; int flag=1; linkcity L; /* head point*/ L = (linkcity)malloc(sizeof(city)); L->next=NULL; while(flag){ /* add a new note to the list with head point */ printf("Input the city's name :"); scanf("%s",name); if (strcmp(name,"out")==0) flag=0; else { newcity = (city *) malloc( sizeof(city)+strlen(name)+1); newcity->name = (char *)newcity + sizeof(city); strcpy(newcity->name,name); printf("city's x : "); scanf("%d",&newcity->location.x); printf("city's y : "); scanf("%d",&newcity->location.y); printf("%s at (%d,%d)\n",newcity->name,newcity->location.x,newcity->location.y); n++; newcity->next=L->next; L->next=newcity; } } printf("you have inputed %d city \n",n); return L;}void findlesscity(linkcity L, point P, double D){ /*find city that it's distance with P is less than S.*/ linkcity headptr; /* moving head point */ headptr = (linkcity)malloc(sizeof(city)); headptr->next=L->next; while (headptr->next != NULL){ if(sqrt(1.0*(pow(headptr->next->location.x-P.x , 2 )+pow(headptr->next->location.y-P.y , 2 ))) <= D ) printf("%s\n",headptr->next->name); headptr->next=headptr->next->next; }}int main (){ linkcity L; point P; double D; L=Creatfromhead(); getpoint( L); printf("city's x : "); scanf("%d",&P.x); printf("city's y : "); scanf("%d",&P.y); printf("input S : "); scanf("%lf",&D); findlesscity(L,P,D); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -