C ++文件 - 写作,阅读,数组;

时间:2017-01-04 18:17:09

标签: c++ arrays file

我做错了什么?我无法理解。如果有人有任何想法如何找出哪个Sea Trip有最大的销售量[门票总数最大]然后我如何显示它 - 你会注意到它应该在源代码中的位置。我一直在尝试和尝试,我就不能了..在过去的7-8小时内没有在笔记本电脑前移动。需要帮助。

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;

struct trip
{
    int day; //day of Sea Trip
    int fln; //No. of Sea Trip
    char name[14]; //Ship name
    char capname[14]; // Captain Name
    char destination[14]; // Destination
    int br1; // No. pass in 1 class
    int br2; // No. pass in 2 class
    float price1; // Ticket price 1 class
    float price2; // Ticket price 2 class
    float sum; // Sum of sold tickets
};
fstream seatrips;
int add_trip(trip trip[]) //adding sea trips
{
    int n;
    seatrips.open("SeaTrips.txt", ios::out);
    if (seatrips.fail())
    {
        cout << "Opening file had no success";
        exit(1);
    }
    cout << "Enter the number of Sea Trips (max 25): ";
    do
    {
        cin >> n;
    } while (n < 0 || n > 25);
    for (int i = 0; i < n; i++)
    {
        cout << "Enter the day of the trip (1-31) : ";
        do{
            cin >> trip.day;
        } while (trip.day < 1 || trip.day > 31);
        seatrips << trip.day << endl;
        cout << "Sea Trip Number : ";
        cin >> trip.fln;
        seatrips << trip.fln << endl;
        cout << "Ship name : ";
        cin >> trip.name;
        seatrips << trip.name << endl;
        cout << "Captain name : ";
        cin >> trip.capname;
        seatrips << trip.capname << endl;
        cout << "Destination : ";
        cin >> trip.destination;
        seatrips << trip.destination << endl;
        cout << "Number of passengers in 1. class : ";
        cin >> trip.br1;
        seatrips << trip.br1 << endl;
        cout << "Number of passengers in 2.class : ";
        cin >> trip.br2;
        seatrips << trip.br2 << endl;
        cout << "Ticket price 1. class : ";
        do{
            cin >> trip.price1;
            seatrips << trip.price1 << endl;
        } while (trip.price1 < 1);
        cout << "Ticket price 2. class : ";
        do{
            cin >> trip.price2;
            seatrips << trip.price2 << endl;
        } while (trip.price2 < 1);
        trip.sum = trip.br1 * trip.price1 + trip.br2 * trip.price2;
        seatrips << trip.sum;
        cout << endl << endl;


    }
    seatrips.close();
    return n;
}
void edit(trip trip[], int n) //editing pass. info
    int t_fln; //temp sea trip number
    seatrips.open("SeaTrips.txt", ios::out);
    seatrips.seekg(0);
    if (seatrips.fail())
    {
        cout << "Unable to open SeaTrips file";
        exit(1);
    }
    cout << "Enter the number of the sea trip u want to edit: ";
    cin >> t_fln;
    for (int i = 0; i < n; i++)
    {
        seatrips >> trip.day;
        seatrips >> trip.fln;
        seatrips >> trip.name;
        seatrips >> trip.br1;
        seatrips >> trip.br2;
        seatrips >> trip.price1;
        seatrips >> trip.price2;
        seatrips >> trip.sum;
        if (t_fln == trip.fln)
        {
            cout << "Add new number of ppl in 1. class : ";
            cin >> trip.br1;
            seatrips << trip.br1 << endl;
            cout << "Add new number of ppl in 2. class : ";
            cin >> trip.br2;
            seatrips << trip.br2 << endl;

        }
    }
    seatrips.close();
}
void look(trip trip[], int n) //search by ship name, and list of all trips
    int choice2, flag = 0;
    string t_name;
    seatrips.open("SeaTrips.txt", ios::in);
    seatrips.seekg(0);
    if (seatrips.fail())
    {
        cout << "Unable to open";
        exit(1);
    }
    do{
        cout << endl << "Choose..." << endl;
        cout << "1. Find a Sea Trip by entered Ship Name." << endl;
        cout << "2. List of all Sea Trips" << endl;
        cout << "3. Show Sea Trip with Maximum Sold Tickets" << endl;
        cout << "4. Exit" << endl;
        cout << "Choice: ";
        cin >> choice2;
        switch (choice2)
        {
        case 1:{cout << "Enter info about the wanted Sea Trip..." << endl;
            cout << "Ship Name: ";
            cin >> t_name;
            for (int i = 0; i < n; i++)
            {
                seatrips >> trip.day;
                seatrips >> trip.fln;
                seatrips >> trip.name;
                seatrips >> trip.capname;
                seatrips >> trip.destination;
                seatrips >> trip.br1;
                seatrips >> trip.br2;
                seatrips >> trip.price1;
                seatrips >> trip.price2;
                seatrips >> trip.sum;
                if (t_name == trip.name)
                {
                    cout << "Day: " << trip.day << endl;
                    cout << "Number of Sea Trip: " << trip.fln << endl;
                    cout << "Ship name : " << trip.name << endl;
                    cout << "Name of captain: " << trip.capname << endl;
                    cout<< "Destination: " << trip.destination << endl;
                    cout << "Number of passengers in 1. class: " << trip.br1 << endl;
                    cout << "Ticket price 1. class " << trip.price1 << endl;
                    cout << "Number of passengers in 2. class: " << trip.br2 << endl;
                    cout << "Ticket price 2. class " << trip.price2 << endl;
                    cout << "Sum of sold tickets: " << trip.sum << endl;
                    cout << endl;
                    flag++;
                }
            } break; }
        case 2:
        for (i = 0; i < n; i++)
        {
                    seatrips >> trip.day;
                    seatrips >> trip.fln;
                    seatrips >> trip.name;
                    seatrips >> trip.capname;
                    seatrips >> trip.destination;
                    seatrips >> trip.br1;
                    seatrips >> trip.br2;
                    seatrips >> trip.price1;
                    seatrips >> trip.price2;
                    seatrips >> trip.sum;

                    cout << "Day: " << trip.day << endl;
                    cout << "Number of Sea Trip: " << trip.fln << endl;
                    cout << "Ship Name: " << trip.name << " Captain Name " << trip.capname << " Destination " << trip.destination << endl;
                    cout << "Number of ppl 1. class  : " << trip.br1 << endl;
                    cout << "Ticket price 1. class: " << trip.price1 << endl;
                    cout << "Number of ppl 2. class: " << trip.br2 << endl;
                    cout << "Ticket price 2. class: " << trip.price2 << endl;
                    cout << "Sum of sold tickets: "<< trip.sum << endl;  }
            } break; }
        case 3:

/* max sales function
*
* Trip's sum is the largest one
* And display the Sea Trip here.
*
*/

    } while (choice2 != 4);
    seatrips.close();
}

void main()
{
    setlocale(LC_ALL, "Bulgarian");
    int choice1;
    int n;
    trip trip[25];
    do
    {
        cout << "Menu..." << endl;
        cout << "1. Adding and saving to file Sea Trips data [deletes any existing]" << endl;
        cout << "2. Editing content" << endl;
        cout << "3. Search and look up" << endl;
        cout << "4. End" << endl;
        cout << "Enter your choice: ";
        cin >> choice1;
        switch (choice1)
        {
        case 1:n = add_trip(trip); break;
        case 2: edit(trip, n); break;
        case 3:look(trip, n);
        }
    } while (choice1 != 4);


}

1 个答案:

答案 0 :(得分:0)

给定vector trip

std::vector<trip> database;

你可以迭代通过它寻找最大的总和,并维持一个最大总和的旅程迭代器:

float largest_sum = -1.0f;
std::vector<trip>::const_iterator trip_with_largest_sum = database.end();
std::vector<trip>::const_iterator iter;
for (iter = database.begin(); iter != database.end(); ++iter)
{
  if (iter->sum > largest_sum)
  {
    largest_sum = iter->sum;
    trip_with_largest_sum = iter;
  }
}

在循环结束时,trip_with_largest_sum将指向具有最大总和的行程或database.end()如果未找到任何项目。

如果你坚持使用数组,相同的算法,使用索引变量而不是迭代器。