C语言 - 【线性代数】二阶、三阶行列式的计算

运行测试:

C语言 - 【线性代数】二阶、三阶行列式的计算

C语言 - 【线性代数】二阶、三阶行列式的计算

C语言 - 【线性代数】二阶、三阶行列式的计算

 

 

源码: 

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

void CleanConsole(int x, int y)     //y是指定要删除的行数,x可以从0位置开始循环替换到的截止位置
{
	COORD loc;
	loc.Y = y;    //固定行数
	for (int i = 0; i < x; i++)     //x从指定位置开始循环进行替换
	{
		loc.X = i;
		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
		cout << " " << endl;;
	}
}
void CleanConsole(int y)     //y是指定要删除的行数,直接指定x:0~50的位置进行清空
{
	COORD loc;
	loc.Y = y;    //固定行数
	for (int i = 0; i < 50; i++)     //x从指定位置开始循环进行替换
	{
		loc.X = i;
		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
		cout << " " << endl;;
	}
}


void WriteChar(short x, short y, char *pchar, char color)
{
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = 1;   //光标的厚度
	cci.bVisible = FALSE;    //光标不可见
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);

	COORD loc = { x,y };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
	cout << pchar;
}


int ShowOptions()
{
	system("mode con cols=85 lines=28");
	char *options[] = { "二阶行列式" , "三阶行列式","注意事项","退出" };    //这里这样定义是为了使得option的值能和options的下标对应,便于刷新输出
	WriteChar(18 + 11, 10, "线性代数之行列式计算 v1.0", 10);
	WriteChar(16 + 11, 12, "→", 10);
	for (int i = 0; i < 4; i++)
	{
		WriteChar(18 + 11, 12 + i, options[i], 10);
	}
	char ch;
	int option = 0;    //默认指向最上面的选项
	while (true)
	{
		if (_kbhit())
		{
			ch = _getch();  //不进行回显

			if (ch == 27)   //esc
			{
				return -1;
			}
			if (ch == 72 || ch == 80 || ch == '\r')  //只检测上下键+回车直接返回当前的option值
			{

				if (ch == 72)	//UP
				{
					WriteChar(16 + 11, 12 + option, "  ", 0);
					option--;
				}
				else if (ch == 80)	//DOWN
				{
					WriteChar(16 + 11, 12 + option, "  ", 0);
					option++;
				}
				if (option < 0)    //防止越界
				{
					option = 0;
				}
				else if (option >= 4)    //一直让其指向租后一个选项
				{
					option--;
				}
				//处理按上下键之后的显示
				WriteChar(16 + 11, 12 + option, "                        ", 0);
				Sleep(100);
				WriteChar(16 + 11, 12 + option, "→", 10);
				WriteChar(18 + 11, 12 + option, options[option], 10);

				if (ch == '\r')
				{
					return option;
				}
			}
		}
	}
}

void Page_1()
{
	system("cls");
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = 1;   //光标的厚度
	cci.bVisible = TRUE;    //光标可见
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);

	printf("请输入二阶行列式的4个数据,按照行式输入:\n");
	int a[4];
	int i = 0;
	int temp_number = 0;
	int result = 0;
	while (i<4)
	{
		scanf("%d", &temp_number);
		if (temp_number > -1000 && temp_number<1000)
		{
			a[i] = temp_number;
			i++;
		}
	}
	result = a[0] * a[3] - a[1] * a[2];
	printf("结果:%ld", result);
	printf("\n\n\n\n");
	printf("**按任意键返回主菜单**");
}
void Page_2()
{
	system("cls");
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = 1;   //光标的厚度
	cci.bVisible = TRUE;    //光标可见
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);

	printf("请输入三阶行列式的9个数据,按照行式输入:\n");
	int a[9];    //设置数组存储三阶行列式的9个数据
	long result_0 = 1;
	long result_1 = 1;
	long result_temp = 0;
	long result = 0;
	int temp_number = 0;
	int i = 0;
	while (i<9)
	{
		scanf("%d", &temp_number);
		if (temp_number > -1000 && temp_number<1000)
		{
			a[i] = temp_number;
			i++;
		}
	}

	int index_0[9] = { 0,4,8,1,5,6,2,3,7 };
	int index_1[9] = { 2,4,6,0,5,7,1,3,8 };    //存储主对角线和副对角线的数组下标
	for (int i = 0; i < 9;)
	{
		result_0 = 1;
		result_1 = 1;
		for (int j = 0; j < 3; j++, i++)
		{
			result_0 *= a[index_0[i]];
			result_1 *= a[index_1[i]];
		}
		result_temp = result_0 - result_1;
		result += result_temp;
	}
	printf("结果:%ld", result);
	printf("\n\n\n\n");
	printf("**按任意键返回主菜单**");
}
void Page_3()
{
	system("cls");
	WriteChar(20, 10, "注意: 行列式中的数值范围为[-1000,1000]\n\n", 15);
	WriteChar(20, 12, "**按任意键返回主菜单**\n\n\n\n", 15);
}


int main()
{
	system("title 线性代数");
	while (true)  
	{
		int result = ShowOptions();
		if (result == 0)
		{
			Page_1();
			CONSOLE_CURSOR_INFO cci;
			cci.dwSize = 1;  
			cci.bVisible = FALSE;   
			SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
			_getch();
			//system("pause");
		}
		else if (result == 1)
		{
			Page_2();
			CONSOLE_CURSOR_INFO cci;
			cci.dwSize = 1;  
			cci.bVisible = FALSE;  
			SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
			_getch();
			//system("pause");
		}
		else if (result == 2)
		{
			Page_3();
			CONSOLE_CURSOR_INFO cci;
			cci.dwSize = 1;   
			cci.bVisible = FALSE;  
			SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
			_getch();
			//system("pause");
		}
		else
		{
			cout << endl << endl;
			return -1;
		}
	}

	return 0;
}