C ++:数据类型和数组

时间:2018-12-14 15:03:41

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

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

julia> using FileIO, JLD2

julia> jldopen("myfile.jld2", "w") do f
           write(f, "x", rand(10))
           write(f, "y", "test")
       end

julia> load("myfile.jld2")
Dict{String,Any} with 2 entries:
  "x" => [0.918336, 0.608631, 0.757459, 0.935133, 0.548579, 0.909, 0.913573, 0.0278975, 0.…

  "y" => "test"

julia> load("myfile.jld2", "y")
"test"

julia> f = jldopen("myfile.jld2", "r")
JLDFile C:\Users\carsten\myfile.jld2 (read-only)
 ├─� x
 └─� y

julia> typeof(f)
JLD2.JLDFile{JLD2.MmapIO}

julia> f["x"]
10-element Array{Float64,1}:
 0.9183355611466055
 0.6086314948624771
 0.757458522055442
 0.9351333595616453
 0.5485794420648191
 0.9089998574850378
 0.9135729509843764
 0.027897482037234633
 0.5827560900500541
 0.9831034815173016

julia> close(f)

2 个答案:

答案 0 :(得分: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;
}

但是根据您的代码和注释,您的方法是对数组进行排序,因此这是您在代码中实现的:

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

答案 1 :(得分:-1)

我只添加一些带有唯一编号的参考数组,然后将它们与值一起排序。排序功能:

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[]的输出。