为了生成一个方程组,今天搓了一个
利用增广矩阵进行操作
#include <stdio.h>
#include<iostream>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
using namespace std; #define MAX_SIZE 200int var_num=0;int matrix[MAX_SIZE][MAX_SIZE];int sum;void print(){for(int i=0;i<sum;i++){for(int j=0;j<sum+1;j++){printf("%d,",matrix[i][j]);}printf("%d",matrix[i][16]);printf("\n");}printf("\n");
}void mul(int i){srand(time(NULL)+ (unsigned) sleep(1));int multiplier= rand() % 8 + 1;for(int j=0;j<sum+1;j++){matrix[i][j]=matrix[i][j]*multiplier;}print();
} void add(){int s=18;while(s--){srand(time(NULL));int random1, random2;do {random1 = 1 + rand() % (sum-1); sleep(1); random2 = 1 + rand() % (sum-1);} while (random1 == random2);for(int i=0;i<=16;i++){matrix[random1][i]+= matrix[random2][i];}print();}
} void print_result(){bool first=true;for(int j=0;j<sum;j++){first=true;for(int i=1;i<sum;i++){if(matrix[j][i-1]!=0&&first){printf("v%d*%d",i,matrix[j][i-1]);first=false; }else if(matrix[j][i-1]!=0)printf("+v%d*%d",i,matrix[j][i-1]); } if(matrix[j][sum-1]!=0){printf("+v16*%d=%d\n",matrix[j][sum-1],matrix[j][sum]);}else{printf("=%d\n",matrix[j][sum-1]);}}
} init(){printf(" ____________________\n");printf("< Equation set maker >\n");printf(" --------------------\n");printf(" \ ^__^\n");printf(" \ (oo)\_______\n");printf(" (__)\ )\/\ \n");printf(" ||----w |\n");printf(" || ||\n");printf("请输入你要求的方程组的解的个数:\n");cin >>sum;printf("请输入你要求的方程的解:\n");for(int i=0;i<sum;i++){scanf("%d",&matrix[i][sum]);print();}for(int i=0;i<sum;i++){matrix[i][i]=1;}print();
}int main(){init();printf("开始进行乘法操作,增广矩阵的每一行乘一个随机数\n"); for(int i=0;i<=sum;i++){mul(i);}printf("乘法完毕\n\n"); printf("对增广矩阵的任意两行相加,开始加法\n"); add(); printf("加法完毕,输出得到的方程组:\n");print_result();}
Linux版本:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>#define MAX_SIZE 200int var_num = 0;int matrix[MAX_SIZE][MAX_SIZE];int sum;void print() {for (int i = 0; i < sum; i++) {for (int j = 0; j < sum + 1; j++) {printf("%d,", matrix[i][j]);}printf("%d", matrix[i][16]);printf("\n");}printf("\n");
}void mul(int i) {srand(time(NULL) + (unsigned)sleep(1));int multiplier = rand() % 8 + 1;for (int j = 0; j < sum + 1; j++) {matrix[i][j] = matrix[i][j] * multiplier;}print();
}void add() {int s = 18;while (s--) {srand(time(NULL));int random1, random2;do {random1 = 1 + rand() % (sum - 1);sleep(1);random2 = 1 + rand() % (sum - 1);} while (random1 == random2);for (int i = 0; i <= 16; i++) {matrix[random1][i] += matrix[random2][i];}print();}
}void print_result() {bool first = true;for (int j = 0; j < sum; j++) {first = true;for (int i = 1; i < sum; i++) {if (matrix[j][i - 1] != 0 && first) {printf("v%d*%d", i, matrix[j][i - 1]);first = false;} else if (matrix[j][i - 1] != 0)printf("+v%d*%d", i, matrix[j][i - 1]);}if (matrix[j][sum - 1] != 0) {printf("+v16*%d=%d\n", matrix[j][sum - 1], matrix[j][sum]);} else {printf("=%d\n", matrix[j][sum - 1]);}}
}void init() {printf(" ____________________\n");printf("< Equation set maker >\n");printf(" --------------------\n");printf(" \\ ^__^\n");printf(" \\ (oo)\\_______\n");printf(" (__)\\ )\\/\\ \n");printf(" ||----w |\n");printf(" || ||\n");printf("请输入你要求的方程组的解的个数:\n");scanf("%d", &sum);printf("请输入你要求的方程的解:\n");for (int i = 0; i < sum; i++) {scanf("%d", &matrix[i][sum]);print();}for (int i = 0; i < sum; i++) {matrix[i][i] = 1;}print();
}int main() {init();printf("开始进行乘法操作,增广矩阵的每一行乘一个随机数\n");for (int i = 0; i <= sum; i++) {mul(i);}printf("乘法完毕\n\n");printf("对增广矩阵的任意两行相加,开始加法\n");add();printf("加法完毕,输出得到的方程组:\n");print_result();return 0;
}