Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <map>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. struct Tuote{
  12. string tuotenimi;
  13. double hinta;
  14. };
  15.  
  16. using Ketjumap = map < string, map < string, vector < Tuote > > >;
  17. using Liikemap = map < string, vector < Tuote > >;
  18. const double TUOTE_LOPPU = -1;
  19.  
  20. ifstream tiedoston_tarkistus(){
  21. string tiedoston_nimi = "";
  22. cout << "Input file: ";
  23. getline(cin, tiedoston_nimi);
  24.  
  25. ifstream tiedosto_olio(tiedoston_nimi);
  26. if ( not tiedosto_olio ) {
  27. cout << "Error: the input file cannot be opened" << endl;
  28. return tiedosto_olio;
  29. } else {
  30. return tiedosto_olio;
  31. }
  32. }
  33.  
  34.  
  35. vector<string> split(const string& s, const char delimiter,
  36. bool ignore_empty = false){
  37. vector<string> result;
  38. string tmp = s;
  39.  
  40. while(tmp.find(delimiter) != string::npos)
  41. {
  42. string new_part = tmp.substr(0, tmp.find(delimiter));
  43. tmp = tmp.substr(tmp.find(delimiter)+1, tmp.size());
  44. if(not (ignore_empty and new_part.empty()))
  45. {
  46. result.push_back(new_part);
  47. }
  48. }
  49. if(not (ignore_empty and tmp.empty()))
  50. {
  51. result.push_back(tmp);
  52. }
  53. return result;
  54. }
  55.  
  56.  
  57.  
  58. Ketjumap tiedoston_luku(ifstream &tiedosto_olio){
  59. const char delimiter = ';';
  60. string rivi;
  61. Ketjumap ketjurakenne;
  62.  
  63. while ( getline(tiedosto_olio, rivi) ) {
  64. vector<string> parts = split(rivi, delimiter, true);
  65. if (parts.size() != 4 ){
  66. return {};
  67. }
  68. for (auto osa : parts){
  69. if (osa.size() == 0){
  70. return {};
  71. }
  72. }
  73.  
  74. string ketju = parts[0];
  75. string liike = parts[1];
  76. string tuote = parts[2];
  77. double hinta;
  78.  
  79.  
  80. if (parts[3] == "out-of-stock"){
  81. hinta = TUOTE_LOPPU;
  82. }
  83. else{
  84. hinta = stod(parts[3]);
  85. }
  86. Tuote uusi_tuote = {tuote, hinta};
  87.  
  88. if (ketjurakenne.count(ketju) == 1){
  89.  
  90.  
  91. if (ketjurakenne[ketju].count(liike) == 1){
  92.  
  93. for (auto tuotestruct : ketjurakenne[ketju][liike]){
  94.  
  95. if (tuotestruct.tuotenimi == tuote){
  96. tuotestruct.hinta = hinta;
  97. }
  98. }
  99.  
  100. }
  101.  
  102. else{
  103.  
  104. ketjurakenne[ketju][liike].push_back(uusi_tuote);
  105. }
  106. }
  107. else{
  108.  
  109. Liikemap uusi_liike;
  110. uusi_liike[liike] = {uusi_tuote};
  111. ketjurakenne[ketju] = uusi_liike;
  112. }
  113.  
  114.  
  115.  
  116. }
  117. /*
  118. for (auto it = ketjurakenne.begin(); it != ketjurakenne.end(); ++it){
  119. cout << it->first << endl << "liikkeet" <<endl;
  120. for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2){
  121. cout << it2->first << endl;
  122.  
  123. }
  124. }
  125. */
  126. return ketjurakenne;
  127.  
  128. }
  129.  
  130. void tulosta_ketjut(const Ketjumap &rakenne){
  131. set <string> ketjut;
  132. for (auto ketju = rakenne.begin(); ketju != rakenne.end(); ++ ketju ){
  133. ketjut.insert(ketju->first);
  134. }
  135. for (auto ketju : ketjut){
  136. cout << ketju << endl;
  137. }
  138. }
  139.  
  140. void tulosta_liikkeet(const Ketjumap &rakenne, string avain){
  141. set <string> liikkeet;
  142.  
  143. if (rakenne.count(avain) != 1){
  144. cout << "Error: unknown chain name";
  145. }else{
  146. for (auto liike = rakenne[avain].begin();liike != rakenne[avain].end();
  147. ++ liike ){
  148. liikkeet.insert(liike->first);
  149. }
  150. for (auto liike : liikkeet){
  151. cout << liike << endl;
  152. }
  153. }
  154. }
  155.  
  156. bool tulkitse_komento(string komento, const Ketjumap &rakenne){
  157. vector <string> splitattu_komento = split(komento, ' ');
  158. unsigned int komento_size = splitattu_komento.size();
  159.  
  160. if (splitattu_komento[0] == "chains" and komento_size == 1){
  161. tulosta_ketjut(rakenne);
  162. }
  163. else if (splitattu_komento[0] == "stores" and komento_size == 2){
  164. tulosta_liikkeet(rakenne, splitattu_komento[1]);
  165. }
  166. }
  167.  
  168.  
  169.  
  170. int main()
  171. {
  172.  
  173. Ketjumap tietorakenne;
  174. ifstream filu = tiedoston_tarkistus();
  175. string komento;
  176.  
  177. if ( not filu){
  178. return EXIT_FAILURE;
  179. }else{
  180. tietorakenne = tiedoston_luku(filu);
  181. }
  182. while (komento != "quit"){
  183. cout << ">";
  184. getline(cin, komento);
  185. tulkitse_komento(komento, tietorakenne);
  186. }
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement