Uvod o zamjeni u C ++
Zamjena nije ništa drugo nego razmjena podataka između varijabli. Kao i bilo koji drugi jezik, također možemo obavljati operacije izmjene u C ++. Izvodi se pomoću dvije metode - korištenjem treće varijable i bez korištenja treće varijable. U ovom ćemo članku razgovarati o ove dvije metode za zamjenu brojeva pomoću primjera. Da biste razumjeli zamijenjeni koncept, razmotrit ćemo jedan primjer - pretpostavimo da imate 500 nota i trebate razmjenu od 500 rupija. Pitali ste svog prijatelja za zamjenu 500, a on vam daje 5 novčanica od 100 u zamjenu za 500 novčanica. Ovdje, u ovom slučaju, vi i vaš prijatelj samo razmjenjujete bilješke. To je ono što se naziva zamjenom razmjene podataka između dvije varijable.
Kako izmjena funkcionira na jeziku C ++?
Zamjena znači razmjenu podataka. Kod C ++ zamjena se može obaviti pomoću dvije metode. Prvo je zamjena pomoću treće varijable, tj. Privremene varijable, a drugo je bez upotrebe treće varijable. U ovom ćemo odjeljku vidjeti kako zamijeniti dva i tri broja koristeći obje metode.
Primjer 1
Mijenjanje dva broja Pomoću treće varijable.
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;
)
Izlaz:
Primjer 2
Mijenjanje dva broja bez upotrebe treće varijable.
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;
)
Izlaz:
Primjer 3
Zamjena tri broja u C ++ Korištenjem treće varijable.
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;
)
Izlaz:
Primjer 4
Mijenjanje tri broja bez upotrebe treće varijable.
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;
)
Izlaz:
Zaključak
U ovom smo članku vidjeli kako zamijeniti dva i tri broja u C ++ koristeći treću varijablu i bez korištenja treće varijable. Nadam se da će vam ovaj članak biti koristan.
Preporučeni članci
Ovo je vodič za Zamjenu u Pythonu. Ovdje smo razgovarali o tome kako zamjena djeluje na jeziku C ++ s primjerima i izlazima. Možete pogledati i sljedeći članak da biste saznali više -
- Preopterećenje u C ++
- Kvadratni korijen u C ++
- C ++ alternative
- Zvjezdani uzorci u c ++
- Zamjena u PHP-u
- Preopterećenje u Javi
- Python preopterećenje
- Kvadratni korijen u PHP-u
- Top 11 značajki i prednosti C ++
- Kvadratni korijen u JavaScript