Created
September 5, 2012 14:49
-
-
Save snorrewb/3637757 to your computer and use it in GitHub Desktop.
IMT1031 Grunnleggende Programmering - Obligatorisk oppgave 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
//pre-declare methods used in the program | |
int menu (); | |
bool legal (int distance); | |
void race (int distance, int lap); | |
bool rerun (); | |
float timeToFloat (int m, int s, int h); | |
void floatToTime (float h); | |
//main, where all our methods are initialized and controlled from | |
int main () { | |
//declare variables | |
const int lapLength = 400; //how long a lap is, to determine total laps | |
int raceLength; //the total length of the intended race | |
bool done = false; //program exits when true | |
//welcome message | |
cout << "Velkommen til tidtakerprogrammet!\n"; | |
while (!done){ //runs races until the user is done | |
raceLength = menu (); //runs menu to dertermine race length | |
race (raceLength, lapLength); //runs the race until its done | |
done = rerun (); //asks the user whether to rerun | |
} | |
//farewell message | |
cout << "\n\nHa en fin dag!"; | |
} | |
//the menu() asks for the length of the race and makes sure it is valid | |
int menu () { | |
//declare variables | |
int inInt; //variable for storing integer input | |
while (true) { //runs until a legal race length is obtained | |
cout << "\n\nVennligst tast inn distansen som skal gåes: \n"; | |
cin >> inInt; //reads the proposed distance | |
//sends the proposed distance to legal() to check validity | |
if (legal(inInt)) { return inInt; } | |
else { cout << "\n" << inInt << " er en ugyldig distanse!\n"; } | |
} | |
} | |
//legal() makes sure that a proposed length of race is valid | |
bool legal (int distance) { | |
//if-else loop returns true if length is valid, false otherwise | |
if (distance == 500 || distance == 1000|| distance == 1500|| | |
distance == 3000|| distance == 5000|| distance == 10000) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
//runs a single race with 2 contestants over the specified distance | |
void race (int distance, int lap) { | |
//declare (and initialize) variables | |
int laps = ceil((float) distance / (float) lap); //number of laps | |
int inInt; //variable for storing integer input | |
float inFloat, tempFloat; //variables for storing float input | |
//let the user know what the race parameters are | |
cout << "\nDa dette løpet er " << distance << " m langt, vil det bli " | |
<< laps << " passeringer.\n"; | |
//runner structure, contains the variables needed to define our runners | |
struct runner { | |
int num; //variable to store the runner's starting number | |
int laps; //variable to store the number of completed laps | |
float elapsed; //variable to store the runner's elapsed time | |
}; | |
//declare struct-based variables | |
runner runner1; //structs for our two runners | |
runner runner2; | |
runner1.num = 1; //set starting numbers for both runners | |
runner2.num = 2; | |
runner1.laps = 0; //initialize laps run before starting | |
runner2.laps = 0; | |
runner1.elapsed = 0; //initialize and set time elapsed before starting | |
runner2.elapsed = 0; | |
//while loop keeps going until both runners have finshed their laps | |
while (runner1.laps < laps || runner2.laps < laps) { | |
//keeps asking for lap times until both runners are done | |
while (true) { | |
//asks what runner a time should be noted for | |
cout << "\nPassering av løper nummer (1-2):"; | |
cin >> inInt; //recieves the user's choice | |
//if-else loop adheres to the user's choice | |
if (inInt == 1) { //handles adding lap times for runner 1 | |
//makes sure the runner isn't finished already | |
if (runner1.laps == laps) { | |
cout << "\nLøper 1 er allerede i mål!"; | |
} else { //adds time and laps to the runner's stats | |
//declare variables | |
int inMin, inSec, inHun; //for recieving the timestamp | |
//while loop keeps going until broken by a valid time | |
while (true) { | |
//asks for and recieves a timestamp | |
cout << "\nPasseringstid (mm ss hh): "; | |
cin >> inMin >> inSec >> inHun; | |
//converts our timestamp to a workable float | |
inFloat = timeToFloat (inMin, inSec, inHun); | |
//if-else loop makes sure we type in valid times | |
if (inFloat >= (runner1.elapsed + 8)) { | |
//calculates and stores the last laptime for print | |
tempFloat = inFloat - runner1.elapsed; | |
break; //leaves the while loop | |
} else { | |
cout << "\nUgyldig tid, prøv igjen\n"; | |
} | |
} | |
//prints out the time of the runner's last lap | |
cout << "Siste rundetid: "; | |
floatToTime(tempFloat); | |
cout << "\n\n"; | |
//updates the tracked stats in the runner1 struct | |
runner1.elapsed = inFloat; | |
runner1.laps++; | |
//if loop checks if runner has finished the race | |
if (runner1.laps == laps) { | |
//prints out the final time of the runner | |
cout << "\nMÅLPASSERING: Løper 1! "; | |
floatToTime(runner1.elapsed); | |
cout << "\n\n"; | |
} | |
} | |
} else if (inInt == 2) { //handles adding lap times for runner 2 | |
//makes sure the runner isn't finished already | |
if (runner2.laps == laps) { | |
cout << "\nLøper 2 er allerede i mål!"; | |
} else { //adds time and laps to the runner's stats | |
//declare variables | |
int inMin, inSec, inHun; //for recieving the timestamp | |
//while loop keeps going until broken by a valid time | |
while (true) { | |
//asks for and recieves a timestamp | |
cout << "\nPasseringstid (mm ss hh): "; | |
cin >> inMin >> inSec >> inHun; | |
//converts our timestamp to a workable float | |
inFloat = timeToFloat (inMin, inSec, inHun); | |
//if-else loop makes sure we type in valid times | |
if (inFloat >= (runner2.elapsed + 8)) { | |
cout << inFloat; | |
//calculates and stores the last laptime for print | |
tempFloat = inFloat - runner2.elapsed; | |
break; //leaves the while loop | |
} else { | |
cout << "\nUgyldig tid, prøv igjen\n"; | |
} | |
} | |
//prints out the time of the runner's last lap | |
cout << "Siste rundetid: "; | |
floatToTime(tempFloat); | |
cout << "\n\n"; | |
//updates the tracked stats in the runner2 struct | |
runner2.elapsed = inFloat; | |
runner2.laps++; | |
//if loop checks if runner has finished the race | |
if (runner2.laps == laps) { | |
//prints out the final time of the runner | |
cout << "\nMÅLPASSERING: Løper 2! "; | |
floatToTime(runner2.elapsed); | |
cout << "\n\n"; | |
} | |
} | |
} | |
//if-loop makes sure the user types a valid number. leaves loop | |
//if valid, otherwise re-runs it | |
if (!(inInt == 1 || inInt == 2)) { | |
cout << "\nUgyldig løper, prøv igjen\n"; | |
} else { break; } //leaves the while-loop | |
} | |
} | |
//if-else loop decides who the winner is, and prints the result | |
if (runner1.elapsed < runner2.elapsed) { | |
cout << "\nLøper 1 vinner med tiden "; | |
floatToTime(runner1.elapsed); | |
cout << "\n\n"; | |
} else if (runner1.elapsed > runner2.elapsed) { | |
cout << "\nLøper 2 vinner med tiden "; | |
floatToTime(runner2.elapsed); | |
cout << "\n\n"; | |
} else if (runner1.elapsed == runner2.elapsed) { | |
cout << "\nVinner må avgjøres på målfoto! "; | |
floatToTime(runner1.elapsed); | |
cout << "\n\n"; | |
} | |
cout << "\n"; | |
} | |
//asks user whether the program should be terminated or rerun | |
bool rerun () { | |
//declare variables | |
char again; //variable for storing char input | |
cout << "\n\nØnsker du å kjøre igjen? (J/N)\n"; | |
while (true) { //asks for a char until a valid one is given | |
cin >> again; | |
again = toupper(again); | |
//if-else loop makes sure we are given a valid char input | |
if (again == 'J') { | |
return false; //exits loop; causes program to re-run | |
} else if (again == 'N') { | |
return true; //exits loop; causes program to terminate | |
} else { //berates user for answering incorrectly | |
cout << "\n\nVennligst tast J eller N!\n"; | |
} | |
} | |
} | |
//handily converts input times from our program into a float we can work with | |
float timeToFloat (int m, int s, int h) { | |
//declare variables | |
float min = m; | |
float sec = s; | |
float hun = h; | |
//simple math equations turns the time into a float measure of seconds | |
float seconds = (hun / 100) + sec + (min*60); | |
return seconds; //returns the time as a float value | |
} | |
//prints out a timestamp based on a float number | |
void floatToTime (float s) { | |
//declare variables | |
int min, sec, hun; | |
//simple maths dissect the float into minutes, seconds and hundredths | |
min = s; | |
hun = (ceil((s - min)*100)); | |
min = min / 60; | |
sec = s - (min*60); | |
//the timestamp is printed out | |
cout << min << " " << sec << " " << hun; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment