从低到高组织整数并在C++中使用结构

Organizing integers from low-high and using structures in C++

本文关键字:C++ 结构 高组织 整数      更新时间:2023-10-16

这是给家庭作业的

因此,对于我的项目,我得到了一个文件,我必须将文件的各个部分(汽车年份,品牌,型号,价格和可用性)分成一个结构(我已经完成了)。因此,对于此功能,我必须按价格从低到高来组织汽车,我已经完成了。我将在下面发布我的函数以及我需要的终端输出。


函数如下:

void sort(cars cars2[]){ //Sorts the cars by price 
int x, y, temp;
float price2[5];
for(x = 0; x < 5; x++){
price2[x] = cars2[x].price; //sets up so the prices can be swapped later on
}
for(x = 1; x < 5; ++x){
for(y = 0; y < (5-x); ++y)
if(price2[y] > price2[y+1]){
temp = price2[y];
price2[y] = price2[y+1];
price2[y+1] = temp; //organizes based on price of the car
}
}
for(x = 0; x < 5; x++){
cout << cars2[x].year <<  " " << cars2[x].make << " " << cars2[x].model << ", " << "$" << price2[x] << " per day, ";

if(cars2[x].ava == 1){
cout << "Available: True" << endl;
}
else {
cout << "Available: False" << endl; //Same display as from function 2
}
}
}

这是我的程序的输出

2014 丰田塔科马, $45.25 每天, 可用: 假

2015 福特 Fusion, $71.27 每天, 可用: 真

2009 道奇霓虹灯,每天 90 美元,可用:假

2015 福特F150, $112 每天, 可用: 真

2016 斯巴鲁傲虎,每天 115 美元,可用:真


这是我需要的输出

2009 道奇霓虹灯,每天 45.25 美元,可用:假

2016 斯巴鲁傲虎, $71.27 每天, 可用: 真

2015 福特 Fusion, $90.89 每天, 可用: 真

2015 福特F150, $112.83 每天, 可用: 真

2014 丰田塔科马, $115.12 每天, 可用: 假


因此,价格掉期的工作方式是,它确实从低到高组织每个价格,但它会删除一些十进制数字 ->这不是一个大问题,但是我将如何将其修复到没有被削减的地方?

主要问题是我如何使汽车也随着价格而交换?我知道我的cout被组织到终端打印出文件中任何内容的地方,价格根据需要变化的唯一原因是因为我制作了围绕价格交换的代码块。那么,我该如何使每辆车及其所有信息从高到低进行组织呢?

如果需要任何进一步的信息,请在评论中告诉我,或者如果需要我的整个程序,我将编辑我的帖子。

此外,禁止使用字符串函数。

感谢您的任何帮助!

您的临时变量是一个整数,但您正在交换浮点数。使温度浮动。

如何让价格与汽车互换?很简单,首先不要将价格与汽车分开。摆脱 price2 数组,只需在汽车数组上进行掉期。这意味着你只使用价格来决定交换哪些汽车,但是当你交换时,你交换了汽车的所有字段,品牌,型号,价格等。

在第 3 行中,将temp声明为 int。因此,当您price2[y]投给它时,C++会将价格转换为 int。尝试将temp声明为浮点数以保持小数。

考虑使用 price[] 作为整数(而不是浮点数)。 即值字段以美分为单位。

优点 - 无浮点转换,无舍入误差

简单冲击 -

将报告代码从

for(x = 0; x < 5; x++){
cout << cars2[x].year <<  " " << cars2[x].make << " " 
<< cars2[x].model << ", " << "$" 
<< price2[x] << " per day, ";
}

for(x = 0; x < 5; x++){
cout << cars2[x].year <<  " " << cars2[x].make << " " 
<< cars2[x].model << ", " << "$" 
<< (price2[x] / 100)  << "." 
<< std::setw(2) << std::setfill('0') << ((price2[x] % 100)) 
<< " per day, ";
}

为了可读性和一致性,您应该考虑将此"报告"代码移动到名为"cars"的类/结构的"内部"。(另外,删除价格2[])

for(x = 0; x < 5; x++) { cars2[x].report() };

并将报告方法添加到 CARS 类中

void cars::report() {
cout << year <<  " " << make << " " << model << ", " << "$" 
<< (price / 100)   // dollars
<< "."             // decimal point
<< std::setw(2) << std::setfill('0') 
<< ((price % 100)) // cents
<< " per day, ";
}

我将如何使汽车也与 价格?

您使用它对浮动价格的"重复"数组进行排序:

for(x = 1; x < 5; ++x){
for(y = 0; y < (5-x); ++y)
if(price2[y] > price2[y+1]){
temp = price2[y];
price2[y] = price2[y+1];
price2[y+1] = temp; //organizes based on price of the car
}
}

现在考虑简单地对汽车数组进行排序:

cars  temp;  // instead of int or float, swap the struct
for(x = 1; x < 5; ++x){
for(y = 0; y < (5-x); ++y)
if(cars2[y].price > cars2[y+1].price){
temp = cars2[y].price;
cars2[y].price = cars2[y+1].price;
cars2[y+1].price = temp; //organizes based on price of the car
}
}