SPN计算周转时间,带权周转时间,平均周转时间,平均带权周转时间

#include 

using namespace std;

class JCB
{
public:
    void input();
    void output();
    void bubbleSort(int start, int len);
    void bubbleSort1(int start, int len);
    int getCount(int x);
    void swap(int x, int y);
    void makeJCB(int x);
    void makeTime(int x);
    void init();
    void printCir();
private:
    int length;
    double arriveTime[10];
    double startTime[10];
    double workTime[10];
    double finishTime[10];
    double cirTime[10];
    double dqzzTime[10];
    string name[10];
};

void JCB::input()
{
    cout << "请输入进程数量: ";
    cin >> length;
    for(int i=0; i<length; i++)
    {
        cout << "进程" << i+1 << ": " << endl;
        cout << "请输入进程名称:"; cin >> name[i];
        cout << "请输入进程到达时间:"; cin >> arriveTime[i];
        cout << "请输入进程运行时间:"; cin >> workTime[i];
    }

}

void JCB::output()
{
    cout << endl;
            cout << "进程名称  " << "到达时间  " << "处理时间  " << "开始时间  "<< "完成时间  " << "周转时间  " << "带权周转时间  " << endl;
    for(int i=0; i<length; i++)
    {
        makeJCB(i);

        cout << name[i] << "  \t" << arriveTime[i] << "  \t" << workTime[i] << "  \t" << startTime[i] << "  \t"
        << finishTime[i] << "  \t" << cirTime[i] << "  \t" << dqzzTime[i] << "\t  " << endl;
        cout << endl;
    }
}

void JCB::bubbleSort(int start, int len)
{
    for(int i=start; i<start+len-1; i++)
        for(int j=start; j<start+len-1; j++)
            if(workTime[j] > workTime[j+1])
                swap(j,j+1);
}

void JCB::bubbleSort1(int start, int len)
{
    for(int i=start; i<start+len-1; i++)
        for(int j=start; j<start+len-1; j++)
            if(arriveTime[j] > arriveTime[j+1])
                swap(j,j+1);
}

int JCB::getCount(int x)
{
    int count = 0;
    int y = x-1;
    for(int i=x; i<length; i++)
        if(arriveTime[i] <= finishTime[y])
            count++;
    return count;
}

void JCB::swap(int x, int y)
{
    double temp;
    string temp1;
    temp = arriveTime[x]; arriveTime[x] = arriveTime[y]; arriveTime[y] = temp;
    temp = workTime[x]; workTime[x] = workTime[y]; workTime[y] = temp;
    temp1 = name[x]; name[x] = name[y]; name[y] = temp1;
}

void JCB::makeTime(int x)
{
    finishTime[x] = startTime[x] + workTime[x];
    cirTime[x] = finishTime[x] - arriveTime[x];
    dqzzTime[x] = cirTime[x] / workTime[x];
}

void JCB::printCir()
{
    double Tcir = 0;
    double Tdqzz = 0;
    for(int i=0; i<length; i++)
    {
        Tcir = Tcir + cirTime[i];
        Tdqzz = Tdqzz + dqzzTime[i];
    }
    cout << "平均周转时间:" << Tcir/length << endl;
    cout << "平均带权周转时间:" << Tdqzz/length << endl;

}

void JCB::makeJCB(int x)
{
    if(x == 0)
    {
        bubbleSort1(x,length);
        startTime[x] = arriveTime[x];
        makeTime(x);
    }
    else
    {
        int count = getCount(x);
        if(count == 0)
        {
            startTime[x] = arriveTime[x];
            makeTime(x);
        }
        else
        {
            bubbleSort(x,count);
            startTime[x] = finishTime[x-1];
            makeTime(x);
        }
    }
}

void JCB::init()
{
    for(int i=0; i<10; i++)
    {
        arriveTime[i] = 0;
        startTime[i] = 0;
        workTime[i] = 0;
        finishTime[i] = 0;
        cirTime[i] = 0;
        dqzzTime[i] = 0;
    }
}

int main()
{
    JCB A;
    A.init();
    A.input();
    A.output();
    A.printCir();
}

SPN计算周转时间,带权周转时间,平均周转时间,平均带权周转时间_第1张图片

你可能感兴趣的:(操作系统,c++,算法)