Final Sig
Final Sig
// MAIN.CPP
//===================================================
//Include libraries
while (exitProgram != true) //while exitProgram is not equal to exit stay in program
//===================================================
// CRASH.CPP
//===================================================
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include "crash.h"
//===================================================
// MENU /UI
//===================================================
void printWelcomeMenu()
cout << "\t│ ├─┤├┬┘ │ ├┬┘├─┤└─┐├─┤ └─┐│││││ ││ ├─┤ │ │ │├┬┘ " << endl;
cout << "\t└─┘┴ ┴┴└─ └─┘┴└─┴ ┴└─┘┴ ┴ └─┘┴┴ ┴└─┘┴─┘┴ ┴ ┴ └─┘┴└─ " << endl;
void menubar()
void menuSpace()
void printMenuChoices()
printWelcomeMenu();
void printVehicleEditMenu()
menubar();
menuSpace();
menuSpace();
menuSpace();
menuSpace();
menuSpace();
menuSpace();
menuSpace();
menubar();
}
// TAKES USER INPUT AND RUNS THE MENU FUNCTION
switch(choice)
constructScenario(vehicleFile);
break;
editFile(vehicleFile);
break;
editVehiclesInFile(vehicleFile);
break;
case 4:
{
readVehicleFile(vehicleFile);
break;
default: //default case, triggers boolean quit, leaves menu and ends program
menubar();
menuSpace();
menubar();
*exitProgram = true;
break;
if((*userMenuChoice > 0) && (*userMenuChoice < 6)) // if choice is valid return choice
return;
}
else // else recurse until input IS valid
//===================================================
// File Functions
//===================================================
resetFileValidate(vehicleFile);
cout << "What kind of vehicle do you want to create?" << endl;
switch(userInput)
case 1:
break;
case 2:
break;
case 3:
{
Bike tempBike; //create temporary bike
break;
default :
cout <<"If you see this error has occured!" << endl; //default case, should never be reached.
break;
string name;
double mass;
double time;
double velocity;
double acceleration;
if(type == 1)
cin.ignore();
getline(cin,name);
else if (type == 2)
cin.ignore();
getline(cin,name);
else
cin.ignore();
getline(cin,name);
//This section the user is prompted to enter the details of their vehicle
cout << "Please enter the mass of " << name <<" (KG) : ";
tempVehicle->setName(&name);
tempVehicle->setTime(&time);
cout << "Please enter the default avg velocity of " << name <<" (m/s) : ";
cout << "Please enter the acceleration of " << name <<" (m/s^2) : ";
tempVehicle->setAcc(&acceleration);
tempVehicle->printStats();
vehicleFile << name << "," << type <<","<< mass<< "," << time << "," << velocity << "," << acceleration <<
",";
cout << "\n\n" << name << " saved successfully to vehicle.txt\n\t\t Returning to menu....\n" << endl;
resetFileValidate(vehicleFile);
string tempstr; // TEMP STRING TO HOLD DATA READ FROM VEHICLE FILE
int tempInt = 0,vCounter = 0,userVchoice = 0;//tempInt counter keeps track of vehicle's stat pointer.
vCounter keeps track of which vehicle #.
if(tempInt % 6 == 0 && tempstr != "") // int%6 == 0 indicates that every 6 iterations it should be a vehicle
name, thus vehicle name and it is not empty.
{
vCounter++;//update vehicle counter, happens every 6 iterations
tempInt++; //every iteration temp int goes up by one to indicate to move to the next stat
tempInt++;
tempInt++;
tempInt++;
tempInt++;
}
else if (tempInt % 6 == 5)// indicates acceleration
cout << "\t\t\t\t\t\tAcceleration : " << tempstr << "\n" << endl;;
tempInt++;
else
Vehicle tempV; //temporary vehicle to be edited and replaced with data from fstream
++tempInt; //
if(statCounter == 0) //stat counter keeps track of which statistic to edit for vehicle
statCounter++;
else if(statCounter == 1)
int tempI = stoi(tempstr); // set the type of vehicle from filestream by converting from string to int
tempV.setType(&tempI);
statCounter++;
else if (statCounter == 2)
tempV.setMass(&tempD);
statCounter++;
else if (statCounter == 3)
double tempD = stod(tempstr); // set time from string using string to double conversion
tempV.setTime(&tempD);
statCounter++;
}
else if (statCounter == 4)
statCounter++;
else if (statCounter == 5)
statCounter++;
Vehicle textToVehicle(fstream& vehicleFile) //takes user's choice-> reads file-> creates vehicle obj and
returns the vehicle
Vehicle tempVehicle; // default constructed vehicle that will be edited and returned.
menubar();
cout <<"\n";
menuSpace();
///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////VEHICLE EDITOR///////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
Vehicle tempVehicle;
int vehiclePick,editPick;
string oldVehicleName;
readVehicleFile(vehicleFile);
tempVehicle = returnVehicle(vehicleFile,vehiclePick);
oldVehicleName = tempVehicle.getName();
tempVehicle.printStats();
while(exitVehicleEdit == false)
menubar();
menuSpace();
printVehicleEditMenu();
switch(editPick)
case 1:
string newName;
cin.ignore();
getline(cin,newName);
tempVehicle.setName(&newName);
tempVehicle.printStats();
break;
case 2:
double tempD;
cout << "Current mass is :" << tempVehicle.getMass() <<" KG"<< endl;
tempVehicle.setMass(&tempD);
tempVehicle.printStats();
break;
}
case 3:
double tempD;
cout << "Current duration of crash is :" << tempVehicle.getTime() << "Seconds" << endl;
tempVehicle.setTime(&tempD);
tempVehicle.printStats();
break;
case 4:
double tempD;
cout << "Current velocity is :" << tempVehicle.getVelocity() << "m/s" << endl;
tempVehicle.setVelocity(&tempD);
tempVehicle.printStats();
break;
case 5:
double tempD;
cout << "Current acceleration is :" << tempVehicle.getAcc() << "m/s^2" << endl;
tempVehicle.setAcc(&tempD);
tempVehicle.printStats();
break;
case 6:
int tempI;
do{
cout << "Pick a new status for " << tempVehicle.getName() << endl;
tempVehicle.setType(&tempI);
tempVehicle.printStats();
break;
default:
saveEditedVehicleToFile(vehicleFile,&tempVehicle,&oldVehicleName);
menuSpace();
menuSpace();
exitVehicleEdit = true;
break;
}
}
resetFileValidate(vehicleFile);
int commaCounter;
string tempString;
//vehicleFile << name << "," << type <<","<< mass<< "," << time << "," << velocity << "," << acceleration
<< ",";
vehicleFile << recievedVehicle->getName() << "," << recievedVehicle->getType() << "," <<
recievedVehicle->getMass() << "," << recievedVehicle->getTime() << "," << recievedVehicle-
>getVelocity() << "," << recievedVehicle->getAcc() <<",";
//===================================================
// VEHICLE
//===================================================
mass = 0.0;
time = 0.0;
type = 0;
}
Vehicle::~Vehicle()
/////////////////////GETTERS//////////////////////
//////////////////////SETTERS/////////////////////
void Vehicle::printStats()
cout <<"\n\n";
menubar();
menubar();
cout <<"\n";
menuSpace();
cout << "Name : " << name << endl; //print name
menuSpace();
cout << "Mass : " << mass << " KG" << endl; //print mass
menuSpace();
cout << "Duration of Crash : " << time << " seconds" <<endl; //print time
menuSpace();
cout << "Velocity : " << velocity <<" m/s" << endl; //print velocity
menuSpace();
cout << "Acceleration : " << acceleration <<" m/s^2" << endl; //print accel
menuSpace();
if(type == 0)
else if (type == 1)
else if (type == 2)
else if (type == 3)
{
cout <<"Type : Motorcycle" << endl;
else
//===================================================
// TRUCK
//===================================================
Truck::Truck() // constructor
time = 0.0;
velocity = 0;
type = 1;
Truck::~Truck() // deconstructor
}
//===================================================
// COUPE
//===================================================
Coupe::Coupe() // constructor
time = 0.0;
velocity = 0;
type = 2;
Coupe::~Coupe() // deconstructor
//===================================================
// BIKE
//===================================================
Bike::Bike() // constructor
time = 0.0;
velocity = 0;
type = 3;
}
Bike::~Bike() // deconstructor
//===================================================
// MAP
//===================================================
///////////////////////////SETTERS////////////////////////
void Map::setVehicle(Vehicle userV) //setting the first vehicle onto the map
v1 = userV;
void Map::setVehicle1(Vehicle userV2) //setting the seond vehicle onto the map
v2 = userV2;
humanMass = humanM;
humanMass2 = humanM2;
///////////////////////GETTERS///////////////////////////
double Map::getHumanMass2()
return humanMass2;
Vehicle Map::getVehicle()
return v1;
Vehicle Map::getVehicle1()
return v2;
double Map::getHumanMass()
return humanMass;
//////////////////////CAR VS CAR/////////////////////////////
vehicle_1_Mass_KG = vehicle1.getMass();//gets variables from the class Vehicle through the recieved
variables of class Map
vehicle_2_Mass_KG = vehicle2.getMass();
human_1_Mass_KG = getHumanMass();
humanMass2 = getHumanMass2();
time_1_Seconds = vehicle1.getTime();
time_2_Seconds = vehicle2.getTime();
vehicle_1_velocity_MetersPerSecond = vehicle1.getVelocity();
vehicle_2_velocity_MetersPerSecond = vehicle2.getVelocity();
menubar();
menuSpace();
menubar();
cout << "\tForce applied between both cars: " << fixed << setprecision(3) <<
((vehicle_1_Mass_KG)*pow(vehicle_1_velocity_MetersPerSecond,2))/2*(.4) +
((vehicle_1_Mass_KG)*pow(vehicle_1_velocity_MetersPerSecond,2))/2*(.4) << " N";//Equation: Force
Appiled(Newtons) = mass*speed^2/2*stopping distance + mass*speed^2/2*stopping distance
cout <<"\n\tForce Applied to human in vehicle 1: " << fixed << setprecision(3) <<
((human_1_Mass_KG)*pow(vehicle_1_velocity_MetersPerSecond,2))/2*(.03) << " N" <<
endl;//Equation: Force Appiled(Newtons) = mass of human*speed^2/2*stopping distance
cout <<"\tForce Applied to human in vehicle 2: " << fixed << setprecision(3) <<
((humanMass2)*pow(vehicle_2_velocity_MetersPerSecond,2))/2*(.03) << " N" << endl;//Equation:
Force Appiled(Newtons) = mass of human*speed^2/2*stopping distance
menubar();
//////////////////////CAR VS WALL//////////////////////////////
mass = vehicle1.getMass();//gets variables from the class Vehicle through the recieved variables of
class Map
humanMass = getHumanMass();
time = vehicle1.getTime();
speed = vehicle1.getVelocity();
menubar();
menuSpace();
menubar();
cout <<"\n\tForce Applied to human: " << fixed << setprecision(3) <<
((humanMass)*pow(speed,2))/2*(.03)<< " N" << endl;//Equation: Force Appiled(Newtons) = mass of
human*speed^2/2*stopping distance
cout <<"\n\tForce Applied to Wall: " << fixed << setprecision(3) << -(mass*speed)/time << " N" <<
endl;//Force of Impulse = change of momentum/time
menubar();
}
Map CollisonCourse;
vehicle2 = textToVehicle(vehicleFile);
menubar();
menubar();
CollisonCourse.setHumanMass(humanMass);
CollisonCourse.setHumanMass2(humanMass2);
CollisonCourse.headOnCollision(vehicle1, vehicle2);
double passengerMass;
Vehicle vehicle1;
Map CollisonCourse;
menubar();
CollisonCourse.setHumanMass(passengerMass);
CollisonCourse.wallCollision(vehicle1);
int menuChoice;
menubar();
menuSpace();
menubar();
menuSpace();
menubar();
headOn(VehicleFile);
wall(VehicleFile);
//////////////////////////////////////////HEADER////////////////////////////////////////////////
#ifndef CRASH_HEADER
#define CRASH_HEADER
//#include headers
#include <iostream>
#include<fstream>
//===================================================
// MENU /UI
//===================================================
void constructScenario(fstream&);
void editVehiclesInFile(fstream&);
void printVehicleEditMenu();
void menuBar();
void menuSpace();
//===================================================
// VEHICLE CLASS
//===================================================
public:
Vehicle(); //constructor
~Vehicle(); //destructor
//SETTERS
//GETTERS
//saveToFile
};
//===================================================
// TRUCK CLASS
//===================================================
public:
Truck();
~Truck();
};
//===================================================
// COUPE CLASS
//===================================================
public:
Coupe();
~Coupe();
};
//===================================================
// BIKE CLASS
//===================================================
public:
Bike();
~Bike();
};
//===================================================
// MAP
//===================================================
class Map
private:
Vehicle v1;
Vehicle v2;
public:
//setters
void setHumanMass(double);
void setHumanMass2(double);
void setVehicle(Vehicle);
void setVehicle1(Vehicle);
//getters
double getHumanMass();
double getHumanMass2();
Vehicle getVehicle();
Vehicle getVehicle1();
void printMap(fstream&);
void wallCollision(Vehicle);
};
void headOn(fstream&);
void wall(fstream&);
//===================================================
// File Functions
//===================================================
void resetFileValidate(fstream&); // resets fstream position to inital and resets eof bit
Vehicle textToVehicle(fstream&); //takes user's choice-> reads file-> creates vehicle obj and returns the
vehicle
void editVehicle(fstream&);
#endif
Mrs. Lara
Programming Fundamentals 3
2 December 2022
Analysis
In this project Mrs. Lara gave us the opportunity to create our own project. With a couple of
requisites. The following requisites were recursion, pointers, sorting, searching, and inheritance.
Our idea is a car crash simulator. The scenarios are tested with different speeds, objects, masses
and with dummies to find out the damage of the person inside. We created a program where it
would test different vehicles, with different speeds, that have different masses, with different
The way that we developed this project was by splitting into different files of
Implementation, Specification and Client files. The Specification file consists of a parent class
Vehicle with protected variables mass, and variables of data type integer. Class Vehicle has a
public with a constructor, and destructor. This parent class Vehicle has three child classes of
Truck, Coupe, and Bike that all access the protected variables of Class Vehicle. The child class
each consists of public constructors and destructors. The Implementation File carries the
definitions of the functions from the publics of the class. This allows you to enter multiple data
into an array of Objects. The array must dynamically allocate enough space to hold information
as needed. The data is read from a file and use the Object Oriented. We also display inheritance,
The user would be able to even create his own vehicle with its own mass and velocity on
the track. The outcome of the program would vary depending on the user which he would choose
the vehicle and the impact being either another vehicle or a massive wall. After the user inputs
both his vehicle and the types of scenarios that are available the program will run the data and
input it into the different formulas that we must get the information about the crash the formulas
that we have stored are such as Kinetic Energy divided by the stopping distance, Force of
output/input/appending.
● while (exitProgram != true) While exitProgram is not equal to exit stay in program.
● This point is reached only after exitProgram evaluates to true vehicleFile.close(); Close
send it to menuChoiceSelect;
SETTERS
GETTERS
File Functions
● void resetFileValidate(fstream&); Resets fstream position to initial and resets eof bit.
saves it to file.
● Vehicle textToVehicle(fstream&); Takes user's choice-> reads file-> creates vehicle obj
● Void menuChoiceSelect Is for the user to select the vehicle and scenario.
● Void readVehicleFile Is used to read the inputs given from the file.
Collision Formulas
(vehicle_1_Mass_KG) *pow(vehicle_1_velocity_MetersPerSecond,2))/2*(.4) +
mass*speed^2/2*stopping distance.
distance.
(mass*speed)/time << " N" << endl; Force of Impulse = change of momentum/time
● void headOn Access to the text file to gather data to manipulate in the calculation
● void wall (fstream &vehicleFile) Access to the text file to gather data to manipulate in the
calculation.
Void Map
● void Map::setVehicle(Vehicle userV) Setting the first vehicle onto the map
{v1 = userV;
● void Map::setVehicle1(Vehicle userV2) Setting the second vehicle onto the map
{v2 = userV2;
{ humanMass = humanM;
{humanMass2 = humanM2;
● This point is reached only after exitProgram evaluates to true vehicleFile.close(); Close
Professor Lara
COSC-2436
01 December 2022
Project Essay
INTRODUCTION
Important algorithms to use in a database type of program are searching and sorting. You
use searching “for an entry in a database” or to “trawl through a virtual space” (Zaveri. 2018).
You also may use sorting because it “can help to sort database information, customer data, and
financial reports“(Fiverr. 2021). No matter the type of search or sort algorithms being used in the
code, the end goal is that it “best matches the size and structure of the database to provide a user-
friendly experience” (Zaveri. 2018), and “beneficial to your business and the right ones to use
when performing certain tasks that may require sorting larger portions of data” (Fiverr. 2021). In
our project, we found it beneficial to use the searching algorithm to properly search the requested
EXPLANATIONS
element or retrieve an element from any data structure where it is stored” (Geeks. 2021). Our
code uses the searching algorithm to search a text file, using the name of the vehicle to retrieve
the requested appropriate info of the vehicle. Since the data we are requesting is a smaller list
compared to other databases, the search algorithm we use is Linear Search. We understand
Binary Search is more efficient, however since the list is smaller, the need to sort, which is
required in Binary Search, isn’t needed nor worth the data processing. The text file separates the
data using camas to be visually easier to read which data is what and easier for the code to
always read after the camas. The data is stored by vehicle name, type, mass, duration of crash,
initial velocity, and acceleration. The way the user searches the desired car is they are given the
list of custom they imputed, and they type in the number associated with the car and the search
function “returnVehicle” will manually start at the beginning of the text file and assign the info
CONCLUSION
In our project, we found it beneficial to use the searching algorithm to properly search the
requested vehicle to be used in formulas to find the force outputted. We used the searching
algorithm to make it user friendly, so the user doesn't have to manually input every answer every
time they run the program. The program will hold the info to be ready for the next run on the
program. As previously stated, the end goal is that it “best matches the size and structure of the
Fiverr Team. “Sorting Algorithms: What Are They and How to Use Them.” Programming
and-how-to-use-them
Zaveri, Meet. “An intro to Algorithms: Searching and Sorting algorithms.” codeburst.
2018. https://codeburst.io/algorithms-i-searching-and-sorting-algorithms-56497dbaef20
Srivastava, Prashant. “Difference Between Searching and Sorting Algorithms.” Geeks for
algorithms/