Esempio n. 1
0
/* validates input and set hand size and number of hands */
int validInput(int argc, char *argv[], int *handSize, int *nhands){
    
    int valid = FALSE;
    
    /* if got right number of arguments, validate their values */
    if (argc == ARGS_COUNT){
        
        int hsize;   /* hand size */
        int numHands;  /* number of hands */
        
        /* check if hand size and number of hands are valid */
        int validHandSize = ((hsize = stringToNumber(argv[1])) <= MAX_HAND ) && hsize >= MIN_HAND;
        int validNumHands = ((numHands = stringToNumber(argv[2])) <= MAX_NUM_HANDS) && numHands >= MIN_NUM_HANDS;
        
        if (validHandSize && validNumHands && (hsize * numHands) <= DECK_SIZE) {
            *handSize = hsize;
            *nhands = numHands;
            valid = TRUE;
        }
        else if (!validHandSize){
            /* feedback on hand size */
            printf("Hand size should be a number between %d and %d\n", MIN_HAND, MAX_HAND);
        }
        else if(!validNumHands){
            /* feedback on number of hands */
            printf("Number of hands should be a number between %d and %d\n", MIN_NUM_HANDS, MAX_NUM_HANDS);
        }
        else{
            /* feedback on number of cards in the deck */
            printf("The deck contains only %d cards\n", DECK_SIZE);
        }
    }
    
    return valid;
}
Esempio n. 2
0
CubicSpline::CubicSpline(std::string filePath)
{
	std::ifstream inputStream(filePath, std::ios::in);
	std::string inputArguments;
	std::string inputResults;
	getline(inputStream, inputArguments);
	getline(inputStream, inputResults);
	inputStream.close();
	for (unsigned int argumentIterator = 0, splineIterator = 0; argumentIterator < inputArguments.length();)
	{
		if (argumentIterator == inputArguments.length() - 1)
		{
			splines_.push_back(emptySpline());
			splines_[splineIterator].argumentValue = stringToNumber(inputArguments);
			splineIterator++;
		}	
		if (inputArguments[argumentIterator] == ' ')
		{
			splines_.push_back(emptySpline());
			splines_[splineIterator].argumentValue = stringToNumber(inputArguments.substr(0, argumentIterator));
			splineIterator++;
			inputArguments = inputArguments.substr(argumentIterator + 1);
			argumentIterator = 0;
		}
		else
			argumentIterator++;
	}
	for (unsigned int resultIterator = 0, splineIterator = 0; resultIterator < inputResults.length();)
	{
		if (resultIterator == inputResults.length() - 1)
		{
			splines_[splineIterator].freeMember = stringToNumber(inputResults);
			splineIterator++;
		}
			
		if (inputResults[resultIterator] == ' ')
		{
			splines_[splineIterator].freeMember = stringToNumber(inputResults.substr(0, resultIterator));
			splineIterator++;
			inputResults = inputResults.substr(resultIterator + 1);
			resultIterator = 0;
		}
		else
			resultIterator++;
	}
	splines_[0].secondFactor = 0;

	tridiagonalMatrixAlgorithm();

	for (unsigned int iterator = splines_.size() - 1; iterator > 0; iterator--)
	{
		splines_[iterator].thirdFactor = (splines_[iterator].secondFactor - splines_[iterator - 1].secondFactor) / argumentDifference(iterator);

		splines_[iterator].firstFactor = argumentDifference(iterator) * (2 * splines_[iterator].secondFactor + splines_[iterator - 1].secondFactor) / 6 + 
										(splines_[iterator].freeMember - splines_[iterator - 1].freeMember) / argumentDifference(iterator);
	}

}
Esempio n. 3
0
void getResult(char input[],double *output)
{
	char *first,*second;
	int index;
	double number1,number2;
	for(index=0;input[index]!=',';index++);
	first = (char *)malloc(index*sizeof(char));
	second = (char *)malloc((inputlen(input)-index)*sizeof(char));
	stringSeperate(input,first,second,index);
	number1=stringToNumber(first);
	number2=stringToNumber(second);
	if(number1>number2)
		*output = number1;
	else
		*output = number2;
}
static int minFromOptArg(char *const arg)
{
	const std::string convertedArg(arg);
	int min;
	if (convertedArg.compare("min") == 0) {
		if (Log::isDebug()) {
			std::cout << "[Debug] 'min' converted to 0"
				<< std::endl;
		}
		min = 0;
	} else if (convertedArg.compare("max") == 0) {
		if (Log::isDebug()) {
			std::cout << "[Debug] 'max' converted to 99"
				<< std::endl;
		}
		min = 99;
	} else {
		if (Log::isDebug()) {
			std::cout << "[Debug] input converted to number"
				<< std::endl;
		}
		min = stringToNumber(convertedArg);
	}
	return min;
}
Esempio n. 5
0
int main() {
  char s[200];
  bigNumber b;
  int count=1;
  while (scanf(" %s", s) ==1) {
    stringToNumber(s, b);
    printf("Instancia %d\n%d\n\n", count, factorial(b));
    count++;
  }
  return 0;
}
Esempio n. 6
0
Real64 JSON::as_real() const
{
	switch (m_type)
	{
	case e_null: return 0;
	case e_bool: return (m_bool ? 1 : 0);
	case e_integer: return Real64(m_integer);
	case e_real: return m_real;
	case e_string: return stringToNumber(m_string).as_real();
	case e_array: return arrayToNumber(m_array).as_real();
	case e_object: return objectToNumber(m_object).as_real();
	}
	return 0;
}
Esempio n. 7
0
bool JSON::as_bool() const
{
	switch (m_type)
	{
	case e_null: return false;
	case e_bool: return m_bool;
	case e_integer: return as_integer() != 0;
	case e_real: return as_real() != Real64(0);
	case e_string: return stringToNumber(m_string).as_bool();
	case e_array: return arrayToNumber(m_array).as_bool();
	case e_object: return objectToNumber(m_object).as_bool();
	}
	return 0;
}
Esempio n. 8
0
time_t  
Convert::stringToDateTime(const string &date_time_string) 
throw(Convert::Exception) {
    if (date_time_string.size() != 10  &&  date_time_string.size() != 19)
        throw Exception(EXCEPTION_CONVERT_BAD_STRING, "Wrong date string '" + date_time_string + "'");

    tm      tm_;
    time_t  t    = 0;

    /*
     *  пример:
     *  date=2010-01-01
     */
    tm_.tm_year = stringToNumber(date_time_string.substr( 0, 4), 10) - 1900;
    tm_.tm_mon  = stringToNumber(date_time_string.substr( 5, 2), 10) - 1;
    tm_.tm_mday = stringToNumber(date_time_string.substr( 8, 2), 10);
    if (date_time_string.size() == 19) {
        tm_.tm_sec  = stringToNumber(date_time_string.substr(18, 2), 10);
        tm_.tm_min  = stringToNumber(date_time_string.substr(15, 2), 10);
        tm_.tm_hour = stringToNumber(date_time_string.substr(11, 2), 10);
    } else {
        tm_.tm_sec  = 0;
        tm_.tm_min  = 0;
        tm_.tm_hour = 0;
    }

    try {
        /* get current timeinfo and modify it to the user's choice */
        //time(&t);
        tm_ = *localtime(&t);
        /* call mktime: timeinfo->tm_wday will be set */
        mktime(&tm_);
    } catch(...) {
        throw Exception(EXCEPTION_CONVERT_BAD_DATE_TIME, "Wrong date time");
    }

    return t;
}
Esempio n. 9
0
void issueCommand(string line, Database* db)
{
	cout << line << endl;
	cout << "********************" << endl;
	string command = line.substr(0, 2);
	if (command == "DP")
	{
		KeyType key2(line.substr(3));
		db->DeletePrimary(key2);
	}
	else if (command == "DS")
	{
		KeyType primeKey(line.substr(3, 7));
		KeyType secondaryKey(stringToNumber(line.substr(11, 2)));
		char type = line.at(14);
		db->DeleteSecondary(primeKey, secondaryKey, type);
	}
	else if (command == "DE")
	{
		KeyType key2(line.substr(3));
		db->Delete(key2);
	}
	else
	{
		char indexType = line.at(3);

		if (command == "FI")
		{
			KeyType key2;

			if (indexType == 'S')
			{
				key2.setKey1(line.substr(5));
			}
			else
			{
				key2.setKey2(stringToNumber(line.substr(5)));
			}
			db->Find(key2, indexType);
		}
		else if (command == "FR") {
			// FR S 9600000 9630000
			// FR A 18 22
			KeyType key_low, key_high;
			int first_end = line.find(' ');
			int second_start = first_end + 1;
			int second_end = line.find(' ', second_start);
			int third_start = second_end + 1;
			int third_end = line.find(' ', third_start);
			int fourth_start = third_end + 1;
			if (indexType == 'S') {
				key_low.setKey1(line.substr(third_start, third_end - third_start));
				key_high.setKey1(line.substr(fourth_start));
				db->FindRange(key_low, key_high, 'S');
			}
			else {
				key_low.setKey2(stringToNumber(line.substr(third_start, third_end - third_start)));
				key_high.setKey2(stringToNumber(line.substr(fourth_start)));
				db->FindRange(key_low, key_high, 'A');
			}
		}
	}
	cout << endl << endl << endl;
}
number_t* calculator::calculate( const std::string& expression, const bool debug)
{
    std::stringstream postfix(expression);
    std::string s;

    try
    {
        while ( postfix >> s )
        {
            number_t* n = stringToNumber(s);

            if( n != nullptr )
                stack_ << n;

            else
            {
                number_t* el1 = nullptr;
                number_t* el2 = nullptr;

                stack_ >> el2; /* Right */
                stack_ >> el1; /* Left  */

                if (debug)
                {
                    std::cout << "( ";
                    el1->who();
                    std::cout << " : ";
                    el1->toStream();
                    std::cout << " ) " << s[0] << " ( ";
                    el2->who();
                    std::cout << " : ";
                    el2->toStream();
                    std::cout << " )" << std::endl;
                }

                try
                {
                    switch( s[0] )
                    {
                        case '+': stack_ << (*el1 + *el2); break;
                        case '-': stack_ << (*el1 - *el2); break;
                        case '*': stack_ << (*el1 * *el2); break;
                        case '/': stack_ << (*el1 / *el2); break;
                        default: throw badSyntaxException(s);
                    }
                }
                catch (const zeroDivisionException &e)
                {
                    std::string response;
                    std::cout << e.what() << "\nDivide by 1 ? [y/n]: ";
                    std::getline(std::cin, response);
                    if (response.size()==1 && (response[0] == 'y' || response[0] == 'Y'))
                        stack_ << (*el1 / integer_t(1));
                    else
                        return nullptr;
                }

                if (el1 != nullptr)
                    delete el1;
                if (el2 != nullptr)
                    delete el2;
            }
        }

        if (stack_.size() > 1)
            throw notEmptyStack();

        return stack_.pop();
    }
    catch (const badPos &e)
    {
        std::cerr << "Internal stack_ error: " << e.what() << std::endl;
    }
    catch (const emptyStack &e)
    {
        std::cerr << "Syntax error (" << e.what() << ")" << std::endl;
    }
    catch (const notEmptyStack &e)
    {
        std::cerr << e.what() << std::endl;
        while (stack_.size() > 1)
            delete stack_.pop();
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << std::endl;
    }

    return nullptr;
}
Esempio n. 11
0
Number Parser::number()
{
	Number result = stringToNumber(m_lexer->lexeme().value);
	m_lexer->nextLexeme();
	return result;
}