0
#include <stdio.h>
2.
include <conio.h>
int k,m,n,q=100,type,get,let,et,code[50],xb[50],ia,iaa[50],indexg, indexl,indexe;
float sum,a[50][50],b[50],c[50];
void initiate();
void solve();
void main(){
int i,j;
// clrscr();
/*** input data ***/
printf("nwelcome to the linear programming solution!nn***nnotice 1!n***nif the type of objective equation is 'max' ,please enter '1'!nelse please enter '0'!nnn");
printf("***nnotice 2!n***ndefine the type of subjective equation as following:n'<='is equal to '0'!n'>=' is equal to '1'!n'='is equal to '2'!n");
printf("nnplease input the coefficients or the constants:n");
printf("the number of subjective equations ? ");
scanf("%d",&m);
printf("the number of variables ? ");
scanf("%d",&k);
printf("the number of ' <= 'subjective equations ? ");
scanf("%d",&let);
printf("the number of ' >= 'subjective equations ? ");
scanf("%d",&get);
printf("the number of ' = 'subjective equations ? ");
scanf("%d",&et);
printf("the type of objective equation ? ");
scanf("%d",&type);
n=k+let+et+2*get;
for(i=0;i<m;i++)
{printf("please input %d's equation:n",i+1);
printf("type ? ");
scanf("%d",&code[i]);
printf("constant ? ");
scanf("%f",&b[i]);
for(j=0;j<k;j++)
{printf("coefficient ? ");
scanf("%f",&a[i][j]);
}
}
printf("plese input the constants of the object equation:n");
for(j=0;j<k;j++)
scanf("%f",&c[j]);
printf("nn***nplease check the data you just input!n*** n");
getch();
if(type)
printf("the type of the object equation is 'max'n");
else
printf("the type of the object equation is 'min'n");
printf("nthe object equation is :n");
for(j=0;j<k;j++)
{printf("(%f)x%d ",c[j],j+1);
if(j!=k-1)
printf("+");
}
printf("nnthe suject equation is :");
for(i=0;i<m;i++)
{printf("nnumber %d suject equation is: %d %f ",i+1,code[i],b[i]);
for(j=0;j<k;j++)
{printf("(%f)x%d ",a[i][j],j+1);
if(j!=k-1)
printf("+");
}
}
/*** initiate data ***/
initiate();
solve();
if(!type)
a[m][n]=-a[m][n];
printf("nnthe optimal value of the original objective function is: %f ",a[m][n]);
getch();
}
/*** initiate variables function ***/
void initiate(){
int i,j;
indexg=k;
indexl=indexg+get;
indexe=indexl+let;;
for(i=0;i<m+1;i++)
for(j=k;j<n+1;j++)
a[i][j]=0;
for(i=0;i<m;i++)
a[i][n]=b[i];
for(i=0;i<m;i++)
switch(code[i])
{ case 0: {xb[i]=indexl;
a[i][indexl++]=1;
break;
};
case 1: {xb[i]=indexe;
iaa[ia++]=i;
a[i][indexe++]=1;
a[i][indexg++]=-1;
break;
};
case 2: {xb[i]=indexe;
iaa[ia++]=i;
a[i][indexe++]=1;
break;
};
}
for(j=0;j<k;j++)
if(type)
a[m][j]=-c[j];
else
a[m][j]=c[j];
for(j=k;j<=n;j++)
a[m][j]=0;
for(j=k+get+let;j<n;j++)
a[m][j]=(float)q;
sum=0;
for(j=0;j<=n;j++)
{sum=0;
for(i=0;i<ia;i++)
sum=sum+a[iaa[i]][j];
a[m][j]=a[m][j]-sum*q;
}
return;
}
/*** process data function ***/
void solve(){
int i,j,mark=1,minus, minusmark,basic=0,divide, dividemark;
float h,p;
while(1)
{mark=0;
minusmark=0;
minus=0;divide=1000;
dividemark=0;
printf("nnbasic solution %d isn",++basic);
for(i=0;i<m;i++)
printf("basic variable %d = x( %d )= %fn",i+1,xb[i]+1,a[i][n]);
printf("ncurrent value of the object equation is: %fn",a[m][n]);
getch();
for(j=0;j<n;j++)
{
if(a[m][j]<-6e-8)
mark++;
if(a[m][j]<minus)
{minus=(int)a[m][j];
minusmark=j;
}
}
if(mark==0)
break;
for(i=0;i<m;i++)
{if(a[i][minusmark]==0)
continue;
if(a[i][n]/a[i][minusmark]<=0)
continue;
if(a[i][n]/a[i][minusmark]<divide)
{divide=(int)(a[i][n]/a[i][minusmark]);
dividemark=i;
}
}
xb[dividemark]=minusmark;
if(divide<0)
printf("there is no solution because of no boundary!");
p=a[dividemark][minusmark];
for(j=0;j<n+1;j++)
a[dividemark][j]=a[dividemark][j]/p;
for(i=0;i<m+1;i++)
{h=a[i][minusmark];
if(i==dividemark)
continue;
for(j=0;j<n+1;j++)
a[i][j]=a[i][j]-h*a[dividemark][j];
}
}
printf("nn***nthe last basic solution is optimal!n*** n");
// return;
}
Tümünü Göster