You have been supplied a file with a data in it. Each line containes a character
ID: 3575068 • Letter: Y
Question
You have been supplied a file with a data in it. Each line containes a character (A or D) and an integer. Your program should read the file, line at a time, reading the character (A or D) and the integer. If an A is read, the following integer should be pushed onto the stack. If a D is read, the following integer should be ignored, and the stack popped.
When your program reaches end of file, it should print out the contents of the stack. With the supplies data1.txt file, the output should be:
7 5 3
This code should be written for arbitrary data, not specifically for this sequence of data.
The only place you need to write a code is marked YOUR CODE GOES HERE.
ACTUAL CODE:
DATA1.TXT FILE:
Problem 1 Stack push, pop, display You have been supplied an IStack class with the following characteristics It holds only integers It recognizes only the following functions Stack constructs an empty stack void push (int x) pushes the argument onto the stack void pop pops and discards the top item int top returns a copy of the top item. if this function is called on an empty stack, the program will crash bool empty returns true if the stack is empty bool full. returns true if the stack is full that is pushing onto a full stack the program will crash. These functions are sufficient to handle any sequence of data without crashing your program however, only a small stream of data is provided for testing and you need not protect your code against all possible inputs You have been supplied a file with data in it (datal.txt) Each line contains a character (A or D) and an integer Your program should read the file, one line at a time, reading the character (A or D) and the integer If an A is read, the following integer should be pushed onto the stack If a D is read, the following integer should be ignored and the stack poppe When your program reaches end of file, it should print out the contents of the stack. With the supplied data1. txt file, the output should be (not including the comment marks, of course) Your grader may of course, replace data1.txt with a file of his own, to make sure your code is written for arbitray data, not specifically for this sequence of data The only place you need to add code is marked YOUR CODE GOES HERE Be sure to remember to close the file upon completion of processingExplanation / Answer
//Note file as per given form in question
//Headers used
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include <fstream>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
//stackfile class for stack operations
class stackfile
{
int arraystack[100]; //stack declaration
int tos; //top of stack
public:
stackfile()
{
tos=-1;
}
void push(int x) //push elemnet
{
if(tos > 99)
{
cout <<"stack is over flow"<<endl;
return;
}
arraystack[++tos]=x;
cout <<"inserted" <<x<<endl;
}
void pop() //delete element from stack top
{
if(tos <0)
{
cout <<"stack is under flow"<<endl;
return;
}
cout <<"deleted" <<arraystack[tos--]<<endl;
}
void display() //display stack elements
{
if(tos<0)
{
cout <<" stack is empty"<<endl;
return;
}
cout <<"Stack Elements"<<endl;
for(int i=tos;i>=0;i--)
cout <<arraystack[i] <<" ";
}
};
main()
{
int ch;
stackfile st;
//file open for read
ifstream infile;
infile.open("data1.txt");
string content;
//loop for check element is A or D to perform push /pop operations
while(infile >> content)
{
int store;
if(!content.compare("A")) //if content char is equal to A
{
infile >> content;
store=atoi(content.c_str()); //convert string to int
st.push(store); //call push operation
}
if(!content.compare("D")) /if content char is equal to D
{
st.pop(); //call pop operation
}
}
st.display(); //call display operation
return (0);
}
Output :
inserted5
deleted5
inserted7
inserted4
Stack Elements
4 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.