需要再qt中可以正常实现图片上的代码运行,多谢各位 麻烦了 谢谢各位
1条回答 默认 最新
- qfl_sdu 2022-05-18 19:04关注
Qt控制台项目还是纯C++项目,稍微有点差别(头文件和返回值有差别,Qt控制台项目多一个函数调用,代码都贴出来了)。
运行结果:Qt控制台项目代码:
#include <QCoreApplication> #include <iostream> #include <time.h> using namespace std; int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); srand((unsigned int)time(0)); int nmbxt = 0, nmbgz = 0; //系统和故障个数 cout << "请输入故障数和系统数:"; cin >> nmbgz >> nmbxt; //输入故障数和系统数量 //矩阵 int** matrix = new int* [nmbgz]; for (int i = 0; i < nmbxt; i++) matrix[i] = new int[nmbxt]; int* gz = new int[nmbgz]; //故障类型 0表示一定发生,1表示可能发生,2表示一定不发生 for (int i = 0; i < nmbgz; i++) gz[i] = 2; //随机生成矩阵 int once = 0; // int count = 0; for (int i = 0; i < nmbxt; i++) { count = 0; for (int j = 0; j < nmbgz; j++) { matrix[j][i] = rand() % 2; if (matrix[j][i] == 1) { count++; } } if (count == 1) once = 1; } //如果没有1,则随机选一个元素设置为1 if (once == 0) { int index = rand() % nmbgz; int xt = rand() % nmbxt; for (int i = 0; i < nmbgz; i++) matrix[i][xt] = 0; matrix[index][xt] = 1; } //输出矩阵 for (int i = 0; i < nmbgz; i++) { cout << "故障" << i+1 << " "; for (int j = 0; j < nmbxt; j++) cout << matrix[i][j]; cout << endl; } //计算每种故障的可能性 int gs_must = 0; int gs_may = 0; int gs_no = 0; //1.先找一定会发生的故障 for (int i = 0; i < nmbxt; i++) { int tms = 0; int gzindex = 0; for (int j = 0; j < nmbgz; j++) { if (matrix[j][i] == 1) { tms++; gzindex = j; } } if (tms == 1) { gz[gzindex] = 0; //一定发生 gs_must++; } } //2 再找可能发生的故障 for (int i = 0; i < nmbxt; i++) { for (int j = 0; j < nmbgz; j++) { if (matrix[j][i] == 1) { if (gz[j] == 0) //如果一定发生 continue; else { gz[j] = 1; //可能发生 } } } } //输出 if (gs_must) { cout << "故障"; for (int i = 0; i < nmbgz; i++) { if (gz[i] == 0) cout << i + 1 << " "; } cout << "一定发生"; } cout << ",故障"; for (int i = 0; i < nmbgz; i++) { if (gz[i] == 1) { gs_may++; cout << i + 1 << " "; } } cout << "可能发生"; if (gs_must + gs_may < nmbgz) { cout << ",故障"; for (int i = 0; i < nmbgz; i++) { if (gz[i] == 2) cout << i + 1 << " "; } cout << "一定不发生" << endl; } return a.exec(); }
纯C++代码:
//#include <QCoreApplication> #include <iostream> #include <time.h> using namespace std; int main(int argc, char* argv[]) { //QCoreApplication a(argc, argv); srand((unsigned int)time(0)); int nmbxt = 0, nmbgz = 0; //系统和故障个数 cout << "请输入故障数和系统数:"; cin >> nmbgz >> nmbxt; //输入故障数和系统数量 //矩阵 int** matrix = new int* [nmbgz]; for (int i = 0; i < nmbxt; i++) matrix[i] = new int[nmbxt]; int* gz = new int[nmbgz]; //故障类型 0表示一定发生,1表示可能发生,2表示一定不发生 for (int i = 0; i < nmbgz; i++) gz[i] = 2; //随机生成矩阵 int once = 0; // int count = 0; for (int i = 0; i < nmbxt; i++) { count = 0; for (int j = 0; j < nmbgz; j++) { matrix[j][i] = rand() % 2; if (matrix[j][i] == 1) { count++; } } if (count == 1) once = 1; } //如果没有1,则随机选一个元素设置为1 if (once == 0) { int index = rand() % nmbgz; int xt = rand() % nmbxt; for (int i = 0; i < nmbgz; i++) matrix[i][xt] = 0; matrix[index][xt] = 1; } //输出矩阵 for (int i = 0; i < nmbgz; i++) { cout << "故障" << i+1 << " "; for (int j = 0; j < nmbxt; j++) cout << matrix[i][j]; cout << endl; } //计算每种故障的可能性 int gs_must = 0; int gs_may = 0; int gs_no = 0; //1.先找一定会发生的故障 for (int i = 0; i < nmbxt; i++) { int tms = 0; int gzindex = 0; for (int j = 0; j < nmbgz; j++) { if (matrix[j][i] == 1) { tms++; gzindex = j; } } if (tms == 1) { gz[gzindex] = 0; //一定发生 gs_must++; } } //2 再找可能发生的故障 for (int i = 0; i < nmbxt; i++) { for (int j = 0; j < nmbgz; j++) { if (matrix[j][i] == 1) { if (gz[j] == 0) //如果一定发生 continue; else { gz[j] = 1; //可能发生 } } } } //输出 if (gs_must) { cout << "故障"; for (int i = 0; i < nmbgz; i++) { if (gz[i] == 0) cout << i + 1 << " "; } cout << "一定发生"; } cout << ",故障"; for (int i = 0; i < nmbgz; i++) { if (gz[i] == 1) { gs_may++; cout << i + 1 << " "; } } cout << "可能发生"; if (gs_must + gs_may < nmbgz) { cout << ",故障"; for (int i = 0; i < nmbgz; i++) { if (gz[i] == 2) cout << i + 1 << " "; } cout << "一定不发生" << endl; } return 0; //return a.exec(); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
- ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
- ¥50 rk3588板端推理
- ¥250 opencv怎么去掉 数字0中间的斜杠。
- ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
- ¥250 paddleocr带斜线的0很容易识别成9
- ¥15 电子档案元素采集(tiff及PDF扫描图片)
- ¥15 flink-sql-connector-rabbitmq使用
- ¥15 zynq7015,PCIE读写延时偏大
- ¥15 使用spss做psm(倾向性评分匹配)遇到问题