Sissejuhatus vahetuses C ++
Vahetamine pole midagi muud kui andmete vahetamine muutujate vahel. Nagu iga teine keel, saame ka C ++ -s teha vahetustehinguid. Selle teostamiseks kasutatakse kahte meetodit - kasutades kolmandat muutujat ja ilma kolmanda muutujata. Selles artiklis käsitleme neid kahte meetodit numbrite vahetamiseks näidete abil. Vahetuskontseptsiooni mõistmiseks käsitleme ühte näidet - oletame, et teil on 500 sedelit ja vajate 500 ruupia vahetust. Palusite sõbralt 500 vahetust ja ta annab teile 5 sedelit 100-st vastutasuks 500 sedelit. Sel juhul vahetate teie ja teie sõber lihtsalt märkmeid. Seda nimetatakse kahe muutuja vahelise andmevahetuse vahetamiseks.
Kuidas vahetamine C ++ keeles töötab?
Vahetamine tähendab andmete vahetamist. C ++ puhul saab vahetamist teha kahel viisil. Esimene on vahetus, kasutades kolmandat muutujat, st ajutist muutujat, ja teine, ilma kolmanda muutujata. Selles jaotises näeme, kuidas mõlemat meetodit kasutades vahetada kaks ja kolm numbrit.
Näide nr 1
Kahe numbri vahetamine kolmanda muutuja abil.
Programm
#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äljund:
Näide 2
Kahe numbri vahetamine ilma kolmandat muutujat kasutamata.
Programm
#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äljund:
Näide 3
Kolme numbri vahetamine rakenduses C ++ kolmanda muutuja abil.
Programm
#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äljund:
Näide 4
Kolme numbri vahetamine ilma kolmandat muutujat kasutamata.
Programm
#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äljund:
Järeldus
Selles artiklis nägime, kuidas vahetada kaks ja kolm numbrit C ++ -s, kasutades kolmandat muutujat ja ilma kolmandat muutujat kasutamata. Loodetavasti leiate sellest artiklist abi.
Soovitatavad artiklid
See on juhend Pythonis vahetamiseks. Siin arutatakse, kuidas vahetamine C ++ keeles töötab näidete ja väljunditega. Lisateabe saamiseks võite vaadata ka järgmist artiklit -
- C ++ ülekoormus
- Ruutjuur C ++ -s
- C ++ alternatiivid
- Tähemustrid c ++ -s
- Vahetamine PHP-s
- Ülekoormus Java-s
- Pythoni ülekoormamine
- Ruutjuur PHP-s
- C ++ 11 peamist funktsiooni ja eelist
- Ruutjuur JavaScriptis