Beispiel #1
0
int main() {
    Operation* pOper = OperationFactory::createOperate("/");
    pOper->SetNumberA(3);
    //pOper->SetNumberB(9.8);

    double ret;
    ret = pOper->GetResult();

    cout << "Result: " << ret << endl;
    
    delete pOper;

    return 0;
}
Beispiel #2
0
//将参数中的两个数字和一个运算符进行运算,返回结果
double Calculator::Calculate(double numA, double numB, OperationFactory::OPERATION op)
{
	Operation* pOper = m_opFac.CreateOperation(op, numA, numB);
	//考虑除数为0的情况
	if ( (op == OperationFactory::OPER_DIV) && 
		(numB <= 0.000001 && numB >= -0.000001) )
	{
		m_strErrorInfo = "除数不能为0!";
		return 0.0;
	}

	double rtval = pOper->GetResult();
	delete pOper;
	pOper = NULL;
	return rtval;
}
Beispiel #3
0
int main(int argc, char* argv[])
{
    Operation oper;
    int numA = 0;
    int numB = 0;
    int res = 0;

    numA = atoi(argv[1]);
    numB = atoi(argv[3]);

    if (0 != CreateOperation(&oper, argv[2][0]))
    {
	printf("创建运算失败.\n");
	return -1;
    }
    res = oper.GetResult(numA, numB);
    printf("result is %d.\n", res);
    return 0;
}
Beispiel #4
0
int main(int argc, char* argv[])
{
	int a = 0;
	int b = 0;
	char op = 0;
	std::string line;
	std::getline(std::cin, line);
	std::string op1, op2, op3;
	int max_ops = 3;
	bool b_reading_val = false;
	for (size_t i = 0; i != line.size(); i++) {
		char c = line[i];
		if (c == '\t')
			return 0;
		else if (my_is_digit(c)) {
			b_reading_val = true;
			if (max_ops == 3) {
				op1 += c;
			} else if (max_ops == 2) {
				return 0;
			} else if (max_ops == 1) {
				op3 += c;
			} else if (max_ops == 0) {
				return 0;
			}
		} else if (my_is_operand(c)) {
			
			if (b_reading_val) {
				b_reading_val = false;
				max_ops--;
			}

			if (max_ops != 2) {
				return 0;
			}
			op2 = c;
			max_ops--;
		} else {
			if (b_reading_val) {
				b_reading_val = false;
				max_ops--;
			}
		}
	}
	//line >> a >> op >> b;
	/*int ret = sscanf(line.c_str(), "%d %c %d", &a, &op, &b);
	if (ret == 0 || ret == EOF) {
		return 0;
	}*/
	a = atoi(op1.c_str());
	op = op2.c_str()[0];
	b = atoi(op3.c_str());

	if (!is_valid_value(a) || !is_valid_value(b)) {
		//std::cout << "You must input 2 positive integers." << std::endl;
	} else {
		Operation *operation = OperationFactory::CreateOperation(op);
		if (operation) {
			operation->m_number1 = a;
			operation->m_number2 = b;
			long long result = operation->GetResult();
			std::cout << result << std::endl;
			delete operation;
		} else {
			//std::cout << "Illegal expression." << std::endl;
		}
		
	}
	//getchar();
	return 0;
}