All pastes #1871353 Raw Edit

panstwa

public cpp v1 · immutable
#1871353 ·published 2010-05-03 21:08 UTC
rendered paste body
// panstwa.hpp:#include <iostream>#include <stdio.h>#include <stdlib.h>#include <fstream>#include <vector>#include <string>#include <cstring>using namespace std;struct COUNTRY{  string Name, Cont, Lang;  int Num, Area;  float Gl;};fstream file, file2;void Menu(vector<COUNTRY> &);void Print(vector<COUNTRY> &);char MenuOption1();char MenuOption1_submenu(char);char MenuOption2();void MenuOption2_submenu(vector<COUNTRY> &, char);void MenuOption3(vector<COUNTRY> &);void MenuOption5(vector<COUNTRY> &);void BubbleSortName(vector<COUNTRY> &, char);void BubbleSortNum(vector<COUNTRY> &, char);void BubbleSortArea(vector<COUNTRY> &, char);void BubbleSortGl(vector<COUNTRY> &, char);void Help();// panstwa.cpp:#include "panstwa.hpp"int main(int argc, char* argv[]){  if (argc < 2)  {    cout << "Niepoprawnie uruchomiony program" << endl << "Uruchom program tak:" << endl << "\tpanstwa.out <plik>" << endl << "(gdzie <plik> to odpowiednio sformatowany plik tekstowy, np. panstwa.txt" << endl;    exit(1);  }  else  {    file.open(argv[1], ios::in);    if(!file.good())    {      cout << "Operacja otwarcia pliku nie powiodla sie!" << endl;      exit(1);    }      short lines=0;      char c;      while(!file.eof())      {        file.get(c);        if(c=='\n') lines++;      }            lines++;            if(file.eof()) file.clear();      file.seekg(ios::beg);      vector<COUNTRY> Kraj(lines);            char znak;      for(int i=0; !file.eof(); i++)      {        getline( file, Kraj[i].Name, file.widen(';'));        file >> Kraj[i].Num >> znak;        file >> Kraj[i].Area >> znak;        getline( file, Kraj[i].Cont, file.widen(';'));        getline( file, Kraj[i].Lang, file.widen('\n'));        float people = static_cast<float>(Kraj[i].Num);        float surface = static_cast<float>(Kraj[i].Area);        Kraj[i].Gl = people/surface;      }            file.close();      Menu(Kraj);            return 0;  }}void Menu(vector<COUNTRY> &Country){  char choice;  float a, b;  cout << "Co chcesz zrobic?:" << endl << "1. Wyswietlic liste panstw posortowana wg zadanych kryteriow." << endl << "2. Wyszukac kraj wg zadanych kryteriow." << endl << "3. Usunac rekord." << endl << "4. Dodac rekord." << endl << "5. Zmodyfikowac rekord." << endl << "6. Pomoc." << endl << "7. Zapis bazy do pliku." << endl << "8. Wyjsc z programu." << endl << "Wybierz opcje: ";  do  {    cin >> choice;    if( (choice!='1') && (choice!='2') && (choice!='3') &&        (choice!='4') && (choice!='5') && (choice!='6') && (choice!='7') && (choice!='8'))      cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";  }    while( (choice!='1') && (choice!='2') && (choice!='3') &&      (choice!='4') && (choice!='5') && (choice!='6') && (choice!='7') && (choice!='8'));    char Opcja;  COUNTRY temp;  short in;  char outfile[30];    switch(choice)  {    case '1':      Opcja = MenuOption1();      switch(Opcja)      {        case '1':          BubbleSortName(Country, MenuOption1_submenu(Opcja));          Print(Country);          break;        case '2':          BubbleSortNum(Country, MenuOption1_submenu(Opcja));          Print(Country);          break;        case '3':          BubbleSortArea(Country, MenuOption1_submenu(Opcja));          Print(Country);          break;        case '4':          BubbleSortGl(Country, MenuOption1_submenu(Opcja));          Print(Country);          break;        default:          break;      }            cin.ignore();      getchar();      Menu(Country);      break;    case '2':      Opcja = MenuOption2();      MenuOption2_submenu(Country, Opcja);      cin.ignore();      getchar();      Menu(Country);      break;    case '3':      MenuOption3(Country);      cin.ignore();      getchar();      Menu(Country);      break;    case '4':      cout << "Dodajesz rekord. Wprowadz kolejno:" << endl;      cout << "Nazwa(wielkie litery): ";      cin.ignore();      getline(cin, temp.Name);      cout << "Liczba ludnosci: ";      //cin.ignore();      cin >> temp.Num;      cout << "Powierzchnia: ";      cin.ignore();      cin >> temp.Area;            cout << "Kontynent(od wielkiej litery): ";      cin.ignore();      getline(cin, temp.Cont);      cout << "Jezyk: ";      //cin.ignore();      getline(cin, temp.Lang);      a = static_cast<float>(temp.Num);      b = static_cast<float>(temp.Area);      temp.Gl = a/b;      Country.push_back(temp);      in = Country.size() - 1;      cout << endl << "Oto dane panstwa " << temp.Name << ": " << endl;      cout << "#" << in+1 << ": ";      cout << Country[in].Name << " | ";      cout << Country[in].Num << " | ";      cout << Country[in].Area << " | ";      cout << Country[in].Cont << " | ";      cout << Country[in].Lang << " | ";      cout << Country[in].Gl;      cin.ignore();      getchar();      Menu(Country);      break;    case '5':      MenuOption5(Country);      cin.ignore();      getchar();      Menu(Country);      break;          case '6':      Help();      cin.ignore();      getchar();      Menu(Country);      break;          case '7':          cout << "Podaj nazwe pliku, do ktorego chcesz zapisac: ";      cin >> outfile;      file2.open(outfile, ios::out|ios::trunc);      if(file2.good())      {        cout << endl <<"Udalo sie otworzyc plik!";      }      else      {        cout << endl << "Operacja otwarcia pliku nie powiodla sie!" << endl;        exit(1);      }      for(unsigned short i=0; i < Country.size(); i++)      {        file2 << Country[i].Name <<';'<<Country[i].Num <<';'<<Country[i].Area <<';'<<Country[i].Cont <<';'<< Country[i].Lang <<          ';'<< Country[i].Gl << endl;      }      file2 << '\0';      file2.close();      cout << endl << "Zapis do pliku zakonczony powodzeniem.";      cin.ignore();      getchar();      Menu(Country);      break;        case '8':      cout << endl << "Nacisnij [return], aby zakonczyc dzialanie programu.";      cin.ignore();      getchar();      break;    default:      break;  }}void Print(vector<COUNTRY> &Country){  int length = Country.size();  for(int i=0; i < length; i++)  {    cout << "#" << i+1 << ": ";    cout << Country[i].Name << " | ";    cout << Country[i].Num << " | ";    cout << Country[i].Area << " | ";    cout << Country[i].Cont << " | ";    cout << Country[i].Lang << " | ";    cout << Country[i].Gl;    cout << endl;    //if ((i+1)%15==0) //getchar();  }}char MenuOption1(){  char choice;  cout << "Wedlug jaki kryteriow chcesz posortowac liste panstw?:" <<endl;  cout << "1. Nazwy." <<endl;  cout << "2. Liczby ludnosci." <<endl;  cout << "3. Powierzchni." <<endl;  cout << "4. Gestosci zaludnienia." <<endl;  cout << "Opcja: ";  do  {    cin >> choice;    if((choice!='1') && (choice!='2') && (choice!='3') && (choice!='4'))      cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";  }    while((choice!='1') && (choice!='2') && (choice!='3') && (choice!='4'));  return choice;}char MenuOption1_submenu(char Opcja){  char choice;  switch(Opcja)  {    case '1':      cout << "Chcesz wyswietlic liste panstw posortowana wg nazw:" << endl;      cout << "1. Malejaco." << endl;      cout << "2. Rosnaco." << endl;      cout << "Opcja: ";            do      {        cin >> choice;        if((choice!='1') && (choice!='2'))          cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";      }            while((choice!='1') && (choice!='2'));      break;    case '2':      cout << "Chcesz wyswietlic liste panstw posortowana wg liczby ludnosci:" << endl;      cout << "1. Malejaco." << endl;      cout << "2. Rosnaco." << endl;      cout << "Opcja: ";      do      {        cin >> choice;        if((choice!='1') && (choice!='2'))          cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";      }            while((choice!='1') && (choice!='2'));      break;        case '3':      cout << "Chcesz wyswietlic liste panstw posortowana wg powierzchni:" << endl;      cout << "1. Malejaco." << endl;      cout << "2. Rosnaco." << endl;      cout << "Opcja: ";            do      {        cin >> choice;        if((choice!='1') && (choice!='2'))          cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";      }      while((choice!='1') && (choice!='2'));      break;    case '4':      cout << "Chcesz wyswietlic liste panstw posortowana wg gestosci zaludnienia:" << endl;      cout << "1. Malejaco." << endl;      cout << "2. Rosnaco." << endl;      cout << "Opcja: ";            do      {        cin >> choice;        if((choice!='1') && (choice!='2'))          cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";      }            while((choice!='1') && (choice!='2'));      break;    default:      break;  }  return choice;}char MenuOption2(){  char choice;  cout << "Wedlug jakich kryteriow chcesz wyszukac panstwo?:" << endl << "1. Nazwy." << endl << "2. Kontynentu." << endl << "3. Jezyka." << endl << "Opcja: ";    do  {    cin >> choice;    if((choice!='1') && (choice!='2') && (choice!='3'))      cout << endl << "Nie ma takiej opcji. Wybierz jedna z podanych: ";  }    while((choice!='1') && (choice!='2') && (choice!='3'));  return choice;}void MenuOption2_submenu(vector<COUNTRY> &Country, char Opcja){  unsigned short i=0;  bool flag = false;  string temp;  unsigned int result;    switch(Opcja)  {    case '1':      cout << "Podaj szukana nazwe (wielkie litery, np. WATYKAN): ";      cin.ignore();      getline(cin, temp);      while(i != Country.size())      {        result = Country[i].Name.find(temp);        if(result!=string::npos)        {          cout << "#" << i << ": " << Country[i].Name << " | " << Country[i].Num << " | " << Country[i].Area << " | " << Country[i].Cont << " | " << Country[i].Lang << " | " << Country[i].Gl << endl;          flag = true;        }        i++;      }            if( flag == false ) cout << endl << "Nie znaleziono kraju o podanej nazwie.";      break;    case '2':      cout << "Podaj kontynent (pierwsza litera wielka, reszta male, np. Australia): ";      cin.ignore();      getline(cin, temp);      //result = strstr( Country[i].Cont, temp );      while(i != Country.size())      {        result = Country[i].Cont.find(temp);        if(result!=string::npos)        {          cout << "#" << i << ": " << Country[i].Name << " | " << Country[i].Num << " | " << Country[i].Area << " | " << Country[i].Cont << " | " << Country[i].Lang << " | " << Country[i].Gl;          flag = true;          cout << endl;        }              i++;      }      if( flag == false ) cout << endl << "Blednie podana nazwa.";      break;    case '3':      cout << "Podaj szukany jezyk (male litery, np. norweski): ";      cin.ignore();      getline(cin, temp);            while(i != Country.size())      {        result = Country[i].Lang.find(temp);                if(result!=string::npos)        {          cout << "#" << i << ": " << Country[i].Name << " | " << Country[i].Num << " | " << Country[i].Area << " | " << Country[i].Cont << " | " << Country[i].Lang << " | " << Country[i].Gl;          flag = true;          cout << endl;        }                i++;      }            if( flag == false ) cout << endl << "Blednie podana nazwa.";      break;    default:      break;  }}void MenuOption5(vector<COUNTRY> &Country){  string temp;  unsigned short i=0, tmp;  bool flag = false;  char choice;  cout << "Chcesz zmodyfikowac rekord. Podaj nazwe panstwa, ktorego dane chcesz zmodyfikowac:" << endl << "Nazwa: ";  cin.ignore();  getline(cin, temp);    while(i != Country.size())  {    if(temp == Country[i].Name)    {      cout << endl << "Oto aktualne dane panstwa " << temp << ": " << endl << "#" << i << ": " << Country[i].Name << " | " << Country[i].Num << " | " << Country[i].Area << " | " << Country[i].Cont << " | " << Country[i].Lang << " | " << Country[i].Gl << endl;      tmp = i;      flag = true;    }        i++;  }    if( flag == false ) cout << endl << "Nie znaleziono kraju o podanej nazwie.";  else if(flag == true)  {    cout << endl << "Czy zmienic nazwe?" << endl << "1. Tak." << endl << "2. Nie." << endl << "Opcja: ";        do    {      cin >> choice;      if( (choice!='1') && (choice!='2'))        cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";    }    while( (choice!='1') && (choice!='2'));    if(choice == '1')    {        cout << endl << "Nowa nazwa: ";        cin.ignore();        getline(cin, Country[tmp].Name);    }    cout << endl << "Czy zmienic liczbe ludnosci?";    cout << endl << "1. Tak.";    cout << endl << "2. Nie.";    cout << endl << "Opcja: ";        do    {      cin >> choice;      if( (choice!='1') && (choice!='2'))        cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych";    }        while( (choice!='1') && (choice!='2'));    if(choice == '1')    {      cout << "Nowa liczba ludnosci: ";      cin >> Country[tmp].Num;    }    cout << endl << "Czy zmienic powierzchnie?" << endl << "1. Tak." << endl << "2. Nie." << endl << "Opcja: ";        do    {      cin >> choice;      if( (choice!='1') && (choice!='2'))        cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";    }        while( (choice!='1') && (choice!='2'));    if(choice == '1')    {      cout << "Nowa powierzchnia: ";      cin >> Country[tmp].Area;    }        cout << endl << "Czy zmienic kontynent?: " << endl << "1. Tak." << endl << "2. Nie." << endl << "Opcja: ";    do    {      cin >> choice;      if( (choice!='1') && (choice!='2'))        cout << endl << "Nie ma takiej opcji! Wybierz jedna z podanych: ";    }    while( (choice!='1') && (choice!='2'));    if(choice == '1')    {      cout << "NOWY kontynent: ";      cin.ignore();      getline(cin, Country[tmp].Cont);    }    cout << endl << "Czy zmienic jezyk?" << endl << "1. Tak." << endl << "2. Nie." << endl << "Opcja: ";        do    {      cin >> choice;      if( (choice!='1') && (choice!='2'))        cout << endl << "Nie ma takiej opcji. Wybierz jedna z podanych: ";    }    while( (choice!='1') && (choice!='2'));    if(choice == '1')    {      cout << "Nowy jezyk: ";      cin.ignore();      getline(cin, Country[tmp].Lang);    }    cout << endl << "Oto zmienione dane panstwa " << temp << ": " << endl << "#" << tmp << ": " << Country[tmp].Name << " | " << Country[tmp].Num << " | " << Country[tmp].Area << " | " << Country[tmp].Cont << " | " << Country[tmp].Lang << " | ";    float a = static_cast<float>(Country[tmp].Num);    float b = static_cast<float>(Country[tmp].Area);    float c = a/b;    Country[tmp].Gl = c;    cout << Country[tmp].Gl;    }}void MenuOption3(vector<COUNTRY> &Country){  string temp;  unsigned short i=0, tmp;  bool flag = false;  char choice;  cout << "Podaj nazwe rekordu, ktory chcesz usunac: ";  cin.ignore();  getline(cin, temp);    while(i != Country.size())  {    if(temp == Country[i].Name)    {      tmp = i;      flag = true;    }        i++;  }    if( flag == false ) cout << endl << "Nie znaleziono kraju o podanej nazwie.";  else  {    cout << endl << "Oto aktualne dane panstwa " << temp << ": " << endl << "#" << tmp << ": " << Country[tmp].Name << " | " << Country[tmp].Num << " | " << Country[tmp].Area << " | " << Country[tmp].Cont << " | " << Country[tmp].Lang << " | " << Country[tmp].Gl << endl << "Czy chcesz ususnac?: " << endl << "1. Tak." << endl << "2. Nie." << endl << "Opcja: ";    do    {      cin >> choice;      if( (choice!='1') && (choice!='2'))        cout << endl << "Nie ma takiej opcji. Wybierz jedna z podanych:";    }        while( (choice!='1') && (choice!='2'));    if(choice == '1')    {      Country.erase(Country.begin() + tmp);      cout << endl << "Rekord usuniety.";    }        else if (choice == '2') cout << endl << "OK. Rekord nienaruszony.";  }}void BubbleSortName(vector<COUNTRY> &Country, char choice){  int i, j, flag = 1;  COUNTRY temp;  int length = Country.size( );  for(i = 1; (i <= length) && flag; i++)  {    flag = 0;    for (j=0; j < (length -1); j++)    {      switch(choice)      {        case '1':          if (Country[j+1].Name > Country[j].Name)          {            temp = Country[j];            Country[j] = Country[j+1];            Country[j+1] = temp;            flag = 1;          }                    break;                  case '2':          if (Country[j+1].Name < Country[j].Name)          {            temp = Country[j];            Country[j] = Country[j+1];            Country[j+1] = temp;            flag = 1;          }                    break;      }    }  }}void BubbleSortNum(vector<COUNTRY> &Country, char choice){  int i, j, flag = 1;  COUNTRY temp;  int length = Country.size( );  for(i = 1; (i <= length) && flag; i++)  {    flag = 0;    for (j=0; j < (length -1); j++)    {      switch(choice)      {              case '1':          if (Country[j+1].Num > Country[j].Num)          {            temp = Country[j];            Country[j] = Country[j+1];            Country[j+1] = temp;            flag = 1;          }          break;        case '2':          if (Country[j+1].Num < Country[j].Num)          {            temp = Country[j];            Country[j] = Country[j+1];            Country[j+1] = temp;            flag = 1;          }          break;      }    }  }}void BubbleSortArea(vector<COUNTRY> &Country, char choice){  int i, j, flag = 1;  COUNTRY temp;  int length = Country.size( );  for(i = 1; (i <= length) && flag; i++)  {    flag = 0;    for (j=0; j < (length -1); j++)    {      switch(choice)      {        case '1':          if (Country[j+1].Area > Country[j].Area)          {            temp = Country[j];            Country[j] = Country[j+1];            Country[j+1] = temp;            flag = 1;          }          break;        case '2':          if (Country[j+1].Area < Country[j].Area)          {            temp = Country[j];            Country[j] = Country[j+1];            Country[j+1] = temp;            flag = 1;          }          break;      }    }  }}void BubbleSortGl(vector<COUNTRY> &Country, char choice){  int i, j, flag = 1;  COUNTRY temp;  int length = Country.size( );  for(i = 1; (i <= length) && flag; i++)  {    flag = 0;    for (j=0; j < (length -1); j++)    {      switch(choice)      {        case '1':          if (Country[j+1].Gl > Country[j].Gl)          {            temp = Country[j];            Country[j] = Country[j+1];            Country[j+1] = temp;            flag = 1;          }          break;        case '2':          if (Country[j+1].Gl < Country[j].Gl)          {            temp = Country[j];            Country[j] = Country[j+1];            Country[j+1] = temp;            flag = 1;          }          break;      }    }  }}void Help(){  cout << "Pomoc."<< endl << "Program, ktorego teraz uzywasz jest prosta baza danych."<< endl << "Kazdy rekord jest nastepujacej postaci:" << endl << "NAZWA PANSTWA | liczba ludnosci | powierzchnia kraju | Kontynent | uzywane jezyki | gestosc zaludnienia" << endl << "Wazne jest, aby podczas korzystania z programu stosowac zaprezentowana forme, czyli:" << endl << "* nazwy panstw wprowadzac WIELKIMI literami (np. POLSKA)" << endl << "* liczba ludnosci - program oczekuje na liczbe (np. 36000000)" << endl << "* powierzchnia kraju - jak wyzej" << endl << "* kontynent - pierwsza litera wielka, pozostale male (np. Azja)" << endl << "* jezyki - stosowac male litery podczas podawania (np. rosyjski, suahili)" << endl;}