Write a function: void introduction(istream &is, ostream &os, string target, str
ID: 662758 • Letter: W
Question
Write a function: void introduction(istream &is, ostream &os, string target, stringreplacement);
That provides instructions for the user. Note that a user who has played the game previously may not wish to see the instructions, and so should be allowed todecline the opportunity to see them. The function should read text from the inputstream is and write the instructions to the output stream os. In the course of writing text to the output stream, any occurrences of the string specified by the target parameter are to be replaced with the string specified by the replacementparameter.
For your testing, to create a set of instructions like those shown in the sample run, you can use the file input.txt and set the target to "$SPACECRAFT" and thereplacement to "APOLLO".
The input.txt file contents are as follows:
LUNAR LANDER INSTRUCTIONS
You are landing on the moon and have taken over manual control 1000 feet above a good landing spot. You have a downward velocity of 50 feet/sec. 150 units of fuel remain.
Here are the rules that govern your $SPACECRAFT space-craft:
Explanation / Answer
# include<iostream.h>
# include<string.h>
# include<cmath>
using namespace std;
string lowercase (sring s);
bool getYesNoResponse(string question);
void instructions(void);
void updateStatus(double &velocity, double &burnAmount, double &fuelremaining, double &height);
void touchdown(double &velocity, double &burnAmount, double &fuelremaining, double &height, double &delta);
void finalAnalysis(int elapsedTime, double delta, double velocity, double fuelremaining);
int main(void)
{
int elapsedTime=0; double height= 1000; double velocity = 50; double fuelRemaining= 150; double burnAmount=0; cout<< "LUNAR LANDER" <<endl; cout << "By Dave Ahl ; cout << "Adapted to C++";
instructions();
while(height>0) {
cout<< endl; cout << "Status Of your APOLLO Spacecraft" <<endl; cout << "Time: " << elapsedTime << endl; cout<< "Height : " << height <<endl; cout << "Speed" << velocity << endl; cout<< "Fuel : " << fuelRemaining <<endl;
If (fuelRemaining > 0) { cout<< "Enter fuel burn amount : " ; cin>> burnAmount; if(burnAmount<0) burnAmount=0; if(burnAmount>30) burnAmount=30; if(burnAmount > fuelRemaining) burnAmount= fuelRemaining;
}
else
{ cout<< "Out Of Fuel"; burnAmount=0; }
updateStatus(velocity, burnAmount,fuelRemaining, height); elapsedTime++; }
double delta; updateStatus(velocity, burnAmount, fuelRemaining, height, delta); elapsedTime--;
finalAnalysis(elapsedTime, delta, velocity, fuelRemaining); return 0; }
string lowercase(string s) { for(unsigned int i=0; i<s.size(); i++ ) if(s[i]>='A' && s[i]<= 'Z') s[i] += 'a' - 'A' ; return s; }
bool getYesNoResponse (string question) { bool done; string response; cout<< question << " "; do {
getline(cin, response); response = lowerCase(response); done= response== "y" || response == "yes" || response == "n" || response == "no" ; if(! done) cout << "Please answer 'y' or 'n' : "; } while (! done) ; return(response == "y" || response == "yes" ) ; }
void instructions(void)
{ bool wantInstructions = getYesNoResponse("Do you want instructions: (y/n)? "); if(! wantInstructions) return;
cout<< "You are landing on the moon and have taken over " << endl; cout << "manual control 1000 feet above a good landing spot" << endl; cout << "You have a downward velocity of 50 feet/sec. 150 units of fuel remain" <<endl; }
void updateStatus(double &velocity, double &burnAmount, double &fuelRemaining, double &height )
{ double newVelocity = velocity - burnAmount + 5; fuelRemaining = fuelRemaining - burnAmount ; height = height - (velocity + newVelocity ) * 0.5; velocity = newVelocity ; }
void touchdown(double &velocity, double &burnAmount, double &fuelRemaining, double &height, double &delta )
{ double newVelocity = velocity ; velocity= velocity - 5 + burnAmount; height = height + (velocity + newVelocity ) * 0.5;
if(burnAmount == 5) delta = height/velocity; else delta= (sqrt(velocity * velocity + height * (10 - burnAmount * 2) ) - velocity )/ ( 5 - burnAmount) ;
newVelocity= velocity + (5- burnAmount) * delta;
velocity = newVelocity; }
void finalAnalysis( int elapsedTime, double delta, double velocity, double fuelRemaining )
{
cout << "Touch Down At : " << elapsedTime + delta << "seconds" << endl ; cout<< "landing velocity : " << velocity << endl; cout << fuelRemaining << "units of fuel remaining" <<endl;
if(velocity<=0) { cout<< "Congrats !! A perfect landing , your license will be renewed"; }
else if(velocity<2){cout<< "A litle bumpy";}
else if (velocity <5) { cout << "you blew it !! your family will be notified" ;}
else if(velocity<10){ cout << "your ship is heap of junk !! your family will be notified" ; }
else if (velocity <30) { cout << "you blasted a huge crater !! your family will be notified" ; }
else if (velocity <50) { cout << "your ship is wreck !! your family will be notified" ;}
else { cout << "you totaled an entire mountain !! your family will be notified" ;}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.