程序源代码:
#include
#include
#define MAX_N 100
#define M 1000000.0;
int m, n;
double A[MAX_N][MAX_N], C[MAX_N], b[MAX_N], seta[MAX_N], CZ[MAX_N];
int num[MAX_N];
int GetOutCow() //求换出基
{
int i,k;
int flag;
double min = 0;
for (i = 0; i < m; i ++)
{
if (b[i] >= 0)
flag = 1;
else
{
flag = 0;
break;
}
}
if (flag == 1)
return -1;
for (i = 0; i < m; i ++)
{
if (b[i] < 0 && min > b[i])
{
min = b[i];
k = i;
}
}
return k;
}
int GetInCow(int p) //求换入基
{
int i, j;
int flag = 0;
double min;
for (j = 0; j < n; j ++)
{
if (A[p][j] >= 0)
flag = 1;
else
{
flag = 0;
break;
}
}
if (flag == 1)
{
printf("\n原线性规划问题无可行解!\n");
return -1;
}
for (j = 0; j < n; j ++)
{
if (A[p][j] < 0)
seta[j] = CZ[j] / A[p][j];
else
seta[j] = M;
}
min = M;
for (j = 0; j < n; j ++)
{
if (min >= seta[j])
{
min = seta[j];
i = j;
}
}
num[p] = i + 1;
return i;
}
void change(int p, int q) //计算新的单纯形表
{
int i, j;
double temp1, temp2, temp3;
temp1 = A[p][q];
for (i = 0; i < m; i ++)
{
if (i != p)
{
if (A[i][q] != 0)
{
temp2 = A[i][q] / temp1;
for (j = 0; j < n; j ++)
A[i][j] = A[i][j] - A[p][j] * temp2;
b[i] = b[i] - b[p] * temp2;
}
}
}
temp3 = CZ[q] / temp1;
for (i = 0; i < n; i ++)
CZ[i] = CZ[i] - A[p][i] * temp3;
for (j = 0; j < n; j ++)
A[p][j] = A[p][j] / temp1;
b[p] = b[p] / temp1;
}
void print1()
{
int i;
printf("\n--------------------------------------------------------------------------\n");
printf("\t");
for(i = 1; i <= n; i ++)
printf("X(%d)\t", i);
printf("RHS\n");
printf("--------------------------------------------------------------------------\n");
printf("\t");
for (i = 0; i < n; i ++)
{
printf("%.3lf\t", -C[i]);
}
printf("\n--------------------------------------------------------------------------\n");
}
void print2()
{
int i, j;
printf("\n-------------------------------------------------------------------------