Afişează pe ecran lista oraşelor, ordonate după aşezarea geografică de la nord la sud in c++. Datele despre orase sunt incluse in fisiere.
Răspunsuri la întrebare
#include <iostream>
#include <fstream>
using namespace std;
int m,n;
struct pos{
int id;
int latit;
int longit;
} a[100];
struct city{
int id;
char nume[20];
char tara[20];
int loc;
} b[100]
int convert(char text){
int numar = 0;
int i = 0;
for(i=strlen(text);i>0;i++){
numar = numar * 10 + (text[i]-'0');
}
if (v[0] == 'S' || v[i] == 'W') return -1*numar;
else return -1
}
void citeste(){
ifstream coord("coordonate.txt");
ifstream orase("orase.txt");
char temp[20]
while(!coord.eof()){
coord >> a[n].id;
coord >> temp;
a[n].latit = convert(temp);
coord >> temp;
a[n].longit = convert(temp);
n++;
}
while(!orase.eof()){
orase >> b[m].id;
orase >> b[m].nume;
orase >> b[m].tara;
orase >> b[m].loc;
m++
}
coord.close();
coord.close();
}
int main(){
citeste();
pos maxim = a[0];
int i,j = 0;
//Bubble sort
int ok = 1;
while(ok){
ok = 0;
for(i=0;i<n-1;i++){
if (a[i] < a[i+1]){
swap(a[i], a[i+1])
ok = 1;
}
}
}
for(i=0;i<n;i++)
for(j=0;j<m;j++){
if (b[j].id == a[i].id){
cout << b[i].nume << endl;
}
}
}
Netestat, pot exista cateva greseli dar asta e ideea.
#include
#include
using namespace std;
int m,n;
int vazute[100];
struct pos{
int id;
int latit;
int longit;
} a[100];
struct city{
int id;
char nume[20];
char tara[20];
int loc;
} b[100];
int numar = 0;
int i = 0;
for(i=1;i numar = numar * 10 + (text[i]-'0');
}
if (text[0] == 'S' || text[i] == 'W') return -1*numar;
else return numar;
}
ifstream coord("coordonate.txt");
ifstream orase("orase.txt");
char temp[20];
while(!coord.eof()){
coord >> a[n].id;
coord >> temp;
a[n].latit = convert(temp);
coord >> temp;
a[n].longit = convert(temp);
n++;
}
while(!orase.eof()){
orase >> b[m].id;
orase >> b[m].nume;
orase >> b[m].tara;
orase >> b[m].loc;
m++;
}
coord.close();
coord.close();
}
//Returneaza indexul elementului maxim nevazut
int mx = -180;
int index = -1;
int i;
for (i=0;i if (vazute[i]) continue;
if (a[i].longit >= mx) {
index = i;
mx = a[i].longit;
//cout << "\nDBG " << index << " " << mx;
}
}
vazute[index] = 1;
//cout << "Se returneaza " << index;
return index;
}
int main(){
citeste();
int i,j = 0;
int identif;
for (i=0;i identif = a[maxim()].id;
//Cauta in b numele orasului cu id respectiv
for(j=0;j if (b[j].id == identif) cout << b[j].nume << endl;
}
}
}