TRENDING NEWS

POPULAR NEWS

Help With Parsing In C And Use Of Strok Function

Help with Parsing in C and use of strok function?

int is_phone_number(char* string)
This function will take in a string and return 1 if it looks like a phone number 0 otherwise. A phone number takes the form “(xxx)-xxx-xxxx” where the x’s are digits 0-9. So for example (123)-456-7890 is a valid phone number while 123 456-7890 is not.

You should also write a main function that parses a text document and prints out all of the phone numbers found. Hint, look up the strtok function.


Sample input:
Please call me at (123)-456-789 sometime tonight.

Sample output:
Phone number : (123)-456-7890

Using the strtok function?

I am working on a shell program, and am trying to parse a line. I learned Java and assembly, but am asked to do this in C++ within linux. :/

I found in the C++ library that you can use the strtok function to seperate sequenced characters with defined delimiters. When I put this into eclipse, it keeps giving me a "Function 'strtok' could not be resolved" error.

#include
#include
#include
#include
#include
using namespace std;

int main() {
char str[100];
char * pch;
//char* dummy;

cout << "Welcome the Command Line Interface" << endl; // prints Welcome to Brian's Command Line Interface
cout << "\n" << ">";

for(;;){
cin.getline(str, 100); // receive command from console
cout << str; //repeat output to console

pch = strtok(str," "); //check command
if (pch == "exit"){break;} //check for exit command

while (pch != NULL){
cout << pch << endl;
pch = strtok(NULL, " ");
}
}
return 0;
}

http://www.cplusplus.com/reference/cstring/strtok/

C parsing command line options?

main() has two parameters. These are often described as int argc and char *argv[], I like to call them int numArgs and char *sArgs[], because that makes more sense to me.

The first is a count of the arguments on the command line, the second is an array of char * pointers to each argument as a zero terminated string. The name of the program is always the first argument so the count will always be at least one. So if the user types this:

foob -t hello

The count will be 3 and the sArgs[0] is "foob", sArgs[1] is "-t" and sArgs[3] is "hello". You search the array of arguments looking for a '-' or whatever rules you want to apply for your parameters.

Note if the user types this:

foob - t -h ello

The count will be 5 and the strings will be "foob", "-", "t", "-h" and "ello". The command line processor simply runs through the line with strtok(), splitting it into strings wherever it finds space. It doesn't treat '-' differently to any other character.

Programming: Parsing a string?

This one brings back memories! What your teacher is asking for is an "Object Oriented" solution for an Expression Evaluator. Ours was done in MFC with a Document/View architecture, but it can be applied in any language. We weren't told how to do it, but we were given clues. This solution is probably overboard for what your instructor is asking for, since my implementation involved any number of parenthesis, with modulus, exponents, factorials, the whole nine yards. There were equations that failed our prof's "test" executable provided to us for comparison, which I caught and implemented correctly.

There is a grammar to the expression, there is precedence that takes place (Lexer), and the expression is tokenized into Objects and stored together (polymorphism), and then a reduction of objects. The base class Operator is inherited by three classes, Infix Postfix and Prefix. Under infix, you have addition, subtraction, multiplication and division. Postfix you have factorials. Prefix you have Identity and Negation. These are all classes inheriting from Postfix/Infix/Prefix. I remember the key bug that was the difference between my working solution and the prof's bugged application was that I converted all subtraction into additions and applied negation to the operand. Basically if you type -4-5= then it is converted into 0 + -4 + -5.

It would be cheating to show you my code, but I can provide a UML diagram from back then which should help a great deal. Check this out: http://i41.tinypic.com/a15xev.gif

You will see methods that take parameters LHS and RHS. This refers to left-hand-side and right-hand-side of an operation. The reduction phase looks for the base precedence and evaluates one operation at a time, modifying the token list until the only object left is an operand.

If you become extremely stuck, please feel free to contact me. I hope this helps. This was some of my best work in college; that is, illustrating the prof's "proof" is flawed.

I need help in the following project in c++?

This sounds like school work, its a fairly simple program. you will probably want to use the cout and cin function which needs you to include iostream.h

simply use cout to print to screen, ask for input etc. then take the input using cin and store it to a int variable.

a good way to do it would be to ask the user what they wish to do, eg subtract, divide and take that operator first, a symbol or a number to show choice. then the next line asks for the numbers to be input.

eg:

---------

Hello. what do you wish to do:
1 - subtract
2 - divide
3 - etc etc

>

-------------

then use an if function or maybe a switch to check what their input was and do the artithmatic.

so a very basic prgram may look like this:
it may have errors i havant checked it.

-----------------

#include

int main()
{
int number;

cout << "Hello, input a number" << endl;
cin >>number;
return 0;
}

------

EDIT; i re read the req, you might find inputting the selection to a char better, then check the char to see what symbol is stored in it, eg + / -. and maybe use a char array for the number input. simple take the char array, check for the space. split the numbers into 2 and caste then to an int. sounds complicated but it isnt to hard.

sorry about spelling mistakes.

TRENDING NEWS