2016-01-31 42 views
-1

我創建用於跟蹤在體育比賽結束時間的程序(滑雪,跑步等)間隔開始的其他對象的自定義時間對象。該程序詢問用戶第一位參賽者何時開始以及參賽者使用什麼樣的間隔(30秒或60秒)。計算與同一類(運算符重載)

我試圖通過計算從結束時間減去開始時間(因爲它們都具有不同的開始時間)在比賽中使用的每個競爭者的時候,但我似乎無法得到它的工作。它記錄了每個參賽者的正確開始和結束時間,但時間已過去的對象(我在競爭對手構造函數中用「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)); 
    } 
+0

歡迎堆棧溢出。尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建[MCVE] ._ –

回答

0

使用成員計算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); 
    }; 
+0

是否允許像我這樣分配對象? –

+0

最好不要像sinceStart那樣保留一個冗餘變量。您的初始化並不意味着SinceStart將被更新爲等於完成 - 開始。看起來你並沒有更新setFinish開始(這可能是一個替代方案,但更糟糕的解決方案)。 – user2672165