2 jimmywang123 jimmywang123 于 2014.01.06 22:47 提问

C 程序没报错,就是调试的时候出现了中断,还请帮忙改正一下

#include
#include
#include
using namespace std;
struct A{ //先来先服务算法从这里开始
char name[10];
float arrivetime;
float servicetime;
float starttime;
float finishtime;
float zztime;
float dqzztime;
float avzztime;
float avdqzztime;
float lefttime;
float stoptime;
int timeprice;
}; //定义一个结构体,里面包含的有一个进程相关的信息

A a[100]; //先来先服务算法从这里开始

void input(A *p, int N)
{
int i;
cout << endl;
printf(" 请您输入进程的 名字 到达时间 服务时间: (例如: a 0 100)\n\n");
for (i = 0; i <= N - 1; i++)
{
printf(" 请您输入进程%d的信息:\t", i + 1);
scanf_s("\t\t\t%s%f%f", &p[i].name, &p[i].arrivetime, &p[i].servicetime);

}

}
void Print(A *p, float arrivetime, float servicetime, float starttime, float finishtime, float zztime, float dqzztime, int N)
{
int k;
cout << endl;
printf("\n 具体进程调度信息:\n\n");
printf("\t进程名 到达时间 服务时间 开始时间 结束时间 周转时间 带权周转时间\n");

for (k = 0; k <= N - 1; k++)
{
printf("\t%s\t%-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\n", p[k].name, p[k].arrivetime,
p[k].servicetime, p[k].starttime, p[k].finishtime, p[k].zztime, p[k].dqzztime);
}
getchar();
}

void sort(A *p, int N) //到达时间排序

{
for (int i = 0; i <= N - 1; i++)
for (int j = 0; j <= i; j++)
if (p[i].arrivetime<p[j].arrivetime)
{
A temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}

void deal(A p, float arrivetime, float servicetime, float starttime, float finishtime, float &zztime, float &dqzztime,/*float &avzztime,float &avdqzztime,/int N) //运行阶段

{
int k;
for (k = 0; k <= N - 1; k++)
{
if (k == 0)
{
p[k].starttime = p[k].arrivetime;
p[k].finishtime = p[k].arrivetime + p[k].servicetime;
}

else
{
p[k].starttime = p[k - 1].finishtime;
p[k].finishtime = p[k - 1].finishtime + p[k].servicetime;
}
}

for (k = 0; k <= N - 1; k++)
{
p[k].zztime = p[k].finishtime - p[k].arrivetime;
p[k].dqzztime = p[k].zztime / p[k].servicetime;
}

}
void FCFS(A *p, int N)
{
float sumzztime = 0, sumdqzztime = 0, avzztime, avdqzztime;
float arrivetime = 0, servicetime = 0, starttime = 0, finishtime = 0, zztime = 0, dqzztime = 0;
sort(p, N);
deal(p, arrivetime, servicetime, starttime, finishtime, zztime, dqzztime, N);
Print(p, arrivetime, servicetime, starttime, finishtime, zztime, dqzztime, N);
for (int k = 0; k <= N - 1; k++)
{
sumzztime = sumzztime + p[k].zztime;
sumdqzztime = sumdqzztime + p[k].dqzztime;
}
avzztime = sumzztime / N;
printf("\n该算法的平均周转时间为:%-.2f\t", avzztime);
avdqzztime = sumdqzztime / N;
printf("该算法的平均带权周转时间为:%-.2f\t\n\n", avdqzztime);
getchar();
} //先来先服务算法到此结束

A a1[100];
void sort1(A *p, int N1)//到达时间排序
{
for (int i = 0; i <= N1 - 1; i++)
for (int j = 0; j <= i; j++)
if (p[i].arrivetime<p[j].arrivetime)
{
A temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
void deal(A *p, float arrivetime, float servicetime, float starttime, float finishtime, int N1)//运行阶段
{
int k;
for (k = 0; k <= N1 - 1; k++)
{
if (k == 0)
{
p[k].starttime = p[k].arrivetime;
p[k].finishtime = p[k].arrivetime + p[k].servicetime;
}//float(p[k].servicetime)/60;}
else
{
p[k].starttime = p[k - 1].finishtime;
p[k].finishtime = p[k - 1].finishtime + p[k].servicetime;
}//float(p[k].servicetime)/60;}
}
for (k = 0; k <= N1 - 1; k++)
{
p[k].zztime = p[k].finishtime - p[k].arrivetime;
p[k].dqzztime = p[k].zztime / p[k].servicetime;

}

}

A a2[100];

void ptt(A *p, float arrivetime, float servicetime, float starttime, float finishtime, float zztime, float dqzztime, float avzztime, float avdqzztime, float lefttime, int timeprice, int N2)
{
float w = 0; int c = 0;
float stoptime = 0;
printf("\n 请输入时间片的值:");
cin >> timeprice;
sort(p, N2);

float d[20], h[20];
for (int k = 0; k <= N2 - 1; k++)
{
d[k] = p[k].servicetime;
if (k == 0)
{
p[k].starttime = p[k].arrivetime;
p[k].finishtime = p[k].arrivetime + p[k].servicetime;
}

else
{
p[k].starttime = p[k - 1].finishtime;
p[k].finishtime = p[k - 1].finishtime + p[k].servicetime;
}

h[k] = p[k].starttime;

p[k].lefttime = p[k].servicetime - timeprice;

if (p[k].lefttime>0)
{
c = c + 1;
p[k].stoptime = p[k].starttime + timeprice;
p[k].finishtime = p[k].stoptime;

}

else p[k].stoptime = p[k].finishtime;

w = p[k].stoptime;

}

printf("\n 第1轮进程调度信息:\n\n");
printf("\t进程名 到达时间 服务时间 开始时间 停止时间 剩余时间\n");

for (int k = 0; k <= N2 - 1; k++)
{
printf("\t%s\t%-.2f\t %-.2f\t %-.2f\t\t %-.2f\t %-.2f\n", p[k].name, p[k].arrivetime,
p[k].servicetime, p[k].starttime, p[k].stoptime, p[k].lefttime);
}
getchar();
int i = 2;
while (c>0)
{
printf("\n 第 %d 轮具体进程调度信息(时间片为 %d ):\n\n", i, timeprice);
printf("\t进程名 服务时间 开始时间 停止时间 剩余时间\n");

for (int k = 0; k <= N2 - 1; k++)
{
if (p[k].lefttime>0)
{
p[k].servicetime = p[k].lefttime;
p[k].starttime = w;
p[k].finishtime = p[k].starttime + p[k].servicetime;
p[k].lefttime = p[k].servicetime - timeprice;

if (p[k].lefttime>0)p[k].stoptime = p[k].starttime + timeprice;

if (p[k].lefttime <= 0)
{
c = c - 1;
p[k].stoptime = p[k].finishtime;

}
w = p[k].stoptime;
printf("\t%s\t%-.2f\t %-.2f %-.2f %-.2f\n", p[k].name,
p[k].servicetime, p[k].starttime, p[k].stoptime, p[k].lefttime);

}

}

i = i + 1;

}

if (c <= 0)
printf("\n 时间片轮转调度过程结束!\n");
for (int k = 0; k <= N2 - 1; k++)
{
p[k].servicetime = d[k];
p[k].starttime = h[k];
p[k].zztime = p[k].finishtime - p[k].arrivetime;
p[k].dqzztime = p[k].zztime / p[k].servicetime;
}
getchar();

}
void tt(A *p, int N2)
{
float sumzztime = 0, sumdqzztime = 0, avzztime = 0, avdqzztime = 0; int timeprice = 0;
float arrivetime = 0, servicetime = 0, starttime = 0, finishtime = 0, zztime = 0, dqzztime = 0, lefttime = 0;

ptt(p, arrivetime, servicetime, starttime, finishtime, zztime, dqzztime, avzztime, avdqzztime, lefttime, timeprice, N2);
printf("\n 综合信息为:\n");
Print(p, arrivetime, servicetime, starttime, finishtime, zztime, dqzztime, N2);

for (int k = 0; k <= N2 - 1; k++)
{
sumzztime = sumzztime + p[k].zztime;
sumdqzztime = sumdqzztime + p[k].dqzztime;
}
avzztime = sumzztime / N2;
printf("\n该算法的平均周转时间为:%-.2f\t", avzztime);
avdqzztime = sumdqzztime / N2;
printf("该算法的平均带权周转时间为:%-.2f\t\n", avdqzztime);
getchar();

}

int main(int argc, char *argv[])
{
int N;
char cse1;
cout << "\t" << "\t\t 1.先来先服务调度算法 " << "\t\t" << endl;
cout << endl;
cout << "\t" << "\t\t 2.时间片轮转调度算法" << "\t\t" << endl;
cout << endl;
printf("输入进程数目:");
scanf_s("%d", &N);
input(a, N);

while (1)
{
cout << "\t\t\t 请输入您的选择(1/2):";
cse1 = getchar();
switch (cse1)
{
case '1':
//int N;

cout << endl;
cout << endl;
printf("\t\t<<---!!!@@@先来先服务调度算法@@@!!!--->>\n");
cout << endl;
//printf("输入进程数目:");

//scanf("%d",&N);

//input(a,N);

FCFS(a, N);
break;

case '2':
//int N2;
cout << endl;
cout << endl;
printf("\t\t<<---!!!@@@时间片轮转调度算法@@@!!!--->>\n");
cout << endl;
//printf("输入进程数目: ");
// scanf("%d",&N2);
//input(a2,N2);
tt(a, N);
break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

1个回答

lr157
lr157   2015.09.30 09:09

不懂,但是我只知道楼楼#include后面少了些什么

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!