예제 #1
0
파일: main.cpp 프로젝트: eindacor/utilities
bigNumber numberFromVector(vector<int> &vec, bool neg, int dec, settings &user)
{
	bigNumber temp;
	temp.setBase(user.getBase());

	for (int i=0; i<vec.size(); i++)
	{
		int numberToUse = vec.at(vec.size()-i-1);
		int locationToSet = PRECISION + i;
		temp.setDigit(locationToSet, numberToUse);
	}
	
	if (neg)
		temp.setNegative();

	temp.divideByTen(dec);

	return temp;
}
예제 #2
0
파일: main.cpp 프로젝트: eindacor/utilities
bool isNumber(char const &c, settings &user)
{
	return (checkNumber(c) >= 0 && checkNumber(c) <= (user.getBase()-1) );
}
예제 #3
0
파일: main.cpp 프로젝트: eindacor/utilities
solution solve(string &c, bigNumber previous, settings &user)
{
    c += '$';
    PTYPE pType=ERROR;
    
    vector<int> first;
    vector<int> second;
    bigNumber bn1;
    bigNumber bn2;
    bigNumber temp;
    
    bn1.setBase(user.getBase());
    bn2.setBase(user.getBase());
    temp.setBase(user.getBase());

    int decimalCount1=0;
    int decimalCount2=0;
    int commaNumbers=0;
    int numbers=0;

    bool decimal=false;
	bool comma=false;
	bool negative1=false;
	bool negative2=false;
    bool done=false;
	bool printExact=false;
	bool printStats=false;

    bigNumber* targetBN = &bn1;
    vector<int>* targetVec = &first;
    int* targetDec = &decimalCount1;
	bool* targetNegative = &negative1;
    
    int counter = c.size();
    
    for (int i=0; i<counter; i++)
    {
        if (checkWord(c, i, "pi"))
        {
            if (numbers>0 || comma==true || decimal==true)
            {
                RETURN_ERROR;
            }
            
            else
            {
                string piString(PI);
		
				for (int piMarker=PRECISION; piMarker>=0; piMarker--)
				{
					char piChar = '0';
					int piNum = piString[PRECISION-piMarker] - piChar;
				
					(*targetVec).push_back(piNum);
				}
                
                done=true;
				*targetDec = PRECISION;
                
                numbers += (PRECISION+1);
                counter += 2;
                i += 2;
            }
        }
        
        if (checkWord(c, i, "theta"))
        {
            if (numbers>0 || comma==true || decimal==true)
            {
                RETURN_ERROR;
            }
            
            else
            {
                string thetaString(THETA);
		
				for (int thetaMarker=PRECISION; thetaMarker>=0; thetaMarker--)
				{
					char thetaChar = '0';
					int thetaNum = thetaString[PRECISION-thetaMarker] - thetaChar;
				
					(*targetVec).push_back(thetaNum);
				}
                
                done=true;
				*targetDec = PRECISION;
                
                numbers += (PRECISION+1);
                counter += 5;
                i += 5;
            }
        }
        
		//if it isn't a space, number, symbol, end marker, or decimal point, return error
		if (checkSpace(c[i])==false && 
			isNumber(c[i], user)==false && 
			isSymbol(c[i])==false && 
			c[i] != '$' && 
			c[i] != ',' && 
			c[i] != '.')
		{
			RETURN_ERROR;
		}

		if (c[i]==',')
		{
			if (comma==false)
			{
				if (decimal==true || numbers==0 || numbers > 3 || done==true)
					RETURN_ERROR;

				else comma = true;
			}

			else if (commaNumbers != 3)
			    RETURN_ERROR;
			    
			commaNumbers=0;    
		}
			
		//if it's a space
		else if (checkSpace(c[i])==true)
		{
			//if it's preceeded by a number, number is complete
			if (isNumber(c[i-1], user))
		    {
		        done = true;
		    }
		    
		    //if negative is set, and it is preceeded by a minus symbol, return error
		    else if (checkSymbol(c[i-1]) == SUBTRACT && *targetNegative == true)
		    {
		        RETURN_ERROR;
		    }
		}

        //if it's a number
        else if (isNumber(c[i], user))
        {        
            //if number was complete, return error
            if (done==true)
			{
                RETURN_ERROR;
			}
			
		    if (comma==true)
		        commaNumbers++;
                
            //otherwise add number to target vector, add decimal count if needed
            (*targetVec).push_back(checkNumber(c[i]));
                numbers++;
                
            if (decimal==true)
			{
                (*targetDec)++;
			}
        }
        
        //if it's a minus symbol
        else if (checkSymbol(c[i]) == SUBTRACT)
        {
            //if no numbers have been added
            if (numbers==0)
            {
                //if target isn't negative, make target negative
                if ((*targetNegative)==false)
                {
                    (*targetNegative) = true;
                    (*targetBN).setNegative();
                }
                
                //if target is negative, no numbers have been added, and the target is the first number: 
				//there are two minus symbols, which means the intent is to 
				//subtract a negative number from previous
                else if (targetBN == &bn1)
                {
                    pType = SUBTRACT;
                    targetBN = &bn2;
                    numbers=0;
					commaNumbers=0;
					comma=false;
                    bn2.setNegative(); //sets 2nd number to negative
					negative2=true; //sets 2nd number to negative
                    targetNegative= &negative2;
                    done=false;
                    targetDec = &decimalCount2;
                    targetVec = &second;
                    decimal=false;
                }
                
                else 
				{
					RETURN_ERROR;
				}
            }
            
            //if numbers have been added, and the target is the first number, the problem type is subtraction
            else if (targetBN == &bn1)
            {
                if (comma==true && decimal==false && commaNumbers != 3)
                    RETURN_ERROR;
                
                pType = SUBTRACT;
                numbers=0;
				commaNumbers=0;
				comma=false;
                done=false;
                targetBN = &bn2;
				targetNegative= &negative2;
                targetDec = &decimalCount2;
                targetVec = &second;
                decimal=false;
            }
            
            //if numbers have been added, and the target is the second number, return error
            else 
			{
				RETURN_ERROR;
			}
        }
        
        //if it's a symbol other than minus
        else if (checkSymbol(c[i]) != ERROR && checkSymbol(c[i]) != SUBTRACT)
        {
            //if the type is already established, return error
            if (pType != ERROR)
			{
            	RETURN_ERROR;
			}
                
            //otherwise, set problem type based on symbol and reset figures
            else 
            {
                if (comma==true && decimal==false && commaNumbers != 3)
                    RETURN_ERROR;
                
                pType = checkSymbol(c[i]);
                targetBN = &bn2;
                numbers=0;
				commaNumbers=0;
				comma=false;
                done=false;
				targetNegative = &negative2;
                targetDec = &decimalCount2;
                targetVec = &second;
                decimal=false;
            }
        }
        
        //if it's a decimal point
        else if (c[i]=='.')
        {
            //if there's already been a decimal point, return error
            if (decimal==true)
			{
                RETURN_ERROR;
			}
                
            else decimal = true;
            
            if (comma==true && commaNumbers != 3)
                RETURN_ERROR;
        }
        
        //if it's an endline character
        else if (c[i] == '$')
        {
            if (comma==true && decimal==false && commaNumbers != 3)
                    RETURN_ERROR;
                    
			//if both numbers are empty
			if (first.size()==0 && second.size()==0)
			{
				if (pType==FACTORIAL)
				{
					bn1 = previous;
				}

				else RETURN_ERROR;
			}

			//if first number is empty, set bn1 to previous
			if (first.size()==0)
			{
				bn1 = previous;
			}

			//otherwise create bn1 from entered and print it as is
            else
            {   
				bn1 = numberFromVector(first, negative1, decimalCount1, user);
				printExact=true;
            }
            
            //if second number is empty
            if (second.size()==0)
            {               
                //if first number is negative and pType is undefined, subtract number from previous
                if (negative1==true && pType == ERROR)
                {
					bn1.setPositive();
                    temp = previous - bn1;

					cout << "Entered: "; 
					displayNumber(previous, user, false, false);
					cout << " - "; 
					displayNumber(bn1, user, true, false);

					RETURN_OK(temp);
					//return solution(temp, 0);
                }
                
                //if first number isn't negative and no problem type has been declared, return that number
                else if (pType == ERROR)
                {
					cout << "Entered: "; 
					displayNumber(bn1, user, true, false);
					RETURN_OK(bn1);
                    //return solution(bn1, 0);
                }
               
                else if (pType != FACTORIAL)
                {
                    RETURN_ERROR;
                }
            }
                
            //otherwise take ints from second vector and use to set bigNumber2
            else 
            {
                bn2 = numberFromVector(second, negative2, decimalCount2, user);
            }

			cout << "Entered: ";
			displayNumber(bn1, user, printExact, printStats);
			
			break;
        }
    }
    
    //use problem type to calculate solution, return with no errors if valid
    switch(pType)
    {
        case ERROR: RETURN_ERROR;
        
        case ADD: cout << " + "; bn2.printNumber();
            temp = bn1 + bn2;
			RETURN_OK(temp);
        
        case SUBTRACT: cout << " - "; bn2.printNumber();
			temp = bn1 - bn2;
			RETURN_OK(temp);
                
        case MULTIPLY: cout << " * "; bn2.printNumber();
			temp = bn1 * bn2;
			RETURN_OK(temp);
                
        case DIVIDE: cout << " / "; bn2.printNumber();
            if (bn2==0)
            {
                RETURN_ERROR;
            }
            temp = bn1 / bn2;
			RETURN_OK(temp);
                
        case FACTORIAL: 
			if (bn1<0 || bn1.getDecimalCount() > 0)
            {
				RETURN_ERROR;
            }
            
            else if (bn1==0)
            {
				cout << "!";
				bigNumber fact(1);
				fact.setBase(user.getBase());
				RETURN_OK(fact);
            }
            
            temp = bigNumber::factorial(bn1);
			cout << "!";
			RETURN_OK(temp);    
           
        case EXPONENT: cout << "^"; bn2.printNumber();
			temp = bigNumber::exponent(bn1, bn2);
			RETURN_OK(temp);
        
        case ITERATION: cout << "c"; bn2.printNumber();
			temp = bigNumber::iterations(bn1, bn2);
			RETURN_OK(temp);
        
        default: RETURN_ERROR;
    }

}