编写C语言程序"输入十个城市的名字,分别按照城市的字母和长度进行排序.使用2个子函数"
要求为“输入十个城市名字,按客户要求选择按照字母或长度进行排序,使用两个子函数” 展开
写了个,反正运行有结果,你自己修改修改吧
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_BUF 255
struct city
{
char name[MAX_BUF];
int length;
};
typedef struct city CITY;
void ShowCity(CITY *pcity,int n);
void CityLength(CITY *pcity,int n);
int main()
{
int i;
CITY city[10];
for(i=0;i<10;i++)
{
printf("please %i input then city names:\n",i+1);
gets(&city[i].name);
city[i].length=strlen(city[i].name);
}
ShowCity(city,10);
CityLength(city,10);
ShowCity(city,10);
system("pause");
return 0;
}
void ShowCity(CITY *pcity,int n)
{
int i;
for (i=0;i<n;i++)
{
printf("the %i city name is %s\n",i+1,(pcity+i)->name);
}
return;
}
void CityLength(CITY *pcity,int n)
{
int i=0;
int j=0;
CITY temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
if((pcity+j)->length<(pcity+j+1)->length)
{
strncpy(&temp.name,(pcity+j)->name,MAX_BUF);
temp.length=(pcity+j)->length;
(pcity+j)->length=(pcity+j+1)->length;
strncpy((pcity+j)->name,(pcity+j+1)->name,MAX_BUF);
(pcity+j+1)->length=temp.length;
strncpy((pcity+j+1)->name,&temp.name,MAX_BUF);
}
}
return;
}