Advertisement
kernel_memory_dump

SSPURV lab2

Mar 16th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <list>
  5. #include <vector>
  6. #include <map>
  7. #include <algorithm>    // std::find
  8.  
  9.  
  10. using namespace std;
  11.  
  12. // помоћне функције
  13. static bool loadStudents(string fileName);
  14. static void printStudentNames();
  15. static void printStudentIndexNums();
  16. static void printStudentNamesAndIndexNums();
  17. static void addStudent();
  18. static void makeIndexToNameMap();
  19. static void printIndexToNameMap();
  20. static void printDesiredIndex(int x);
  21. static void makeNameToIndexMap();
  22. static void printDesiredName(string name);
  23. static void printSpecial();
  24. static void replace();
  25.  
  26. // глобалне променљиве
  27. static list<string> studentNames;
  28. static vector<int> studentIndexNums;
  29. static map<int, string> studentIndexToNameMap;
  30. static map<string, int> studentNameToIndexMap;
  31.  
  32.  
  33. int main(int argc, char* argv[])
  34. {
  35.     if (argc != 4)
  36.     {
  37.         cout << "Niste uneli potrebne argumente za pokretanje programa!" << endl;
  38.         cout << "Argumenti komandne linije treba da budu:" << endl;
  39.         cout << "1. apsolutna ili relativna putanja do datoteke: \"baza_studenata.txt\"" << endl;
  40.         cout << "2. ime studenta" << endl;
  41.         cout << "3. prezime studenta" << endl;
  42.         exit(-1);
  43.     }
  44.  
  45.     bool retVal;
  46.  
  47.     retVal = loadStudents(argv[1]);
  48.  
  49.     if (retVal == false)
  50.     {
  51.         cout << "Greska prilikom ucitavanja ulazne datoteke!\n" << endl;
  52.         exit(-1);
  53.     }
  54.  
  55.     printStudentNames();
  56.     printStudentIndexNums();
  57.  
  58.     printStudentNamesAndIndexNums();
  59.  
  60.     addStudent();
  61.  
  62.     printStudentNamesAndIndexNums();
  63.  
  64.     makeIndexToNameMap();
  65.     printIndexToNameMap();
  66.  
  67.     int x;
  68.     cout << "Unesite broj indeksa: ";
  69.     cin >> x;
  70.  
  71.     printDesiredIndex(x);
  72.  
  73.     makeNameToIndexMap();
  74.  
  75.     string name;
  76.     string surname;
  77.     // TODO: име и презиме студента преузети из прослеђених аргумената командне линије
  78.     name = argv[2];
  79.     surname =  argv[3];
  80.  
  81.  
  82.     string fullName;
  83.     // TODO: формирај од имена и презимена један знаковни низ
  84.     fullName = name + " " + surname;
  85.  
  86.     printDesiredName(fullName);
  87.  
  88.     // Додатни
  89.     printSpecial();
  90.  
  91.     replace();
  92.  
  93.     cout << endl << endl;
  94.  
  95.     return 0;
  96. }
  97.  
  98.  
  99. bool loadStudents(string fileName)
  100. {
  101.     // учитава датотеку "baza_studenata.txt"
  102.  
  103.     ifstream inputFile(fileName);
  104.  
  105.     if (inputFile.is_open() == false)
  106.     {
  107.         cout << endl << "GRESKA! Pogresno ime datoteke" << endl << endl;
  108.         return false;
  109.     }
  110.  
  111.     string ime;
  112.     string prezime;
  113.     int brojIndeksa;
  114.     string ime_prezime;
  115.  
  116.  
  117.     do
  118.     {
  119.         inputFile >> ime;
  120.         inputFile >> prezime;
  121.         inputFile >> brojIndeksa;
  122.  
  123.         // место за код
  124.         // TODO: направите од имена и презимена један знаковни низ: надаље "име" се односи на име и презиме
  125.         // TODO: попуните studentNames са именима, а studentIndexNums са бројевима индекса
  126.         ime_prezime  = ime + " " + prezime;
  127.  
  128.         studentNames.push_back(ime_prezime);
  129.  
  130.  
  131.         studentIndexNums.push_back(brojIndeksa);
  132.  
  133.     } while (inputFile.eof() == false);
  134.  
  135.     return true;
  136. }
  137.  
  138.  
  139. void printStudentNames()
  140. {
  141.     list<string>:: iterator it;
  142.     for (it=studentNames.begin();it!=studentNames.end();it++)
  143.         cout<<"Imena svih studenata su: "<<*it<<endl;
  144.     // TODO: имплементирати
  145. }
  146.  
  147.  
  148. void printStudentIndexNums()
  149. {
  150.     // studentIndexNums
  151.     vector<int>:: reverse_iterator it;
  152.     for(it=studentIndexNums.rbegin();it!=studentIndexNums.rend();it++)
  153.         cout<<"Brojevi indexa studenata u obrnutom redosledu su: "<<*it<<endl;
  154.  
  155.     // штампа на екран студентске индексе али у обрнутом редоследу
  156.     // TODO: имплементирати
  157. }
  158.  
  159.  
  160. void printStudentNamesAndIndexNums()
  161. {
  162.     // штампа на екран име студента праћено бројем индекса
  163.     // користити информације у studentNames и studentIndexNums
  164.     // TODO: имплементирати
  165.  
  166.     list<string>::iterator it1;
  167.     vector<int>::iterator it2;
  168.     for (it2=studentIndexNums.begin(), it1=studentNames.begin();it1!=studentNames.end(),it2!=studentIndexNums.end();it1++,it2++){
  169.             cout<<"Studenti sa njihovim brojevima indexa su: " <<*it1 << *it2<<endl;
  170.  
  171.         }
  172.        
  173.     }
  174.  
  175.  
  176.  
  177. void addStudent()
  178. {
  179.     string studentsName = "Marko Kraljevic";
  180.     int studentsIndexNum = 54321;
  181.     list<string>::iterator it1;
  182.     vector<int>::iterator it2;
  183.  
  184.    
  185.     it1=find(studentNames.begin(),studentNames.end(),"Mali Radojica");
  186.     it2=find(studentIndexNums.begin(),studentIndexNums.end(),12345);
  187.  
  188.     //  myvector.insert ( it , 200 );
  189.     studentNames.insert(it1,"Marko Kraljevic");
  190.     studentIndexNums.insert(it2,54321);
  191.     // TODO: додати студента Краљевића Марка тачно иза Малог Радојице (12345)
  192.  
  193. }
  194.  
  195.  
  196. void makeIndexToNameMap()
  197. {
  198.     // TODO: направити мапу пресликавања бројева индекса на имена
  199.     //  kljuc se preslikava na vrednost
  200.     //  f(x) = x^2
  201.     // f(2) = 4
  202.     //  x -> y
  203.     //  f(2) = 4
  204.  
  205.     //mapica[1323] -> dobijemo Malog Radojicu
  206.     //map<int,string> mapa;
  207.     //studentIndexToNameMap vec definisano!!! samo korisitmo!!!!
  208.     // 1 for petlja koja ide kroz vektor i listu ovde ubaciti
  209.  
  210.     list<string>::iterator it1;
  211.     vector<int>::iterator it2;
  212.     for (it2=studentIndexNums.begin(), it1=studentNames.begin();it1!=studentNames.end(),it2!=studentIndexNums.end();it1++,it2++){
  213.             studentIndexToNameMap.insert(   pair<int, string>(*it2, *it1)         ) ;
  214.     }
  215.         //  
  216. }
  217.  
  218.  
  219. void printIndexToNameMap()
  220. {
  221.     // TODO: одшампати на екран садржај IndexToName мапе
  222.     //  cout << it->first << it->second
  223. }
  224.  
  225.  
  226. void printDesiredIndex(int x)
  227. {
  228.     // TODO: наћи име студента у мапи на основу броја индекса, и одшампати име на екран
  229.     // претпоставити да је број индекса увек валидан
  230.  
  231.  
  232.     // it = find(mapa.begin(), mapa.end(),  x);
  233.     // cout << it->second << endl;
  234. }
  235.  
  236.  
  237. void makeNameToIndexMap()
  238. {
  239.     // TODO: направити мапу пресликавања имена на бројеве индекса
  240.     // isto .x.x.x
  241. }
  242.  
  243.  
  244. void printDesiredName(string name)
  245. {
  246.     // TODO: наћи индекс студента на основу имена, и одшампати име на екран
  247.     // претпоставити да је могућ унос невалидног имена
  248.     // it  = find(mapa, mapa, name);
  249.  
  250.     // if ( it == mapa.end() ) // nema ga
  251.  
  252. }
  253.  
  254.  
  255. void printSpecial()
  256. {
  257.     // TODO: коришћењем итератора проћи кроз целу мапу studentIndexToNameMap
  258.     // и исписати на екран парове индекс и име_презиме студената, за све студенте
  259.     // осим последњег. За последњег студента исписати само име и презиме.
  260.     /*
  261.     for(it  = begin; it != mapa.end(); it++ ){
  262.         if ( it == mapa.end() -1 ) { // poslednji!
  263.             {
  264.                 cout << it->second;
  265.             }
  266.         else {
  267.             cout << it->first << it-> second;
  268.         }
  269.     }
  270.  
  271.    
  272.     */
  273.  
  274. }
  275.  
  276.  
  277. void replace()
  278. {
  279.     // TODO: коришћењем метода знаковног низа у задатој реченици заменити зарезе размацима
  280.     string str = "Sve,zareze,izmedju,reci,zameniti,razmacima";
  281.  
  282.     cout << endl << "Recenica koju treba prepraviti je:";
  283.     cout << endl << str << endl;
  284.  
  285.     // TODO: имплементирати замену
  286.     replace(str.begin(), str.end(), ' ', ',');
  287.  
  288.     cout << endl << "Ispravljena recenica:";
  289.     cout << endl << str << endl;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement