很令我痛苦的事情,求各路大神帮忙,哪里有错
// g.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include"stdio.h"
#include"stdlib.h"
#include"string"
#include"iostream"
using namespace std;
struct location
{
int longitude;
int latitude;
};
struct census
{
string name;
long population;
location position;
};
census * create();
census * sort1(census *); // sort on city names;
census * sort2(census *); // sort on population;
census * sort3(census *); // sort on longtitude;
census * sort4(census *); // sort on latitude;
void display(census *, int);
int _tmain(int argc, _TCHAR* argv[])
{
census * k = create();
display(sort1(k), 1);
display(sort2(k), 2);
display(sort3(k), 3);
display(sort4(k), 4);
system("Pause");
return 0;
}
census * create()
{
static census p[5];
string name[5] = {"Beijing","Guangzhou","Shanghai","Xi'an","Hefei"};
long po[5] = {32000000,24000000,35000000,18400000,15200000};
int lo[5] = {54,32,43,60,38};
int la[5] = {210,180,138,260,208};
for (int i = 0;i < 5;i++)
{
a[i].name = name[i];
a[i].population = po[i];
a[i].position.latitude = la[i];
a[i].position.longitude = lo[i];
}
return a;
}
census * sort1(census * a)
{
static census p[5];
int i,j;
census m;
for (i = 0;i < 5;i++)
p[i] = a[i];
for (i = 0;i < 5;i++)
for (j = i + 1;j < 5;j++)
if (p[j].name < p[j - 1].name)
{
m = p[j];
p[j] = p[j - 1];
p[j - 1] = m;
}
return p;
}
census * sort2(census * b)
{
static census p[5];
int i,j;
census m;
for (i = 0;i < 5;i++)
p[i] = b[i];
for (i = 0;i < 5;i++)
for (j = i + 1;j < 5;j++)
if (p[j].population < p[j - 1].population)
{
m = p[j];
p[j] = p[j - 1];
p[j - 1] = m;
}
return p;
}
census * sort3(census * c)
{
static census p[5];
int i,j;
census m;
for (i = 0;i < 5;i++)
p[i] = c[i];
for (i = 0;i < 5;i++)
for (j = i + 1;j < 5;j++)
if (p[j].position.latitude < p[j - 1].position.latitude)
{
m = p[j];
p[j] = p[j - 1];
p[j - 1] = m;
}
return p;
}
census * sort4(census * d)
{
static census p[5];
int i,j;
census m;
for (i = 0;i < 5;i++)
p[i] = d[i];
for (i = 0;i < 5;i++)
for (j = i + 1;j < 5;j++)
if (p[j].position.longitude < p[j - 1].position.longitude)
{
m = p[j];
p[j] = p[j - 1];
p[j - 1] = m;
}
return p;
}
void display(census * p, int n)
{
int i;
switch (n)
{
case 1:
printf("Sort the list alphabetically:\n");
for (i = 0;i < 5;i++)
cout<<p[i].name<<"\n";
break;
case 2:
printf("Sort the list based on population:\n");
for (i = 0;i < 5;i++)
cout<<p[i].population<<"\n";
break;
case 3:
printf("Sort the list based on northwards: ");
for (i = 0;i < 5;i++)
cout<<p[i].position.latitude<<"\n";
break;
default:
printf("Sort the list based on westwards: ");
for (i = 0;i < 5;i++)
cout<<p[i].position.longitude<<"\n";
}
}