Dec 22, 2011 at 1:40am UTC
Dear All,
Here is my successful programming only for reading the file and calculate it using the priority formula. After compile and it was give the correct result.
However, I try to use priority queue to sort based on priority field.
Can anyone modify and add in to my programming?
example.txt
Bryan Devaux 53000 5 1
Amanda Trapp 89000 3 2
Baclan Nguyen 93000 3 3
Sarah Gilley 17000 1 4
Warren Rexroad 72000 7 5
Jorge Gonzales 65000 2 6
Paula Hung 34000 3 7
Lou Mason 21000 6 8
Steve Chu 42000 4 9
Dave Lightfoot 63000 3 10
Joanne Brown 33000 2 11
main.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
typedef struct {
char name[10];
char surname[15];
long mileage;
int years;
int sequence;
int priority;
} MYPRIORITY;
MYPRIORITY person[10];
int main(int argc, char *argv[])
{
char name1[10];
char surname1[15];
long mileage1;
int years1;
int sequence1;
int priority1;
int count=0;
cout<<"Name\t\t\tMileage\tYears\tSequence\tPriority"<<endl<<endl;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
myfile>>name1>>surname1>>mileage1>>years1>>sequence1;
strcpy(person[count].name,name1);
strcpy(person[count].surname,surname1);
person[count].mileage = mileage1;
person[count].years = years1;
person[count].sequence=sequence1;
priority1=(person[count].mileage/1000)+person[count].years-person[count].sequence;
person[count].priority=priority1;
count++;
cout<<name1<<" "<<surname1<<"\t\t"<<mileage1<<"\t"<<years1<<"\t"<<sequence1<<"\t\t"<<priority1<<endl<<endl;
}
myfile.close();
}
else
cout << "Unable to open file";
system("PAUSE");
return EXIT_SUCCESS;
}
Output for NON-PRIORITY QUEUE
Name Mileage Years Sequence Priority
Bryan Devaux 53000 5 1 57
Amanda Trapp 89000 3 2 90
Baclan Nguyen 93000 3 3 93
Sarah Gilley 17000 1 4 14
Warren Rexroad 72000 7 5 74
Jorge Gonzales 65000 2 6 61
Paula Hung 34000 3 7 30
Lou Mason 21000 6 8 19
Steve Chu 42000 4 9 37
Dave Lightfoot 63000 3 10 56
Joanne Brown 33000 2 11 24
Press any key to continue . . .