#include<stdio.h> #include <stdlib.h> #include<string.h> #include<map> #include<queue> #include<iostream> #include <unistd.h> /* Name : Mohamad Hassan N C Roll No : 111801026 */ using namespace std; class packet { public: int id; float trans; int length; packet(int ids , float tran , int len) { id=ids; trans=tran; length=len; } }; // void printl(list<packet> const &list) // { // for (auto const &i: list) { // cout << i.id <<" "; // } // } int smallest(int turn1 ,int turn2,int queturn,int quesz) { int count1,count2; count1=0; count2=0; while(queturn!=turn1) { turn1=(turn1+1)%quesz; count1+=1; } while(queturn!=turn2) { turn2=(turn2+1)%quesz; count2+=1; } if(count1<count2) { return turn1; } else { return turn2; } } int main(int argc, char * argv[]) { /* code */ FILE * fp; int id,length,bucket_cap,remain, bucket_init,queue_id,flag,turn,flagl; float trantime,rate,token,timer,time; char * filename; rate = 4.0;// first argument // rate = atof(argv[2]); //second argument // filename = argv[4]; // printf("%f\n",rate); // printf("%d\n",bucket_cap); // printf("%s\n",filename); // fp = fopen(argv[4], "r"); // if (fp == NULL) // return -1; flagl=0; queue<float> pt; queue<int> pid; queue<int> pqid; queue<int> plen; bucket_init = bucket_cap ; // initially bucket is filled timer=0.0; // initial timer value map <int,queue <packet> > mymap; time=0.0; turn=-1; while (scanf("%f\t%d\t%d\t%d\n",&trantime,&id,&queue_id,&length)!=EOF) { pt.push(trantime); pid.push(id); pqid.push(queue_id); plen.push(length); } while (!pt.empty() && !pid.empty() && !pqid.empty() && !plen.empty()) { // reading the input line by line id= pid.front(); trantime = pt.front(); length = plen.front(); queue_id = pqid.front(); pid.pop(); pt.pop(); plen.pop(); pqid.pop(); packet p1 = packet(id,trantime,length); // printf("%f\t%d\t%d\n",trantime,id,length ); flag=1; // cout<<"Map size : "<<mymap.size()<<endl; if(p1.trans<=time || mymap.size()==0) { // if(queue_id>mymap.size()) // { // cout<<"1 . add in id :"<<p1.id<<"in queue_id : "<<queue_id-1<<endl; // queue <packet> newlist = {}; // // list <int> packets ={id,trantime,length}; // newlist.push(p1); // mymap.insert({queue_id-1,newlist}); // cout<<"current queue top of queue_id "<< queue_id-1 <<" is "<<mymap[queue_id-1].front().id<<" size is " << mymap[queue_id-1].size()<<endl; // } // else // { // cout<<"2 . add in id :"<<p1.id<<"in queue_id : "<<queue_id-1<<endl; // // list <int> packets ={id,trantime,length}; // mymap[queue_id-1].push(p1); // } // while(p1.trans==pt.front() && !(pt.empty())) // { // mymap[pqid.front()-1].push(packet(pid.front() , pt.front() , plen.front())); // pid.pop(); // pt.pop(); // plen.pop(); // pqid.pop(); // } mymap[queue_id-1].push(p1); // cout<<"current queue top of queue_id "<< queue_id-1 <<" is "<<mymap[queue_id-1].front().id<<" size is " << mymap[queue_id-1].size()<<endl; // printf("adding for id : %d\n",p1.id); } else { // printf("dropping for id : %d\n",p1.id); while(p1.trans>=time && flag) { flag=0; turn = (turn+1)%(mymap.size()); // if(!(pt.empty())) // { // printf("1st : %d with id : %d\n",pqid.front(),pid.front()); // printf("2nd : %d with id : %d\n",queue_id,id); // printf("turn : %d \n",turn); // printf("smallest values is %d \n",smallest( pqid.front(),queue_id,turn,mymap.size()) ); // if(smallest( pqid.front()-1,queue_id-1,turn,mymap.size())==pqid.front() && pt.front()==trantime) // { // // turn = (turn+1)%(mymap.size()); // printf("breaking\n"); // break; // } // } // printf("turn is %d\n",turn); // printf("%d %d %d\n",turn,pqid.front()-1,flagl); if(turn==queue_id-1 && flagl==1 ) { turn-=1; break; } if( !(mymap[turn].empty()) ) { packet p = mymap[turn].front(); // printf("packet : %d is dropped\n",p.id); mymap[turn].pop(); // cout<<"current queue top of queue_id "<< turn <<" is "<<mymap[turn].front().id<<endl; if(time<p.trans) { time=p.trans; } // cout<<"add "<<(float)(p.length/rate) <<" to "<<time<<endl; time+=(float)(p.length/rate); // printf("poping\n"); printf("%.2f %d\n",time,p.id); // cout<<turn<<endl; } // printf("turn is %d\n",turn); for(int i=0;i<mymap.size();++i) { if(!(mymap[i].empty())) { flag=1; break; } } // if(flag==0) // { // time=p1.trans; // } } // if(queue_id>mymap.size()) // { // queue <packet> newlist = {}; // // list <int> packets ={id,trantime,length}; // cout<<"3 .add in id :"<<p1.id<<" in queue_id : "<<queue_id-1<<endl; // newlist.push(p1); // mymap.insert({queue_id-1,newlist}); // cout<<"current queue top of queue_id " << queue_id-1 <<" is "<<mymap[queue_id-1].front().id<<" size is " << mymap[queue_id-1].size()<<endl; // } // else // { // cout<<"4 .add in id :"<<p1.id<<" in queue_id : "<<queue_id-1<<endl; // // list <int> packets ={id,trantime,length}; // mymap[queue_id-1].push(p1); // cout<<"current queue top of queue_id "<< queue_id-1 <<" is "<<mymap[queue_id-1].front().id<<" size is " << mymap[queue_id-1].size()<<endl; // } if(p1.trans==pt.front()) flagl=1; else flagl=0; // while(p1.trans==pt.front() && !(pt.empty())) // { // // printf("%d and %d has same\n",pid.front(),p1.id); // mymap[pqid.front()-1].push(packet(pid.front() , pt.front() , plen.front())); // pid.pop(); // pt.pop(); // plen.pop(); // pqid.pop(); // } mymap[queue_id-1].push(p1); } // sleep(1); // printf("\n"); // printf("queue status ............. at id : %d\n and time : %.2f ",p1.id,time); // for(int i=0 ;i < mymap.size();++i) // { // printf("%ld ",mymap[i].size()); // } // printf("\n"); } flag=1; while(flag==1) { flag=0; turn = (turn+1)%(mymap.size()); if(!(mymap[turn].empty())) { packet p = mymap[turn].front(); mymap[turn].pop(); if(time<p.trans) { time=p.trans; } time+=(float)(p.length/rate); printf("%.2f %d\n",time,p.id); } for(int i=0;i<mymap.size();++i) { if(!(mymap[i].empty())) { flag=1; break; } } } return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}