Cod sursa(job #1708473)

Utilizator champsteauaTrocan Gabriel champsteaua Data 27 mai 2016 10:16:46
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.52 kb

#include<iostream>
#include<string.h>
#include<fstream>
#include<string>
#include <cstdlib>

#include "complex.H"
#include "Object.h"
#include "Heap.h"
using namespace std;

ifstream f("algsort.in");
ofstream g("algsort.out");




int main()
{
	complex val;
	int p;
	Object<complex> d[1000000];

	Heap<complex> h;
	int length;

        //!Code for InfoArena ALGSORT;
/*
	f>>length;

	for (int  i = 1; i <= length; i++)
	{
		//cout<<"Enter Value: ";
		f>>val;
		p=val;
		d[i] = new Object<int>(val,p);
		h.SetIndex(d[i],i);
	}
	h.build_maxheap(length);
	h.heapsort(length);

	for (int  i = 1; i <= length; i++)
        g<<h.GetValue(i).GetValue()<<" ";
  return 0;


*/
         //!InfoArena Code done up here instead of the For below

    cout<<"Enter no of elements of array";
	cin>>length;

	for (int  i = 1; i <= length; i++)
	{
		cout<<"Enter Value: ";
		cin>>val;
		cout<<"Enter Priority: ";
		cin>>p;
		d[i] = new Object<complex>(val,p);
		h.SetIndex(d[i],i);
			h.build_maxheap(length);
	h.heapsort(length);
	}



	//cout<<"Compare: ";
	//cout<<h.CompareTo(1,2);
	char op;
	do
	{
		int ch;

		cout<<"----------Menu-------------\n\n";
		cout<<"\t Press 1 to Insert a new node"<<endl;
        cout<<"\t Press 2 for Extracting the first element in Queue"<<endl;
        cout<<"\t Press 3 to see the Priority Queue"<<endl;
        cout<<"\t Press 4 to compare 2 indexes"<<endl;
        cout<<"\t Press 5 to Order by Value"<<endl;

		cout<<"\n\n\tEnter your Choice<1,2,3,4,5> ";
		cout<<endl;
		cin>>ch;
		switch(ch)
		{
		case 1 :{  length++;
			cout<<"Enter Value: ";

			cin>>val;
			cout<<"Enter Priority (KEY): ";
			cin>>p;

			d[length] = new Object<complex>(val,p);
			h.SetIndex(d[length],length);
			h.build_maxheap(length);
			h.heapsort(length);

			cout<<endl;
			break;
				}

				  case 2 : {  cout<<"\n PRIORITY QUEUE \n";

				     h.heap_extract_min(length);
				    //  free(h.GetValue(length));
                    length--;
				h.build_maxheap(length);
			h.heapsort(length);
				break;
				}

		case 3:{
			for(int i=1;i<=length;i++){
				Object<complex> temp = h.GetValue(i);
				cout<<"Value: "<<temp.GetValue()<<" "<<"Prior: "<<temp.GetPriority()<<endl;
		/*		  h.heap_extract_min(length);

                    length--;
				h.build_maxheap(length);
			h.heapsort(length);
				cout<<endl;
				*/
			}
			/*
						h.heap_extract_min(length);

			length--;
			cout<<endl;
			*/
			break;

			   }


			     case 4:
            { int index1,index2;
                cout<<"Compare: index1: ";
                cin>>index1;
                cout<<"index2: ";
                cin>>index2;
                cout<<h.CompareTo(index1,index2);
                cout<<endl;
                    break;
            }

       case 5:
            { int index,indes;
            cout<<"Value order : "<<endl;

                    Object<complex> temp;
                 while(length>0){

            indes=1;
            for(int i=2;i<=length;i++){
                if(h.CompareTo(indes,i)>0){
                    indes=i;
                } }
                index=indes;

                h.heap_extracting(index,length);

             length--;
				h.build_maxheap(length);
			h.heapsort(length);

                  }
                  cout<<endl;
                  break;
            }



}
		cout<<"\n\nDo You Want to Continue <Y/N> ?";
		cout<<endl;
		cin>>op;
	}
	while(op=='Y' || op=='y');

	return 0;

}