c++ - 如何对同一数组索引下的结构成员进行排序?
问题描述
我正在开发一个 C++ 程序,但我发现用于对从文本文件传输的数组结构成员进行排序的函数没有执行并最终显示未排序的结构成员。
这个程序是为我大学课程的学期项目设计的,在那里我制作了一个基于 C++ 的基本拼车程序。程序必须读取包含驱动程序信息的文本文件并将其传输到数组结构中,然后它将开始从最低价格到最高价格排序并显示排序后的结构成员。我对一本 C++ 教科书进行了一些研究,甚至在几个论坛上找到了类似的问题,但我一直得到与最初的文本文件相同的结果。
这里是文本文件的内容供参考。
Annie Aliston
0174987723
Range Rover Evoque
60
6.00
Riley Winston
0174965739
Ford Everest
70
2.50
这是我的编码
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
using namespace std;
struct ProSort
{
char nameProvider[10][40]; //40 character limit for nameProvider
char numPhoneProvider[10][11]; //11 character limit for numPhoneProvider
char nameVehicle[10][40]; //40 character limit for nameVehicle
double KMh[10];
double price[10];
};
ProSort sortingS[7]; //7 set of structs, but I'll put one of the said struct in the sorting function as an example below.
void sortS(ProSort, int);
void share_ride_sort_input(ProSort sortingS[], fstream& File)
{
File.open("sList/s4-Wheels.txt", ios::in);
{
if (File.is_open())
{
int a = 0;
while (!File.eof())
{
File >> ws;
File.getline(sortingS[0].nameProvider[a], 40);
File >> ws;
File.getline(sortingS[0].numPhoneProvider[a], 11);
File >> ws;
File.getline(sortingS[0].nameVehicle[a], 40);
File >> sortingS[0].KMh[a];
File >> sortingS[0].price[a];
//Contents of the text file will be assigned to the struct members above
a++; //Array index number will increase until the end of the text file
}
}
}
File.close();
}
void sortS(ProSort sortingS, int SIZE) //The sorting function for said issue above
{
int index;
int smallestIndex;
int location;
char temp[100];
double temp2;
for (index = 0; index < SIZE - 1; index++)
{
smallestIndex = index;
for (location = index + 1; location < SIZE; location++)
{
if (sortingS.price[index] > sortingS.price[smallestIndex])
{
smallestIndex = location;
strcpy(temp, sortingS.nameProvider[smallestIndex]);
strcpy(sortingS.nameProvider[smallestIndex], sortingS.nameProvider[index]);
strcpy(sortingS.nameProvider[index], temp);
strcpy(temp, sortingS.numPhoneProvider[smallestIndex]);
strcpy(sortingS.numPhoneProvider[smallestIndex], sortingS.numPhoneProvider[index]);
strcpy(sortingS.numPhoneProvider[index], temp);
strcpy(temp, sortingS.nameVehicle[smallestIndex]);
strcpy(sortingS.nameVehicle[smallestIndex], sortingS.nameVehicle[index]);
strcpy(sortingS.nameVehicle[index], temp);
temp2=sortingS.KMh[smallestIndex];
sortingS.KMh[smallestIndex]=sortingS.KMh[index];
sortingS.KMh[index]=temp2;
temp2=sortingS.price[smallestIndex];
sortingS.price[smallestIndex]=sortingS.price[index];
sortingS.price[index]=temp2;
// Basically all of the arrayed struct members with the same array index will move together as one whole set of driver info until every set of struct members is sorted
}
}
}
}
void share_ride_output(ProSort sortingS[], fstream& File) //Function for displaying the sorted struct members by writing to a text file.
{
File.open("sList/s4-Wheels-sorted.txt", ios::out);
{
if (File.is_open())
{
for(int i=0; i<2; i++)
{
File<<sortingS[0].nameProvider[i]<<endl;
File<<sortingS[0].numPhoneProvider[i]<<endl;
File<<sortingS[0].nameVehicle[i]<<endl;
File<<sortingS[0].KMh[i]<<" km/h"<<endl;
File<<"£"<<sortingS[0].charge[i]<<endl;
File<<"\n";
} //This is for writing 2 sets of struct members that was assigned in the share_ride_sort_input function to another text file.
}
}
File.close();
}
int main()
{
fstream File;
const int SIZE = 7;
share_ride_sort_input(sortingS, File);
for(int i=0; i<7; i++) //Originally this was meant for 7 car classes, but only the struct members from the s4-wheels.txt file will be put as an example
{
sortS(sortingS[i], SIZE);
}
share_ride_output(sortingS, File); //Sorted struct members will be written to a text file.
return 0;
}
我希望文本文件的输出为:
Riley Winston
0174965739
Ford Everest
70
2.50
Annie Aliston
0174987723
Range Rover Evoque
60
6.00
但相反,我得到的输出是这样的:
Annie Aliston
0174987723
Range Rover Evoque
60
6.00
Riley Winston
0174965739
Ford Everest
70
2.50
不显示错误消息,因为程序在没有来自编译器的任何警告的情况下运行。我会假设我在排序公式中做错了,但我似乎也无法获得其他解决方案。
解决方案
您的代码的主要问题是它实际上不是 C++。它主要是C,更难处理。
正如有人在评论中指出的第二个问题,您颠倒了任务提示。相反,您在结构内部创建了一个数组,而不是结构数组,这在这种情况下使事情变得更加困难。
编写 C++ 代码时,不要使用 C 功能,例如:char[]
用于字符串(使用std::string
)、C 数组SomeType variable[number]
(使用std::vector
或std::array
)。
从类似的东西和使用开始std::sort
,它会变得很容易:
struct Ride {
std::string dirver;
std::string phone;
std::string vehicle;
double distance;
double price;
};
std::istream& loadRide(std::istream& input, Ride& ride)
{
input >> std::ws; // consume white spaces in front
std::getline(input, ride.dirver);
std::getline(input, ride.phone);
std::getline(input, ride.vehicle);
return input >> ride.distance >> price;
}
std::istream& loadRides(std::istream& input, std::vector<Ride>& rides)
{
rides.clear();
Ride ride;
while(loadRide(input, ride)) {
rides.push_back(ride);
}
}
std::vector<Ride> loadRidesFromFile(const std::string& fileName)
{
std::ifstream f{ fileName };
std::vector<Ride> rides;
loadRides(f, rides);
return rides;
}
推荐阅读
- r - 用于多步前进和滞后回归器的模型时间包 -R
- android - ReactNative 未列出通过 Android 中的 USB 串行端口连接的设备
- r - 使用 group_by 在分组数据帧上应用 SummarizeGrowth
- javascript - 如何在 JavaScript 中抓取 URL 而不会损害嵌入式对象和内联 URL
- javascript - 如何从 Google Map JavaScript 中删除多边形的单边
- android - Android MediaCodec 实时 h264 编码/解码延迟
- postgresql - 如何在 Python Flask 中使用数据库模型?
- r - 基于现有变量创建多个新变量并同时重新编码它们 R
- python - 使用 python 更新 mongoDB
- rust - 返回 PhysicalDevice (Vulkano) 时“无法返回引用局部变量的值”