Advertisement
nRikee

String Calculator (2)

Jul 10th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. /** Realiza la suma de una cadena de números dados en formato string, separados entre comas. */
  7. int Add(string);
  8. /** Devuelve el número de divisiones */
  9. int numPartes(string);
  10.  
  11. /**
  12.   * Función main de prueba
  13.   */
  14. int main()
  15. {
  16.     cout << "Prueba Add(): " << Add("") << endl << endl;
  17.     cout << "Prueba Add( 2 ): " << Add("2") <<endl << endl;
  18.     cout << "Prueba Add( 8347 ): " << Add("8347") <<endl << endl;
  19.     cout << "Prueba Add( 5,4 ): " << Add("5,4") <<endl << endl;
  20.     cout << "Prueba Add( 5,5,5,5,4,100 ): " << Add("5,5,5,4,5,100") << endl << endl;
  21.     cout << "Prueba Add( 1,2,3,4,5,6,7,8,9,10,1000 ): " << Add("1,2,3,4,5,6,7,8,9,10,1000") << endl << endl;
  22.  
  23.     return 0;
  24. }
  25.  
  26. /**
  27.   * Realiza la suma de una cadena de números dados en formato string, separados entre comas.
  28.   */
  29. int Add(string numeros){
  30.     int NUMERO_PARTICIONES = numPartes(numeros); // ¿Cuántos números hay en la cadena?
  31.     string parte[NUMERO_PARTICIONES];
  32.     string auxiliar;
  33.     int aux = 0;
  34.     int pos, resultado = 0;
  35.  
  36.     /** Case para 'n' != 0 y 1 */
  37.     if(numeros.find(',')!=-1){
  38.         parte[0]=numeros;
  39.         for( int a=1; aux!=-1; a++ ){
  40.             aux=0;
  41.             auxiliar=parte[a-1];
  42.             pos = auxiliar.find(',');
  43.             parte[a-1] = auxiliar.substr(aux,pos); // Parte dividida entre aux=0 y pos=posicionDeLaComa
  44.             parte[a] = auxiliar.substr(pos+1,numeros.length()); // El resto de la cadena (desde pos+1 hasta el final)
  45.             aux = parte[a].find(','); // -1 si no encuentra ','
  46.         }
  47.     }
  48.     /** Case un solo numero */
  49.     if(numeros.find(',')==-1 || numeros.length()==1){ // Si no hay ninguna coma, o solo tiene un valor
  50.         parte[0]=numeros;
  51.         for(int a=1;a<NUMERO_PARTICIONES;a++) parte[a]="";
  52.     }
  53.     /** Case ningún número */
  54.     if(numeros.length()==0){
  55.         return 0;
  56.     }
  57.  
  58.     /** Conversión de strings a enteros y suma */
  59.     for(int a=0; a<NUMERO_PARTICIONES; a++){
  60.         std::stringstream(parte[a]) >> aux; // Convierte la string en un int.
  61.         resultado += aux; // Se suma al resultado
  62.     }
  63.     return resultado;
  64. }
  65.  
  66. /**
  67.   *  Devuelve el número de divisiones
  68.   */
  69. int numPartes(string numeros){
  70.     int aux, resultado = 0;
  71.     string cortada = numeros;
  72.     aux = cortada.find(',');
  73.  
  74.     while(aux!=-1){
  75.         aux = cortada.find(',');
  76.         cortada = cortada.substr(aux+1,cortada.length());
  77.         resultado++;
  78.         aux = cortada.find(',');
  79.     }
  80.  
  81.     return resultado+1;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement