Basic string parser

Discussion in 'Programming General' started by Govind, Feb 7, 2008.

Basic string parser
  1. Unread #1 - Feb 7, 2008 at 11:34 AM
  2. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Basic string parser

    This does not provide argument checking and will only perform operations if all the arguments are one-digit numbers as it only reads 1 character for number arguments. But if you know how to use the string class, this shouldn't be difficult to understand, etc....
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    int main(int argc, char *argv[])
    {
        string str1;
        while(true)
        {
                   str1.clear();
        cout << "Calc> "; getline(cin,str1);
        if(str1.substr(0,3)=="ADD")
        {
             string temp = str1.substr(4,1);
             int a = atoi(temp.c_str());
             temp.clear();
             temp = str1.substr(6,1);
             int b = atoi(temp.c_str());
             int c = a + b;
             cout << a+b << endl;
        }
        else if(str1.substr(0,3)=="SUB")
        {
           
             string temp = str1.substr(4,1);
             int a = atoi(temp.c_str());
             temp.clear();
             temp = str1.substr(6,1);
             int b = atoi(temp.c_str());
             int c = a - b;
             cout << c << endl;
        }
        else if(str1.substr(0,3)=="MUL")
        {
         
             string temp = str1.substr(4,1);
             int a = atoi(temp.c_str());
             temp.clear();
             temp = str1.substr(6,1);
             int b = atoi(temp.c_str());
             int c = a * b;
             cout << c << endl;
        }
        else if(str1.substr(0,3)=="DIV")
        {
             //note that i'm using float for division, not int
        
             string temp = str1.substr(4,1);
             float a = atoi(temp.c_str());
             temp.clear();
             temp = str1.substr(6,1);
             float b = atoi(temp.c_str());
             float c = a/b;
             cout << c << endl;
        }
        else if(str1.substr(0,3)=="POW")
        {
           
             string temp = str1.substr(4,1);
             int a = atoi(temp.c_str());
             temp.clear();
             temp = str1.substr(6,1);
             int b = atoi(temp.c_str());
             int c = 1;
             if(b>=1)
             {
                     for(int i = 0; i < b; i++)
                     {
                             c*=a;
                     }
             }
             cout << c << endl;
        }
        else if(str1.substr(0,3)=="BYE")
        {
             exit(0);
        }
        else
        {
            cout << "Invalid command, must be ADD, SUB, MUL, DIV, POW or BYE" << endl;
        }
    }
        return 0;
    }
    Example usage:
    Code:
    Calc> ADD 2 3
    5
    Calc> SUB 8 3
    5
    Calc> MUL 2 8
    16
    Calc> DIV 6 2
    3
    Calc> POW 2 3
    8
    Calc> BYE
     
  3. Unread #2 - Feb 7, 2008 at 7:20 PM
  4. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    Basic string parser

    It would be tidier with a switch :p
     
  5. Unread #3 - Feb 7, 2008 at 8:26 PM
  6. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Basic string parser

    That wouldn't compile on GCC with a switch statement.
     
  7. Unread #4 - Feb 7, 2008 at 8:28 PM
  8. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    Basic string parser

    Any chance of a C version for me to study?
     
  9. Unread #5 - Feb 7, 2008 at 9:58 PM
  10. Olan14
    Joined:
    Jan 26, 2007
    Posts:
    581
    Referrals:
    0
    Sythe Gold:
    0

    Olan14 Forum Addict

    Basic string parser

    Code:
    #include  #include  #include  #include   void getline(char[]); void getsubstr(char[], int, int); void handleop(char[]);  int end = 0;  int main() {     while (1) {         printf(&quot;%s&quot;, &quot;CALC> &quot;);         char[] line;         getline(line);         handleop(line);         if (end) {             break;         }     }     return 0; }   void getline(char[] buf) {     int c;     int idx = 0;     while ((c = getchar()) != '\n') {         buf[idx++] = c;     } }  void getsubstr(char[] buf, char[] src, int off, int len) {     assert(strlen(buf) >= off && strlen(buf) >= off + len && strlen(buf) >= len);     memcpy(buf, src + off, off + len); }  void handleop(char[] line) {     assert(strlen(line) >= 7);     char[] op;     getsubstr(line, 0, 3);     char[][2] rawnums;     getsubstr(rawnums[0], 4, 1);     getsubstr(rawnums[1], 6, 1);     int[] nums;     /* DANGER: atoi WILL RETURN 0 IF NON INTEGER */     nums[0] = atoi(rawnums[0]);     nums[1] = atoi(rawnums[1]);     if (!strcmp(op, &quot;ADD&quot;)) {         printf(&quot;%d&quot;, nums[0] + nums[1]);     } else if (!strcmp(op, &quot;MUL&quot;)) {         printf(&quot;%d&quot;, nums[0] * nums[1]);     } else if (!strcmp(op, &quot;DIV&quot;)) {         if (!nums[1]) {             fprintf(stderr, &quot;%s&quot;, &quot;cannot divide by 0!&quot;);             return;         }         float a = nums[0];         float b = nums[1];         printf(&quot;%d&quot;, (a / b));     } else if (!strcmp(op, &quot;POW&quot;)) {         int result = -123;         if (nums[0] == 1) {             result = 1;         } else if (nums[1] == 1) {             result = nums[0];         } else if (!nums[0] || !nums[1]) {             result = 0;         }         if (result == -123) {             result = 1;             for (int i = 0; i < nums[1]; ++i) {                 result *= result;             }         }         printf(&quot;%d&quot;, result);     } else if (!strcmp(op, &quot;BYE&quot;)) {         end = 1;     } else {         printf(&quot;%s&quot;, &quot;Invalid operation. Only ADD, SUB, MUL, DIV, POW, and BYE are supported.&quot;);     }     printf(&quot;%c&quot;, '\n'); }
    written by me.
     
  11. Unread #6 - Feb 7, 2008 at 10:03 PM
  12. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    Basic string parser

    Newlines and indentation are your friend :p
     
  13. Unread #7 - Feb 8, 2008 at 5:11 PM
  14. Olan14
    Joined:
    Jan 26, 2007
    Posts:
    581
    Referrals:
    0
    Sythe Gold:
    0

    Olan14 Forum Addict

    Basic string parser

    Ignorant. This forum has a faggotry text parsing system, that temoved my includes and my indentation.
     
  15. Unread #8 - Feb 8, 2008 at 6:35 PM
  16. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    Basic string parser

    Try pastebin.ca?
     
  17. Unread #9 - Mar 1, 2008 at 3:18 PM
  18. zstars
    Referrals:
    1

    zstars Guest

    Basic string parser

    If you don't want to use non-standard and legacy functions such as atoi, you can use std::stringstream for parsing and converting.

    std::stringstream ss("Hello 123");
    std::string str;
    int n;
    ss >> str >> n;

    std::cout << str << " " << n; // Would print "Hello 123"

    Or, if you have boost, boost::lexical_cast.
     
  19. Unread #10 - Mar 1, 2008 at 4:05 PM
  20. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Basic string parser

    Ah, nice find. Never knew of that before.
     
  21. Unread #11 - Mar 4, 2008 at 10:26 PM
  22. [OneWhoSighs]
    Referrals:
    0

    [OneWhoSighs] Guest

    Basic string parser

    You could of done it like this,
    Code:
       if(!memcmp(str1.c_str(),"ADD ",4))
        {
            int num1,num2;
            sscanf(str1.c_str(),"ADD %d %d",&num1,&num2);
            cout << num1+num2;
        }
    
     
< First VB6 Program (Slayer Counter) | NEED autoclicker code! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site