কম্পিউটার টিউটোরিয়াল

C++-এ রেস্টুরেন্ট ফুড অর্ডার ডিসপ্লে টেবিল


রেস্টুরেন্টের অর্ডার লিস্ট orders দেওয়া আছে: [customer, table, food]। প্রতিটি টেবিলের প্রতিটি ফুড আইটেম কতবার অর্ডার হয়েছে তা দেখানোর টেবিল বানাতে হবে। প্রথম কলাম টেবিল নম্বর, বাকি কলামগুলো ফুড আইটেম (অ্যালফাবেটিক্যালি)। প্রথম রো হেডার।

ইনপুট উদাহরণে ৬টি অর্ডার → আউটপুট টেবিলে টেবিল ৩, ৫, ১০ এবং ফুড আইটেম Biryni, Fried Chicken, Paratha, Water।

সমাধানের ধাপগুলো:

  • ম্যাপ m[table][food] দিয়ে কাউন্ট রাখব

  • সেট names দিয়ে সব ফুড আইটেম, t দিয়ে সব টেবিল নম্বর রাখব

  • হেডার রো: "Table" + সর্টেড ফুড নাম

  • প্রতি টেবিলের জন্য রো: টেবিল নম্বর + প্রতিটি ফুডের কাউন্ট

  • টেবিল নম্বর অনুযায়ী সর্ট করব (হেডার বাদে)

উদাহরণ

নিচের কোডটি দেখুন −

#include <bits/stdc++.h>
using namespace std;

void print_vector(vector<vector<string>> v) {
    cout << "[";
    for(int i = 0; i < v.size(); i++) {
        cout << "[";
        for(int j = 0; j < v[i].size(); j++) {
            cout << v[i][j] << ", ";
        }
        cout << "],";
    }
    cout << "]" << endl;
}

typedef long long int lli;

class Solution {
public:
    vector<string> split(string& s, char delimiter) {
        vector<string> tokens;
        string token;
        istringstream tokenStream(s);
        while(getline(tokenStream, token, delimiter)) {
            tokens.push_back(token);
        }
        return tokens;
    }
    static bool cmp(vector<string>& a, vector<string>& b) {
        lli an = stol(a[0]);
        lli bn = stol(b[0]);
        return an < bn;
    }
    vector<vector<string>> displayTable(vector<vector<string>>& o) {
        map<string, map<string, int>> m;
        set<string> names;
        set<string> t;
        for(auto& it : o) {
            t.insert(it[1]);
            bool ok = true;
            if(!ok) {
                vector<string> v = split(it[2], ' ');
                for(auto& x : v) {
                    m[it[1]][x]++;
                    names.insert(x);
                }
            } else {
                m[it[1]][it[2]]++;
                names.insert(it[2]);
            }
        }
        vector<vector<string>> ret;
        vector<string> temp(names.begin(), names.end());
        temp.insert(temp.begin(), "Table");
        ret.push_back(temp);
        for(auto& it : t) {
            vector<string> te;
            te.push_back(it);
            for(auto& x : names) {
                te.push_back(to_string(m[it][x]));
            }
            ret.push_back(te);
        }
        sort(ret.begin() + 1, ret.end(), cmp);
        return ret;
    }
};

int main() {
    Solution ob;
    vector<vector<string>> v = {{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak","3","Paratha"}};
    print_vector(ob.displayTable(v));
}

ইনপুট

{{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak","3","Paratha"}}

আউটপুট

[[Table, Biryni, Fried Chicken, Paratha, Water, ],[3, 0, 1, 2, 0, ],[5, 0, 0, 1, 1, ],[10, 1, 0, 0, 0, ],]

  1. C++-এ String at() মেথড ব্যবহার

  2. C++-এ ভেক্টর সর্ট করা (Sorting a vector in C++)

  3. C++-এ স্ট্রিং টোকেনাইজেশন: স্ট্রিম এবং getline() ব্যবহার

  4. C++-এ স্ট্রিং টোকেনাইজ (Tokenize) করা