这是在爱问上对一位朋友问题的解答,代码编的有点仓促,格式没有调的太好。
<script type="text/javascript" src="http://ads.adbrite.com/mb/text_group.php?sid=170596&col=3&br=1"></script>
<script type="text/javascript" src="http://ads.adbrite.com/mb/text_group.php?sid=170596&col=3&br=1"></script>
#include
<
stdio.h
>
#include < string .h >
#include < alloc.h >
typedef struct _tagCity
{
char m_Name[20];
int m_Number;
struct _tagCity * m_pNext;
} City;
City * head;
City * Search ( char * name )
{
City *p = head;
while ( p->m_pNext!= NULL ) {
if(strcmp(p->m_pNext->m_Name, name) == 0){
return p;
}
p=p->m_pNext;
}
return NULL;
}
void Init(City * p)
{
char name[20];
printf("Please input the city's name:");
scanf("%s",name);
while( Search(name) != NULL ) {
printf("The city has been in the list ");
printf("Please reinput a name:");
scanf("%s",name);
}
strcpy(p->m_Name, name);
printf("Please input the city's population:");
scanf("%d",&p->m_Number);
p->m_pNext = head->m_pNext;
return;
}
void Add ()
{
City *newcity = NULL;
newcity = ( City * ) malloc ( sizeof ( City ) );
if ( NULL == newcity ) {
printf("There is not enough memory!! ");
return ;
}
Init ( newcity );
head->m_pNext = newcity;
printf("The new city has been added. ");
return;
}
void Del ()
{
char name[20];
City * p=NULL, *q;
printf("Please input the name of the city which you want to delete:");
scanf("%s",name);
printf("Now searching... ");
p = Search ( name );
if ( NULL == p ) {
printf ( "The city "%s" has not been in the link. " ,name);
return;
}
q=p->m_pNext;
p->m_pNext = q->m_pNext;
q->m_pNext = NULL;
free (q);
printf("The city "%s" has been deleted.",name);
}
void Sum ()
{
City * p=NULL;
int i = 0;
long sum=0;
p=head->m_pNext;
while(p!=NULL){
sum += (long) p->m_Number;
p=p->m_pNext;
++i;
}
printf("There are total %d city(ies). ",i);
printf("The total population is %ld ", sum);
return ;
}
void Search_City()
{
char name[20];
City *p =NULL;
printf("Please input the name you want to search:");
scanf("%s",name);
printf("Now searching... ");
p = Search ( name );
if ( NULL == p ) {
printf("The city has not been in the list. ");
return;
}
p = p->m_pNext;
printf("The detail of the city "%s": ",p->m_Name);
printf("Name: %s ", p->m_Name );
printf("Population: %d ", p->m_Number);
return;
}
void menu()
{
printf(" MENU " );
printf("1. The total population ");
printf("2. Search a city ");
printf("3. Add a new city ");
printf("4. Delete a exist city ");
printf("5. The menu ");
printf("6. Exit ");
printf(" Pleas input a number of the thing you want.");
printf(" %");
}
int main()
{
int i,f=1;
char input[10];
head = ( City * ) malloc ( sizeof( City ) );
if ( NULL == head ){
printf("Not enough memory!! ");
return 1;
}
head->m_pNext = NULL;
menu();
while( f != 0 ) {
i = atoi ( fgets(input, 3, stdin) );
switch (i) {
case 1 : Sum(); break;
case 2 : Search_City(); break;
case 3 : Add(); break;
case 4 : Del(); break;
case 5 : menu(); break;
case 6 : f=0; break;
default: f=1;
}
printf("%");
}
return 0;
}
#include < string .h >
#include < alloc.h >
typedef struct _tagCity
{
char m_Name[20];
int m_Number;
struct _tagCity * m_pNext;
} City;
City * head;
City * Search ( char * name )
{
City *p = head;
while ( p->m_pNext!= NULL ) {
if(strcmp(p->m_pNext->m_Name, name) == 0){
return p;
}
p=p->m_pNext;
}
return NULL;
}
void Init(City * p)
{
char name[20];
printf("Please input the city's name:");
scanf("%s",name);
while( Search(name) != NULL ) {
printf("The city has been in the list ");
printf("Please reinput a name:");
scanf("%s",name);
}
strcpy(p->m_Name, name);
printf("Please input the city's population:");
scanf("%d",&p->m_Number);
p->m_pNext = head->m_pNext;
return;
}
void Add ()
{
City *newcity = NULL;
newcity = ( City * ) malloc ( sizeof ( City ) );
if ( NULL == newcity ) {
printf("There is not enough memory!! ");
return ;
}
Init ( newcity );
head->m_pNext = newcity;
printf("The new city has been added. ");
return;
}
void Del ()
{
char name[20];
City * p=NULL, *q;
printf("Please input the name of the city which you want to delete:");
scanf("%s",name);
printf("Now searching... ");
p = Search ( name );
if ( NULL == p ) {
printf ( "The city "%s" has not been in the link. " ,name);
return;
}
q=p->m_pNext;
p->m_pNext = q->m_pNext;
q->m_pNext = NULL;
free (q);
printf("The city "%s" has been deleted.",name);
}
void Sum ()
{
City * p=NULL;
int i = 0;
long sum=0;
p=head->m_pNext;
while(p!=NULL){
sum += (long) p->m_Number;
p=p->m_pNext;
++i;
}
printf("There are total %d city(ies). ",i);
printf("The total population is %ld ", sum);
return ;
}
void Search_City()
{
char name[20];
City *p =NULL;
printf("Please input the name you want to search:");
scanf("%s",name);
printf("Now searching... ");
p = Search ( name );
if ( NULL == p ) {
printf("The city has not been in the list. ");
return;
}
p = p->m_pNext;
printf("The detail of the city "%s": ",p->m_Name);
printf("Name: %s ", p->m_Name );
printf("Population: %d ", p->m_Number);
return;
}
void menu()
{
printf(" MENU " );
printf("1. The total population ");
printf("2. Search a city ");
printf("3. Add a new city ");
printf("4. Delete a exist city ");
printf("5. The menu ");
printf("6. Exit ");
printf(" Pleas input a number of the thing you want.");
printf(" %");
}
int main()
{
int i,f=1;
char input[10];
head = ( City * ) malloc ( sizeof( City ) );
if ( NULL == head ){
printf("Not enough memory!! ");
return 1;
}
head->m_pNext = NULL;
menu();
while( f != 0 ) {
i = atoi ( fgets(input, 3, stdin) );
switch (i) {
case 1 : Sum(); break;
case 2 : Search_City(); break;
case 3 : Add(); break;
case 4 : Del(); break;
case 5 : menu(); break;
case 6 : f=0; break;
default: f=1;
}
printf("%");
}
return 0;
}