callResultType numberToString_func(parameterType param, sptrObject& obj){
    if (param.size() > 2){
        std::ostringstream os;
        os << "number->string. need 1 or 2, given " << param.size();
        return std::make_pair(Interp::EVAL_PARAMETER_NUMBER_MISMATCH, os.str());
    }

    int default_radix = 10;
    if (param.size() == 2){
        if (! static_cast<ObjectDef::Number*>(param[1].get())->getExactness()){
            return std::make_pair(Interp::EVAL_CONTRACTION_VIOLATION, "number->string. Position 1, need exact integer, given inexact integer");
        }

        if (ObjectDef::equal(param[1], getTen())){
            default_radix = 10;
        }
        else if (ObjectDef::equal(param[1], getSixteen())){
            default_radix = 16;
        }
        else if (ObjectDef::equal(param[1], getEight())){
            default_radix = 8;
        }
        else if (ObjectDef::equal(param[1], getTwo())){
            default_radix = 2;
        }
        else {
            std::ostringstream os;
            os << "number->string. Position, need 2, 8, 10 or 16, given" << param[1]->getExternalRep();
            return std::make_pair(Interp::EVAL_CONTRACTION_VIOLATION, os.str());
        }
    }

    if (default_radix != 10){
        if (param[0]->getType() == ObjectDef::ObjectType::COMPLEX){
            if (static_cast<ObjectDef::Complex*>(param[0].get())->getReal()->getType() == ObjectDef::ObjectType::REAL ||
                static_cast<ObjectDef::Complex*>(param[0].get())->getImag()->getType() == ObjectDef::ObjectType::REAL){
                return std::make_pair(Interp::EVAL_CONTRACTION_VIOLATION, "number->string. base has to be 10 to convert a complex containing real part");
            }
        }
        if (param[0]->getType() == ObjectDef::ObjectType::REAL){
            return std::make_pair(Interp::EVAL_CONTRACTION_VIOLATION, "number->string. base has to be 10 to convert a real");
        }
    }

    obj = sptrObject(new ObjectDef::String(numberToString_impl(param[0], default_radix), true, false));

	return std::make_pair(Interp::EVAL_NO_ERROR, "");
}
callResultType stringToNumber_func(parameterType param, sptrObject& obj){
    if (param.size() > 2){
        std::ostringstream os;
        os << "string->number. need 1 or 2, given " << param.size();
        return std::make_pair(Interp::EVAL_PARAMETER_NUMBER_MISMATCH, os.str());
    }

    int default_radix = 10;
    if (param.size() == 2){
        if (! static_cast<ObjectDef::Number*>(param[1].get())->getExactness()){
            return std::make_pair(Interp::EVAL_CONTRACTION_VIOLATION, "string->number. Position 1, need exact integer, given inexact integer");
        }

        if (ObjectDef::equal(param[1], getTen())){
            default_radix = 10;
        }
        else if (ObjectDef::equal(param[1], getSixteen())){
            default_radix = 16;
        }
        else if (ObjectDef::equal(param[1], getEight())){
            default_radix = 8;
        }
        else if (ObjectDef::equal(param[1], getTwo())){
            default_radix = 2;
        }
        else {
            std::ostringstream os;
            os << "string->number. Position, need 2, 8, 10 or 16, given" << param[1]->getExternalRep();
            return std::make_pair(Interp::EVAL_CONTRACTION_VIOLATION, os.str());
        }
    }

    int ret = ParserEnv::Parser::parseNumber(obj, static_cast<ObjectDef::String*>(param[0].get())->getInterpretedString(), default_radix);
	if (ret != ParserEnv::PARSE_NORMAL_EXIT){
        return std::make_pair(Interp::EVAL_INVALID_NUMBER, static_cast<ObjectDef::String*>(param[0].get())->getInterpretedString());
	}

	return std::make_pair(Interp::EVAL_NO_ERROR, "");
}
Esempio n. 3
0
 string getHundred(int num) {
     string HUNDRED = "C", FIVEHUNDRED = "D", THOUSAND = "M", result = "";
     if (num >= 900) {
         result = HUNDRED + THOUSAND;
     }
     else if ( num < 900 && num >= 500) {
         result = FIVEHUNDRED;
         while (num >= 600) {
             result += HUNDRED;
             num -= 100;
         }
     }
     else if (num >= 400) {
         result = HUNDRED + FIVEHUNDRED;
     }
     else if (num >= 100) {
         while (num >= 100) {
             result += HUNDRED;
             num -= 100;
         }
     }
     return result + getTen(num % 100);
 }