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

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

这是作业

所以对于我的项目,我得到了一个文件,我必须将文件的各个部分(汽车年份、制造商、型号、价格和可用性)分成一个结构(我已经这样做了)。所以对于这个功能,我必须按照我已经完成的价格从低到高组织汽车。我将在下面 post 我的函数以及我需要的终端输出。


函数如下:

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 Toyota Tacoma, .25 per day, Available: False

2015 Ford Fusion, .27 per day, Available: True

2009 Dodge Neon, per day, Available: False

2015 Ford F150, 2 per day, Available: True

2016 Subaru Outback, 5 per day, Available: True


这是我需要的输出

2009 Dodge Neon, .25 per day, Available: False

2016 Subaru Outback, .27 per day, Available: True

2015 Ford Fusion, .89 per day, Available: True

2015 Ford F150, 2.83 per day, Available: True

2014 Toyota Tacoma, 5.12 per day, Available: False


所以价格互换的工作原理是,它确实将每个价格从低到高组织起来,但它删除了一些小数 -> 这并不是一个大问题,但我如何将其修复到没有任何内容被删除的地方?

主要问题是我如何才能让汽车也随着价格交换?我知道我的 cout 被组织到终端打印出文件中任何内容的位置,价格根据需要变化的唯一原因是因为我制作了一段代码来交换价格。那么我如何才能让每辆车连同它的所有信息都按照从高到低的价格排列呢?

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

也禁止使用字符串函数。

感谢您的帮助!

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

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

在第 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, ";
}

为了可读性和一致性,您应该考虑移动 class / 名为 "cars" 的结构的 'reporting' 代码 'inside'。 (另外,删除 price2[])

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

并将报告方法添加到汽车中class

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

how would I make it so the cars would also swap around along with the prices?

您使用它对 'duplicate' 浮动价格数组进行排序:

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
   }
}