⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 三种方法.cpp

📁 数值分析中三中方法求解
💻 CPP
字号:
//*************************************************
//计算机011班 19号 覃亮
//题目:P356.3(d),(f)
//功能:线形方程三种方法求解
//*************************************************
#include <iostream.h>
#include <iomanip.h>
#include "stdio.h"
#include <math.h>
#define Num 20
void nemu();
void disp(float s[][Num], int c, int r)
{ 
	int i,j;
   for(i=0; i<r; i++)
	{
		for(j=0; j<c; j++)
			cout<<s[i][j]<<"   ";
		cout<<endl;
	}
}

float f(float s[][Num], int i, int column, float x[])// 方法1返回函数!
{
	float value=0;
	int j;
	for(j=i+1; j<column-1; j++)
	{
		value+=s[i][j]*x[j];
	}
	return(value);
}


float g(float *Nrow[], int i, int column, float x[])// 方法2返回函数!
{
	float value=0;
	int j;
	for(j=i+1; j<column-1; j++)
	{
		value+=*(Nrow[i]+j)*x[j];
	}
	return(value);
}


int Max(float *Nrow[], int i, int r)
{
	int k,j,h;
	    k=i;
		h=i;
	    do         //寻找p中最大的一组
	    {
			for(j=i; j<r-1; j++)
			{
			  if(fabs(*(Nrow[j]+i))>fabs(*(Nrow[k]+i)))
				  break;
			}
	        if(fabs(*(Nrow[j]+i))>fabs(*(Nrow[k]+i)))
			    k=j;
			h++;
			  
		}while(h<r);
		return(k);
}



void Pivoting(float s[][Num], int c, int r)
{
	int i,j,k,p,h,l;
	float m,t;	
	float x[Num],*temp;
    float *Nrow[Num];
	for(i=0; i<r; i++)//  主程序(对数组进行初始化)
		Nrow[i]=s[i];
	disp(s,c,r);
	cout<<endl;
	for(i=0; i<r-1; i++)
	{
		p=Max(Nrow,i,r);
//		cout<<"p="<<p<<endl;
		if(s[p][i]==0)
	    {
			cout<<"No Unique solution exists!"<<endl;
	        return;
	    }
	    
		if(p!=i)
	    {
			temp=Nrow[i];
			Nrow[i]=Nrow[p];
			Nrow[p]=temp;
		}
		
/*		for(h=0; h<r; h++)
		{
			for(l=0; l<c; l++)
				cout<<*(Nrow[h]+l)<<"   ";
			cout<<endl;
		}
		cout<<endl<<endl<<endl;*/
		for(j=i+1; j<r; j++)      
		{
			t=*(Nrow[i]+i);
			m=*(Nrow[j]+i)/t;
			for(k=i; k<c; k++)// 处理完一组元素
	        *(Nrow[j]+k)=*(Nrow[j]+k)-m*(*(Nrow[i]+k));
                	
	    for(h=0; h<r; h++)
			{
			    for(l=0; l<c; l++)
				   printf("%0.1f\t",*(Nrow[h]+l));
			       cout<<endl;
			}
			cout<<endl<<endl<<endl;  
		}
	}
	if(*(Nrow[r-1]+r-1)==0) 
	{
		cout<<"No Unique solution exists!"<<endl;
	    return;
	}
	k=c-1;
	t=*(Nrow[r-1]+c-2);
	x[k]=*(Nrow[r-1]+c-1)/t;
	for(i=k-1; i>=0; i--)
	{
        m=*(Nrow[i]+i);
		x[i]=(*(Nrow[i]+c-1)-g(Nrow,i,c,x))/m;
	}
	cout<<endl<<endl<<endl;
	cout<<"x的解为:"<<endl;
	for(i=0; i<c-1; i++)
		cout<<"x"<<i<<"="<<x[i]<<" , "; 
}




void Gassian(float s[][Num], int c, int r)
{
	int i,j,h,k,p;
	float m;	
	float x[Num],temp[Num];

    for(i=0; i<r-1; i++)//  主程序
	{
	    p=i;
	    do             //寻找p!=0的一组
	      {
			  if(s[p][i]!=0)
		      break;
		      p++;
	      }while(p<r);
	    
		if(p==r)
	    {
			cout<<"No Unique solution exists!"<<endl;
	        return;
	    }
	    if(p!=i)
	      {
			   for(h=0; h<c; h++)
				   temp[h]=s[i][h];
			   for(h=0; h<c; h++)
			       s[i][h]=s[p][h];
			   for(h=0; h<c; h++)
				  s[p][h]=temp[h];
		   }
	
		for(j=i+1; j<r; j++)      
		{
			m=s[j][i]/s[i][i];
			for(k=i; k<c; k++)// 处理完一组元素
			    s[j][k]=s[j][k]-m*s[i][k];
			disp(s, c, r);
			cout<<endl<<endl<<endl;   
		}
	}
	disp(s, c, r);        //第一次输出
	if(s[r-1][c-2]==0) 
	{
		cout<<"No Unique solution exists!"<<endl;
	    return;
	}
	k=c-1;
	x[k]=s[r-1][c-1]/s[r-1][c-2];
	for(i=k-1; i>=0; i--)
	{
        x[i]=(s[i][c-1]-f(s,i,c,x))/s[i][i];
	}
	cout<<endl<<endl<<endl;
	cout<<"x的解为:"<<endl;
	for(i=0; i<c-1; i++)
		cout<<"x"<<i<<"="<<x[i]<<" , "; 
}


void  Scaled_pivoting(float s[][Num], int c, int r)// 列主元消元法
{
	int i,j,sign=0,k,p,h,l;
	float t,m;
	float key[Num],*temp,x[Num];
	float *Nrow[Num];
	for(i=0; i<r; i++)//  对数组进行初始化
		Nrow[i]=s[i];
	disp(s,c,r);
	for(i=0; i<r-1; i++)//  主程序 
	{
		for(h=i,j=0; h<r; h++)
		{
			k=0;
		    while(j<c)
			{
				if(fabs(*(Nrow[h]+sign))<fabs(*(Nrow[h]+k)))
					sign=k;
			    j++;
			    k++;
			}
	        if(*(Nrow[h]+sign)==0)
			{
				cout<<"no unique solution exists"<<endl;
			    return;
			}
		    key[h]=(float)(fabs(*(Nrow[h]+sign)));
		}
	
	    for(h=0; h<r; h++)// 对key[i]进行输出
			cout<<"key["<<h<<"]="<<key[h]<<endl;
		for(j=i,sign=i; j<r; j++)
			if(fabs(*(Nrow[sign]+i))/key[sign]<fabs(*(Nrow[j]+i))/key[j])
				sign=j;
	    p=sign;
	    cout<<"p="<<p<<endl;
		if(p!=i)
	    {
			temp=Nrow[i];
			Nrow[i]=Nrow[p];
			Nrow[p]=temp;
		}
		
		for(h=0; h<r; h++)
		{
			for(l=0; l<c; l++)
				cout<<*(Nrow[h]+l)<<"   ";
			cout<<endl;
		}
		cout<<endl<<endl<<endl;
		for(j=i+1; j<r; j++)      
		{
			t=*(Nrow[i]+i);
			m=*(Nrow[j]+i)/t;
			for(k=i; k<c; k++)// 处理完一组元素
	        *(Nrow[j]+k)=*(Nrow[j]+k)-m*(*(Nrow[i]+k));
                	
	    for(h=0; h<r; h++)
			{
			    for(l=0; l<c; l++)
				   printf("%0.1f\t",*(Nrow[h]+l));
			       cout<<endl;
			}
			cout<<endl<<endl<<endl;  
		}
	}
	if(*(Nrow[r-1]+r-1)==0) 
	{
		cout<<"No Unique solution exists!"<<endl;
	    return;
	}
	k=c-1;
	t=*(Nrow[r-1]+c-2);
	x[k]=*(Nrow[r-1]+c-1)/t;
	for(i=k-1; i>=0; i--)
	{
        m=*(Nrow[i]+i);
		x[i]=(*(Nrow[i]+c-1)-g(Nrow,i,c,x))/m;
	}
	cout<<endl<<endl<<endl;
	cout<<"x的解为:"<<endl;
	for(i=0; i<c-1; i++)
		cout<<"x"<<i<<"="<<x[i]<<" ,  ";
	cout<<endl<<endl;
}

void choose(float s[][Num],int c, int r)
{
	int ch;
    do
	{
	cout<<endl;
	cout<<"选择你想使用的方法 :                             "<<endl;
	cout<<"1-------------6.1   Backward Substitution   -----------------"<<endl;
	cout<<"2-------------6.2   Partial Pivoting        -----------------"<<endl;
	cout<<"3-------------6.3   Scaled Partial Pivoting ------------------"<<endl;
	cout<<"0-------------       Exit                   -----------------"<<endl;
	cin>>ch;
	switch(ch)
	{
	case 1:
		{
			Gassian(s, c, r);
            nemu();		
            break;
		}
	case 2:
		{
			Pivoting(s, c, r);
	        nemu();	
			break;
		}
	case 3:
		{
		    Scaled_pivoting(s, c, r);// 列主元消元法
            nemu();
			break;
		}
	case 0:
		{
		
			break;
		}
	default:
		{
			cout<<"输入错误"<<endl;
		    nemu();
			return;
		}
	}
	}while(ch!=0);
}

	  
void nemu()
{
	int i,j,column=5,row=4,ch;
	float matrix5[Num][Num];
    float matrix[4][20]={{1,1,0,1,2},{2,1,-1,1,1},{-1,2,3,-1,4},{3,-1,-1,2,-3}};
	float matrix2[4][20]={{1,-0.5,1,0,4},{2,-1,-1,1,5},{1,1,0,0,2},{1,-0.5,1,1,5}};
	float matrix3[3][20]={{1,1,-1,1},{1,1,4,2},{2,-1,2,3}};
	float matrix4[3][20]={{2,-3,2,5},{-4,2,-6,14},{2,2,4,8}};
	cout<<endl;
	do
	{
	cout<<"f.  1, 1, 0, 1, 2 "<<"   d.    1,-0.5, 1, 0, 4"<<endl;
	cout<<"    2, 1,-1, 1, 1 "<<"         2,  -1,-1, 1, 5"<<endl;
    cout<<"   -1, 2, 3,-1, 4 "<<"         1,   1, 0, 0, 2"<<endl;
    cout<<"    3,-1,-1, 2,-3 "<<"         1, -0.5,1, 1, 5"<<endl;
    cout<<"    矩阵(A)       "<<"         矩阵(B)        "<<endl;
	cout<<endl<<endl;
	cout<<"b.  1, 1,-1, 1    "<<"    c.   2,-3, 2, 5    "<<endl;
	cout<<"    1, 1, 4, 2    "<<"        -4, 2,-6,14     "<<endl;
	cout<<"    2,-1, 2, 3    "<<"         2, 2, 4, 8    "<<endl;
	cout<<"    矩阵(C)       "<<"         矩阵(D)        "<<endl;
	cout<<"1.------------   矩阵(A)    ---------------"<<endl;
	cout<<"2.------------   矩阵(B)    ---------------"<<endl;
	cout<<"3.------------   矩阵(C)    ---------------"<<endl;
	cout<<"4.------------   矩阵(D)    ---------------"<<endl;
	cout<<"5.-------    你可以选择自己输入矩阵--------"<<endl;
	cout<<"0.------------   退出       ---------------"<<endl;     
	cout<<"      输入符合你函数的选项       "<<endl;
	cin>>ch;
	switch(ch)
	{
	case 1:
		{
			choose(matrix, column, row);
			break;
		}
	case 2:
		{
			choose(matrix2, column, row);
			break;
		}
	case 3:
		{
			choose(matrix3, column, row);
			break;
		}
	case 4:
		{
			choose(matrix4, column, row);
			break;
		}
	case 5:
		{
			
	        cout<<"创建一个矩阵: "<<endl;
	        cout<<"输入行row: "<<endl;
	        cin>>row;
	        cout<<"输入列column:"<<endl;
	        cin>>column;

	        for(i=0; i<row; i++)
		       for(j=0; j<column; j++)
			   {
			       cout<<"输入matrix5["<<i<<"]["<<j<<"] "<<endl;
			       cin>>matrix5[i][j];
			   }
           disp(matrix5,column,row);
	       cout<<endl<<endl<<endl;	
		   choose(matrix5, column, row);
		   break;
		}
    case 0: break;
	default:
		{
			cout<<"输入错误"<<endl;
			nemu();
			return;
		}
	}
	}while(ch!=0);
}
		




void main()
{
	nemu();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -