Program help



Hi!I have a slight problem with this program and I will be glad if anyone can help me:

The elements of the stacks have to be sorted with the bubble sort.
Creates a dynamic structure deque,which contains the elements of the two stacks and finds the smallest number,higher than the average value of all elements in the deque.
Function that reades the elements of the 2 stacks and the deck.The problem is that the sort function and the built function,which I use to create the new deque and to find the smallest number,higher than the average value of the elements of the deque don`t work.
.Thanks in advance!


#include<iostream>
#include<fstream>
using namespace std;

fstream fp;
//Files to read

char t1[]="F1.txt";

char t2[]="F2.txt";


//Files for record

char tt1[]="FF.txt";


//Deque structure

struct dek {

int key;

dek *next;

} *left = NULL, *right=NULL;


//Initialising of the 2 stacks

struct elem{

int key;


elem *next;

} *start1=NULL,*start2=NULL,*p;


void add_1(int n) //Adding elements in the first stack

{

elem *p = start1;

start1 = new elem;

start1->key = n;

start1->next = p;

}

void add_2(int n) //Adding elements in the second stack

{

elem *p = start2;

start2 = new elem;

start2->key = n;

start2->next = p;
}

// Прехвърляне на 1 стек в 1 файл

elem* readFile1(elem *start1, char *t1)

{
elem e; //Local variable of a stack type
int br1; //Local variable,whichs finds the number of elements in the stack

fp.open("F1.txt",ios::binary,ios::app| ios::in|ios::out); //Opens the binary file for reading,recording and applying


if (!fp.fail())

{

cout << "Greshka pri otvarqne! ";

exit(0);

}

fp.write((char*)&e,sizeof(start1)); //1 stacks goes in the file


fp.seekg(0L,ios::beg);

fp.read((char*)&e,br1*sizeof(start1)); //Filling the structure
return start1;

fp.close();
}

elem* readFile2(elem *start2, char *t2)
{

elem e;
int br2;

fp.open("F2.txt",ios::binary,ios::app| ios::in);


if (!fp.fail()) /

{

cout << "Greshka pri otvarqne! ";

exit(0);

}


fp.write((char*)&e,sizeof(start2)); /

br2=(int)fp.tellg()/sizeof(start2);

fp.seekg(0L,ios::beg);
fp.read((char*)&e,br2*sizeof(start2));


return start2;
fp.close();
}




void sort (elem * start)

{

int min, rab;

for( elem* i=start; i->next; i=i->next)

{

min=i->key;

for( elem* j=i->next; j;j=j->next)



if(min>j->key)

{

rab=min;

min=j->key;

j->key=rab;

}



i->key=min;

}

}


// Adding elements in the deque

void push_l(elem *left,elem *right,int n)//Adds from the left

{

elem *p ;

p=left;

left=new elem;

left->key = n;

left->next = p;



if (right == NULL)
{


right=left;}

}

void push_r(elem *left,elem *right,int n) //Adds from the right

{

elem *p ;

p=right;

right=new elem;

right->key = n;

left->next = NULL;



if (left == NULL)



left=right;
else
p->next=right;

}


void list(elem *start) //Shows every stacks`s elements
{
if(start)
{
cout<<"\n Stekat e:\t";
elem *p=start;
while(p)
{
cout<<p->key<<"\t";
p=p->next;
}
}
}


void build (int n) //Creates the new deque wth the elements of the stack

{

elem * n1 = start1;

elem* n2 = start2;

while (n1 != NULL || n2 != NULL)

{

if (n1 && (!n2 || (n1->key < n2->key)))

{

push_l(n1,n1->next,n);

n1 = n1->next;

}

else

{

push_r(n1,n2->next,n);

n2 = n2->next;

}

}

}




void save_file( char *fname) //Records the outer results

{
int br1;
elem e;
fp.open("FF.txt",ios::binary| ios::out |ios::app |ios::in);

if (fp.fail())

{

cout << "Грешка при отваряне ";

exit(0);

}

fp.write((char*)&e,sizeof(elem));

br1=(int)fp.tellg()/sizeof(elem);

fp.seekg(0L,ios::beg);

fp.read((char*)&e,br1*sizeof(elem));

fp.close();


}


char menu()

{

char ch;
system("cls");
cout<<"////////////////Menu//////////////"<<endl;

cout<< "1. Typing the 2 stack`s elements"<<endl;

cout<< "2. Bubble sort of the stack`s elements"<<endl;

cout<< "3. Creating a dynamic structure deque "<<endl;

cout<< "4.Recording the final results in an uter file"<<endl;

cout<<"5.Reading the stacks"<<endl;

cout<<endl;

cout<< "-> -> Please select from 1 to 5 <- <- "<<endl;

cin>>ch;


return ch;


}


int main()

{

char ch;

int n=1;

int p=1;

do{

ch=menu();

// setlocale(LC_ALL, ".OCP");


switch (ch) {

case '1':

cout << "Stack 1" << endl; //List 1


while(n!=0)
{
cout<<"Type a number"<<endl;
cin>>n;

add_1(n);

}

readFile1(start1,t1);

list(start1);

cout << endl;

cout << "Stack 2" << endl; // List 2
while(p!=0)
{

cout<<"Type anumber<<endl;
cin>>p;

add_2( n);

}


readFile2(start2,t2);

list(start2);

break;

case '2':

cout << "Stek 1" << endl;


sort(start1);

list(start1);

cout << endl << endl;

cout << "Stek 2" << endl;

sort(start2);

list(start2);

cout << endl << endl;

break;

case '3':

cout <<"Dek:" << endl;

build(n);



break;

case '4':

save_file(tt1);

break;
case'5':
readFile1(start1,t1);
readFile2(start2,t2) ;


}


cin.get();

}

while(ch!='5');


return 0;
system("pause");

}



Topic archived. No new replies allowed.