首页 > C++ > (怀旧向)第一弹:矩阵程序

(怀旧向)第一弹:矩阵程序

2012年7月25日 发表评论 阅读评论

好吧,最近我闲的蛋疼,于是就搞个专题,怀旧向程序。哎呀,就是把以前那些写过的好玩的代码拿出来看看,然后。。。额。。没然后了。。就是拿出来看看。。。

矩阵程序是我大一学完C++的那个假期无聊写的一个代码,现在看起来只能说,我去,真是弱爆了,倒不是说那个编程的代码的优化啊什么的,就是看那个花括号的位置和for写在一行,变量命名完全不没规律,双目运算符前后没有空一格之类的,还有就是一开始学的时候是在VC6上写的,现在那东西都被我抛弃多少年了??放上来之前用Alt+F8优化了一下。。

不过嘛,我个人一直觉得这个程序写出来很有成就感,当年大一,接触到编程这个概念也才10个月,而且中国的大学教育下,咳咳,你懂得。然后我花了3天吧,扒拉扒拉的写出了这个程序,反正就是个大杂烩,对着线代的课本,把证明题以外的东西全部集成进去,矩阵的运算自然不说,然后还有解线性方程,求秩,化简,然后施密特正交变换什么的,最后最让人抓狂的就是算特征值和特征向量,当时完全不懂这个算法有多难,然后去图书馆找了半天书,想了个方法最后却被证明想错了,没办法之下只好用暴力枚举解决,额,算了,大一些的小东西嘛,怀念怀念就好。

#include <iomanip.h>
#include <conio.h>
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define cls system("cls")
#define pause system("pause")

int max(int m,int n)
{
    m=abs(m);
    n=abs(n);
    int a = (m > n) ? m : n;
    int b = (m > n) ? n : m;
    int c = 0;
    if(m!=0&&n!=0)
    {
        while ((a % b) != 0)
        {
            c = a % b;
            a = b;
            b = c;
        }
        return b;
    }
    else
        return 1;
}

class fraction
{
public:
    fraction(){}
    fraction(int u,int d=1):up(u),down(d){}
    fraction(fraction &x){setup(x.getup());setdown(x.getdown());}
    int getup();
    int getdown();
    void setup(int);
    void setdown(int);
    friend istream & operator >>(istream &,fraction&);
    friend ostream & operator <<(ostream &,fraction &);
    friend bool operator >(fraction,fraction);
    friend bool operator >=(fraction,fraction);
    friend bool operator <(fraction,fraction);
    friend bool operator <=(fraction,fraction);
    friend fraction operator +(fraction,fraction);
    friend fraction operator -(fraction,fraction);
    friend fraction operator *(fraction,fraction);
    friend fraction operator /(fraction,fraction);
    void simplify();
    operator float();
    //operator int();
private:
    int up;
    int down;
};

fraction abs(fraction k)
{
    fraction temp(k);
    if (temp.getup()<0) temp.setup(temp.getup()*-1);
    if (temp.getdown()<0) temp.setdown(temp.getdown()*-1);
    return temp;
}

istream &operator >>(istream & input,fraction& x)
{
    float n;
    cin>>n;
    int m=1,z=int(n);
    while((n-z)!=0)
    {
        m*=10;
        n*=10;
        z=int(n);
    }
    x.setup(z);
    x.setdown(m);
    x.simplify();
    return input;
}

void fraction::simplify()
{
    int mod =max(up,down);
    up=up/mod;
    down=down/mod;
    if(down<0)
    {
        up*=-1;
        down*=-1;
    }
}

ostream & operator <<(ostream &output,fraction &x)
{
    if(x.getdown()==1)
        output<<x.getup();
    else if(x.getup()==0)
        cout<<0;
    else
        output<<x.getup()<<"/"<<x.getdown();
    return output;
}

fraction operator +(fraction x,fraction y)
{
    fraction temp;
    temp.setdown(x.getdown()*y.getdown());
    temp.setup(x.getup()*y.getdown()+x.getdown()*y.getup());
    temp.simplify();
    return temp;
}

fraction operator -(fraction x,fraction y)
{
    fraction temp;
    temp.setdown(x.getdown()*y.getdown());
    temp.setup(x.getup()*y.getdown()-x.getdown()*y.getup());
    temp.simplify();
    return temp;
}

fraction operator *(fraction x,fraction y)
{
    fraction temp;
    temp.setdown(x.getdown()*y.getdown());
    temp.setup(x.getup()*y.getup());
    temp.simplify();
    return temp;
}

fraction operator /(fraction x,fraction y)
{
    fraction temp;
    temp.setdown(x.getdown()*y.getup());
    temp.setup(x.getup()*y.getdown());
    temp.simplify();
    return temp;
}

bool operator >(fraction x,fraction y)
{
    fraction temp=x-y;
    if(temp.getdown()*temp.getup()>0)
        return true;
    else return false;
}

bool operator >=(fraction x,fraction y)
{
    fraction temp=x-y;
    if(temp.getdown()*temp.getup()>=0)
        return true;
    else return false;
}

bool operator <(fraction x,fraction y)
{
    fraction temp=x-y;
    if(temp.getdown()*temp.getup()<0)
        return true;
    else return false;
}

bool operator <=(fraction x,fraction y)
{
    fraction temp=x-y;
    if(temp.getdown()*temp.getup()<=0)
        return true;
    else return false;
}

fraction::operator float()
{
    return float(up)/float(down);
}

int fraction::getdown()
{
    return down;
}

int fraction::getup()
{
    return up;
}

void fraction::setup(int u)
{
    up=u;
}

void fraction::setdown(int d)
{
    down=d;
}

class matrix
{
public:
    matrix();
    matrix(int,int);
    matrix(matrix &);
    fraction get(int m,int n);
    void set(int m,int n,fraction newp);
    int getl();
    int geth();
    friend istream &operator >>(istream &,matrix&);
    friend ostream & operator <<(ostream &,matrix &);
    friend matrix operator ~(matrix);
    friend matrix operator +(matrix,matrix);
    friend matrix operator -(matrix,matrix);
    friend matrix operator *(matrix,matrix);
    friend matrix operator *(matrix,fraction);
    friend matrix operator ^(matrix,int);
    matrix operator =(matrix);
    friend fraction abs(matrix);
    matrix yzs(int m,int n);
    matrix unit();
    void sway(int,int);
    bool rankc1(int);
    bool rankc2(int);
    int rank();
    bool find(fraction);
    matrix eig();

private:
    fraction **m;
    int length;
    int heigth;
};

bool matrix::find(fraction n)
{
    for(int i=0;i<getl();i++)
        if(n==get(1,i+1)) return true;
        return false;
}

matrix matrix::operator =(matrix x)
{
    for(int i=0;i<x.geth();i++)
        for(int j=0;j<getl();j++)
        {
            set(i+1,j+1,x.get(i+1,j+1));
        }
        return *this;
}

void matrix::sway(int m,int n)
{
    fraction temp;
    for(int i=0;i<getl();i++)
    {
        temp=get(m+1,i+1);
        set(m+1,i+1,get(n+1,i+1));
        set(n+1,i+1,temp);
    }
}

ostream & operator <<(ostream &output, matrix &x)
{
    int heigth=x.geth();
    int length=x.getl();
    if(heigth!=1)
    {
        for(int k=1;k<=heigth;k++)
            for(int l=1;l<=length;l++)
            {
                if(k==1&&l==1) cout<<"┏";
                else if(k==heigth&&l==1) cout<<"┗";
                else if(l==1) cout<<"∣";
                output<<setw(6)<<x.get(k,l)<<" ";
                if(k==1&&l==length) cout<<setw(6)<<"┓"<<endl;
                else if(k==heigth&&l==length) cout<<setw(6)<<"┛"<<endl;
                else if(l==length) cout<<setw(6)<<"∣"<<endl;

            }
    }
    else
    {
        output<<"┏";
        for(int i=0;i<x.getl();i++)
            output<<setw(6)<<x.get(1,i+1)<<" ";
        cout<<"┓"<<endl;
        cout<<"┗";
        cout<<setw(i*7+2)<<"┛"<<endl;
    }
    return output;
}

istream &operator >>(istream &input,matrix &x)
{
    cls;
    cout<<x<<"请继续输入:";
    for (int i=0;i<x.geth();i++)
    {
        for (int k=0;k<x.getl();k++)
        {
            input>>x.m[i][k];
            cls;
            cout<<x<<"请继续输入:";

        }
    }
    cout<<endl;
    cls;
    cout<<x;
    return input;
}

matrix operator ~(matrix c)
{
    matrix temp(c.getl(),c.geth());
    for (int i=0;i<temp.geth();i++)
        for (int k=0;k<temp.getl();k++)
            temp.m[i][k]=c.m[k][i];
        return temp;
}

matrix operator +(matrix x,matrix y)
{
    matrix temp(x.geth(),x.getl());
    for(int i=0;i<temp.geth();i++)
        for(int j=0;j<temp.getl();j++)
            temp.m[i][j]=x.m[i][j]+y.m[i][j];
        return temp;
}

matrix operator -(matrix x,matrix y)
{
    matrix temp(x.geth(),x.getl());
    for(int i=0;i<temp.geth();i++)
        for(int j=0;j<temp.getl();j++)
            temp.m[i][j]=x.m[i][j]-y.m[i][j];
        return temp;
}

matrix operator *(matrix x,matrix y)
{
    matrix result(x.geth(),y.getl());
    for(int i=0;i<x.geth();i++)
        for(int j=0;j<y.getl();j++)
        {
            fraction sum=0;
            for(int k=0;k<x.getl();k++)
                sum=sum+x.get(i+1,k+1)*y.get(k+1,j+1);
            result.set(i+1,j+1,sum);
        }
        return result;
}

matrix operator *(matrix x,fraction n)
{
    matrix temp(x);
    for(int i=0;i<temp.geth();i++)
        for(int j=0;j<temp.getl();j++)
            temp.set(i+1,j+1,temp.get(i+1,j+1)*n);
        return temp;
}

matrix operator ^(matrix x,int n)
{
    matrix temp(x.geth(),x.getl());
    for(int i=0;i<x.geth();i++)
        temp.set(i+1,i+1,1);
    for(i=0;i<n;i++)
        temp=temp*x;
    return temp;
}

int matrix::geth()
{
    return heigth;
}

int matrix::getl()
{
    return length;
}

fraction matrix::get(int k,int n)
{
    return m[k-1][n-1];
}

void matrix::set(int k,int n,fraction newp)
{
    m[k-1][n-1]=newp;
}

matrix::matrix(int hei,int len)
{
    length=len;
    heigth=hei;
    m=new fraction*[hei];
    for(int l=0;l<hei;l++)
    {
        m[l]=new fraction[len];
        for(int k=0;k<len;k++)
        {
            m[l][k]=0;
        }
    }
}

matrix::matrix(matrix &c)
{
    length=c.getl();
    heigth=c.geth();
    m=new fraction*[heigth];
    for(int l=0;l<heigth;l++)
    {
        m[l]=new fraction[length];
        for(int k=0;k<length;k++)
        {
            m[l][k]=c.m[l][k];
        }
    }
}

matrix matrix::yzs(int m,int n)
{
    matrix temp(heigth-1,length-1);
    for(int h=0,h2=0;h<heigth;h++)
    {
        if(h==m) continue;
        for(int l=0,l2=0;l<length;l++)
        {
            if(l==n) continue;
            temp.set(h2+1,l2+1,get(h+1,l+1));
            l2++;
        }
        h2++;
    }
    return temp;
}

matrix matrix::unit()
{
    matrix temp(*this);
    for(int i=0,j=0;i<geth()&&j<getl();i++,j++)
    {
loop:
        if(get(i+1,j+1)==0)
        {
            for(int l=i+1;l<heigth;l++)
            {
                if(get(l+1,j+1)!=0)
                {
                    sway(i,l);
                    break;
                }
                if(l==(length-1))
                {
                    j++;
                    goto loop;
                }
            }
        }
        for(int k=i+1;k<geth();k++)
        {
            fraction m=get(k+1,j+1)/get(i+1,j+1);
            for(int l=j;l<getl();l++)
            {
                set(k+1,l+1,get(k+1,l+1)-m*get(i+1,l+1));
            }
        }
    }

    for( i=0;i<geth();i++)
    {
        int j=0;
        if(get(i+1,j+1)==0)
        {
            for(int l=j;l<getl();l++)
            {
                if(get(i+1,l+1)!=0)
                {
                    j=l;

                    break;
                }
                if(l==(getl()-1))
                    return *this;
            }
        }
        fraction temp=get(i+1,j+1);
        for(int k=j;k<getl();k++)
            set(i+1,k+1,get(i+1,k+1)/temp);
        for(k=i-1;k>=0;k--)
        {
            fraction m=get(k+1,j+1)/get(i+1,j+1);
            for(int l=j;l<getl();l++)
                set(k+1,l+1,get(k+1,l+1)-m*get(i+1,l+1));
        }
    }
    return *this;
}

bool matrix::rankc1(int n)//lie
{
    for(int i=0;i<geth();i++)
    {
        if(get(i+1,n+1)!=0) return true;
    }
    return false;
}

bool matrix::rankc2(int n)//hang
{
    for(int i=0;i<getl();i++)
    {
        if(get(n+1,i+1)!=0) return true;
    }
    return false;
}

int matrix::rank()
{
    int i,j,k=0,l=0;
    matrix temp(unit());
    for( i=0;i<geth();i++)
        if(rankc2(i)) k++;
        for( j=0;j<getl();j++)
            if(rankc1(j)) l++;
            return k<l?(k):(l);
}

matrix matrix::eig()
{
    matrix temp(*this),temp2(*this),result(1,getl());
    for(int l=1;l<=result.getl();l++)
        result.set(1,l,100);
    fraction min=1;
    int mark=1;
    cout<<"0%                                              100%"<<endl;
    putch('|');
    fraction jindu(1,1);
    for(fraction i=0;i<=fraction(50,1);i=i+fraction(1,1000))
    {
pp:
        if(jindu-i==0)
        {
            putch('|');
            jindu=jindu+fraction(1,1);
        }
        matrix *p=new matrix(temp2);
        for(int j=0;j<geth();j++)
        {
            p->set(j+1,j+1,p->get(j+1,j+1)-i);
        }
        if(abs(*p)==0)
        {
            result.set(1,mark++,i);
            if(mark==result.getl()+1)
            {
                for(;jindu<fraction(51);jindu=jindu+fraction(1,1))
                    putch('|');
                return result;
            }
        }
        delete p;
        if(i>fraction(0))
        {
            i=i*fraction(-1);
            goto pp;
        }
        if(i<fraction(0))
            i=i*fraction(-1);
    }
    return result;
}

fraction dot(matrix a,matrix b)
{
    fraction sum=0;
    for(int i=0;i<a.getl();i++)
        sum=sum+a.get(1,i+1)*b.get(1,i+1);
    return sum;
}

void antim()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->矩阵求逆\n" <<endl;
    int heigth;
    cout<<"输入行列数:";
    cin>>heigth;
    matrix x(heigth,heigth);
    cin>>x;
    fraction mod=abs(x);
    if(mod==0)
    {
        cout<<"矩阵"<<x<<endl<<"不可求逆!!"<<endl;
        return;
    }
    if(heigth==1) {
        cout<<"逆矩阵为:\n"<<fraction(1)/mod<<endl;
        return;}
    matrix temp(heigth,heigth);
    for(int m=0;m<heigth;m++)
        for(int n=0;n<heigth;n++)
        {
            temp.set(n+1,m+1,abs(x.yzs(m,n))*fraction(pow(-1,m+n))/mod);
        }
        cout<<"逆矩阵为:\n"<<temp<<endl;
}
void convert()
{
    cls;
    cout << "            矩阵计算器 -> 一个矩阵的运算->计算矩阵倒置\n" << endl;
    int length,heigth;
    cout<<"输入行数:";
    cin>>heigth;
    cout<<"输入列数:";
    cin>>length;
    matrix m(heigth,length);
    cin>>m;
    matrix n(~m);
    cls;
    cout<<m<<"倒置后为:\n";
    cout<<n<<endl;
}

fraction abs(matrix x)
{
    if(x.geth()==1)
    {
        return x.get(1,1);
    }

    int n=1;
    fraction sum=0;
    for(int i=0;i<x.getl();i++)
    {
        sum=sum+x.get(1,i+1)*abs(x.yzs(0,i))*fraction(n);
        n*=-1;
    }
    return sum;
}

void add()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->矩阵相加\n" << endl;
    int length,heigth;
    cout<<"输入行数:";
    cin>>heigth;
    cout<<"输入列数:";
    cin>>length;
    matrix m(heigth,length),n(heigth,length);
    cin>>m>>n;
    matrix result(m+n);
    cout<<m<<endl<<"与"<<endl<<endl<<n<<endl<<"相加结果为:\n\n"<<result<<endl;
}

void sub()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->矩阵相减\n" << endl;
    int length,heigth;
    cout<<"输入行数:";
    cin>>heigth;
    cout<<"输入列数:";
    cin>>length;
    matrix m(heigth,length),n(heigth,length);
    cin>>m>>n;
    matrix result(m-n);
    cout<<m<<endl<<"与"<<endl<<endl<<n<<endl<<"相加结果为:\n\n"<<result<<endl;
}

void mul()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->矩阵相乘\n" << endl;
    int length1,heigth1,length2,heigth2;
    cout<<"输入第一个矩阵行数:";
    cin>>heigth1;
    cout<<"输入第一个矩阵列数:";
    cin>>length1;
    cout<<"输入第二个矩阵行数:";
    cin>>heigth2;
    cout<<"输入第二个矩阵列数:";
    cin>>length2;
    if(length1!=heigth2)
    {
        cout<<"不符合矩阵相乘规则,不能想乘!!!"<<endl;
        return;
    }
    else
    {
        matrix x(heigth1,length1),y(heigth2,length2);
        cin>>x>>y;
        cout<<endl;
        cls;
        cout<<x<<"\n与\n\n"<<y<<"相乘结果为:"<<endl;
        cout<<x*y<<endl;
    }
}

void pow()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->矩阵求n次幂\n" << endl;
    int length;
    cout<<"输入方阵行列数:";
    cin>>length;
    int n;
    cout<<"输入幂:";
    cin>>n;
    matrix x(length,length);
    cin>>x;
    cout<<x<<"\n的"<<n<<"次幂为:\n\n"<<(x^(n))<<endl;
}

void simplify()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->矩阵化简\n" << endl;
    int length,heigth;
    cout<<"输入行数:";
    cin>>heigth;
    cout<<"输入列数:";
    cin>>length;
    matrix m(heigth,length);
    cin>>m;
    cout<<"\n\n的最简阶梯型为:\n\n"<<m.unit()<<endl;
}

void rank()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->矩阵求秩\n" << endl;
    int length,heigth;
    cout<<"输入行数:";
    cin>>heigth;
    cout<<"输入列数:";
    cin>>length;
    matrix m(heigth,length);
    cin>>m;
    cout<<"\n\n\nRank(A)="<<m.rank()<<endl;
}

void function()
{
    cls;
    fraction temp;
    cout << "            矩阵计算器 -> 两个矩阵的运算->解线性方程\n" << endl;
    int xnum,funnum;
    cout<<"输入方程个数:";
    cin>>funnum;
    cout<<"输入未知数个数:";
    cin>>xnum;
    cout<<endl<<endl<<endl;
    matrix liner(funnum,xnum),eliner(funnum,xnum+1);
    for(int i=1;i<=funnum;i++)
    {
        for(int j=1;j<=xnum;j++)
        {
            cout<<"a"<<i<<j<<"=";
            cin>>temp;
            liner.set(i,j,temp);
            eliner.set(i,j,temp);
            /*if(j==xnum)
            {
            cout<<"y"<<i<<"=";
            cin>>temp;
            eliner.set(i,xnum,temp);
        }*/
        }
        cout<<"y"<<i<<"=";
        cin>>temp;
        eliner.set(i,xnum+1,temp);
    }
    //cout<<liner<<eliner<<endl;
    //cout<<liner<<eliner;
    cout<<"\n\n方程:\n";
    for(i=0;i<liner.geth();i++)
    {
        for(int j=0;j<liner.getl();j++)
        {
            if(liner.get(i+1,j+1)!=0)
            { cout<<liner.get(i+1,j+1)<<" * X"<<j+1<<" ";
            if(j==liner.getl()-1) cout<<" = ";
            else cout<<" + ";}
        }
        cout<<eliner.get(i+1,eliner.getl())<<endl;
        //cout<<endl;
    }
    if(eliner.unit().rank()>liner.unit().rank())
    {cout<<"方程无解!!"<<endl;
    return;
    }
    cout<<"\n\n最简形式矩阵为:\n";
    cout<<eliner;
    cout<<"\n\n解为:\n";
    matrix result(1,eliner.rank());
    for(i=0;i<result.getl();i++)
    {
        for(int j=0;j<eliner.getl()-1;j++)
        {
            if(eliner.get(i+1,j+1)==0)
                continue;
            else
            {
                result.set(1,i+1,j);
                break;
            }
        }
    }
    //cout<<result;
    for(i=0;i<result.getl();i++)
    {
        cout<<"x"<<result.get(1,i+1)+fraction(1)<<" = ";
        for(int j=0;j<eliner.getl()-1;j++)
        {
            if(!result.find(j))
            {
                if(eliner.get(i+1,j+1)!=0)
                {
                    cout<<fraction(-1)*eliner.get(i+1,j+1)<<" * X"<<j+1;
                    cout<<" + ";
                }
            }
        }
        cout<<eliner.get(i+1,eliner.getl())<<endl;
    }
}

int min(int m,int n)
{
    return m*n/max(m,n);
}

void smitchange(matrix x)
{
    //matrix result(x.geth(),x.getl());
    fraction *k=new fraction[x.geth()];
    matrix **beta=new matrix*[x.geth()];
    matrix **afa=new matrix*[x.geth()];
    for(int i=0;i<x.geth();i++)
    {
        beta[i]=new matrix(1,x.getl());
        afa[i]=new matrix(1,x.getl());
    }
    for(int j=0;j<x.geth();j++)
        for(i=0;i<x.getl();i++)
            afa[j]->set(1,i+1,x.get(j+1,i+1));

        *beta[0]=*afa[0];
        for(i=1;i<x.geth();i++)
        {
            *beta[i]=*afa[i];
            for(int j=0;j<i;j++)
            {
                k[j]=fraction(-1)*dot(*afa[i],*beta[j])/dot(*beta[j],*beta[j]);
                *beta[i]=*beta[i]+(*afa[j])*k[j];
            }
        }
        for( j=0;j<x.geth();j++)
        {
            int mul=(*beta[j]).get(1,1).getdown();
            for(int i=1;i<x.getl();i++)
            {
                mul=min(mul,(*beta[j]).get(1,i+1).getdown());
            }
            *beta[j]=*beta[j]*fraction(mul,1);
        }
        for( j=0;j<x.geth();j++)
        {
            int mul=(*beta[j]).get(1,1).getup();
            for(int i=1;i<x.getl();i++)
            {
                if((*beta[j]).get(1,i+1).getup()!=0)
                    mul=max(mul,(*beta[j]).get(1,i+1).getup());
            }
            *beta[j]=*beta[j]*fraction(1,mul);
        }
        //cout<<*afa[0]<<*afa[1]<<*afa[2];
        //cout<<*beta[0]<<*beta[1]<<*beta[2];
        cout<<"施密特正交变换后为:"<<endl;
        for(i=0;i<x.geth();i++)
        {
            cout<<"β"<<i+1<<":"<<endl;
            fraction mod=dot(*beta[i],*beta[i]);
            //cout<<"√("<<mod<<")*";
            cout<<*beta[i];
            for(int j=0;j<x.getl()*7+3;j++) cout<<"-";
            cout<<endl;
            cout<<"√("<<mod<<")"<<endl<<endl<<endl;
        }
}

void eig()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->计算特征根,特征向量,施密特正交变换\n" << endl;
    cout<<"鉴于本算法并不完善,如果某一特征根过大,将会出错。若特征根为精度过大或为无理数,也将无法算出.\n敬请原谅!!\n\n\n\n\n"<<endl;
    int n;
    cout<<"输入方阵阶数:";
    cin>>n;
    matrix m(n,n),smit(n,n);
    int smitt=0;
    cin>>m;
    cout<<"请稍等,计算中。。。。。。"<<endl;
    matrix eig(m.eig());
    cout<<endl;
    cls;
    /*cout<<m<<"特征根为:"<<endl;
    for(int i=0;i<eig.getl();i++)
    {
    if(eig.get(1,i+1)!=100)
    {
    cout<<eig.get(1,i+1)<<"    ";
    }
}*/
    cout<<m<<endl;
    int eignum=1;
    for(int i=0;i<n;i++)
    {
        if(eig.get(1,i+1)!=100)
        {
            matrix *p=new matrix(m);
            int ff=0;
            for(int j=0;j<m.geth();j++)
            {
                p->set(j+1,j+1,p->get(j+1,j+1)-eig.get(1,i+1));
            }
            for( j=0;j<m.geth();j++)
                for(int l=0;l<m.geth();l++)
                    p->set(j+1,l+1,p->get(j+1,l+1)*fraction(-1,1));
                p->unit();
                int prank=p->rank(),re=prank;
                re++;
                cout<<endl;
                for(int jj=0;jj<n-prank;jj++)
                {
                    cout<<"λ";
                    cout<<eignum++;
                    cout<<"=";

                }
                cout<<eig.get(1,i+1)<<"\n化简后为:"<<endl<<*p<<endl;//namuda
                matrix record(1,n);
                for(j=0;j<n;j++)
                    record.set(1,j+1,n);
                for(j=0;j<n;j++)
                    for(int l=0;l<n;l++)
                    {
                        if(p->get(j+1,l+1)==1)
                        {
                            record.set(1,j+1,l);
                            break;
                        }
                    }
                    //cout<<"\nrecord:"<<record<<endl;

                    cout<<"特征向量为:"<<endl;
                    for(j=0;j<n;j++)
                    {

                        if(!record.find(j))
                        {//smit++
                            int cc=0;
                            for(int l=0;l<prank;l++)
                            {

                                while(!record.find(cc)) cc++;
                                smit.set(smitt+1,cc+1,p->get(l+1,j+1)*fraction(-1));
                                cc++;
                            }
                            //cout<<smit<<endl;
                            while(record.find(ff)) ff++;
                            smit.set(smitt+1,ff+1,1);
                            ff++;
                            matrix tmp(1,n);
                            for(int kk=0;kk<n;kk++)
                                tmp.set(1,kk+1,smit.get(smitt+1,kk+1));
                            cout<<tmp<<endl;
                            smitt++;
                        }
                    }
                    //cout<<endl<<endl<<endl<<record<<endl;
                    //cout<<endl<<smit<<endl;
                    //cout<<eig.get(1,i+1)<<"   ";
        }
    }
    cout<<"要进行施密特正交变换吗?(Y/N)"<<endl;
    char choose;
    cin>>choose;
    if(choose=='Y'||choose=='y')
    {
        cls;
        cout<<m;
        cout<<endl;
        smitchange(smit);
    }
    cout<<endl;
}

void smitt()
{
    cls;
    cout << "            矩阵计算器 -> 两个矩阵的运算->施密特正交变换\n" << endl;
    cout<<"输入向量个数:";
    int m;
    cin>>m;
    cout<<"输入维数:";
    int n;
    cin>>n;
    matrix k(m,n);
    matrix **afa=new matrix*[m];
    for(int i=0;i<m;i++)
    {
        afa[i]=new matrix(1,n);
        //cout<<"α"<<i+1<<":"<<endl;
        cin>>*afa[i];
    }
    for( i=0;i<m;i++)
        for(int j=0;j<n;j++)
            k.set(i+1,j+1,(*afa[i]).get(1,j+1));
        cout<<endl;
        cls;
        for(i=0;i<m;i++)
            cout<<"α"<<i+1<<":\n"<<*afa[i]<<endl;
        //cout<<k;
        smitchange(k);
}
void show1();
void show2();
void show3();
void show4();

int main()
{
    char choose;
    while(1)
    {
loop1:show1();
      choose=getche();
      switch(choose)
      {
      case '1':cls;
loop2:show2();
      choose=getche();
      switch(choose)
      {
      case '1':convert();
          pause;
          cls;
          goto loop2;
      case '2':
          antim();
          pause;
          cls;
          goto loop2;
      case '3':cls;
          cout << "            矩阵计算器 -> 一个矩阵的运算->计算行列式的值\n" << endl;
          int heigth;
          cout<<"输入行列数:";
          cin>>heigth;
          matrix *p;
          p=new matrix(heigth,heigth);
          cin>>*p;
          cout<<"det(A)="<<abs(*p)<<endl;
          delete p;
          pause;
          cls;
          goto loop2;
      case '4':pow();
          pause;
          cls;
          goto loop2;
      case '5':function();
          pause;
          cls;
          goto loop2;
      case '6':
          rank();
          pause;
          cls;
          goto loop2;
      case '7':eig();
          pause;
          cls;
          goto loop2;
      case '8':
          simplify();
          pause;
          cls;
          goto loop2;
      case '9':
          smitt();
          pause;
          cls;
          goto loop2;
      case '0':cls;
          goto loop1;
      default:cout<<"输入错误~~~"<<endl;
          pause;
          cls;
          goto loop2;
      }
      case '2':cls;
loop3:show3();
      choose=getche();
      switch(choose)
      {case '1':add();
      pause;
      cls;
      goto loop3;
      case '2':sub();
          pause;
          cls;
          goto loop3;
      case '3':
          mul();
          pause;
          cls;
          goto loop3;
      case '4':cls;
          goto loop1;
      default:cout<<"输入错误~~~"<<endl;
          pause;
          cls;
          goto loop3;
      }
      case '3':return 0;
      default:cout<<"输入错误~~~"<<endl;
          pause;
          cls;
          goto loop1;
      }
    }
    // matrix a(3,3);
    // cin>>a;
    // cout<<a.eig();

}
void show1()
{
    cout << "*********************************矩阵计算器*******************************\n" << endl;
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n";
    cout <<"                              1:(一个矩阵)\n";
    cout <<"                              2:(两个矩阵)\n";
    cout <<"                              3:(退出)\n  " << endl;
    cout << "\n\n\n\n\n\n\n                               输入命令: "<<endl;
}
void show2()
{
    cout <<"**************************矩阵计算器 -> 一个矩阵的运算**************************\n" << endl;
    cout <<"\n\n\n\n\n\n";
    cout <<"                              1:(转置)\n";
    cout <<"                              2:(逆矩阵)\n";
    cout <<"                              3:(求行列式的值)\n";
    cout <<"                              4:(求n次幂)\n";
    cout <<"                              5:(解线性方程)\n";
    cout <<"                              6:(求秩)\n";
    cout <<"                              7:(求特征根,特征向量,施密特正交变换)\n";
    cout <<"                              8:(化简矩阵)\n";
    cout <<"                              9:(施密特正交变换)\n";
    cout <<"                              0:(返回)\n\n" << endl;
    cout <<"                              输入命令: "<<endl;
}/*坐标变换,对角化*/
void show3()
{
    cout << "**************************矩阵计算器 -> 两个矩阵的运算**************************" << endl;
    cout << "\n\n\n\n\n\n                              1:(相加)\n";
    cout << "                              2:(相减)\n";
    cout << "                              3:(相乘)\n";
    cout << "                              4:(返回)\n\n" << endl;
    cout << "                              输入命令: "<<endl;
}

【完】

本文内容遵从CC版权协议,转载请注明出自http://www.kylen314.com

分类: C++ 标签: ,
  1. 本文目前尚无任何评论.
验证码:9 + 5 = ?

友情提示:留言可以使用大部分html标签和属性;

添加代码示例:[code lang="cpp"]your code...[/code]

添加公式请用Latex代码,前后分别添加两个$$