Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <map>
- #include <vector>
- #include <algorithm>
- #include <set>
- using namespace std;
- struct Tuote{
- string tuotenimi;
- double hinta;
- };
- using Ketjumap = map < string, map < string, vector < Tuote > > >;
- using Liikemap = map < string, vector < Tuote > >;
- const double TUOTE_LOPPU = -1;
- ifstream tiedoston_tarkistus(){
- string tiedoston_nimi = "";
- cout << "Input file: ";
- getline(cin, tiedoston_nimi);
- ifstream tiedosto_olio(tiedoston_nimi);
- if ( not tiedosto_olio ) {
- cout << "Error: the input file cannot be opened" << endl;
- return tiedosto_olio;
- } else {
- return tiedosto_olio;
- }
- }
- vector<string> split(const string& s, const char delimiter,
- bool ignore_empty = false){
- vector<string> result;
- string tmp = s;
- while(tmp.find(delimiter) != string::npos)
- {
- string new_part = tmp.substr(0, tmp.find(delimiter));
- tmp = tmp.substr(tmp.find(delimiter)+1, tmp.size());
- if(not (ignore_empty and new_part.empty()))
- {
- result.push_back(new_part);
- }
- }
- if(not (ignore_empty and tmp.empty()))
- {
- result.push_back(tmp);
- }
- return result;
- }
- Ketjumap tiedoston_luku(ifstream &tiedosto_olio){
- const char delimiter = ';';
- string rivi;
- Ketjumap ketjurakenne;
- while ( getline(tiedosto_olio, rivi) ) {
- vector<string> parts = split(rivi, delimiter, true);
- if (parts.size() != 4 ){
- return {};
- }
- for (auto osa : parts){
- if (osa.size() == 0){
- return {};
- }
- }
- string ketju = parts[0];
- string liike = parts[1];
- string tuote = parts[2];
- double hinta;
- if (parts[3] == "out-of-stock"){
- hinta = TUOTE_LOPPU;
- }
- else{
- hinta = stod(parts[3]);
- }
- Tuote uusi_tuote = {tuote, hinta};
- if (ketjurakenne.count(ketju) == 1){
- if (ketjurakenne[ketju].count(liike) == 1){
- for (auto tuotestruct : ketjurakenne[ketju][liike]){
- if (tuotestruct.tuotenimi == tuote){
- tuotestruct.hinta = hinta;
- }
- }
- }
- else{
- ketjurakenne[ketju][liike].push_back(uusi_tuote);
- }
- }
- else{
- Liikemap uusi_liike;
- uusi_liike[liike] = {uusi_tuote};
- ketjurakenne[ketju] = uusi_liike;
- }
- }
- /*
- for (auto it = ketjurakenne.begin(); it != ketjurakenne.end(); ++it){
- cout << it->first << endl << "liikkeet" <<endl;
- for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2){
- cout << it2->first << endl;
- }
- }
- */
- return ketjurakenne;
- }
- void tulosta_ketjut(const Ketjumap &rakenne){
- set <string> ketjut;
- for (auto ketju = rakenne.begin(); ketju != rakenne.end(); ++ ketju ){
- ketjut.insert(ketju->first);
- }
- for (auto ketju : ketjut){
- cout << ketju << endl;
- }
- }
- void tulosta_liikkeet(const Ketjumap &rakenne, string avain){
- set <string> liikkeet;
- if (rakenne.count(avain) != 1){
- cout << "Error: unknown chain name";
- }else{
- for (auto liike = rakenne[avain].begin();liike != rakenne[avain].end();
- ++ liike ){
- liikkeet.insert(liike->first);
- }
- for (auto liike : liikkeet){
- cout << liike << endl;
- }
- }
- }
- bool tulkitse_komento(string komento, const Ketjumap &rakenne){
- vector <string> splitattu_komento = split(komento, ' ');
- unsigned int komento_size = splitattu_komento.size();
- if (splitattu_komento[0] == "chains" and komento_size == 1){
- tulosta_ketjut(rakenne);
- }
- else if (splitattu_komento[0] == "stores" and komento_size == 2){
- tulosta_liikkeet(rakenne, splitattu_komento[1]);
- }
- }
- int main()
- {
- Ketjumap tietorakenne;
- ifstream filu = tiedoston_tarkistus();
- string komento;
- if ( not filu){
- return EXIT_FAILURE;
- }else{
- tietorakenne = tiedoston_luku(filu);
- }
- while (komento != "quit"){
- cout << ">";
- getline(cin, komento);
- tulkitse_komento(komento, tietorakenne);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement