Ejemplo n.º 1
0
Archivo: g7e4.c Proyecto: pingicx/cx
/*
Chooses the calculation function wanted. Accepts operand 1, operand 2 and a pointer to the operator.
Returns the value calculated by the operating functions.
*/
float calculate(float n1, float n2, int *op) {
	switch (*op) {									// compares to the value of the pointer to the operator. 
	case '+':
		return plus1(n1,n2);
		break;

	case '-':
		return minus1(n1,n2);
		break;

	case '/':
		return divide1(n1,n2);
		break;

	case '*':
		return multiply1(n1,n2);
		break;

	case '^':
		return xor1(n1,n2);
		break;

	case '|':
		return or1(n1,n2);
		break;		

	case '&':
		return and1(n1,n2);
		break;

	default:
		break;
	}
}
Ejemplo n.º 2
0
SuperNumber<integerType>::SuperNumber(const std::string &value, unsigned short radix) : SuperNumber<integerType>() {
	if (radix > 36) {
		throw std::out_of_range("The radix was " + std::to_string(radix) + ". That is bigger than 36. The maxiumum radix.");
	}
	else if (radix < 2) {
		throw std::out_of_range("The radix was " + std::to_string(radix) + ". That is less than 2. The minimum radix.");
	}

	std::map<char, SuperNumber<integerType>> allowedCharacters;
	SuperNumber<integerType> minus1(-1);

	char i;
	for (i = 0; i < 10; i++) {
		allowedCharacters['0' + i] = i;
	}
	for (i = 10; i < radix; i++) {
		allowedCharacters['7' + i] = i;
		allowedCharacters['W' + i] = i;
	}
	allowedCharacters['.'] = minus1;

	std::map<char, SuperNumber<integerType>>::const_iterator it;
	std::map<char, SuperNumber<integerType>>::const_iterator end(allowedCharacters.end());
	size_t stringSize = value.size();
	bool commaFound = false;
	SuperNumber<integerType> Radix(radix);
	SuperNumber<integerType> Divisor(1);

	for (size_t i = 0; i < stringSize; i++) {
		it = allowedCharacters.find(value[i]);
		if (it != end) {
			if (commaFound) {
				if (it->second == minus1) {
					throw invalid_argument("\"" + value + "\" could not be parsed as a number!");
				}
				Divisor *= Radix;
				*this += it->second / Divisor;
			}
			else {
				if (it->second == minus1) {
					commaFound = true;
				}
				else{
					*this *= Radix;
					*this += it->second;
				}
			}
		}
		else {
			throw invalid_argument("\"" + value + "\" could not be parsed as a number!");
		}
	}
}