| | |
Creating a Command Line Parser
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Solved Threads: 0
I need help developing a program that acts as a command line parser. Essentially, my program needs to ask the user to input a command line: eg - A+B=C. It will then output the following:
A
+
B
=
C.
Although this seems basic, my program needs to recognize different groups of characters and output them correctly. The following are examples my program needs to take into consideration:
1) ABKLJFDLKJLKKDFJ++++++ajkadsl;fkjld;akjk=====daskflj342343
1a)
ABKLJFDLKJLKKDFJ
++++++
ajkadsl
;
fkjld
;
akjk
=====
daskflj342343
Explanation: program groups letters and characters together, but moves to next line when a symbol is reached.
2) It also must recognize whitespace: A + B = C
2a)
A
+
B
=
C
3) It must recognize a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6]
3a)
D_1
=
[1 3; 2 3 4; ; 4 5 6]
4) If the user forgets the second bracket to a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6
4a)
D_1
=
Need ']' to close matrix
5) A regular sentence: Hello How are you
5a)
Hello
How
are
you
6) Anything including invalid segments. The only valid operator segments are +-/* Invalid operator segments could be: +%-, +&!
Valid number segments can start with '.' or a digit. an invalid number would be: 3.abc, which should be parsed as 3. and then abc. Only numbers, whitespace, and ';' should be allowed in the whitespace. a sequence of ';' and ',' is valid: ;;;,;;;,,,;
I have worked on a pseudocode for the main part of my program thus far, and I think I have all the functions named within it that I would need. I was wondering if someone could help me develop the functions correctly for my program, thanks!
Below is my pseudocode:
A
+
B
=
C.
Although this seems basic, my program needs to recognize different groups of characters and output them correctly. The following are examples my program needs to take into consideration:
1) ABKLJFDLKJLKKDFJ++++++ajkadsl;fkjld;akjk=====daskflj342343
1a)
ABKLJFDLKJLKKDFJ
++++++
ajkadsl
;
fkjld
;
akjk
=====
daskflj342343
Explanation: program groups letters and characters together, but moves to next line when a symbol is reached.
2) It also must recognize whitespace: A + B = C
2a)
A
+
B
=
C
3) It must recognize a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6]
3a)
D_1
=
[1 3; 2 3 4; ; 4 5 6]
4) If the user forgets the second bracket to a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6
4a)
D_1
=
Need ']' to close matrix
5) A regular sentence: Hello How are you
5a)
Hello
How
are
you
6) Anything including invalid segments. The only valid operator segments are +-/* Invalid operator segments could be: +%-, +&!
Valid number segments can start with '.' or a digit. an invalid number would be: 3.abc, which should be parsed as 3. and then abc. Only numbers, whitespace, and ';' should be allowed in the whitespace. a sequence of ';' and ',' is valid: ;;;,;;;,,,;
I have worked on a pseudocode for the main part of my program thus far, and I think I have all the functions named within it that I would need. I was wondering if someone could help me develop the functions correctly for my program, thanks!
Below is my pseudocode:
c Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::cin; using std::endl; int main() { cout << " Welcome to My Command Line Parser!" << endl; cout << " XXXXXXXXXX, XXXXXXXXXXXXXXXXXXXXXX" << endl; cout << " XXXXXXXX, 2007\n" << endl; cout << "\n" << endl; cout << "Input a Command Line\n"; cin.getline(buffer, 500); char piece[500]; int st = 0, ed = 0, err_code = 1; while( buffer[st]!=0 ) { st = find_first_element( ); if (buffer[st] == 0) err_code = 0; ed = find_last_element( st ); // err_code can be set inside if (err_code == 0) break; else { _copy_segment(piece, st, ed); _display_segment(piece); st = ed + 1; } } return err_code; } //Functions
•
•
•
•
1) ABKLJFDLKJLKKDFJ++++++ajkadsl;fkjld;akjk=====daskflj342343
1a)
ABKLJFDLKJLKKDFJ
++++++
ajkadsl
;
fkjld
;
akjk
=====
daskflj342343
Explanation: program groups letters and characters together, but moves to next line when a symbol is reached.
•
•
•
•
2) It also must recognize whitespace: A + B = C
2a)
A
+
B
=
C
•
•
•
•
3) It must recognize a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6]
3a)
D_1
=
[1 3; 2 3 4; ; 4 5 6]
•
•
•
•
4) If the user forgets the second bracket to a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6
4a)
D_1
=
Need ']' to close matrix
•
•
•
•
5) A regular sentence: Hello How are you
5a)
Hello
How
are
you
•
•
•
•
6) Anything including invalid segments. The only valid operator segments are +-/* Invalid operator segments could be: +%-, +&!
Valid number segments can start with '.' or a digit. an invalid number would be: 3.abc, which should be parsed as 3. and then abc. Only numbers, whitespace, and ';' should be allowed in the whitespace. a sequence of ';' and ',' is valid: ;;;,;;;,,,;
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Solved Threads: 0
•
•
•
•
What about them? Would aple banena charry be acceptable as a sentence? I think this would be beyond the scope of your program.
I am having trouble implementing all this with the current arrangement I have. I looked up the functions you suggested to me and understand what they do. But I am unclear as to how to implement them into my main function. am I going to have to do some sort of 'if' statement?
c Syntax (Toggle Plain Text)
if isalpha() = 0 { cout << endl; }
I'm just really confused as to where I need to begin with this current program that I have and am not sure where to start. thanks!
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Solved Threads: 0
help! this is due in a couple of hours and I just have a problem with the matrix. even when the matrix is valid my code says it is invalid, can anyone help? thanks!
c Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::cin; using std::endl; bool Word_Tracker(char a); bool Symbol_Digit_Word_Tracker(char a); bool Operator_Checker(char a); bool Number_Checker(char a); bool Is_Matrix(char a); bool Characters_Allowed_Matrix(char a); bool Comma_Semicolon(char a); void Clears(char a[], int size); void First_Whitespace(char a[], int& pos); void Display(char a[]); int main() { char string[500]; cout << " Welcome to My Command Line Parser!" << endl; cout << " Raymond Guido, Northwestern University" << endl; cout << " Copyright, 2007\n" << endl; cout << "\n" << endl; cout << "Input a Command Line\n"; cin.getline(string, 500); int x = 0; char word[500] = {0}; char oper_ator[500] = {0}; char number[500] = {0}; char matrix[500] = {0}; char comma_semicolon[500] = {0}; while( string[x]!=0 ) { First_Whitespace(string,x); if (Operator_Checker(string[x]) == true) { while (Operator_Checker(string[x]) == true) { oper_ator[x]= string[x]; x ++; } Display(oper_ator); Clears(oper_ator,500); } else if (Word_Tracker(string[x]) == true) { while ((Word_Tracker(string[x]) == true || Symbol_Digit_Word_Tracker(string[x]) == true)) { word[x]= string[x]; x ++; } Display(word); Clears(word,500); } else if (Number_Checker(string[x]) == true) { while (Number_Checker(string[x]) == true) { number[x]= string[x]; x ++; } Display(number); Clears(number,500); } else if (Is_Matrix(string[x]) == true) { while (Is_Matrix(string[x]) == true || Characters_Allowed_Matrix(string[x]) == true) { matrix[x]= string[x]; x ++; } if ( string[x] == ']') { matrix[x]= string[x]; x ++; Display(matrix); Clears(matrix,500); } else { cout << "Invalid Matrix" << endl; break; } } else if (Comma_Semicolon(string[x]) == true) { while (Comma_Semicolon(string[x]) == true) { comma_semicolon[x]= string[x]; x ++; } Display(comma_semicolon); Clears(comma_semicolon,500); } else { cout << "\n" <<"You have entered invalid operators"; break; } } cout << "\n" << "Command Line Separated" << endl; } //Functions void First_Whitespace(char a[], int& pos) { while(a[pos]==' ') { pos ++; } } bool Comma_Semicolon(char a) { bool b; if ((a == ';') || (a == ',')) { b = true; return b; } else { b = false; return b; } } bool Number_Checker(char a) { bool b; if ((a == '.') || (a >= '0' && a <= '9')) { b = true; return b; } else { b = false; return b; } } bool Operator_Checker(char a) { bool b; if ((a == '+') || (a == '-') || (a == '*') || (a == '/') || (a == '=')) { b = true; return b; } else { b = false; return b; } } bool Word_Tracker(char a) { bool b; if ((a >= 'A' && a <= 'Z') || (a >= 'a' && a <= 'z')) { b = true; return b; } else { b = false; return b; } } bool Symbol_Digit_Word_Tracker(char a) { bool b; if ((a == '.') || (a == ',') || (a== '_') || (a >= '0' && a <= '9')) { b = true; return b; } else { b = false; return b; } } bool Is_Matrix(char a) { bool b; if (a == '[') { b = true; return b; } else { b = false; return b; } } bool Characters_Allowed_Matrix(char a) { bool b; if ((a == ' ') || (a >= 0 && a <= 9) || (a == ';')) { b = true; return b; } else { b = false; return b; } } void Clears(char a[], int c) { for(int b=0; b<c; b++) a[b] = 0; } void Display(char a[]) { char *b = a; while( *b == 0) { b ++; } cout << b << endl; }
You need to put single quotes around 0 and 9 for them to be treated as characters...
C++ Syntax (Toggle Plain Text)
bool Characters_Allowed_Matrix(char a) { bool b; if ((a == ' ') || (a >= '0' && a <= '9') || (a == ';')) { b = true; return b; } else { b = false; return b; } }
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- HELP!!!need help with command line arguments and creating a package in java. HELP!!!! (Java)
- Help with Creating a Command Line Menu (C++)
Other Threads in the C++ Forum
- Previous Thread: TAX processing system suggestion...
- Next Thread: Exceptions
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






