C/Visual C - De C++ a C

 
Vista:
sin imagen de perfil

De C++ a C

Publicado por Musiker81 (14 intervenciones) el 09/08/2010 19:14:21
Hola a todos.
Me haríais un grandísimo favor si alguien me pudiera pasar a C este programa hecho en C++. Lo que hace es pedir un número en la base que se desee, y da el resultado en la misma u otra base deseada (sólo en bases de 1 a 9).
Muchas gracias.

string convert(int M, int N)
{
//create a stream used for conversion
std::stringstream out;
//if M > N
if (M < N)
{
//No need to continue: Create a new string with the result
out << M;
return string(out.str());
}
else
{
//Compute the modulo and convert it as string
out << M % N;
return convert(M/N, N) + string(out.str());
}
}
extern "C" int baseConverter(int number, int base, int newBase)
{
//Convert the input number to string
std::stringstream out;
out << number;
string s(out.str());
//convert to base 10
int conversion = 0;
int power = s.length()-1;
for ( unsigned int pos = 0; pos < s.length(); pos++)
{
char digit = s.at(pos);
conversion += atoi(&digit)*(int)pow(base, power);
power--;
}
//convert to the new base
cout << "(" << number << ")" << base << " =" << " (" << convert(conversion, newBase) << ")" << newBase << endl;
return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder