【C++ 程序】 解线性方程组(Cramer法则)(分数形式结果)
生活随笔
收集整理的這篇文章主要介紹了
【C++ 程序】 解线性方程组(Cramer法则)(分数形式结果)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
f畢竟數學多用分數,所以修改了原程序:
程序
Source.cpp
#include <iostream> #include <conio.h> #include <Windows.h> #include "Det.h" #include "Fraction.h" using namespace std;int main() {int num;unsigned m = 0;deque<deque<double>> mat;deque<deque<deque<double>>> D;// instructioncout << "Please input like this:\n"<< "a_11 * x_1 + a_12 * x_2 + ... + a_1n * x_n = b_1\n"<< "a_21 * x_1 + a_22 * x_2 + ... + a_2n * x_n = b_2\n"<< "...\n"<< "a_n1 * x_1 + a_n2 * x_2 + ... + a_nn * x_n = b_n"<< "\n" << endl;// inputwhile (1){deque<double> row;while (cin >> num){row.push_back(num);if (getchar() == '\n') break;}mat.push_back(row);m = (row.size() > m) ? row.size() : m;if (mat.size() >= m - 1) break;}// hide the Console CursorHANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(hOut, &cci);cci.bVisible = FALSE;SetConsoleCursorInfo(hOut, &cci);// calculate & printfor (unsigned i = 0; i < m; i++){deque<deque<double>> temp = mat; // D[m - 1][m]for (unsigned j = 0; j != m - 1; j++){if (i) temp[j][i - 1] = mat[j][m - 1];temp[j].pop_back();}D.push_back(temp);}double D0 = det_is(D[0]);if (D0 != 0){cout << "\n-------------------------------------------------------------------------\n"<< "By default you view the solution in the FRACTION form.\n"<< "You can switch to DECIMAL by any keyboard press except Enter.\n"<< "Press Enter to end the program.\n"<< "-------------------------------------------------------------------------\n" << endl;for (unsigned i = 1; i < m; i++){cout << "x_" << i << " = " << decimal_to_fraction(det_is(D[i]) / D0) << endl;}while (1){if (_kbhit()){if (_getch() != '\r') // not Enter{// return to the front of the line (\r)// cover the initial input by pressing the keyboardcout << "\r\t\r\n-------------------------------------------------------------------------\n" << endl;for (unsigned i = 1; i < m; i++){cout << "x_" << i << " = " << det_is(D[i]) / D0 << endl;}}cout << "\n-------------------------------------------------------------------------" << endl;break;}}}else{cout << "\n-------------------------------------------------------------------------" << endl;cout << "\nNo finite solution!" << endl;cout << "\n-------------------------------------------------------------------------" << endl;}return 0; }Fraction.h
#ifndef _FRACTION_ #define _FRACTION_ #include <string> #include <vector>std::string decimal_to_fraction(double n) {std::string fra = "";std::string sgn = (n >= 0) ? "" : "-";n = fabs(n);double epsilon = 1E-10;if (n - static_cast<int>(n) < epsilon){fra = std::to_string(static_cast<int>(n));return fra;}else if (n - static_cast<int>(n) > 1 - epsilon){fra = std::to_string(static_cast<int>(n) + 1);return fra;}else{std::vector<int> denominator;for (int i = 2; i <= 100000; i++)denominator.push_back(i);for (auto c : denominator) // every number within 100000{if (c * n - static_cast<int>(c * n) < epsilon){fra = sgn + std::to_string(static_cast<int>(c * n)) + "/" + std::to_string(c);return fra;}if (c * n - static_cast<int>(c * n) > 1 - epsilon){fra = sgn + std::to_string(static_cast<int>(c * n) + 1) + "/" + std::to_string(c);return fra;}}}return std::to_string(n); }#endif // !_FRACTION_Det.h
見我的博客 【C++ 程序】 行列式。
輸出示例
分析
- 輸出分數原理:判斷分數值與小數值相差不超過一個極小的值,此處即epsilon。特別注意負數時候的情況!
- \r回車,回到一行最前端。(事實上Enter=\r\n,不過C++中\n就是\r\n)
- 其他詳見我的博客 【C++ 程序】 解線性方程組(Cramer法則)。
ALL RIGHTS RESERVED ? 2020 Teddy van Jerry
歡迎轉載,轉載請注明出處。
See also
Teddy van Jerry 的導航頁
總結
以上是生活随笔為你收集整理的【C++ 程序】 解线性方程组(Cramer法则)(分数形式结果)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Thread类的有关方法以及优先级
- 下一篇: Node.js 安装配置