I need help implementing the stack, I\'m confused on where to start. For machine
ID: 3828933 • Letter: I
Question
I need help implementing the stack, I'm confused on where to start. For machine.cpp im confused about the TODO: implement me im not sure what i need to do. Its a virtual machine and the other instruction for it is
`push n` Push the constant `n` onto the operand stack
`pop` Pop the top operand on the stack, discarding it
`copy` Push a copy of the top operand on the stack
`add` Pops two operands, pushes the result of adding the first to the second
`sub` Pops two operands, pushes the result of subtracting the first from the second
`mul` Pops two operands, pushes the result of multiplying the second by the first
`div` Pops two operands, pushes the quotient of dividing the second by the first
`rem` Pops two operands, pushes the remainder of dividing the second by the first
`print` Pops one operand, prints the value to standard output
`read` Read one value from standard input, push it on the stack
machine.hpp
// Operation codes. These represent operations that can be executed
// by the virtual machine.
enum
{
// Basic push/pop
push_op, // Push a constant operand
pop_op, // Pop an operand
copy_op, // Copy the top operand
// Arithmetic
add_op, // Add the top two operands
sub_op, // Subtract the top from the lower operands
mul_op, // Multiply the top two operands
div_op, // Divide the lower from the top
rem_op, // Remainder of lower divided by the top
// Misc.
print_op, // Pop the top value and print.
read_op, // Read a value, push it.
halt_op, // Stop executing
};
// Represents an instruction. Every instruction has an operation
// code (one of the values above), and an integer operand.
struct Instruction
{
Instruction(int o, int a)
: op(o), arg(a)
{ }
Instruction(int o)
: op(o)
{ }
int op;
int arg;
};
// Represents the virtual machine. Each machine instance contains
// the source code for a single program.
struct Machine
{
Machine(std::istream&);
void run();
// Program control
Instruction fetch();
// Operand stack methods
int top() const;
void push(int);
int pop();
// Operations
void copy();
void add();
void sub();
void mul();
void div();
void rem();
void print();
void read();
void halt();
Vector<Instruction> prog; // A loaded program
Stack<int> stack; // The operand stack
// Registers
int pc;
};
#endif
machine.cpp
// Returns the op code found in the first n characters of s. Throws an
// exception if the operation name is invalid.
static int
get_op(String const& s)
{
// A lookup table that maps from strings to opcodes.
static std::map<String, int> ops {
{"push", push_op},
{"pop", pop_op},
{"copy", copy_op},
{"add", add_op},
{"sub", sub_op},
{"mul", mul_op},
{"div", div_op},
{"rem", rem_op},
{"print", print_op},
{"read", read_op},
{"halt", halt_op},
};
auto iter = ops.find(s);
if (iter == ops.end()) {
String msg = "no such opcode '" + s + "'";
throw std::runtime_error(msg);
}
return iter->second;
}
int
get_arg(String const& s)
{
if (s.empty())
return 0;
else
return std::stoi(s);
}
Machine::Machine(std::istream& is)
{
// Parse instructions from input.
while (is) {
String s;
getline(is, s);
if (!is)
break;
// Search for a ';', indicating a comment and strip that from the line.
std::size_t k = s.find(';');
if (k != String::npos)
s = s.substr(0, k);
// Skip empty lines.
if (s.empty())
continue;
// Parse out the opcode and operand.
std::stringstream ss(s);
std::string opstr, argstr;
ss >> opstr >> argstr;
int op = get_op(opstr);
int arg = get_arg(argstr);
Instruction ins(op, arg);
prog.push_back(ins);
}
}
void
Machine::run()
{
// Start the pc at the first instruction.
pc = 0;
while (pc != prog.size()) {
// Get the next instruction.
Instruction ins = fetch();
// "Decode" and execute the instruction.
switch (ins.op) {
case push_op:
push(ins.arg);
break;
case pop_op:
pop();
break;
case copy_op:
copy();
break;
case add_op:
add();
break;
case sub_op:
sub();
break;
case mul_op:
mul();
break;
case div_op:
div();
break;
case rem_op:
rem();
break;
case print_op:
print();
break;
case read_op:
read();
break;
case halt_op:
halt();
break;
}
}
}
Instruction
Machine::fetch()
{
return prog[pc++];
}
int
Machine::top() const
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::push(int n)
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
int
Machine::pop()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::copy()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::add()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::sub()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::mul()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::div()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::rem()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::print()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::read()
{
// TODO: Implement me!
throw std::logic_error("not implemented");
}
void
Machine::halt()
{
pc = prog.size();
}
stack.hpp
#include <stack>
// TODO: You *must* implement a stack.
template<typename T>
using Stack = std::stack<T>;
class Stack{
public:
//default constructor
Stack<T> {s1;}
//copy constructor
Stack<T> {s2 = s1;}
//copy assignment
{
s1 = s2
}
// A member function to determine if the stack is empty. This member function
// shall not modify its object.
bool b = l1.empty();
// A member function function that returns the number of elements in
// the stack. Returns a std::size_t value. This member function shall
// not modify its object.
std::size_t n = l1.size();
// Element access.
int x1 = s.top(); // Constant
int& x2 = s.top(); // Non-constant
// Push. Adds an element to the top of the stack.
s.push_front(0);
// Pop. Removes an element from the top of the stack. Does not return a
// value. The stack shall not be empty.
s.pop();
private:
};
main
#include "machine.hpp"
#include <iostream>
#include <fstream>
int
main(int argc, char* argv[])
{
if (argc < 2) {
std::cerr << "error: vm input-file";
return 1;
}
std::ifstream ifs(argv[1]);
Machine vm(ifs);
vm.run();
}
Explanation / Answer
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
main()
{
int ch;
stack st;
while(1)
{
cout <<" 1.push 2.pop 3.display 4.exit Enter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.