Home c++ C++ Stack implementation

C++ Stack implementation

Author

Date

Category

Task: Write a program to simulate the T-shaped sorting node on the railway using the stack container class. The program should divide into two directions composition consisting of two types of wagons (for each direction the composition of the wagons of one type is formed). Provide the ability to enter the source data from the keyboard and from the file.

Hello everyone needs help. Enter and display data from the keyboard, output from the file I have already implemented I can not understand how to make it helped to decide. Here is an example code

# include & lt; iostream & gt;
#Include & lt; fstream & gt;
CLASS CMYSTACK {
Public:
  CMYStack ();
  ~ CMYStack ();
  Void Push (int d);
  INT POP ();
  BOOL ISEMPTY ();
  Void Print ();
Private:
  STRUCT NODE {
    int d;
    Node * P;
  };
  Node * Top;
};
CMYSTACK :: CMYSTACK (): TOP (NULL) {}
void CMYSTACK :: Push (int d) {
  Node * pv = new node;
  PV- & GT; D = D;
  PV- & GT; P = TOP;
  Top = PV;
}
INT CMYSTACK :: POP () {
  if (isempty ()) {
    Return -1;
  }
  int temp = top- & gt; d;
  Node * PV = TOP;
  Top = Top- & gt; p;
  Delete PV;
  RETURN TEMP;
}
BOOL CMYSTACK :: ISEMPTY () {
  RETURN TOP? False: True;
}
Void CMYSTACK :: Print () {
  While (TOP) {
    STD :: COUT & LT; & LT; Pop () & lt; & lt; '';
  }
  STD :: COUT & LT; & LT; STD :: ENDL;
}
CMYSTACK :: ~ CMYSTACK () {
  While (TOP) {
    Node * PV = TOP;
    Top = Top- & gt; p;
    Delete PV;
  }
}
Void EmptyStack (CMYSTACK & AMP; Stack);
INT MAIN () {
  CMYStack Trainbase, Oneetrain, Twotrain;
  std :: ifstream in ("trains.txt", std :: ios :: in);
  if (! In) {
    STD :: COUT & LT; & LT; "CANNOT OPEN FILE 'TRAIS.TXT' FOR READING" & LT; & LT; STD :: ENDL;
    Return 1;
  }
  While (! in.eof ()) {
    int pr;
    in & gt; & gt; PR;
    Trainbase.push (PR);
  }
  While (! trainbase.isempty ()) {
    int pr = trainbase.pop ();
    Switch (PR) {
    Case 1:
      ONETRAIN.PUSH (PR);
      Break;
    Case 2:
      Twotrain.push (PR);
      Break;
    }
  }
  Oneetrain.print ();
  Twotrain.print ();
  int _getch ();
  SYSTEM ("PAUSE");
  Return 0;
}
Void EmptyStack (CMYSTACK & AMP; Stack) {
  if (stack.isempty ()) {
    STD :: COUT & LT; & LT; "Stack IS Empty" & LT; & LT; STD :: ENDL;
  } else {
    STD :: COUT & LT; & LT; "Stack Is Not Empty" & lt; & lt; STD :: ENDL;
  }
}

Answer 1, Authority 100%

If your problem is only on how to implement and output from a file and output from the keyboard, it is easily solved if you write your code to the function, and simply call the function with the file object or from STD :: CIN:

void programmm (std :: iStream & amp; INPUT)
{
  CMYStack Trainbase, Oneetrain, Twotrain;
  Std :: Stack Trainbase, Oneetrain, Twotrain;
  int pr;
  while (INPUT & GT; & gt; PR) // here I entered the amendment
    Trainbase.push (PR);
  While (! trainbase.isempty ()) {
    int pr = trainbase.pop ();
    Switch (PR) {
      Case 1:
      ONETRAIN.PUSH (PR);
      Break;
      Case 2:
      Twotrain.push (PR);
      Break;
    }
  }
  Oneetrain.print ();
  Twotrain.print ();
}
INT MAIN ()
{
  std :: ifstream in ("trains.txt", std :: ios :: in);
  if (! In) {
    STD :: COUT & LT; & LT; "CANNOT OPEN FILE 'TRAIS.TXT' FOR READING" & LT; & LT; STD :: ENDL;
    Return 1;
  }
  // Call a function with a keyboard input program
  Programm (STD :: CIN);
  // or to enter from the file
  PROGRAMM (IN);
  Return 0;
}

I just rewrote your code with a small amendment, but I do not think that this is what requires your task

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions