📄 code.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define NUM 10
#define MAC 5
#define WP 5
char filename[100];
int m[MAC][NUM];/*每台机器的工件序列*/
int mw[MAC][NUM];/*每台机器中工序*/
int cost[NUM][WP];
int sequence[NUM][WP];
int temp1[NUM];
int temp2[NUM];
int p[10];/*工序数*/
int t[5];/*每台机器目前累计时间*/
int w[5];/*当前工序的时间*/
int n[5];/*每台机器工序的数目*/
int at[5];
int t1[5];/*判断机器是否有空*/
int n1[5];
int d[5];
void ready();
void init(int a);
void print();
/*void print2();*/
void arrange();
void inputdata();
void result();
int select();
int test(int i);
main(){
int i;
char buf1[100]={"d:\\turbo2\\la01.txt\0"};
/* char buf2[100]={"d:\\turbo2\\la02.txt\0"};
char buf3[100]={"d:\\turbo2\\la03.txt\0"};
char buf4[100]={"d:\\turbo2\\la04.txt\0"};
char buf5[100]={"d:\\turbo2\\la05.txt\0"};*/
char message[]={"INSTRUCTION:\n"
" Wellcom to Job-shop Solution Space."};
char menu[]={"-menu-\n"
"**********datefile*********\n"
"*'1'.use the default data file\n\n"
"*'2'.manual input\n\n"
"*'3'.initiate \n\n"
"*'4'.adjust \n\n"
"*'q'.exit \n\n"
"**********datefile*********\n"};
char e;
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
gotoxy(4,9);
printf("%s",message);
gotoxy(10,22);
printf("PRESS ANY KEY TO MENU!");
getch();
do
{
clrscr();
gotoxy(1,9);
printf("%s",menu);
gotoxy(10,22);
printf("make a choice according to the menu please: ");
scanf("%c",&e);
switch(e)
{
case '1':{clrscr();
strcpy(filename,buf1);
inputdata();
gotoxy(1,9);
printf("\n the default file is 'la01.txt'.");
printf( "\n input sucess! "
"\n next,press any key to menu and then turn to initiate it .");
getch();
}
break;
case '2':{
clrscr();
printf("input the filename(n=10):");
scanf("%s",filename);
inputdata();
printf("\n press any key to menu!");
getch();
} break;
case '3':{
clrscr();
ready();
printf("\n press any key to menu!");
getch();
} break;
case '4':{
clrscr();
result();
printf("\n press any key to menu!");
getch();
} break;
/* case '5':{
clrscr();
strcpy(filename,buf5);
ready();
printf("\n press any key to menu!");
getch();
} break;*/
case 'q':{
clrscr();
gotoxy(10,9);
printf("***********"
"good bye!"
"***********\n");
break;
}
}
}while(e!='q');
getch();
clrscr();
}
void inputdata()
{
int i,j;
FILE *fp;
if((fp=fopen(filename,"r"))==NULL)
printf("error");
for(i=0;i<NUM;i++)
for(j=0;j<MAC;j++){
fscanf(fp,"%d",&sequence[i][j]);
fscanf(fp," %d",&cost[i][j]);
}
fclose(fp);
}
void ready()
{
int i,j;
int total=0;
FILE *fp;
if((fp=fopen(filename,"r"))==NULL)
printf("error");
for(i=0;i<NUM;i++)
for(j=0;j<MAC;j++){
fscanf(fp,"%d",&sequence[i][j]);
fscanf(fp," %d",&cost[i][j]);
}
fclose(fp);
for(i=0;i<5;i++)
{
d[i]=0;
t1[i]=0;
at[i]=0;
t[i]=0;
w[i]=0;
n[i]=0;
n1[i]=0;}
for(i=0;i<10;i++)
p[i]=0;
for (i=0;i<5;i++)
init(i);
printf("After initiate:\n");
print();
printf("\n***************************************************************\n");
for(i=0;i<NUM*WP;i++)
arrange();
}
void init(a)
{
int i,j;
int k,l;
for (i=0;i<NUM;i++)
p[i]=a;
for(i=0;i<NUM;i++)
{
temp1[i]=i;
temp2[i]=cost[i][a];
}
for(i=0;i<NUM;i++){
k=i;
for(j=i+1;j<NUM;j++)
{
if(temp2[j]<temp2[k])
k=j;
}
l=temp1[k];
temp1[k]=temp1[i];
temp1[i]=l;
l=temp2[k];
temp2[k]=temp2[i];
temp2[i]=l;
}
for(i=0;i<NUM;i++)
{
l=temp1[i];
j=sequence[l][a];
m[j][n[j]]=l;
mw[j][n[j]]=p[i];
w[j]=cost[m[j][0]][0];
if(a==0){
t1[j]=t1[j]+cost[l][0];
n1[j]=n[j]+1;
}
t[j]=t[j]+cost[l][a];
n[j]=n[j]+1;
}
printf("\n");
}
void arrange()
{
int i,k,q,x,y;
i=select();
k=m[i][at[i]];/*第几个工件*/
p[k]=p[k]+1;
q=sequence[k][p[k]];/*增加工序对应的机器*/
if(t1[q]<w[i]){
t[q]=t[q]+w[i]-t1[q];
cost[k][p[k]]=cost[k][p[k]]+w[i]-t1[q];
t1[q]=w[i];
}/*防止工序冲突*/
for(x=n1[q];x<NUM;x++)
if(m[q][x]==k&&x!=n1[q]){
y=m[q][x];
m[q][x]=m[q][n1[q]];
m[q][n1[q]]=y;
y=mw[q][x];
mw[q][x]=mw[q][n1[q]];
mw[q][n1[q]]=y;
if(d[q])
d[q]=0;
}/*调整位置*/
if(at[i]!=n1[i]-1)
{
at[i]=at[i]+1;
w[i]=w[i]+cost[m[i][at[i]]][mw[i][at[i]]];/*修改当前剩余时间*/
}
else if(at[i]==n1[i]-1&&at[i]!=9)
d[i]=1;/*空闲状态*/
}
int select()/*找出最快结束的工序*/
{
int i,k=0;
for(i=1;i<MAC;i++)
{
if(w[k]>w[i]&&!d[i])
k=i;
}
return k;
}
void result()
{
int i,k,j=0;
k=0;
printf("\nAfter adjust:\n");
print();
for(i=1;i<MAC;i++)
if(t[i]>t[k])
{
k=i;
}
printf("the optimal result: %d",t[k]);
}
void print()
{
int i,j,k,q,x;
printf(" -Machine-1------2------3------4------5------6------7------8------9-----10-----\n");
for(i=0;i<MAC;i++)
{
printf(" %d :",i);
for(j=0;j<10;j++)
{
k=m[i][j];
q=mw[i][j];
x=cost[k][q];
printf(" %d.%d ",k,q);
}
printf("\n");
if(t[i]>1000)t[i]-=600;
}
for(i=0;i<5;i++)
printf("\nt%d=%d n=%d\n",i,t[i],n[i]);
}
int test(int i)
{
int j,k,y;
k=m[i][at[i]];
j=sequence[k][p[k]+1];
for(y=0;y<n[j];y++)
{
if(m[j][y]==k)
return 0;
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -