数据结构作业——三元矩阵相加

数据结构作业——三元矩阵相加

写得一般,恳请大家批评指正

// 数据结构作业 三元组矩阵相加C=A+B;
// 由于时间所限 这里仅考虑算法内核 不考虑软件容错及外包界面的工作
#include < algorithm >
#include
< cmath >
#include
< cstdio >
#include
< iostream >
#include
< cstring >
using   namespace  std;
#define   MAXSIZE 10000

struct  triple
{

    
int x,y;
    
int val;
}
;

struct  tripletable
{
    
    triple data[MAXSIZE];
    
int len,wide,num;
}
;

tripletable a,b;

int  result[MAXSIZE][MAXSIZE];


int  main ()
{
    
int i,j;
    
int p;

    cout
<<"                        数据结构作业之 三元组矩阵相加C=A+B ";
    cout
<<"                                                                 ——by abilitytao (指导老师:Mr Zhang Hong)"<<endl<<endl;
    cout
<<"请输入第一个矩阵的长:";
    cin
>>a.len;
    cout
<<"请输入第一个矩阵的宽:";
    cin
>>a.wide;
    cout
<<"请输入矩阵a:"<<endl;
    p
=1;
    a.num
=0;
    
for(i=1;i<=a.wide;i++)
    
{
        
        
for(j=1;j<=a.len;j++)
        
{
            
            
int temp;
            cin
>>temp;
            
if(temp!=0)
            
{
                a.data[p].x
=i;
                a.data[p].y
=j;
                a.data[p].val
=temp;
                p
++;
                a.num
++;
            }

            
        }

    }

    cout
<<endl<<endl<<"ATTENTION:由于本程序不考虑容错,请确保两个矩阵的长宽相等"<<endl<<endl<<endl;
    cout
<<"请输入第二个矩阵的长:";
    cin
>>b.len;
    cout
<<"请输入第二个矩阵的宽:";
    cin
>>b.wide;
    cout
<<"请输入矩阵b:"<<endl;
    p
=1;
    b.num
=0;
    
for(i=1;i<=b.wide;i++)
    
{
        
        
for(j=1;j<=b.len;j++)
        
{
            
            
int temp;
            cin
>>temp;
            
if(temp!=0)
            
{
                b.data[p].x
=i;
                b.data[p].y
=j;
                b.data[p].val
=temp;
                p
++;
                b.num
++;
            }

            
        }

    }

    
for(i=1;i<=a.num;i++)
    
{
        
        result[a.data[i].x][a.data[i].y]
+=a.data[i].val;
    }

    
for(i=1;i<=b.num;i++)
    
{
        result[b.data[i].x][b.data[i].y]
+=b.data[i].val;
    }

    cout
<<"矩阵相加的结果是:"<<endl;
    
for(i=1;i<=a.wide;i++)
    
{
        
        
for(j=1;j<=a.len;j++)
        
{
            
            cout
<<result[i][j]<<' ';
            
        }

        cout
<<endl;
        
    }

    
    system(
"pause");
    
return 0;
    
}






你可能感兴趣的:(数据结构作业——三元矩阵相加)