i know the answer don't read this
i've written a programme and this was the question of it
we read the numbers and push them into the stack without printing them until we read a negative number at this time we stop reading and pop five items from the stack and print them if there are fewer than 5items in the stack we print error message after printing the five items we resume reading data and placing them in the stack when the end of the file is detected we print a message and the item remaining in the stack.
and i want to do it in two files one is a .dat file and other is to read from the .dat file i've did it and put the two files in the same directory but it doesn't work and there are my 2files:
Code:
//file name lab66.dat
#include <stdio.h>
#include <malloc.h>
struct node{
int data;
node *link;
node(int x){
data = x;
link=NULL;}
};
class stack{
private:
node *tos;
int s;
public:
int _size(){ //return the size of the stack
return s;}
stack(int x=0){ //constructor
tos=NULL;
s=x;}
void push(int x){
node *ptr;
if(!tos){
tos= new node(x); return;}
ptr=new node(x);
ptr->link=tos;
tos=ptr;
++s;
}
int pop(){
int x;
node *ptr;
ptr = tos;
if(!ptr){printf("The stack is empty\n");
return 0;}
x=ptr->data;
tos=ptr->link;
delete ptr;
--s;
return x;
}
void print(){
if(!tos)
{printf("Cannot print The stack is empty\n");
return;}
node *ptr;
ptr=tos;
while(ptr){
printf("%d ",ptr->data);
ptr=ptr->link;
}
printf("\n");
}
};
//file2 lab6.cpp
#include <stdio.h>
int main(){
FILE *f;
int r,i;
f=fopen("lab66.dat","r");
if(f==NULL){
printf("File doesn't exist\n");
return -1;}
r=fscanf(f,"%d",&i);
stack t;
while(r!=EOF){
if(i>=0)
t.push(i);
else {if(t._size>=5)
for(int j=0;j<5;j++) printf("%d",t.pop());
printf("\n\n");
else
printf("Error\n");}
while(t._size()>0)
t.pop();
while(t._size()>0)
printf("%d", t.pop());
r=fscanf(f,"%d",&i);
}
return 0;
}
shall i include the two files in one file and make a new file that have the test data numbers if so how can i do it can u please help me?