计算具有相同 class 的其他对象的自定义时间对象(运算符重载)

Calculate a custom time object with other objects of same class (operator overloading)

我正在创建一个程序来跟踪体育比赛(滑雪、运行 等)的完成时间,并有间隔开始。该程序询问用户第一个参赛者何时开始,以及参赛者使用什么样的间隔(30 秒或 60 秒)。

我试图通过从结束时间减去开始时间来计算每个参赛者在比赛中使用的时间(因为他们都有不同的开始时间),但我似乎无法让它工作。它为每个竞争对手注册了正确的开始和结束时间,但是经过的时间对象(我在 Competitor 构造函数中使用 "elapsed = finish - start" 计算)仅设置为 00:00:00.

谁能看出我做错了什么?我的重载运算符有问题吗?

#include <iostream>
#include <iomanip>
#include <fstream>
#include "timer.h"
using namespace std;

const int MAX_NAME = 30;                        // Maks. lengde på navn/nasjonalitet
const int MAX_COMPS = 20;                       // Antall utøvere
const int MAX_LINE = 60;                        // Length of line + '/0'

class Time
    {
    private:
        int hh,
            mm,
            ss;
    public:
        Time()
            { hh = mm = ss = 0; }
        Time(int h, int m, int s)
            { hh = h; mm = m; ss = s; }
        Time operator + (Time t);
        Time operator - (Time t);
        bool operator < (Time t);
        Time operator * (int n);
        void showTime();

    };

class Competitor
    {
    private:
        int startNumber;
        char name[MAX_NAME + 1];
        char nationality[MAX_NAME + 1];
        Time start;
        Time finish;
        Time sinceStart = finish - start;
    public:
        Competitor()
            {
            startNumber = 0;
            strcpy_s(name, "");
            strcpy_s(nationality, "");
            }
        Competitor(int n, char nm[], char nt[], Time calcStart);
        void showCompetitor();
        void setCompetitor(char buffer[], int n);
        void setFinish(int h, int m, int s);
    };

void menu();
void readFromFile();
void showAllCompetitors();
Time setInterval();
void registerFinish();
int read(char text[], int one, int two);
char read();

Competitor competitors[MAX_COMPS];                  // Array med plass til MAX_COMPS løpere

int lastCompetitor = 0;                             // Holder styr på antall løpere
Time compStart,
     interval;

// MAIN-FUNKSJON
int main()
    {
    int startH,
        startM;
    char choice;

    interval = setInterval();

    cout << "\nEnter start Time for the first competitor (HH MM): ";
    cin >> startH >> startM; compStart = Time(startH, startM, 0);

    readFromFile();             // Løpere leses inn fra fil og legges i løper-array

    menu();
    cout << "\nEnter choice: "; choice = read();
    while(choice != 'Q')
        {
        switch(choice)
            {
                case 'R':
                    registerFinish();
                    break;
                case 'D':
                    showAllCompetitors();
                    break;
                case 'W':
                    // Lag funksjon som skriver resultater til fil
                    // break;
                default:
                    menu();
                    break;
            }
        cout << "\nEnter choice: "; choice = read();
        }

    competitors[1].showCompetitor();

    cout << endl << endl;



    return 0;
    }

// KLASSE-FUNKSJONSDEFINISJONER
void Competitor::setCompetitor(char buffer[], int n)
    {
    startNumber = n;
    strncpy_s(name, buffer, MAX_NAME);
    strncpy_s(nationality, buffer + 30, MAX_NAME);
    }

void Competitor::showCompetitor()
    {
    cout << startNumber << ". " << name << " (" << nationality << ")";
    cout << "\nStarted: "; start.showTime();
    cout << "\nFinished: "; finish.showTime();
    cout << "\nTime used: "; sinceStart.showTime();
    }

Competitor::Competitor(int n, char nm[], char nt[], Time calcStart)
    {
    startNumber = n;
    strncpy_s(name, nm, MAX_NAME);
    strncpy_s(nationality, nt, MAX_NAME);
    start = compStart + interval * (startNumber - 1);
    }

void Competitor::setFinish(int h, int m, int s)
    { finish = Time(h, m, s); }

Time Time::operator + (Time t)
    {
    int totalSecs,
        totalSecs2,
        diffSecs;
    Time elapsed;
    totalSecs = hh * 3600 + mm * 60 + ss;                   // Objekt-tidspunkt i sekunder.
    totalSecs2 = t.hh * 3600 + t.mm * 60 + t.ss;            // Starttidspunkt for løp i sek.
    diffSecs = totalSecs + totalSecs2;                      // Differanse mellom starttidspunkt og objekt-tidspunkt i sek.

    elapsed.hh = diffSecs / 3600;
    elapsed.mm = (diffSecs % 3600) / 60;
    elapsed.ss = (diffSecs % 60);

    return elapsed;
    }

Time Time::operator - (Time t)
    {
    int totalSecs,
        totalSecs2,
        diffSecs;
    Time diff;
    totalSecs = hh * 3600 + mm * 60 + ss;
    totalSecs2 = t.hh * 3600 + t.mm * 60 + t.ss;
    diffSecs = totalSecs2 - totalSecs;

    diff.hh = diffSecs / 3600;
    diff.mm = (diffSecs % 3600) / 60;
    diff.ss = (diffSecs % 60);

    return diff;
    }

bool Time::operator < (Time t)
    {
    int totalSecs,
        totalSecs2;
    totalSecs = hh * 3600 + mm * 60 + ss;
    totalSecs2 = t.hh * 3600 + t.mm * 60 + t.ss;
    if(totalSecs < totalSecs2)
        return true;
    else
        return false;
    }

Time Time::operator * (int n)
    {
    int totalSec;                   // Gjør om Tid intervall til en int med 30/60
    Time secToTime;                 // Tiden siden start for hver enkelt løper

    totalSec = (hh * 3600 + mm * 60 + ss) * n;      // Multipliserer intervall-tid med startnummer-1
    secToTime.hh = totalSec / 3600;
    secToTime.mm = (totalSec % 3600) / 60;
    secToTime.ss = (totalSec % 60);
    return secToTime;               // Returnerer Tid-objekt med tid siden løpets start
    }

void Time::showTime()
    {
    cout << " " << setw(2) << setfill('0') << hh << ":"
        << setw(2) << setfill('0') << mm << ":"
        << setw(2) << setfill('0') << ss;
    }

// ANDRE FUNKSJONSDEFINISJONER
void menu()
    {
    cout << "\nR - Register a new finish";
    cout << "\nD - Displays the current list of finished competitors";
    cout << "\nW - Write results to file";
    cout << "\nQ - Quit program";
    cout << endl;
    }

void readFromFile()
    {
    ifstream infile("COMPETITORS.DTA");
    int count = 1;
    char name[MAX_NAME];
    char nat[MAX_NAME];
    Time calcStart;

    if(infile)
        {
        infile.getline(name, MAX_NAME);
        infile.getline(nat, MAX_NAME);
        competitors[count] = Competitor(count, name, nat, calcStart);
        while(infile)
            {
            ++lastCompetitor;
            infile.getline(name, MAX_NAME);
            infile.getline(nat, MAX_NAME);
            competitors[++count] = Competitor(count, name, nat, calcStart);
            }
        }
    }

void showAllCompetitors()
    {
    for(int i = 0; i < lastCompetitor; i++)
        {
        competitors[i+1].showCompetitor();
        cout << endl;
        }
    }

Time setInterval()
    {
    int choice;
    cout << "\nEnter interval between competitors\n\t1. 30 seconds\n\t2. 60 seconds";
    choice = read("Choice", 1, 2);
    if(choice == 1)
        return Time(0, 0, 30);
    else
        return Time(0, 1, 0);
    }

void registerFinish()
    {
    Timer t;
    int n,
        h,
        m,
        s;
    n = read("\nEnter start number", 1, lastCompetitor);
    t.hent(h, m, s);
    competitors[n].setFinish(h, m, s);
    }

int read(char text[], int one, int two)
    {
    int n;
    do
        {
        cout << endl << text << " (" << one << " - " << two << "): ";
        cin >> n;
        } while(n < one || n > two);
    return n;
    }

char read()
    {
    char ch;
    cin >> ch; cin.ignore();
    return (toupper(ch));
    }

使用成员计算 sinceStart:

class Competitor
    {
    private:
        int startNumber;
        char name[MAX_NAME + 1];
        char nationality[MAX_NAME + 1];
        Time start;
        Time finish;
        Time sinceStart() const {return finish - start;}
    public:
        Competitor()
            {
            startNumber = 0;
            strcpy_s(name, "");
            strcpy_s(nationality, "");
            }
        Competitor(int n, char nm[], char nt[], Time calcStart);
        void showCompetitor();
        void setCompetitor(char buffer[], int n);
        void setFinish(int h, int m, int s);
    };