c++ 求矩陣行列式

概述

求矩陣行列式

代碼

#include<iostream>
#include<stdlib.h>
using namespace std;

// 全局變量
int MenuSelect;
int matrix[10][20];	// 矩陣
int clone[10][20];
int n;
int DetValue;

// 函數
void MainMenu();//主界面
void InputMatrix();//輸入矩陣
void LookMatrix();//查看矩陣
void SeekDet();//求行列式

int main()
{
	MainMenu();
	cin>>MenuSelect;
	while(1)
	{
		if(MenuSelect == 1)
		{
			InputMatrix();
		}
		else if(MenuSelect == 2)
		{
			LookMatrix();
		}
		else if(MenuSelect == 3)
		{
			SeekDet();
		}
		getchar();
		cout<<"按任意鍵回到主菜單"<<endl;
		getchar();
		system("cls");
		MainMenu();
		cin>>MenuSelect;
	}
	return 0;
}

void MainMenu()
{
	cout<<"     矩陣操作系統"<<endl;
	cout<<"1.輸入矩陣"<<endl;
	cout<<"2.查看矩陣"<<endl;
	cout<<"3.求行列式"<<endl;
	cout<<"4.求逆矩陣"<<endl;
	cout<<"請輸入操作指令:";
}

void InputMatrix()
{
	system("cls");
	int i, j, temp;
	cout<<"請輸入矩陣的階數:";
	cin>>n;
	cout<<"請輸入矩陣的值:";
	for(i = 0; i < n; i++)
	{
		for(j = 0; j < n; j++)
		{
			cin>>temp;
			matrix[i][j] = temp;
			clone[i][j] = matrix[i][j];
		}
	}
}

void LookMatrix()
{
	system("cls");
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			cout<<clone[i][j]<<' ';
		}
		cout<<endl;
	}
}

void SeekDet()
{

	if(n == 2)
	{
		DetValue = (matrix[0][0] * matrix[1][1]) - (matrix[0][1]*matrix[1][0]);
	}
	else if(n == 3)
	{
		DetValue = (matrix[0][0] * matrix[1][1] * matrix[2][2]) + (matrix[1][0] * matrix[2][1] * matrix[0][2]) + (matrix[1][0] * matrix[1][2] * matrix[2][0])
			- (matrix[0][2] * matrix[1][1] * matrix[2][0]) - (matrix[0][1] * matrix[1][0] * matrix[2][2]) - (matrix[0][0] * matrix[1][2] * matrix[2][1]);
	}
	system("cls");
	cout<<DetValue<<endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章