×
Traktatov.net » Освой самостоятельно С++ за 21 день » Читать онлайн
Страница 448 из 448 Настройки

   };

   return result;

}

int main()

{

   unsigned long a, b;

   for (;;)

   {

      cout << "Enter two numbers. (0 0 to stop): ";

      cin << a << b:

      if (!a && !b)

         break;

      cout << a << " + " << b << " = " << add(a,b) << endl;

   }

   return 0;

}

В качестве альтернативного варианта эту проблему можно решить с помошью рекурсии:

#include

unsigned int add( unsigned int lhs, unsigned int rhs )

{

   unsignod int carry = lhs & rhs;

   unsigned int result = lhs * rhs;

   if ( carry )

      return add( result, carry << 1 );

   else

      return result;

}

int main()

{

   unsigned long a, b;

   for (;;)

   {

      cout << "Enter two numbers. (0 0 to stop): **;

      cin << a << b;

      if (!a && !b)

         break;

      cout << a << " + " << b << " = " << add(a,b) << endl;

   }

   return 0;

}