Mój plik wejściowy wygląda tak

in.txt

1 0 10

Gdzie każdy wiersz jest indywidualnym identyfikatorem struktury (dowolnym, nie oznacza kolejności przybycia), arrivalTime i burstTime

Chcę uzyskać dane wyjściowe takie jak poniżej po otrzymaniu danych wejściowych od użytkownika, którego algorytmu należy użyć.

Np. użytkownik wpisz FSCS następnie

S.algorithm: FCFS
Total 1 tasks are read

<system time 0> process 1 is running
<system time 1> process 1 is running
<system time 2> process 1 is running
<system time 3> process 1 is finished 

Jakakolwiek pomoc w rzeczywistym kodzie byłaby BARDZO mile widziana !!!

Moja praca jeszcze

    while (file)
{
    file >> temp.ID >> temp.arrivalTime >> temp.burstTime;
    d.push_back(temp);

    int i = 0, time = 0;
    while (i < d.size())
            {
                if (d[i].arrivalTime > time)
                    cout << "Time " << time << "process is idle";

                time += d[i].arrivalTime;
                cout << "Time " << time << " Process " << d[i].ID << " is running" << endl;

                time += d[i].burstTime;

                i++;

            }

}

Wejście.txt.

1 0 5
2 5 8
3 9 6

Wynik

<system time <0> process 1 is running
<system time <1> process 1 is running
<system time <2> process 1 is running
<system time <3> process 1 is running
<system time <4> process 1 is running
<system time <5> process 1 finished
<system time <5> process 2 is running
<system time <6> process 2 is running
<system time <7> process 2 is running
<system time <8> process 2 is running
<system time <9> process 2 is running
<system time <10> process 2 is running
<system time <11> process 2 is running
<system time <12> process 2 is running
<system time <13> process 2 finished
<system time <13> process 3 is running
<system time <14> process 3 is running
<system time <15> process 3 finished
Press any key to continue . . .
0
user10177976 3 listopad 2018, 13:01

1 odpowiedź

Najlepsza odpowiedź

Do obsługi plików użyj czegoś takiego

ifstream file("input.txt")

if(!file)
{
   std::cerr<<"Cannot open file"<<std::endl;
   exit(1);
}

myData temp;

while(file)
{
    file >> temp.Id >> temp.arrivalTime >> temp.burstTime;
    d.push_back(temp);
}


 int time,i;
 time = i = 0;
 while( i < d.size())
 {
    if( time < d[i].arrivalTime )
    {
         std::cout<<"<system time <"<<time<<"> system is idle"<<std::endl;
         time++;
    }
    else if( time < d[i].arrivalTime + d[i].burstTime )
    {
         std::cout<<"<system time <"<<time<<"> process "<<i<<" is running"<<std::endl;
         time++;
    }    
    else{
         std::cout<<"<system time <"<<time<<"> process "<<i<<" finished"<<std::endl;
          i++;
    }

 }

Edytować:

test.cpp

#include <iostream>                                                             
#include <fstream>                                                              
#include <vector>                                                               
#include <cstdlib>                                                              

using namespace std;                                                            
struct myData {                                                                 
    int Id;                                                                     
    int arrivalTime;                                                            
    int burstTime;                                                              
    int finishTime;                                                             
};                                                                              
int main()                                                                      
{                                                                               
ifstream file("input.txt");                                                     
vector<myData> d;                                                               
if(!file)                                                                       
{                                                                               
   std::cerr<<"Cannot open file"<<std::endl;                                    
   exit(1);                                                                     
}                                                                               

myData temp;                                                                    
temp.finishTime = -1;                                                           
while(file)                                                                     
{                                                                               
    if(file.good())                                                             
        file >> temp.Id >> temp.arrivalTime >> temp.burstTime;                  
    d.push_back(temp);                                                          
}                                                                               

 int time,i;                                                                    
 time = i = 0;                                                                  
 while( i < d.size() - 1 )                                                      
 {                                                                              
    if( time < d[i].arrivalTime )                                               
    {                                                                           
         std::cout<<"<system time <"<<time<<"> system is idle"<<std::endl;         
         time++;                                                                
    }                                                                           
    else if( time < d[i].finishTime || d[i].finishTime==-1 )                    
    {                                                                           
         if(d[i].finishTime == -1)                                              
            d[i].finishTime = d[i].burstTime + time;                            
         std::cout<<"<system time <"<<time<<"> process "<<i+1<<" is running"<<std::endl;
         time++;                                                                
    }                                                                           
    else{                                                                       
         std::cout<<"<system time <"<<time<<"> process "<<i+1<<" finished"<<std::endl;
          i++;                                                                  
    }                                                                           

 }                                                                              
}   

I input.txt 1 0 3

0
scipsycho 3 listopad 2018, 15:12