• Log in or sign up for an account here: https://www.sythe.org/login

Basic string parser

Govind

The One Musketeer

Highly Respected
Mudkips
Retired Administrator
Joined
Apr 22, 2005
Sythe Gold
23
Messages
7,825
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
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
 
Basic string parser
It would be tidier with a switch :P
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Basic string parser
That wouldn't compile on GCC with a switch statement.
 
Basic string parser
Any chance of a C version for me to study?
 
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.
 
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.
Newlines and indentation are your friend :P
 
Basic string parser
Newlines and indentation are your friend :P

Ignorant. This forum has a faggotry text parsing system, that temoved my includes and my indentation.
 
Basic string parser
Ignorant. This forum has a faggotry text parsing system, that temoved my includes and my indentation.
Try pastebin.ca?
 
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.
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Basic string parser
Ah, nice find. Never knew of that before.
 
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;
    }
 
Runescape Gamblingrunescape gambling
Back
Top