按字母顺序打印存储在字符数组中的信息

Print information stored in arrays of characters in alphabetic order

本文关键字:数组 字符 信息 存储 顺序 打印      更新时间:2023-10-16

在一个文件中,我将不同人的姓名和地址存储在一个数组中。此数组的类型为 Person(结构(,由两个字符数组组成,一个用于姓名和姓氏,一个用于地址。我已经设法在屏幕上显示人物,但现在我必须按照姓氏的字母顺序排列他们。

在下面显示的代码中,我首先找到了姓氏的第一个元素的索引,并将它们保存在一个数组中。然后我创建了另一个数组,其中存储了姓氏的第一个字符。之后,我做了一个 for 循环,并将数组上的第一个字母与 if 语句进行了比较,并对气泡进行了排序。不幸的是,在我打印了人们的名字/姓氏和地址后,它们没有正确排序。

#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<math.h>
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<math.h>
#include<time.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct Person {
char name[30];
char address[50];
};
typedef struct Person Person;
//to find in only for one person
//only for one individual
int getSurnameIndex(Person people )
{
int count=0; //to keep track in what character we are
int len=strlen(people.name);
int surnameIndex=0;
for(int i=0;i<len;i++)
{
if(people.name[i]== ' ')
{
surnameIndex=count+1; //surname comes one character after the space
}
count++;
}
return(surnameIndex);
}
int main()
{
ifstream a1("a1.txt");
Person people[20];
int numPeople=0;
int i=0; //we can use numPeople because is the same thing but only because we are used with for(int i=0;...) will put i on there
while(!a1.eof())
{
a1.getline(people[i].name, 30);
a1.getline(people[i].address, 50);
a1.get();
i++;
numPeople++;
}
cout<<"The number of people in the file is "<<numPeople<<"."<<endl<<endl;
for(int i=0;i<numPeople;i++)
{
cout<<people[i].name<<endl<<people[i].address<<endl<<endl;
}
//now how to print the letter of surname

int surnameIndex=getSurnameIndex(people[0]);
cout<<"The index for the first person is: "<< surnameIndex<<endl;
//now to make if for all the people and store the indexes on an array
int surnameIndexes[20];
cout<<"Here are the indexes of of the people stored in the array:" <<endl;
for(int i=0;i<numPeople;i++)
{
surnameIndexes[i]=getSurnameIndex(people[i]);
cout<<surnameIndexes[i]<<endl;
}
// here we store the first letters of surnames into a char array
char surnameLetter[20];
for(int i=0;i<numPeople;i++)
{
surnameLetter[i]=people[i].name[surnameIndexes[i]];
}
cout<<endl<<endl;
for(int i=0;i<numPeople;i++)
{
cout<<surnameLetter[i]<<endl;
}
//now comparing the letters and bubble bobble sort
//this is not sorting properly
for(int i=1;i<6;i++)
{
for(int j=1;j<6;j++)
{
if(surnameLetter[i-1] > surnameLetter[i])
{
Person temp;
temp=people[i-1];
people[i-1]=people[i];
people[i]=temp;
char tempChar;
tempChar=surnameLetter[i-1];
surnameLetter[i-1]=surnameLetter[i];
surnameLetter[i]=tempChar;
}
}
}

for(int i=0;i<numPeople;i++)
{
cout<<people[i].name<<endl<<people[i].address<<endl<<endl;
}
return 0;
}

错误出在 if 语句的气泡排序上。if 语句应比较该数组的 "j" 元素,而不是 "i"。工作代码是:

for(int i=1;i<6;i++)
{
for(int j=1;j<6;j++)
{
if(surnameLetter[j-1] > surnameLetter[j])
{
Person temp;
temp=people[j-1];
people[j-1]=people[j];
people[j]=temp;
char tempChar;
tempChar=surnameLetter[j-1];
surnameLetter[j-1]=surnameLetter[j];
surnameLetter[j]=tempChar;
}
}
}

排序算法中的两个for循环在应该numPeople时最多最多 6 个。 此外,您可以通过每次迭代减小第二个循环的范围来将气泡排序的计算减少一半:

for(int i = 1; i < numPeople; i++) 
for(int j = 1; j < numPeople-i+1; j++) 
if(people[j-1] > people[j])
swap(people[j-1],people[j])