SHARE
TWEET

Untitled

a guest Oct 18th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. #include <sstream>
  5.  
  6. void Banner() {
  7.     std::cout << "****************************************" << std::endl;
  8.     std::cout << "*   Welcome to ReAL Location Service   *" << std::endl;
  9.     std::cout << "****************************************" << std::endl;
  10.     std::cout << "* 1) Register User                     *" << std::endl;
  11.     std::cout << "* 2) Check In                          *" << std::endl;
  12.     std::cout << "* 3) View feed                         *" << std::endl;
  13.     std::cout << "* 4) Subscribe                         *" << std::endl;
  14.     std::cout << "****************************************" << std::endl;
  15. }
  16.  
  17. std::string LimitedGetline() {
  18.     constexpr int MaxSize = 80;
  19.     std::string line;
  20.     while (std::cin && line.size() < MaxSize) {
  21.         int symbol = std::cin.get();
  22.         if (symbol == '\n') break;
  23.         line.push_back(symbol);
  24.     }
  25.     return line;
  26. }
  27.  
  28. int GetCmd() {
  29.     std::string cmdStr = LimitedGetline();
  30.     std::stringstream stream(cmdStr);
  31.     int cmd;
  32.     stream >> cmd;
  33.     return cmd;
  34. }
  35.  
  36.  
  37. struct Location {
  38.     double longitude, latitude;
  39. };
  40.  
  41. struct User {
  42.     std::string username;
  43.     std::vector<Location> posts;
  44.  
  45.     std::vector<User*> subscribers;
  46.     std::vector<std::pair<User*, Location*>> feed;
  47. };
  48.  
  49. int main() {
  50.     constexpr int MaxUsers = 100;
  51.     std::vector<std::unique_ptr<User>> users;
  52.     auto getUser = [&] () -> User* {
  53.         std::cout << "Enter username:";
  54.         std::string username = LimitedGetline();
  55.         for (auto& user : users) {
  56.             if (user->username == username) {
  57.                 return user.get();
  58.             }
  59.         }
  60.  
  61.         std::cout << "User not found" << std::endl;
  62.         return nullptr;
  63.     };
  64.  
  65.     for (int i = 0; i < 1000; ++i) {
  66.         Banner();
  67.  
  68.         switch (GetCmd()) {
  69.         case 1: {
  70.             if (users.size() > MaxUsers) {
  71.                 std::cout << "Users limit reached" << std::endl;
  72.                 break;
  73.             }
  74.             std::cout << "Enter username:";
  75.             std::string username = LimitedGetline();
  76.             users.emplace_back(new User{});
  77.             users.back()->posts.reserve(16);
  78.             users.back()->username = username;
  79.             std::cout << "Registered user " << username << std::endl;
  80.             break;
  81.         }
  82.         case 2: {
  83.             auto user = getUser();
  84.             if (!user) {
  85.                 break;
  86.             }
  87.  
  88.             std::cout << "Enter your location:";
  89.             Location location;
  90.  
  91.             std::string line = LimitedGetline();
  92.             std::stringstream stream(line);
  93.             stream >> location.longitude >> location.latitude;
  94.             user->posts.emplace_back(location);
  95.             for (auto subscriber : user->subscribers) {
  96.                 subscriber->feed.emplace_back(user, &user->posts.back());
  97.             }
  98.  
  99.             std::cout << "User " << user->username << " checked in" << std::endl;
  100.             break;
  101.         }
  102.         case 3: {
  103.             auto user = getUser();
  104.             if (!user) {
  105.                 break;
  106.             }
  107.  
  108.             std::cout << "Feed for user " << user->username << std::endl;
  109.             for (auto post : user->feed) {
  110.                 std::cout << post.first->username << " is at "
  111.                     << post.second->longitude << " " << post.second->latitude
  112.                     << std::endl;
  113.             }
  114.             break;
  115.         }
  116.         case 4: {
  117.             auto user = getUser();
  118.             if (!user) {
  119.                 break;
  120.             }
  121.  
  122.             auto subscribeTo = getUser();
  123.             if (!subscribeTo) {
  124.                 break;
  125.             }
  126.  
  127.             subscribeTo->subscribers.push_back(user);
  128.             std::cout << "User " << user->username << " subscribed to " << subscribeTo->username << std::endl;
  129.             break;
  130.         }
  131.         }
  132.     }
  133.  
  134.     return 0;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top