Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
C
#include <bits/stdc++.h>
using namespace std;


//for time I made a class
class DepartureTime
{   
int hh;
int mm;
int ss;

public:
DepartureTime()
{
hh =0; mm = 0; ss=0;
}

DepartureTime(int hr, int min, int sec)
{
hh = hr;
mm = min;
ss = sec;
}

//return as int
int getTime()
{
return hh*10000 + mm*100 + ss;
}

//return as string to display
string toString()
{
string a =to_string(hh) + " : " +to_string(mm) +" : " + to_string(ss);
return a;
}
};

//strucy
struct planes
{
int planeID;
int routeID;
DepartureTime time; // hr:mm:ss in 24 hour format
planes* plane; //self referential structure
};

//fill planeid and route id
void function1(planes array[], int size)
{
for(int i=0; i<size; i++)
{
//rand gives a randome number, take mod by 900 to get a number less than 900 and add 100 to get a number from
// 0 t0 1000
int v = rand() % 900 + 100;
int v2 = rand() %1000 + 1000;
array[i].planeID = v;
array[i].routeID = v2;
}
};


//initialise with random time
void function2(planes array[], int size)
{
for(int i=0; i<size; i++)
{
int hr = rand() %24;
int min = rand() %60;
int sec = rand() %60;

DepartureTime t(hr, min, sec);
array[i].time = t;
}
}


//display
void function3(planes array[], int size)
{
cout<<"Plane ID\t Route ID\t Departure DepartureTime\n";
for(int i =0; i<size; i++)
{
cout<<array[i].planeID<<"\t\t"<<array[i].routeID<<"\t\t"<<array[i].time.toString()<<endl;
}
}


//sort in descending using selection sort
void function4(planes array[], int size)
{
for(int i=0; i<size; i++)
{
int min =i;
for(int j=i+1; j<size; j++)
{
if(array[i].time.getTime() < array[j].time.getTime())
{
planes temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

}


void function5(stack<planes>* s)
{
//last element of array will be removed, which is done by size-- itself
(*s).pop();   
}

void function6(queue<planes>* q, planes array[], int size)
{
(*q).pop();
//also remove from first element of array
for(int i=1; i<size; i++)
{
array[i-1] = array[i];
}
}

int main()
{
planes array[15];
int size =15;
function1(array, size);
function2(array, size);

function4(array, size);
function3(array, size);

//implement stack and queue
// in stack last in first out, use stack to take off plane with earliest departure
//in queue first in first out, use stack to take off plane with last scheduled time
stack<planes> s;
for(int i=0; i<size; i++)
{
//no planes struct exist before 0
if(i != 0)
array[i].plane = &(array[i-1]);

s.push(array[i]);
}

queue<planes> q;
for(int i=0; i<size; i++)
{
q.push(array[i]);
}

//in function 5 we pass stack by address to show the changes in main fucntion and decrease size to reduce plane by 1
function5(&s);
size--;
cout<<endl;
cout<<"After a plane takes off from schedule\n";
cout<<endl;
//DISPLAy again
function3(array, size);

cout<<endl;
cout<<"After taking off the last schedules plane\n";
cout<<endl;
function6(&q, array, size);
size--;
cout<<endl;
//DISPLAy again
function3(array, size);
}


What I have tried:

changing cout to printf and scanf
Posted
Updated 25-Mar-21 22:41pm
v2

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

Think about it: C does not have classes (that's why it's called "C++") so you'd have to emulate the whole class system which would be ... um ... nontrivial.
 
Share this answer
 
Start by learning both languages. C supports structures so you can convert the classes to structures and the class methods to functions that operate on structures. Then you need to learn how to deal with strings and write your own queue and stack libraries. A general purpose, dynamic array library could probably do most of what the stack and queue do. It will be quite challenging I'm sure.

Rewriting entirely would be a better option than converting that code I think.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900