hi,
i need to write a c++ program that takes input of a expression like say

y = 5 + 2b;

and gives out the output

y = 5 + 2b

it uses linked lists and the expression can be composed of integers, variables and operators. the encoding of tokens can be done by variables representing as negative integers like say -1 to -26 and other tokens can be represented by negative numbers less than -26.

I dont know where to get started for a program like this, I need help figuring it out.

hmm.. this is wicked easy

cin >> expression;
cout << expression;

I dont think you understood it correctly. I need to use lists and the input isnt the same as the output
input is y tab space =3+ 2spaces 2b;
and output should be y = 3 + 2b

like the input can have many spaces in between but the output can have only one. and the input ends with a semicolon, if i enter only a semicolon as an input, the program suppose to exit.

The first thing you need to do is to be sure you understand the problem and can express it in your own words. Here's an example.

Accept input as a string. The string will represent an expression. The expression input will be terminated by a semicolon. Once you have the expression break down the expression into tokens. Each token will be stored as the data member of node in a list. Each token can be either a variable, an operator or an integer. This process will remove all spaces from the input. Finally you want to display the exrpession using the data members from the list, separating each token by a single space, and omitting the terminal semicolon.

Once you have created your own problem description then you can expand or specialize each statement in the problem description until you have it in code. I like to use the statements in my problem description as comments in my code, like this:

//The string will represent an expression.
string expression;

//Accept input as a string. Each expression in the string will be terminated by a semicolon.
getline(cin, expression, ';');

etc.

Then when I'm all done I go back and remove the comments I don't want to retain.

Comments
good (iamthwee)

The first thing you need to do is to be sure you understand the problem and can express it in your own words. Here's an example.

Accept input as a string. The string will represent an expression. The expression input will be terminated by a semicolon. Once you have the expression break down the expression into tokens. Each token will be stored as the data member of node in a list. Each token can be either a variable, an operator or an integer. This process will remove all spaces from the input. Finally you want to display the exrpession using the data members from the list, separating each token by a single space, and omitting the terminal semicolon.

Once you have created your own problem description then you can expand or specialize each statement in the problem description until you have it in code. I like to use the statements in my problem description as comments in my code, like this:

//The string will represent an expression.
string expression;

//Accept input as a string. Each expression in the string will be terminated by a semicolon.
getline(cin, expression, ';');

etc.

Then when I'm all done I go back and remove the comments I don't want to retain.

yeah I understood what you are saying, its getting very clear now but i have few more questions. like if i i want the tokens to be variables, numbers and operators. i can i use switch for every single one of them but for variables, how do i do it? coz there are 26 alphabets. thats why the program states that i use -1 to -26 to represent variables and -27 to ... for operators. if so, how do i use this scheme to encode the tokens. like iam having trouble assigning variables to numbers and numbers to variables and so on. thanks for ur response

If you get the token 'A', that is the same as the number 65 (ASCII A == numeric value 65
In other words:

(char)'A' = (int) 65
(char)'B' = (int) 66
(char)'C' = (int) 67
(char)'D' = (int) 68
(char)'E' = (int) 69
etc.

(char)'a' = (int) 97
(char)'b' = (int) 98
(char)'c' = (int) 99
(char)'d' = (int) 100
(char)'e' = (int) 101
etc.

Therefore, if your token ch contains 'B' you can convert it easily to your 'variable' value of -2 with

var = -(ch - (int)'A');

hmm.. this is wicked easy

cin >> expression;
cout << expression;

LOL. Is there point or use for this program?

I dont think you understood it correctly. I need to use lists and the input isnt the same as the output
input is y tab space =3+ 2spaces 2b;
and output should be y = 3 + 2b

This article has been dead for over six months. Start a new discussion instead.