daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // testing.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <sstream>
  9. #include<queue>
  10. using namespace std;
  11. struct job{
  12.     int position = 0;
  13.     int TT = 0;
  14.     int remain = 0;
  15.     int wait = 0;
  16. };
  17.     string A[100];
  18.     int top = -1;
  19.     int back = -1;
  20.     bool isempty = true;
  21.     queue<job> myq;
  22.  
  23. void readFile(const char* filename) {
  24.    
  25.     ifstream inf(filename);
  26.     string data;
  27.     int i = 0;
  28.    
  29.     if (!inf) {
  30.         cout << "the file is not open" << endl;
  31.     }
  32.     else {
  33.         job j;
  34.         int pos = 1;
  35.         while (!inf.eof()) {
  36.  
  37.             getline(inf, data, ' ');
  38.                 j.position = pos;
  39.                 j.TT = stoi(data);
  40.                 j.remain = j.TT;
  41.                 myq.push(j);
  42.             }
  43.         }
  44.     inf.close();
  45.     }
  46.    
  47.  
  48. void outputFile(const char * filename) {
  49.     ofstream out(filename);
  50.     int size = myq.size();
  51.     if (out) {
  52.         for (int i = 1; i <= myq.size(); i++) {
  53.             out << "job " << i << ", burst time: " << myq.front().TT << ", waiting time: " <<  myq.front().wait<<endl;
  54.             myq.pop();
  55.         }
  56.         out << "job " << size << ", burst time: " << myq.front().TT << ", waiting time: " << myq.front().wait << endl;
  57.     }
  58.     out.close();
  59. }
  60.  
  61.  
  62. int main()
  63. {
  64.     readFile("input2.txt");
  65.     bool workDone = false;
  66.     int WT = 0;
  67.     int counter = 0;
  68.     while (!workDone) {
  69.         int numFinished = 0;
  70.         for (int i = 0; i < myq.size();i++) {
  71.             if (myq.front().remain == 0) {
  72.                 numFinished++;
  73.                 myq.push(myq.front());
  74.                 myq.pop();
  75.             }
  76.             else if (myq.front().remain <= 2) {
  77.                 myq.front().wait = WT;
  78.                 WT = WT + myq.front().remain;
  79.                 myq.front().remain = 0;
  80.                 numFinished++;
  81.                 myq.push(myq.front());
  82.                 myq.pop();
  83.                
  84.             }
  85.             else {
  86.                 myq.front().wait = WT;
  87.                 WT = WT + 2;
  88.                 myq.front().remain -= 2;
  89.                 myq.push(myq.front());
  90.                 myq.pop();
  91.             }
  92.        
  93.         }
  94.         if (numFinished == myq.size()) {
  95.             workDone = true;
  96.         }
  97.         else {
  98.             WT = WT - 2;
  99.         }
  100.     }
  101.  
  102.     outputFile("output2.txt");
  103.    
  104.  
  105.    
  106.     return 0;
  107.    
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top