Created
March 1, 2026 10:16
-
-
Save thinkphp/930414b03bb9e32a0655f51e94e0412f to your computer and use it in GitHub Desktop.
complex2-sortate.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <math.h> | |
| using namespace std; | |
| class Complex { | |
| public: | |
| float x, y; | |
| float modul(); | |
| }; | |
| float Complex::modul() { | |
| return sqrt(x*x + y*y); | |
| } | |
| /* | |
| Programul urmator sorteaza un vector de numere complexe | |
| si le afiseaza in functie de modulul lor descrescator | |
| */ | |
| int main(int argc, char const *argv[]) | |
| { | |
| Complex v[ 100 ]; //declarat un vector de 100 de numere complexe | |
| Complex man; | |
| int inversari; | |
| //v[0], v[1], v[2] | |
| int n, i; | |
| cout<<"n="; | |
| cin>>n; | |
| for(int i = 0; i < n; ++i) { | |
| cout<<"Numar complex "<<i + 1; | |
| cout<<"parte reala = ";cin>>v[i].x; | |
| cout<<"parte imaginara = ";cin>>v[i].y; | |
| } | |
| //sortam numerele complexe prin metoda bulelor Bubble Sort | |
| do { | |
| inversari = 0; | |
| for(int i = 0; i < n - 1; ++i) { | |
| if(v[i].modul() < v[i+1].modul()) { | |
| man = v[i]; | |
| v[i] = v[i+1]; | |
| v[i+1] = man; | |
| inversari = 1; | |
| } | |
| } | |
| } while( inversari ); | |
| //numerele complexe sortate dupa modul | |
| for(int i = 0; i < n; ++i) { | |
| cout<<v[i].x<<" i"<<v[i].y<<endl; | |
| } | |
| return 0; | |
| } | |
| /* | |
| 2 3 9 10 11 | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment