| | |
Need help with C++ programming assignment, urgent.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
Me and a friend have been trying desperately to get this done, but we barely know where to start. We know we need the ifstream to read in from the file, but that's about it. Anyone got any tips for us, please? (Note: we're both fairly new to programming, so please put it as simple as possible)
A stack machine is a model for executing computer programs. There are compilers that translate a high-level computer language into a set of instructions to be executed using a stack machine model. A stack machine consists of operands, instructions, a stack, and an evaluation model.
Consider the following stack machine instructions and their purpose.
INSTRUCTION PURPOSE EXAMPLE
Push- push the value onto the stack push 8
Pushc- push the contents of the variable onto the stack push x
Add- pop two operands from the stack, add them, and push the result onto the stack push 6
push 10
add
Mult - pop two operands from the stack, multiply them, and push the result onto the stack push 61
push 38
mult
Sub pop two operands from the stack, subtract them (top element is subtracted from the second from the top), and push the result onto the stack. push 5
push 8
sub
Div pop two operands from the stack, divide them (top element is divided by the second from the top), and push the result onto the stack. push 5
push 10
div
Assn pops two values from the stack and assigns the value of the top element to the variable that is second from the top push 4
push x
assn
Dspy pops and prints the value at the top of the stack
push 10
dspy
Imagine a hypothetical compiler that translates a computer program written in a language called fluffy (funny language used for fun yall) into an equivalent set of stack machine instructions. These instructions are then executed by the stack machine's evaluation model to carry out the meaning of the computer program. Here are example fluffy programs and their equivalent stack instructions.
fluffy stack machine instructions
output 8 + 6 push 8
push 6
add
dspy
fluffy stack machine instructions
x = 4
y = (7+x)*9
output y push 4
push x
assn
push 7
pushc x
add
push 9
mult
push y
assn
dspy
For this assignment you are to write a program that will act as an interpreter for stack machine instructions. The input to your program will be a file containing stack machine instructions. Your program will execute those instructions. Any output generated by the instructions should display to the screen.
The basic algorithm for a stack machine evaluation model is:
1. Create a stack
2. Create a symbol table for the variables x,y, and z. This is an array of size 3. Array position 0 is for x, position 1 is for y, and position 2 is for z.
2. while there are more instructions to evaluate do
get next instruction
if the instruction is 'push' then push the operand onto the stack
if the instruction is 'pushc' then get value of operand from the symbol table and push it onto the stack
if the instruction is 'add', 'mult', 'div', or 'sub', pop two operands from the stack and perform the operation. push the result back onto the stack
if the instruction is 'assn' pop two operands from the stack, put the value of the top element in the symbol table for the second top element
if the instruction is 'dspy' pop an operand from the stack stack and print it to the screen
end do
fluffy
To run your program, the user will be prompted for a file name containing a list of instructions.
Your program will open the file, and read and execute the instructions, sending any output to the screen. You can assume the file contains an error-free and correct set of instructions.
Use either a Linked Stack or an array-based Stack to implement this program.
A stack machine is a model for executing computer programs. There are compilers that translate a high-level computer language into a set of instructions to be executed using a stack machine model. A stack machine consists of operands, instructions, a stack, and an evaluation model.
Consider the following stack machine instructions and their purpose.
INSTRUCTION PURPOSE EXAMPLE
Push- push the value onto the stack push 8
Pushc- push the contents of the variable onto the stack push x
Add- pop two operands from the stack, add them, and push the result onto the stack push 6
push 10
add
Mult - pop two operands from the stack, multiply them, and push the result onto the stack push 61
push 38
mult
Sub pop two operands from the stack, subtract them (top element is subtracted from the second from the top), and push the result onto the stack. push 5
push 8
sub
Div pop two operands from the stack, divide them (top element is divided by the second from the top), and push the result onto the stack. push 5
push 10
div
Assn pops two values from the stack and assigns the value of the top element to the variable that is second from the top push 4
push x
assn
Dspy pops and prints the value at the top of the stack
push 10
dspy
Imagine a hypothetical compiler that translates a computer program written in a language called fluffy (funny language used for fun yall) into an equivalent set of stack machine instructions. These instructions are then executed by the stack machine's evaluation model to carry out the meaning of the computer program. Here are example fluffy programs and their equivalent stack instructions.
fluffy stack machine instructions
output 8 + 6 push 8
push 6
add
dspy
fluffy stack machine instructions
x = 4
y = (7+x)*9
output y push 4
push x
assn
push 7
pushc x
add
push 9
mult
push y
assn
dspy
For this assignment you are to write a program that will act as an interpreter for stack machine instructions. The input to your program will be a file containing stack machine instructions. Your program will execute those instructions. Any output generated by the instructions should display to the screen.
The basic algorithm for a stack machine evaluation model is:
1. Create a stack
2. Create a symbol table for the variables x,y, and z. This is an array of size 3. Array position 0 is for x, position 1 is for y, and position 2 is for z.
2. while there are more instructions to evaluate do
get next instruction
if the instruction is 'push' then push the operand onto the stack
if the instruction is 'pushc' then get value of operand from the symbol table and push it onto the stack
if the instruction is 'add', 'mult', 'div', or 'sub', pop two operands from the stack and perform the operation. push the result back onto the stack
if the instruction is 'assn' pop two operands from the stack, put the value of the top element in the symbol table for the second top element
if the instruction is 'dspy' pop an operand from the stack stack and print it to the screen
end do
fluffy
To run your program, the user will be prompted for a file name containing a list of instructions.
Your program will open the file, and read and execute the instructions, sending any output to the screen. You can assume the file contains an error-free and correct set of instructions.
Use either a Linked Stack or an array-based Stack to implement this program.
Last edited by KOFSoldier; Oct 10th, 2009 at 6:02 pm.
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
3
#2 Oct 10th, 2009
Welcome to the real world. There is not free lunch.... sorry.
So
(a) either attend your classes and actually pay attention.
(b) decide that the class is not a good use of time and instead study some books in the library.
(c) Fail your degree/course.
But because this is the real world, I care very very little other than if you pick option (c), you just might be competing with me for a job, and then I will have a little smile on my face.
To go from zero knowledge to that assignment is about 10 hours of reading. (I would figure it is 6 lectures (50min) and corresponding work
(5 hours)). That is one long evening.
[Quick note: option (b) was taken by a a REALLY bright individual at my college and he did really well -- although his invidual exam marks had a huge distribution]
So
(a) either attend your classes and actually pay attention.
(b) decide that the class is not a good use of time and instead study some books in the library.
(c) Fail your degree/course.
But because this is the real world, I care very very little other than if you pick option (c), you just might be competing with me for a job, and then I will have a little smile on my face.
To go from zero knowledge to that assignment is about 10 hours of reading. (I would figure it is 6 lectures (50min) and corresponding work
(5 hours)). That is one long evening.
[Quick note: option (b) was taken by a a REALLY bright individual at my college and he did really well -- although his invidual exam marks had a huge distribution]
Last edited by StuXYZ; Oct 10th, 2009 at 6:24 pm.
experience is the most expensive way to learn anything
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#3 Oct 10th, 2009
•
•
•
•
Welcome to the real world. There is not free lunch.... sorry.
So
(a) either attend your classes and actually pay attention.
(b) decide that the class is not a good use of time and instead study some books in the library.
(c) Fail your degree/course.
But because this is the real world, I care very very little other than if you pick option (c), you just might be competing with me for a job, and then I will have a little smile on my face.
To go from zero knowledge to that assignment is about 10 hours of reading. (I would figure it is 6 lectures (50min) and corresponding work
(5 hours)). That is one long evening.
[Quick note: option (b) was taken by a a REALLY bright individual at my college and he did really well -- although his invidual exam marks had a huge distribution]
b) Teacher is actually a good teacher, his wording is a little unspecific though. Such as what to name the file I'll be reading in from.
I won't even address C.
Anyone with any actual help? As oppose to criticisms. When I read the "Please read this before posting" topic, it said that people here would help with questions, they just won't do it for you. Which I am fine with.
0
#4 Oct 10th, 2009
OK, valid help.
Can you presently write a program that implements a stack of integers, and the operations upon the stack?
Can you write code that will read from a file? Yes, you need to use the fstream library in order to us an ifstream to read the file. Note that some lines of your file will have an instruction and an operand (variable or value) and some may have just an instruction. To deal with this, you could read char by char until either space or newline, and figure out what you've got, or read in the whole line into a char array (string) and parse that (look for a space that indicates presence of an operand)
You'll need a branching mechanism to handle each instruction type, once you've isolated an instruction.
Take a shot at it. Start small, start simple. Get a bit working, then move on to the next part. Probably start with the basic stack, then the file reading.
Show us some work, and we'll move on from there.
Can you presently write a program that implements a stack of integers, and the operations upon the stack?
Can you write code that will read from a file? Yes, you need to use the fstream library in order to us an ifstream to read the file. Note that some lines of your file will have an instruction and an operand (variable or value) and some may have just an instruction. To deal with this, you could read char by char until either space or newline, and figure out what you've got, or read in the whole line into a char array (string) and parse that (look for a space that indicates presence of an operand)
You'll need a branching mechanism to handle each instruction type, once you've isolated an instruction.
Take a shot at it. Start small, start simple. Get a bit working, then move on to the next part. Probably start with the basic stack, then the file reading.
Show us some work, and we'll move on from there.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
0
#5 Oct 10th, 2009
Well ok then don't word the problem as if it is verbatim from the question sheet. Doesn't matter were you are tell us what you have got.
The problem can be broken down in to several parts (a) start
(b) read in the input (or a bit of it) (c) process that input. (d) give output.
I put (a) up there because that is were you will want to initialize state and things like that.
Now were are you ??? What have you managed? What specifically have you failed to do ? The basic rules here are post something, pseudo code if you wish, C++ that doesn't work, just about anything.
-- Programming has a lot of unspecifics, if there is no file name then it is a runtime variable. Ie, the user gets to choose, but if it makes it simpler you choose a fixed value e.g "script.txt" Then generalize you program later.
-- When you read the post forum rules it said don't do peoples homework for them but help them understand what they are stuck on.
The problem can be broken down in to several parts (a) start
(b) read in the input (or a bit of it) (c) process that input. (d) give output.
I put (a) up there because that is were you will want to initialize state and things like that.
Now were are you ??? What have you managed? What specifically have you failed to do ? The basic rules here are post something, pseudo code if you wish, C++ that doesn't work, just about anything.
-- Programming has a lot of unspecifics, if there is no file name then it is a runtime variable. Ie, the user gets to choose, but if it makes it simpler you choose a fixed value e.g "script.txt" Then generalize you program later.
-- When you read the post forum rules it said don't do peoples homework for them but help them understand what they are stuck on.
experience is the most expensive way to learn anything
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
-1
#6 4 Days Ago
Hey sorry to reply so late to you all but I actually did get a 100 on it. vmanes advice actually helped alot. Instead of tackling the whole thing head on, I broke down each part individually and kind of combined them in the end. It actually wasnt all that hard and definitely wasn't 6 hours worth of lecturing (but then again, I already took fundamentals).
![]() |
Similar Threads
- Need help with C programming assignment (will pay money) (C)
- Help with programming assignment (Python)
- Windows programming task (C)
- i need help getting started with my programming assignment (C++)
Other Threads in the C++ Forum
- Previous Thread: I need help with this program. I can't figure out the errors.
- Next Thread: I badly need help with this program. PLEASE someone help me.
| Thread Tools | Search this Thread |
access activation api array arrays assignment auto binary bitmap blogger blogging bug c# c++ char char* class code codebox combo computer console crashcourse daniweb data desktop development dialog download dropdownlist encryption equation error faq file format fstream function functions game gdata google graph hash html hyper ifstream int java linus linux math method microsoft module mosaic multiple net newbie news os output password php prime problem product proficiency program programming projects python read recursion recursive reuse rss scroll single snakes snippet source string tagging template test text tree tutorial university url user vb vector virus visual web win32 windows xml







