Last active
August 29, 2015 14:02
-
-
Save dd1994/b50bc5a9b3c79ff12ef9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://en.wikipedia.org/wiki/Simplex_algorithm | |
| #include "SimplexAlgorithm.h" | |
| #define M 200 | |
| #define N 100 | |
| int main(void) | |
| { | |
| int n, m, i, j; | |
| printf("请输入总的变量的个数和松弛变量的个数:\n"); | |
| scanf("%d %d", &m, &n); | |
| typedef struct | |
| { | |
| float a[M][N]; | |
| float b[M]; | |
| float c[N]; | |
| float d[N]; | |
| }vector; | |
| vector sample; | |
| printf("请输入所有变量的价值系数:\n"); | |
| for (i = 0; i < n; ++i) | |
| { | |
| scanf("%f", &sample.c[i]); | |
| } | |
| for (i = 0; i < n; ++i) | |
| { | |
| //初始化检验数组d=c | |
| sample.d[i] = sample.c[i] | |
| } | |
| printf("输入约束方程组右端的系数\n"); | |
| for (i = 0; i < m; ++i) | |
| { | |
| scanf("%f", &sample.b[i]); | |
| } | |
| printf("输入典式中约束方程组的系数矩阵:\n"); | |
| for (i = 0; i < m; ++i) | |
| { | |
| for (j = 0; j < n; ++j) | |
| { | |
| scanf("%f", &sample.a[i][j]); | |
| } | |
| } | |
| while(0 == determine(sample.d, n)) | |
| { | |
| i = findMaxIndex(sample.d, n); | |
| j = findBasicVariable(sample.b, sample.a, i) | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://en.wikipedia.org/wiki/Simplex_algorithm | |
| #include <stdio.h> | |
| #include <math.h> | |
| int determine(float *d, int n) | |
| { | |
| //判断一个数组是否所有的元素都小于等于零 | |
| //如果是,返回1,否则返回0 | |
| int flag = 1; | |
| int i; | |
| for (i = 0; i < n; ++i) | |
| { | |
| if (d[i] > 0) | |
| { | |
| flag = 0; | |
| break; | |
| } | |
| } | |
| return flag; | |
| } | |
| int findMaxIndex(float *d, int n) | |
| { | |
| //返回一个数组的最大值的下标 | |
| float max; | |
| int i, maxIndex; | |
| max = d[0]; | |
| maxIndex = 0; | |
| for (i = 1; i < n; ++i) | |
| { | |
| if (d[i] > max) | |
| { | |
| max = d[i]; | |
| maxIndex = i; | |
| } | |
| } | |
| return maxIndex; | |
| } | |
| int findBasicVariable(float *b, float (*a)[N], int i) | |
| { | |
| //返回需要变成基变量竖直方向的下标 | |
| float temp[M]; | |
| int j, k, basicVariableIndex; | |
| for (j = 0, k = 0; j < m; ++j, ++k) | |
| { | |
| if (a[i][j] = 0) | |
| { | |
| temp[k] = 0; | |
| } | |
| else | |
| { | |
| temp[k] = (float)(b[j] / a[i][j]); | |
| } | |
| } | |
| basicVariableIndex = findMaxIndex(temp, k); | |
| return basicVariableIndex; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment