【问题标题】:C++: Data Types & ArraysC++:数据类型和数组
【发布时间】:2019-05-15 20:53:27
【问题描述】:

我正在编写一个程序,以确定 5 艘命名船的最低运营成本。这是使用存储在数组中的预定整数以及用户输入整数来计算的。 我能够完成程序,但我想命名每艘船并输出最低的运营成本及其对应的船名。任何见解表示赞赏!谢谢并原谅我草率的代码,我是新手。

#include <string>
#include <iostream>
#include <stdio.h> 
using namespace std;


   void swap(int *xp, int *yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 

void bubbleSort(int arr[], int n) 
{ 
   int i, j; 
   for (i = 0; i < n-1; i++)       

       for (j = 0; j < n-i-1; j++)  
           if (arr[j] > arr[j+1]) 
              swap(&arr[j], &arr[j+1]); 
} 

void printArray(int arr[], int size) 
{ 
    int i; 
    for (i=0; i < size; i++) 
        printf("%d ", arr[i]); 
        cout<<"\n";
     }


int main()
{

//Declarations

int numBoats = 5;

int boatData[5];

int costGas;

int yearsService;

int i;

int j;

int boatArray[5][3] = {{30, 100, 5000}, 
                     {20, 200, 10000}, 
                     {30, 200, 2000}, 
                     {25, 150, 12000}, 
                     {30, 50, 8000}};

//User input 

cout << "Please enter the first boats cost of gas: "<<endl;
cin >> costGas;
cout << "The cost of gas you entered is " << costGas << endl;

cout << "Please enter the number of years in service: " << endl;
cin >> yearsService;
cout << "The number of years in service you entered is " << yearsService << endl;

//Calculations

for (i = 0; i < numBoats; i++) {

int mpg = boatArray[i][1];
int maintenanceCost = boatArray[i][2]; 
int purchaseCost = boatArray[i][3];

int totalMiles = 15000 * yearsService;
int gasTotal = totalMiles / mpg;
int maintenanceTotal = maintenanceCost * yearsService;
int operatingCost = purchaseCost + gasTotal + maintenanceTotal;

boatData[i] = operatingCost;
}

//Print operating costs

 cout<<"The operating costs of each boat is: "<<endl;

printArray(boatData, numBoats);

//Bubblesort

bubbleSort(boatData, numBoats); 
printArray(boatData, numBoats);

cout<<"Your lowest operating cost is "<<boatData[0]<<endl;

    return 0;
}

【问题讨论】:

  • 您好 Neo J。欢迎来到 SO。你知道struct吗?
  • 我目前正在第一次进行调查......任何见解将不胜感激。谢谢!
  • 你为什么不试试上课呢?作为 c++,您可以拥有一个带有多个参数(名称、成本、年份等)的船类,并且主要创建 5 个船对象,实例化参数并调用该类的函数!
  • “我想给每艘船命名,并输出最低的运营成本和它对应的船名”——好的,你有什么问题?
  • 既然你是初学者,让我先告诉你,你的程序找到了解决方案,但不是以最好的方式。您应该just 查找最小值,而不是按值对数组进行排序(如果您理解这部分,请告诉我)。但是使用您的方法仍然可以打印船的名称。 1) 创建一个名称数组,2) 创建一个 swapName 函数,您每次调用 swap 时都会调用该函数和 3)cout&lt;&lt;"Your lowest ... 的末尾添加名称的输出。这对你有意义吗??

标签: c++ arrays multidimensional-array data-structures types


【解决方案1】:

这就是我要找到最低运营成本的方法:

#include <string>
#include <iostream>
#include <stdio.h> 
using namespace std;


int NUM_BOATS = 5;

struct Boat {
  int mpg;
  int maintenanceCost;
  int purchaseCost;
  string name;
  int operatingCost; // total of your calculation
};

void caculation(Boat& boat, int yearsService){
  int totalMiles = 15000 * yearsService;
  int gasTotal = totalMiles / boat.mpg;
  int maintenanceTotal = boat.maintenanceCost * yearsService;
  boat.operatingCost = boat.purchaseCost + gasTotal + maintenanceTotal;
}

void print(Boat& boat){
  cout << "mpg: " << boat.mpg << endl;
  cout << "maintenanceCost: " << boat.maintenanceCost << endl;
  cout << "purchaseCost: " << boat.purchaseCost << endl;
  cout << "name: " << boat.name << endl;
  cout << "operatingCost: " << boat.operatingCost << endl;
}

int main()
{
  Boat BoatData[5] = { {30, 100, 5000, "Boat a", 0},
                       {20, 110, 5500, "Boat b", 0},
                       {35, 120, 4000, "Boat c", 0},
                       {10, 300, 1000, "Boat d", 0},
                       {40, 200, 3000, "Boat e", 0},
                      } ;
  //User input 
  int costGas;
  cout << "Please enter the first Boats cost of gas: "<<endl;
  cin >> costGas;
  cout << "The cost of gas you entered is " << costGas << endl;
  int yearsService;
  cout << "Please enter the number of years in service: " << endl;
  cin >> yearsService;
  cout << "The number of years in service you entered is " << yearsService << endl;

  int iMin = 0;
  int minCost = 99000; // a really high number

  for(int i=0; i<NUM_BOATS; ++i){
    print(BoatData[i]);
    caculation(BoatData[i], yearsService);  // calculate all your operatingCost
    if (BoatData[i].operatingCost < minCost){  // and check if is the lowest operatingCost
      iMin = i;      // if it is, save that index
    }
  }

  cout << "Your lowest operating cost is " << BoatData[iMin].operatingCost << " with the boat " << BoatData[iMin].name << endl;

  return 0;
}

但是根据您的代码和 cmets,您的方法是对数组进行排序,所以这就是您应该在代码中实现的:

#include <string>
#include <iostream>
#include <stdio.h> 
using namespace std;


void swap(int *xp, int *yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 

void swapName(string& x, string& y) 
{ 
    string temp(x); 
    x = y; 
    y = temp; 
} 

void bubbleSort(int arr[], string boatName[], int n) 
{ 
   int i, j; 
   for (i = 0; i < n-1; i++)       

       for (j = 0; j < n-i-1; j++)  
           if (arr[j] > arr[j+1])
           { 
              swap(&arr[j], &arr[j+1]);              // if you have to swap the data
              swapName(boatName[j], boatName[j+1]);  // swap also the names
           }
} 

void printArray(int arr[], int size) 
{ 
    int i; 
    for (i=0; i < size; i++) 
        printf("%d ", arr[i]); 
}


int main()
{
  //Declarations
  int numBoats = 5;
  int boatData[5];
  string boatName[5] = {"a", "gg", "tt", "hh", "jj"};//
  int costGas;
  int i;
  int j;
  int boatArray[5][3] = {{30, 100, 5000}, 
                       {20, 200, 10000}, 
                       {30, 200, 2000}, 
                       {25, 150, 12000}, 
                       {30, 50, 8000}};

  //User input 
  cout << "Please enter the first boats cost of gas: "<<endl;
  cin >> costGas;
  cout << "The cost of gas you entered is " << costGas << endl;
  int yearsService;
  cout << "Please enter the number of years in service: " << endl;
  cin >> yearsService;
  cout << "The number of years in service you entered is " << yearsService << endl;

  //Calculations
  for (i = 0; i < numBoats; i++) {
    int mpg = boatArray[i][1];
    int maintenanceCost = boatArray[i][2]; 
    int purchaseCost = boatArray[i][3];

    int totalMiles = 15000 * yearsService;
    int gasTotal = totalMiles / mpg;
    int maintenanceTotal = maintenanceCost * yearsService;
    int operatingCost = purchaseCost + gasTotal + maintenanceTotal;

    boatData[i] = operatingCost;
  }

  //Print operating costs
   cout<<"The operating costs of each boat is: "<<endl;
  printArray(boatData, numBoats);

  //Bubblesort
  bubbleSort(boatData, boatName, numBoats);  // pass also the name's array
  printArray(boatData, numBoats);

  cout<<"Your lowest operating cost is "<<boatData[0]<< " with the boat " << boatName[0] << endl;  // print the name

  return 0;
}

【讨论】:

    【解决方案2】:

    我只想为船添加一些具有唯一编号的参考数组,并将它们与值一起排序。排序函数:

    void bubbleSort (int id[], int arr[], int n) 
    { 
    int i, j; 
    for (i = 0;  i < n - 1;  i++)
        for (j = 0;  j < n - i - 1;  j++)  
            if  ( arr[j] > arr[j+1] ) {
                swap ( arr[j], arr[j+1] ); 
                swap ( id[j], id[j+1] ); 
            }
    }
    

    然后我通过这些排序的 ID 引用名称。
    这个简化的示例展示了它如何应用于您的案例,它在排序前后打印一些数据(此处为名称):

    int main () 
    {
    
    const int n = 5;
    int     boatID[n] = { 0, 1, 2, 3, 4 };
    string  name[n] = { "oak", "pine", "maple", "cypress", "elm" };
    int     value[n] = { 20, 10, 0, 25, 15 };
    
    for (int i = 0; i < n; i++) 
        cout << "id: " << boatID[i] << "  value: " << value[i] << "  name: " << name[boatID[i]] << endl;
    
    bubbleSort (boatID, value, n);  // sort references by value
    
    cout << "* sorted *" << endl;
    for (int i = 0; i < n; i++) 
        cout << "id: " << boatID[i]  << "  value: " << value[i] << "  name: " << name[boatID[i]] << endl;
    
    } 
    

    请注意,boatID[i] 用于代替索引 name[] 输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 2014-03-25
      • 1970-01-01
      • 2012-07-29
      • 2011-08-26
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多