Advertisement
HyperSensualNarwhal

Swap

Nov 21st, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. /*
  2. Замена двух переменных местами
  3. */
  4.  
  5. // 1. С помощью третьей переменной
  6. #include <iostream>
  7.  
  8. using std::cout;
  9. using std::cin;
  10. using std::endl;
  11.  
  12. #define TAB '\t'
  13.  
  14. void main()
  15. {
  16.     setlocale(0, "");
  17.  
  18.     int a = (cout << "Введите значение певой переменной ->  ", cin >> a, a);
  19.     int b = (cout << "Введите значение второй переменной -> ", cin >> b, b);
  20.     int temporary;
  21.  
  22.  
  23.     cout << endl << "Значение первой переменной: " << a << endl;
  24.     cout << "Значение второй переменной: " << b << endl << endl;
  25.  
  26.     temporary = a;
  27.     a = b;
  28.     b = temporary;
  29.  
  30.     cout << "Значение первой переменной после перестановки: " << a << endl;
  31.     cout << "Значение второй переменной после перестановки: " << b << endl;
  32.  
  33. }
  34.  
  35.  
  36. /*
  37.  
  38. //2. Без помощи третьей переменной
  39. #include <iostream>
  40.  
  41. using std::cout;
  42. using std::cin;
  43. using std::endl;
  44.  
  45. void main()
  46. {
  47.     setlocale(0, "");
  48.  
  49.     int a, b;
  50.  
  51.     cout << "Введите две переменные -> "; cin >> a >> b;
  52.  
  53.     a = a + b;
  54.     b = a - b;
  55.     a = a - b;
  56.  
  57.     cout << "Вы ввели:\n" << a << "\t" << b << endl;
  58.  
  59.     cout << "Переменные поменяны местами:\n" << a << "\t" << b << endl;
  60.  
  61. }
  62. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement