Advertisement
nRikee

main.cpp.zip

Jul 11th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.     int Add(string); /** Realiza la suma de una cadena de números dados en formato string, separados entre comas. */
  5.     int numPartes(string); /** Devuelve el número de divisiones */
  6.      
  7.     int main(){ /**Función main de prueba*/
  8.         cout << "Prueba Add(): " << Add("") << endl << endl << "Prueba Add( 2 ): " << Add("2") <<endl << endl << "Prueba Add( 8347 ): " << Add("8347") <<endl << endl; << "Prueba Add( 10\\n8 ): " << Add("10\n8") <<endl << endl
  9.              << "Prueba Add( 8,400 ): " << Add("8,400") <<endl << endl<< "Prueba Add( 5,5\\n5,4,5\\n100 ): " << Add("5,5\n5,4,5\n100") << endl << endl<< "Prueba Add( 1,2,3\\n4\\n5,6,7,8,9,10\\n1000 ): "
  10.              << Add("1,2,3\n4\n5,6,7,8,9,10\n1000") << endl << endl;
  11.         return 0;}
  12.      
  13.     int Add(string numeros){ /**Realiza la suma de una cadena de números dados en formato string, separados entre comas.*/
  14.         int NUMERO_PARTICIONES = numPartes(numeros),pos, pos2, posBueno, resultado = 0,aux = 0,aux2; string parte[NUMERO_PARTICIONES],auxiliar;
  15.         if(numeros.find(',')!=-1){ parte[0]=numeros;
  16.             for( int a=1; aux!=-1 || aux2!=-1; a++ ){
  17.                 auxiliar = parte[a-1]; aux = 0; pos = auxiliar.find(','); pos2 = auxiliar.find("\n");
  18.                 if(pos!=-1 && pos2!=-1){
  19.                     if(pos<pos2) posBueno=pos; else posBueno=pos2;
  20.                     parte[a-1] = auxiliar.substr(aux, posBueno);
  21.                     parte[a] = auxiliar.substr(posBueno+1,auxiliar.length());}
  22.                 if(pos==-1 && pos2!=-1){
  23.                     parte[a-1] = auxiliar.substr(aux, pos2);
  24.                     parte[a] = auxiliar.substr((pos>pos2?pos:pos2)+1,auxiliar.length());}
  25.                 aux = parte[a].find(','); aux2 = parte[a].find("\n"); }}
  26.         if(NUMERO_PARTICIONES==2){
  27.             parte[0] = numeros.substr(0,(numeros.find("\n")==-1?numeros.find(','):numeros.find("\n")));
  28.             parte[1] = numeros.substr((numeros.find("\n")==-1?numeros.find(','):numeros.find("\n"))+1,numeros.length());}
  29.         if((numeros.find(',')==-1 && numeros.find("\n")==-1) || numeros.length()==1){parte[0]=numeros;}
  30.         if(numeros.length()==0){return 0;}
  31.         for(int a=0; a<NUMERO_PARTICIONES; a++){
  32.             std::stringstream(parte[a]) >> aux;
  33.             resultado += aux;}
  34.         return resultado;
  35.     }
  36.      
  37.     int numPartes(string numeros){ /**Devuelve el número de divisiones*/
  38.         int aux, aux2, resultado = 1;string cortada = numeros;
  39.         aux = cortada.find(',');aux2 = cortada.find("\n");
  40.         while(aux!=-1 || aux2!=-1){
  41.             if(aux!=-1 && aux2!=-1) {cortada = cortada.substr((aux<aux2?aux:aux2)+1,cortada.length());}
  42.             else cortada = cortada.substr((aux>aux2?aux:aux2)+1,cortada.length());
  43.             resultado++;aux = cortada.find(',');aux2 = cortada.find("\n");}
  44.         return resultado;
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement