Výmena v C ++ - Ako funguje výmena v jazyku C ++?

Obsah:

Anonim

Úvod do swapovania v C ++

Výmena nie je nič iné ako výmena údajov medzi premennými. Rovnako ako akýkoľvek iný jazyk, aj v C ++ môžeme vykonávať swapovacie operácie. Vykonáva sa dvoma spôsobmi - použitím tretej premennej a bez použitia tretej premennej. V tomto článku budeme diskutovať o týchto dvoch metódach na výmenu čísel pomocou príkladov. Aby sme pochopili koncepciu swapu, môžeme diskutovať jeden príklad - predpokladajme, že máte 500 bankoviek a potrebujete výmenu 500 rupií. Požiadali ste svojho priateľa o výmenu 500 a on vám dá 5 poznámok po 100 za 500 poznámok. V tomto prípade si vy a váš priateľ len vymieňate poznámky. Toto sa nazýva výmena údajov medzi dvoma premennými.

Ako funguje výmena v jazyku C ++?

Výmena znamená výmenu údajov. V C ++ je možné vymeniť za použitia dvoch metód. Prvým je swapovanie pomocou tretej premennej, tj dočasnej premennej, a druhé je bez použitia tretej premennej. V tejto časti sa pozrieme na to, ako vymeniť dve a tri čísla pomocou oboch metód.

Príklad č. 1

Výmena dvoch čísiel Pomocou tretej premennej.

Program

#include
using namespace std;
int main()
(
int first_num, second_num, temp_num;
cout << "Enter first number: "; //allow user to add first number
cin >> first_num;
cout << "Enter second number: "; //allow user to add second number
cin >> second_num;
cout << "Before swapping " << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num < temp_num = first_num; //first number is assigned to temp
first_num = second_num; //second number is assigned to first number
second_num = temp_num; //first number is assigned to secind number
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl;
cout << "Second number: " << second_num;
return 0;
)
#include
using namespace std;
int main()
(
int first_num, second_num, temp_num;
cout << "Enter first number: "; //allow user to add first number
cin >> first_num;
cout << "Enter second number: "; //allow user to add second number
cin >> second_num;
cout << "Before swapping " << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num < temp_num = first_num; //first number is assigned to temp
first_num = second_num; //second number is assigned to first number
second_num = temp_num; //first number is assigned to secind number
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl;
cout << "Second number: " << second_num;
return 0;
)

Výkon:

Príklad č. 2

Zameniť dve čísla bez použitia tretej premennej.

Program

#include
using namespace std;
int main()
(
int first_num, second_num;
cout << "Enter first number: ";
cin >> first_num; //9
cout << "Enter second number: ";
cin >> second_num; //10
cout << "Before swapping " << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
first_num = first_num * second_num; //9 * 10 = 90
second_num = first_num / second_num; // 90 / 10 = 9
first_num = first_num / second_num; // 90 / 9= 10
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl; 10
cout << "Second number: " << second_num << endl; //9
return 0;
)

Výkon:

Príklad č. 3

Výmena troch čísiel v C ++ pomocou tretej premennej.

Program

#include
using namespace std;
int main()
(
int first_num, second_num, third_num, temp_num;
cout << "Enter first number: "; //allow user to add first number
cin >> first_num;
cout << "Enter second number: "; //allow user to add second number
cin >> second_num;
cout << "Enter third number: "; //allow user to add third number
cin >> third_num;
cout << "Before swapping" << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: "<< third_num << endl;
temp_num =first_num;
first_num = second_num; //second number is assigned to first number
second_num = third_num; //third number is assigned to second number
third_num = temp_num; //first number is assigned to third number
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: " << third_num << endl;
return 0;
)

Výkon:

Príklad č. 4

Zameniť tri čísla bez použitia tretej premennej.

Program

#include
using namespace std;
int main()
(
int first_num, second_num, third_num;
cout << "Enter first number: ";
cin >> first_num; //10
cout << "Enter second number: ";
cin >> second_num; //5
cout << "Enter third number: ";
cin >> third_num; //20
cout << "Before swapping" << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: " << third_num << endl;
first_num = first_num + second_num + third_num; // 10 + 5 + 20= 35
second_num = first_num - (second_num + third_num); // 35 - (5 + 20) = 10
third_num = first_num - (second_num + third_num); // 35 - (10 + 20) = 5
first_num = first_num - (second_num + third_num); 35 - (10 + 5) = 20
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl; //20
cout << "Second number: "<< second_num << endl; //10
cout << "Third number: " << third_num << endl; //5
return 0;
)

Výkon:

záver

V tomto článku sme videli, ako zamieňať dve a tri čísla v C ++ pomocou tretej premennej a bez použitia tretej premennej. Dúfam, že vám tento článok pomôže.

Odporúčané články

Toto je sprievodca Swapovaním v Pythone. Tu diskutujeme o tom, ako Výmena funguje v jazyku C ++ s príkladmi a výstupmi. Viac informácií nájdete aj v nasledujúcom článku -

  1. Preťaženie v C ++
  2. Štvorcový koreň v C ++
  3. Alternatívy C ++
  4. Vzory hviezd v c ++
  5. Výmena v PHP
  6. Preťaženie v Jave
  7. Preťaženie Pythonu
  8. Štvorcový koreň v PHP
  9. Prvých 11 funkcií a výhod C ++
  10. Štvorcový koreň v JavaScripte