C ++ Car程序

时间:2014-02-17 22:04:41

标签: c++ function

#include <iostream>;
#include <iomanip>;
#include <string>;
using namespace std;

//here are the functions.  the names are the same as below to make it easier for me and you to read but 
// i fully understand that they do not have to be the same names do to the scopes of the variables

    class car
    {
        public:
        int current;


        void get_Accelerate (int, int);
        int accelerate() {return current;};
        void get_Brake (int, int);
        int brake() {return current;};
        void get_Cruise (int, int);
        int cruise() {return current;};


    };

    double updateIntervalFeetTraveled(double previousSpeed, double currentSpeed) {
         int timeInterval =1;
         double averageSpeed = (previousSpeed + currentSpeed)/2;
         double averageSpeed_FeetPerSecond = averageSpeed * 5280.0/3600.0;
         double intervalFeetTraveled = averageSpeed_FeetPerSecond * timeInterval;


        return intervalFeetTraveled;
    }
    void car::get_Accelerate (int previousSpeed, int speed) {
        if((previousSpeed + speed) > 125){
            current = 125;
        }else{

         current = previousSpeed + speed;
        }


    }
    void car::get_Cruise (int previousSpeed, int speed) {

        current = previousSpeed;

    }
    void car::get_Brake (int previousSpeed, int speed) {
    if((previousSpeed - speed) >=0){
         current = previousSpeed - speed;
    }
    else{
        current = 0;
    }


    }
        int previousSpeed =0;
        int currentSpeed=0;
        int speedInt=0;
        double intevalFeet=0;
        double totalFeet =0;
        string function, currentState, command;
        car car1;
void demoFunction(string x, int speedInt){      



        if(x.compare("a") == 0) {
            car1.get_Accelerate(previousSpeed, speedInt);
            currentSpeed = car1.accelerate();
            intevalFeet = updateIntervalFeetTraveled(previousSpeed, currentSpeed);
            totalFeet = totalFeet + intevalFeet;
            function="accelerate";
            if(currentSpeed>previousSpeed){
                    currentState = "Accelerating";
                }else if (currentSpeed == 0){
                    currentState="Stopped";
                }else if(currentSpeed == previousSpeed){
                    currentState="Cruising";
                }else if(currentSpeed<previousSpeed){
                    currentState="Braking";
                }
            previousSpeed = currentSpeed;
        }else if(x.compare( "c") == 0){
            car1.get_Cruise(previousSpeed, speedInt);
            currentSpeed = car1.cruise();
            intevalFeet = updateIntervalFeetTraveled(previousSpeed, currentSpeed);
            totalFeet = totalFeet + intevalFeet;
            function="cruise";
            if(currentSpeed>previousSpeed){
                    currentState = "Accelerating";
                }else if (currentSpeed == 0){
                    currentState="Stopped";
                }else if(currentSpeed == previousSpeed){
                    currentState="Cruising";
                }else if(currentSpeed<previousSpeed){
                    currentState="Braking";
                }
            previousSpeed = currentSpeed;

        }else if(x.compare("b") == 0) {

            if(currentSpeed>0){
                car1.get_Brake(previousSpeed, speedInt);
                currentSpeed = car1.brake();
                intevalFeet = updateIntervalFeetTraveled(previousSpeed, currentSpeed);
                totalFeet = totalFeet + intevalFeet;
                function="brake";
                if(currentSpeed>previousSpeed){
                    currentState = "Accelerating";
                }else if (currentSpeed == 0){
                    currentState="Stopped";
                }else if(currentSpeed == previousSpeed){
                    currentState="Cruising";
                }else if(currentSpeed<previousSpeed){
                    currentState="Braking";
                }
                previousSpeed=currentSpeed;
            }else{
                car1.get_Cruise(previousSpeed, speedInt);
                currentSpeed = car1.cruise();
                intevalFeet = updateIntervalFeetTraveled(previousSpeed, currentSpeed);
                totalFeet = totalFeet + intevalFeet;
                function="brake";
                if(currentSpeed>previousSpeed){
                    currentState = "Accelerating";
                }else if (currentSpeed == 0){
                    currentState="Stopped";
                }else if(currentSpeed == previousSpeed){
                    currentState="Cruising";
                }else if(currentSpeed<previousSpeed){
                    currentState="Braking";
                }
                previousSpeed=currentSpeed;



            }


        }

        if(command != "d"){
        cout<<setw(5)<< "Function"<<setw(15)<<"Current State"<<setw(15)<<"Current Speed"<<setw(15)<<"Interval Distance"<<setw(15)<<"Total feet traveled"<<endl;
        }
        cout<<setw(10)<<function<<setw(15)<<currentState<<setw(15)<<currentSpeed<<setw(15)<<intevalFeet<<setw(15)<<totalFeet<<endl;
}
int main(){

string demo[] = {"c","c","c","a","a","a","a","c","c","c","c","b","b","b","b","b","b","b","b"};


cout << "2013 Ford Fiesta.  119 hp, 1.4 liter engine.  the fastest car around.....that a 22 year old can afford."<<endl;
cout<<""<<endl;
cout<< fixed<<setprecision(2)<<showpoint;
cout<<" "<<endl;
cout<<endl;

while (true){


cout<<" "<<endl;
cout<< "Enter single-letter command (or 'h' for help): "<<endl;
getline(cin, command);

    if( command == "h") {
        //help command
        cout << "Supported commands: \n"
                << "        a       Accelerate the car\n"
                << "        h       print this help text.\n"
                << "        b       brake the car\n"
                << "        c       Cruise the car\n"
                << "        q       Exit the program\n"
                << "        d       Demo program\n"
                << endl;
    }else if(command =="q"){
        //command to quit the program
        cout<<"Exit.";
        return 0;
    }else if(command == "a"){
        //command to accelerate the car
        cout<<"Enter a positive integer to accelerate by"<<endl;
        cin >> speedInt;
        cin.ignore();
        if(!cin){
            cin.clear();
            cout<<"You have entered a non numeric character"<<endl;
        }else{
            if(speedInt > 0){
                    demoFunction("a", speedInt);
            }else{
                cout<<"you have entered a negative number or a 0.  Please try again"<<endl;
            }

        }
    }else if(command =="b"){
        //command to brake the car
        cout<<"Enter a positive integer to decelerate by"<<endl;
        cin >>speedInt;
        cin.ignore();
        if(!cin){
            cin.clear();
            cout<<"You have entered a non numeric characer"<<endl;
        }else{
            if(speedInt > 0){
                if(speedInt <=currentSpeed){
                    demoFunction("b", speedInt);
                }else{
                    speedInt = currentSpeed;
                    demoFunction("b", speedInt);
                }
            }else{
                cout<<"you have entered a negative number or a 0.  Please try again"<<endl;
            }
        }
    }else if(command =="c"){
        //command to cruise the car
        demoFunction("c", 0);


    }else if(command == "d"){
        cout<<setw(10)<< "Function"<<setw(15)<<"Current State"<<setw(15)<<"Current Speed"<<setw(15)<<"Interval Distance"<<setw(15)<<"Total feet traveled"<<endl;


            int size_of_Demo = sizeof( demo ) / sizeof( demo[ 0 ] );
            for(int i=0; i<size_of_Demo;i++){
            string x = demo[i];
            demoFunction(x,5);


            }


    }else{

        cout<<"That is not a proper input command, please try again.  press 'h' for help"<<endl;

    }

}
return 0;

}

我必须采取我迄今为止所做的工作并制造和加速/制动/巡航功能。我需要这样做,所以我可以制作一个演示命令,将这些函数调用一段预定的时间。我将使用“d”命令来运行演示。我不确定如何创建我可以调用的函数,而不是手动输入,就像我为'a','b'和'c'的输入选项所做的那样。

你们帮了很多忙。这就是我现在所做的工作。我唯一的问题是如果为速度输入非数字编号,如何使程序不崩溃。我试图尝试捕捉工作,但我还没有完全弄清楚如何正确地做到这一点。

这是问题的最终答案。谢谢您的帮助。如果有人能将问题重新提升到0,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

建议使用属性currentSpeed,previousSpeed等创建Car类。将此类所需的函数添加到此类中,如acceleration(double amt),brake(double amt)等。您已经编写了这些函数。然后

main()
{
    Car car;  // creates a stopped car
...
while (true)
{
    ...
    if (command == "a") car.accelerate(5);
    else if (command == "b") car.brake(5);
etc. etc.

我认为这将为您提供更清晰,更容易修改的解决方案。