Advertisement
mixone

cmd.cpp

Sep 2nd, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include "Skyscraper.h"
  5. #include "Tree.h"
  6.  
  7.  
  8. #define NEW_LINE cout << endl;
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     string prompt = "=>";
  15.     string cmdline;
  16.     bool on = true;
  17.     while (on)
  18.     {
  19.         NEW_LINE
  20.         cout << prompt; getline(cin, cmdline);
  21.         if (cmdline == "exit") on = false; // Exit clause
  22.  
  23.         // Multiple argument commands
  24.         else if (!cmdline.empty())
  25.         {
  26.             // Separate cmdline input into vector containing each arg
  27.  
  28.             const char* yourStr = cmdline.c_str();
  29.             char str[100];
  30.             strncpy(str, yourStr, sizeof(str));
  31.             char* parts[10] = {0};
  32.             unsigned int index = 0;
  33.             parts[index] = strtok(str," ");
  34.             while(parts[index] != 0)
  35.             {
  36.                 ++index;
  37.                 parts[index] = strtok(0, " ");
  38.             }
  39.  
  40.             // Now evaluate command
  41.  
  42.             if (string(parts[0]) == "hex2dec")  // Convert hexadecimal to decimal
  43.             {
  44.                 cout << parts[1] << " base 16 = ";
  45.                 int converted = strtol(parts[1], NULL, 16);
  46.                 cout << converted << " base 10.";
  47.             }
  48.             else if (string(parts[0]) == "dec2hex")  // Convert decimal to hexadecimal
  49.             {
  50.                 cout << parts[1] << " base 10 = ";
  51.                 int converted = strtol(parts[1], NULL, 10);
  52.                 cout << hex << uppercase << converted << " base 16.";
  53.             }
  54.             else if (string(parts[0]) == "prompt")  // Change the prompt
  55.             {
  56.                 prompt = string(parts[1]);
  57.             }
  58.             else if (string(parts[0]) == "draw")    // Draw a....
  59.             {
  60.                 if (string(parts[1]) == "tree")     // Tree must specify height
  61.                 {
  62.                     int height = strtol(parts[2], NULL, 10);
  63.                     Tree arbre(height);
  64.                     arbre.drawTree();
  65.                 }
  66.                 else if (string(parts[1]) == "house") // House with n parameters
  67.                 {
  68.                     int params = strtol(parts[2], NULL, 10);
  69.                     switch (params)                   // Number of parameters
  70.                     {
  71.                     case 0:                           // No params
  72.                         {
  73.                             Skyscraper house;         // Default construct
  74.                             house.drawBuilding();
  75.                             break;
  76.                         }
  77.                     case 1:                           // Only number of floors specified
  78.                         {
  79.                             int nF = strtol(parts[3], NULL, 10);
  80.                             Skyscraper house(nF);
  81.                             house.drawBuilding();
  82.                             break;
  83.                         }
  84.                     case 2:                           // Number floors and floor height specified
  85.                         {
  86.                             int nF = strtol(parts[3], NULL, 10);
  87.                             int fH = strtol(parts[4], NULL, 10);
  88.                             Skyscraper house(nF, fH);
  89.                             house.drawBuilding();
  90.                             break;
  91.                         }
  92.                     case 3:                          // Number floors + floor height + floor length
  93.                         {
  94.                             int nF = strtol(parts[3], NULL, 10);
  95.                             int fH = strtol(parts[4], NULL, 10);
  96.                             int fL = strtol(parts[5], NULL, 10);
  97.                             Skyscraper house(nF, fH, fL);
  98.                             house.drawBuilding();
  99.                             break;
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement