按字母顺序打印存储在字符数组中的信息
Print information stored in arrays of characters in alphabetic order
在一个文件中,我将不同人的姓名和地址存储在一个数组中。此数组的类型为 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])
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 按字符值访问int数组
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 移动二维数组中的字符
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- C++ 带结构的数组字符
- 数组字符包含量超出预期
- 读取文件并添加到数组字符
- 如何在Qt中更改或替换数组字符
- 为什么编译器不在参数中传递数组字符 *arr[] 的大小?
- 井字棋数组字符错误
- 分割数组字符