tan dosent在C ++上正常工作

时间:2016-06-07 20:41:18

标签: c++ arithmetic-expressions

我正在制作这个游戏而我遇到了一个问题。那么游戏基本上是两个玩家,计算机会以这种格式问你问题:

  

[数字] [度/弧度]

的[sin / cos / tan]是多少

那么现在我几乎完成了弧度,但由于某种原因切线决定搞砸......当我检查错误时,它告诉我棕褐色(pi)是-1.22465e-16 ......? ?什么tan(pi)实际上是0.它还在cpp.sh中告诉我同样的事情:http://cpp.sh/6m5q3 ..我在dictonary类源文件中的游戏代码是:

#include<vector>
#include "../Header Files/Dictonary.h"

#ifndef PI
    #define PI 3.14159265358979323846  
#endif    

Dictonary::Dictonary()
{
}

int myRand(int low, int high) 
{
   return rand() % (high - low + 1) + low;
}


string Dictonary::randomArray(const string &opt)
{
    short ran = myRand(0,16);
    if(opt.compare("degree") == 0)
        return ( (arrayPossibledegrees[ran]) );
    else
        return ( (arrayPossibleRandians[ran]) );
}

string Dictonary::radOrDegRAND()
{
    return(radDeg[myRand(0,1)]);
}

string Dictonary::getPossibleArethemtic()
{
    return(possibleArethemtic[myRand(0,2)]);
}


double Dictonary::getComputedValue()
{
    string unit = this -> radOrDegRAND();
    string arethemtic = this -> getPossibleArethemtic();
    string userAnswer;
    double userAnwserDoub, AIANWSERDOUB;
    //Ex; pi/2
    string val = randomArray(unit);
    cout << "\n\nWhat is the " << arethemtic <<  " Of " <<val << " " << unit << endl;
    std::cin >> userAnswer;

    checkUserAnwser(userAnswer,userAnwserDoub);


    //VALUE
    if(unit == "radian")
    {
        checkVAL(val,AIANWSERDOUB);
        if(arethemtic.compare("sin") == 0)
        {
             cout << "USER ANWSER : " << userAnwserDoub << endl;
             cout << "The Sin of " << AIANWSERDOUB <<  "Is " << sin(AIANWSERDOUB) << endl;
        }

        else if(arethemtic.compare("cos") == 0)
        {
            cout << "USER ANWSER : " << userAnwserDoub << endl;
            cout << "The Cos of " << AIANWSERDOUB << " Is " << cos(AIANWSERDOUB) << endl;
        }
        else
        {
            cout << "USER ANWSER : " << userAnwserDoub << endl;
            cout << "The Tan of " << AIANWSERDOUB  << " Is " << tan(AIANWSERDOUB) << endl;
        }

    }
}

double Dictonary::conversionPItoREAL(string piNONREAL)
{
    short PIREAL;
    //Conversion "pi/2" to PI/2 MATH FORM
    //"11pi/2" to 11PI/2
    //"3pi/2" to 3PI/2
    if(piNONREAL.find("p"))
    {

    }
}

//Check Anwser
void Dictonary::checkUserAnwser(string &userAnswer, double &userAnwserDoub)
{
    if(userAnswer.compare("s(3)/2"))
        userAnwserDoub = (sqrt(3)/2);
    else if(userAnswer.compare("-s(3)/2"))
        userAnwserDoub = (-(sqrt(3)/2));
    else if(userAnswer.compare("s(2)/2"))
        userAnwserDoub = (sqrt(2)/2);
    else if(userAnswer.compare("-s(2)/2"))  
        userAnwserDoub = (-(sqrt(2)/2));
    else if(userAnswer.compare("-1/2"))
        userAnwserDoub = ((-1)/2);
    else if(userAnswer.compare("1/2"))
        userAnwserDoub = (1/2);
    else if(userAnswer.compare("0"))
        userAnwserDoub = (0);
    else if(userAnswer.compare("-1"))
        userAnwserDoub = (-1);
    else if(userAnswer.compare("1"))
        userAnwserDoub = (1);
    else if(userAnswer.compare("s(3)"))
        userAnwserDoub = (sqrt(3));
    else if(userAnswer.compare("-s(3)"))
        userAnwserDoub = (-(sqrt(3)));
    else if(userAnswer.compare("-s(3)/3"))
        userAnwserDoub = (-((sqrt(3))/3));
    else if(userAnswer.compare("s(3)/3"))
        userAnwserDoub = ( (sqrt(3))/3 );    
    else
    {
        cout << "INVALID NUMBER. EXITING OUT OF PROGRAM " << endl;
        exit(1);
    }
}


void Dictonary::checkVAL(string &val, double &AIANWSERDOUB)
{
    /*
    string arrayPossibleRandians[17] = { "0","pi/6", "pi/4" "pi/3", "pi/2", "2pi/3", "3pi/4", "5pi/6"
        ,"pi", "7pi/6", "5pi/4", "4pi/3", "3pi/2", "5pi/3", "7pi/4"
        ,"11pi/6", "2pi" };
    */
    if(val.compare("0") == 0)
        AIANWSERDOUB = 0;
    else if(val.compare("pi/6") == 0)
        AIANWSERDOUB = (PI/6);
    else if(val.compare("pi/4") == 0)
        AIANWSERDOUB = (PI/4);
    else if(val.compare("pi/3") == 0)
        AIANWSERDOUB = (PI/3);
    else if(val.compare("pi/2") == 0)
        AIANWSERDOUB = (PI/2);
    else if(val.compare("2pi/3") == 0)
        AIANWSERDOUB = ( (2*PI) / 3);
    else if(val.compare("3pi/4") == 0)
        AIANWSERDOUB = ( (3*PI) / 4);
    else if(val.compare("5pi/6") == 0)
        AIANWSERDOUB = ( (5*PI) /6 );
    else if(val.compare("pi") == 0)
        AIANWSERDOUB = PI;
    else if(val.compare("7pi/6") == 0)
        AIANWSERDOUB = ( (7*PI)/6 );
    else if(val.compare("5pi/4") == 0)
        AIANWSERDOUB = ( (5*PI) /4);
    else if(val.compare("4pi/3") == 0)
        AIANWSERDOUB = ( (4*PI) /3);
    else if(val.compare("3pi/2") == 0)
        AIANWSERDOUB = ( (3*PI)/2 );
    else if(val.compare("5pi/3") == 0)
        AIANWSERDOUB = ( (5*PI) /3 );
    else if(val.compare("7pi/4") == 0)
        AIANWSERDOUB = ( (7*PI) /4 );
    else if(val.compare("11pi/6") == 0)
        AIANWSERDOUB = ( (11*PI) /6 );
    else if(val.compare("2pi") == 0)
        AIANWSERDOUB = (2*PI);


}
//degreeToRadians
double Dictonary::d2r(double d)
{
    return(d/180) * ((double) PI);
}

Dictonary::~Dictonary()
{
}

解决我的问题。制作自定义Tan功能:

//AI
double Dictonary::customTan(double convertToTan)
{
    double returned;

    if(convertToTan == 0)
        returned = 0;
    else if(convertToTan == (PI/6))
        returned = (sqrt(3)/3);
    else if(convertToTan == (PI/4))
        returned = 1;
    else if(convertToTan == (PI/3))
        returned = sqrt(3);
    else if(convertToTan == (PI/2))
    {
        cout << "GAME EXITING. UNDEFINED NUMBER " << endl;
        exit(1);
    }

    else if(convertToTan == ((2*PI) / 3))
        returned = -(sqrt(3));
    else if(convertToTan == ((3*PI) / 4))
        returned = -1;
    else if(convertToTan == ((5*PI) /6))    
        returned = (-((sqrt(3))/3));
    else if(convertToTan == PI)
        returned = 0;
    else if(convertToTan == (7*PI)/6)
        returned = ( (sqrt(3))/3 );
    else if( convertToTan == (5*PI) /4 )    
        returned = 1;
    else if((4*PI) /3)
        returned = sqrt(3);
    else if( convertToTan == (3*PI)/2 )
    {
        cout << "GAME EXITING. UNDEFINED NUMBER " << endl;
        exit(1);
    }
    else if(convertToTan == (5*PI) /3 )
        returned = -sqrt(3);
    else if(convertToTan == ( (7*PI) /4))
        returned = -1;
    else if(convertToTan == ((11*PI) /6))
        returned = (-((sqrt(3))/3));
    else if(convertToTan == (2*PI))
        returned = 0;
    else
    {
        cout << "INVALID NUMBER. EXITING OUT OF PROGRAM " << endl;
        exit(1);
    }
    return returned;    
}

0 个答案:

没有答案