最后一次执行 strcpy_s 时莫名其妙字符串拷贝不过来,断点检查时就是“烫烫..”了
#include<stdio.h>
#include<string.h>
struct job {
char name[10]; //作业的名字
int starttime; //作业到达系统时间
int needtime; //作业运行时间
int endtime; //作业结束时间
int runtime; //作业周转时间
};
void strcopy(char *str1, char *str2) {
char temp[10];
strcpy_s(temp, strlen(str1) + 1, str1);
strcpy_s(str1, strlen(str2) + 1, str2);
strcpy_s(str2, strlen(temp) + 1, temp);
}
void sort(struct job jobs[50], int n);
void FCFS(struct job jobs[50], int n);
void SJF(struct job jobs[50], int n);
void result(struct job jobs[50], int n);
int main()
{
struct job jobs[50];
int n, i;
printf("输入作业个数:");
scanf_s("%d", &n);
printf("输入每个作业的作业名,到达时间,服务时间:\n");
for (i = 0; i < n; i++)
{
scanf_s("%s", jobs[i].name, 10);
scanf_s("%d", &jobs[i].starttime);
scanf_s("%d", &jobs[i].needtime);
}
printf("\n");
printf("作业名\t到达系统时间\t运行时间\n");
for (i = 0; i < n; i++)
{
printf("%s\t %d\t %d\n", jobs[i].name, jobs[i].starttime, jobs[i].needtime);
}
FCFS(jobs, n);
printf("先来先服务(FCFS)算法运行结果:\n");
result(jobs, n);
SJF(jobs, n);
printf("最短作业优先算法(SJF)运行结果:\n");
result(jobs, n);
}
void FCFS(struct job jobs[50], int n)
{
sort(jobs, n);
int i;
for (i = 0; i < n; i++)
{
if (i == 0) {
jobs[i].runtime = jobs[i].needtime;
jobs[i].endtime = jobs[i].starttime + jobs[i].needtime;
}
else if (jobs[i].starttime > jobs[i - 1].endtime) {
jobs[i].runtime = jobs[i].needtime;
jobs[i].endtime = jobs[i].starttime + jobs[i].needtime;
}
else {
jobs[i].runtime = jobs[i].needtime + jobs[i - 1].endtime - jobs[i].starttime;
jobs[i].endtime = jobs[i].starttime + jobs[i].runtime;
}
}
}
void SJF(struct job jobs[50], int n)
{
int i = 0, j = 0, b = 1, sum = 0;
char temp[10];
int min;
sort(jobs, n);
jobs[0].endtime = jobs[0].starttime + jobs[0].needtime;
jobs[0].runtime = jobs[0].needtime;
while (b < n)
{
for (i = b; i < n; i++)
{
if (jobs[i].starttime <= jobs[j].endtime)
sum = sum + 1;
}
for (i = b; i <= sum - 1; i++)
{
if (jobs[i].needtime > jobs[i + 1].needtime)
{
min = jobs[i].starttime;
jobs[i].starttime = jobs[i + 1].starttime;
jobs[i + 1].starttime = min;
min = jobs[i].needtime;
jobs[i].needtime = jobs[i + 1].needtime;
jobs[i + 1].needtime = min;
strcopy(jobs[i].name, jobs[i + 1].name);
}
}
b = sum;
j = b - 1;
}
for (i = 1; i < n; i++)
{
if (jobs[i].starttime > jobs[i - 1].endtime)
{
jobs[i].endtime = jobs[i].starttime + jobs[i].needtime;
jobs[i].runtime = jobs[i].needtime;
}
else
{
jobs[i].endtime = jobs[i - 1].endtime + jobs[i].needtime;
jobs[i].runtime = jobs[i].endtime - jobs[i].starttime;
}
}
}
void result(struct job jobs[50], int n)
{
int i;
int sum_runtime = 0;
double sum_dqzztime = 0.00;
printf("作业名\t到达时间\t运行时间\t完成时间\t周转时间\n");
for (i = 0; i < n; i++)
{
printf("%s\t %d\t %d\t %d\t %d\n", jobs[i].name, jobs[i].starttime, jobs[i].needtime, jobs[i].endtime, jobs[i].runtime);
sum_runtime = sum_runtime + jobs[i].runtime;
}
printf("\n");
}
void sort(struct job jobs[50], int n)
{
int i = 0, b = 0;
char temp[10];
int min;
for (i = 0; i < n - 1; i++) //按作业到达系统时间进行排序,最早到达的排在最前面
{
if (jobs[i].starttime > jobs[i + 1].starttime)
{
min = jobs[i].starttime;
jobs[i].starttime = jobs[i + 1].starttime;
jobs[i + 1].starttime = min;
min = jobs[i].needtime;
jobs[i].needtime = jobs[i + 1].needtime;
jobs[i + 1].needtime = min;
strcopy(jobs[i].name, jobs[i + 1].name);
}
}
}