编写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;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-11
表意不清,况且你不是为了学习,是为了任务的,为你的任务花那么大精力不值。追问

= =没办法 不会做 下午就要交了

本回答被网友采纳
第2个回答  2013-05-09
你是华侨大学的么?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网