永发信息网

麻烦你再帮我看看这个,谢谢。

答案:1  悬赏:40  手机版
解决时间 2021-07-31 21:54
  • 提问者网友:世勋超人
  • 2021-07-30 23:47


#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct Node{
char data1[20];
int data2,data3;
struct Node *next;
}Node,*LinkList;

void Initlinklist(LinkList *l)
{
*l=(LinkList)malloc(sizeof(Node));
(*l)->next=NULL;
}
void CreateFromTail(LinkList L)
{
Node *r, *city;
char a[10][20]={'\0'},*b;
int i=0,flag=1,c,d;
r=L;
while(flag)
{
printf("input the City:\n");
gets(a[i]);
printf("input the site:\n");
scanf("%d%d",&c,&d);
if(strcmp(a[i],"$"))
{
city=(Node*)malloc(sizeof(Node));
strcpy(city->data1,a[i]);
city->data2=c;
city->data3=d;
r->next=city;
r=city;
}
else{
flag=0;
r->next=NULL;
}
if(i>9){//跳出
flag=0;
r->next=NULL;
}
i++;
}
}

int search()
{
LinkList L;
Node *p;
int flag=0;
int i;
char c,a[20];
Initlinklist(&L);
printf("input the element,end by '$'\n");
CreateFromTail(L);
p =L->next;
printf("Which city are you want to search:\n");
gets(a);
while(p!=NULL)
{
if(!strcmp(p->data1,a)){
printf("%s's site is:(%d,%d)",p->data1,p->data2,p->data3);
p=NULL;
break;
}
p=p->next;
}
}

int count()
{
LinkList L;
Node *p,*q;
int flag=0;
int i,e,f,g,h;
char c;
Initlinklist(&L);
printf("input the element,end by '$'\n");
CreateFromTail(L);
p =L->next;
printf("Input the site:\n");
scanf("%d%d",&e,&f);
printf("Input the distance:\n");
scanf("%d",&g);
while(p!=NULL)
{
f=e+f+g;
g=p->data2+p->data3;
if(g<=f){
printf("%s's site is:(%d,%d)\n",p->data1,p->data2,p->data3);
h++;
}
p=p->next;

}
printf("There are %d cities to meet the requirements\n",h);
}

void main()
{
int a;
printf("search or count:\n1.search\t2.count\n");
scanf("%d",&a);
if(a==1)
search();
else if(a==2)
count();
else
printf("error program exit!");
getch();
}

最佳答案
  • 五星知识达人网友:平生事
  • 2021-07-31 00:35

不好意思. 早上有事. 帮你改好了,加粗的地方.你看看.


在CreateFromTail函数中.我改成输入城市名如果是$就退出. 照你原本的写法,如果城市名为$的话,还必须输入坐标才结束. 所以稍微改动了下.




#include<stdio.h>
#include <conio.h>
#include<malloc.h>
#include<string.h>


typedef struct Node{
char data1[20];
int data2,data3;
struct Node *next;
}Node,*LinkList;


void Initlinklist(LinkList *l)
{
*l=(LinkList)malloc(sizeof(Node));
(*l)->next=NULL;
}


void CreateFromTail(LinkList L)
{
Node *r, *city;
char a[10][20]={'\0'},*b;
int i=0,flag=1,c,d;
r=L;
while(flag)
{
getchar(); //吸收上一次输入的回车符.
printf("input the City:\n");
gets(a[i]);
if(strcmp(a[i],"$"))
{
printf("input the site:\n");
scanf("%d%d",&c,&d);
city=(Node*)malloc(sizeof(Node));
strcpy(city->data1,a[i]);
city->data2=c;
city->data3=d;
r->next=city;
r=city;
}
else{
flag=0;
r->next=NULL;
}
if(i>9){//跳出
flag=0;
r->next=NULL;
}
i++;
}
}


//函数并没有返回任何数据. 所以用void.


void search()
{
LinkList L;
Node *p;
int flag=0;
int i;
char c,a[20];
Initlinklist(&L);
printf("input the element,end by '$'\n");
CreateFromTail(L);
p =L->next;
printf("Which city are you want to search:\n");
gets(a);
while(p!=NULL)
{
if(!strcmp(p->data1,a)){
printf("%s's site is:(%d,%d)",p->data1,p->data2,p->data3);
p=NULL;
break;
}
p=p->next;
}
}


//同上.


void count()
{
LinkList L;
Node *p,*q;
int flag=0;
int i,e,f,g,h=0; //h用来计数所以需要要初始化.
char c;
Initlinklist(&L);
printf("input the element,end by '$'\n");
CreateFromTail(L);
p =L->next;
printf("Input the site:\n");
scanf("%d%d",&e,&f);
printf("Input the distance:\n");
scanf("%d",&g);
while(p!=NULL)
{
f=e+f+g;
g=p->data2+p->data3;
if(g<=f){
printf("%s's site is:(%d,%d)\n",p->data1,p->data2,p->data3);
h++;
}
p=p->next;


}
printf("There are %d cities to meet the requirements\n",h);
}


void main()
{
int a;
printf("search or count:\n1.search\t2.count\n");
scanf("%d",&a);
if(a==1)
search();
else if(a==2)
count();
else
printf("error program exit!");
getch();
}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯