c 언어로 FCFS 및 SJF 작성

4954 단어
#include 
#define MAXNUM 100
int id[MAXNUM];
double send[MAXNUM];//    
double process[MAXNUM];//    
double start[MAXNUM];//    
double finsh[MAXNUM];//    
double zz[MAXNUM];//    
double dz[MAXNUM];//      
double p = 0;//      
double d = 0;//        
static int n;//   
void input()//     
{
	printf("      :");
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		id[i] = i + 1;
		send[i] = 0;
		process[i] = 0;
		finsh[i] = 0;
		zz[i] = 0;
		dz[i] = 0;
	}
}
void inputSend()//           
{
	printf("       :");
	for (int i = 0; i < n; i++)
	{
		scanf("%lf", &send[i]);
	}
	printf("       :");
	for (int i = 0; i < n; i++)
	{
		scanf("%lf", &process[i]);
	}
}
void display()//    
{
	printf("******************************************************************
"); printf(" :
"); printf(" :%d
", n); printf("
"); for (int i = 0; i < n; i++) { printf(" %-2d %5.2f %5.2f
", id[i], send[i], process[i]); } printf("******************************************************************
"); } void displayResult()// { printf("
"); for (int j = 1; j <= n; j++) { for (int i = 0; i < n; i++) { if (id[i] == j) { printf(" %-2d %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f
", id[i], send[i], process[i], start[i], finsh[i], zz[i], dz[i]); } } } printf(" : %5.2f
", p); printf(" : %5.2f
", d); } void SortST()// { double temp1 = 0, temp2 = 0, temp3 = 0; for (int i = 0; i < n; i++) {// , for (int j = i; j < n; j++) { if (send[i] > send[j]) { temp1 = send[i]; send[i] = send[j]; send[j] = temp1; temp2 = process[i]; process[i] = process[j]; process[j] = temp2; temp3 = id[i]; id[i] = id[j]; id[j] = temp3; } } } } void SortSPT() {// , double temp1 = 0, temp2 = 0, temp3 = 0; for (int i = 0; i < n; i++) {// , for (int j = i; j < n; j++) { if (send[i] >= send[j]) { if (send[i] > send[j]) {// temp1 = send[i]; send[i] = send[j]; send[j] = temp1; temp2 = process[i]; process[i] = process[j]; process[j] = temp2; temp3 = id[i]; id[i] = id[j]; id[j] = temp3; } else { if (process[i]>process[j]) {// temp1 = send[i]; send[i] = send[j]; send[j] = temp1; temp2 = process[i]; process[i] = process[j]; process[j] = temp2; temp3 = id[i]; id[i] = id[j]; id[j] = temp3; } } } } } } void SortPT(int i)// { int m = i; double temp1 = 0, temp2 = 0, temp3 = 0; for (i; i < n; i++) { for (int j = i; j < n; j++) { if (process[i] > process[j]) { temp1 = send[j]; send[j] = send[i]; send[i] = temp1; temp2 = process[j]; process[j] = process[i]; process[i] = temp2; temp3 = id[j]; id[j] = id[i]; id[i] = temp3; } } } } void Fcfs() {// FCFS start[0] = send[0]; finsh[0] = start[0] + process[0]; zz[0] = finsh[0] - send[0]; p = p + zz[0]; dz[0] = zz[0] / process[0]; d = d + dz[0]; for (int i = 1; i < n; i++) { start[i] = finsh[i - 1]; if (start[i]= send[i])// , { // ,m , m double temp1 = 0, temp2 = 0, temp3 = 0; temp1 = send[i]; send[i] = send[m]; send[m] = temp1; temp2 = process[i]; process[i] = process[m]; process[m] = temp2; temp3 = id[i]; id[i] = id[m]; id[m] = temp3; break;// } } } void SJF() // { start[0] = send[0]; finsh[0] = start[0] + process[0]; zz[0] = finsh[0] - send[0]; p = p + zz[0]; dz[0] = zz[0] / process[0]; d = d + dz[0]; for (int i = 1; i < n; i++) { SortPT(i);// , FindS(i);// start[i] = finsh[i - 1]; finsh[i] = start[i] + process[i]; zz[i] = finsh[i] - send[i]; p = p + zz[i]; dz[i] = zz[i] / process[i]; d = d + dz[i]; } p = p / n; d = d / n; } void Choose(){ printf(" :
"); printf("1. FCFS 2. SJF
"); int m = 0; scanf("%d", &m); if (m == 1) { SortST(); Fcfs(); } else if (m == 2) { SortSPT(); SJF(); } else { printf(" !!!
"); \ printf("******************************************************************
"); Choose(); } } int main() { input(); inputSend(); display(); Choose(); displayResult(); getchar(); getchar(); return 0; }

좋은 웹페이지 즐겨찾기