Advertisement
nRikee

String Calculator (1)

Jul 10th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. /**
  2.   * Kata String Calculator, hasta el apartado 1.
  3.   * URL del problema: http://www.solveet.com/exercises/Kata-String-Calculator/8
  4.   */
  5.  
  6. #include <iostream>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. int Add(string);
  12.  
  13. int main()
  14. {
  15.     cout << "Prueba Add(): " << Add("") <<endl;
  16.     cout << "Prueba Add( 1 ): " << Add("1") <<endl;
  17.     cout << "Prueba Add( 1,2 ): " << Add("1,2") <<endl;
  18.  
  19.     return 0;
  20. }
  21.  
  22. int Add(string numeros){
  23.     int NUMERO_PARTICIONES = 3;
  24.     string parte[NUMERO_PARTICIONES];
  25.     int aux = 0;
  26.     int pos, resultado;
  27.  
  28.     if(numeros.length()!=1){
  29.         for( int a=1; aux!=-1; a++ ){
  30.             if(aux!=0) aux++;
  31.             pos = numeros.find(',');
  32.             parte[a-1] = numeros.substr(aux,pos);
  33.             parte[a] = numeros.substr(pos+1,numeros.length());
  34.             aux = parte[a].find(',');
  35.         }
  36.     }
  37.     else{
  38.         parte[0]=numeros;
  39.         for(int a=1;a<NUMERO_PARTICIONES;a++) parte[a]="";
  40.     }
  41.  
  42.  
  43.     if(numeros.length()==0) resultado=0;
  44.     else{
  45.         for(int a=0; a<NUMERO_PARTICIONES; a++){
  46.             if(parte[a].compare("")==0) aux = 0;
  47.             else std::stringstream(parte[a]) >> aux;
  48.             resultado += aux;
  49.         }
  50.     }
  51.  
  52.     return resultado;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement