冰( •̀∀•́ )瞳
2021-10-14 22:06
采纳率: 0%
浏览 21

关于#输出#的问题,如何解决?

构造输出函数式是告诉我应输入“;”,可那是函数名,不可能啊,还有就是说Displayhe和Addmatrix以及main函数是非法定义

img

#include "addmatrix.h"
int main()
{
    TSMatrix A, B, C;
    ifstream filein("TSMatrix.txt");
    if (filein.fail())
    {
        cerr << "无法打开文件!" << endl;
        exit(OVERFLOW);
    }
    InitTSMatrix(A, filein);
    cout << "矩阵A为:" << endl;
    Display(A,A.mu,A.nu); 
    InitTSMatrix(B, filein);
    cout << "矩阵B为:" << endl;
    Display(B,B.mu,B.nu);
    Addmatrix(A, B, C);
    cout << "A+B为:" << endl;
    Display(C,C.mu,C.nu);
}
#include "matrix.h"
Status Addmatrix(TSMatrix A, TSMatrix B, TSMatrix& C)
{
    if (A.mu != B.mu || A.nu != B.nu)  return error;
    C.mu = A.mu; C.nu = A.nu;
    for (int k = 1; k <= C.mu; k++) C.rpos[k] = 0;
    int i = 1, j = 1, k = 1;
    while (i <= A.tu && j <= B.tu)
    {
        if ((A.data[i].x == B.data[j].x) && (A.data[i].y == B.data[j].y))
        {
          ElemType temp = A.data[i].e + B.data[j].e;
            if (temp)
            {
                C.data[k].x = A.data[i].x; C.data[k].y = A.data[i].y; C.data[k].e = temp; C.rpos[A.data[i].x]++; k++; i++; j++;
            }
            else
            {
                i++; j++;
            }
        }
        else if ((A.data[i].x < B.data[j].x) || (A.data[i].x == B.data[j].x && A.data[i].y < B.data[j].y))
        {
            C.data[k].x = A.data[i].x; C.data[k].y = A.data[i].y; C.data[k].e = A.data[i].e; C.rpos[A.data[i].x]++;
            i++; k++;
        }
        else
        {
            C.data[k].x = B.data[j].x; C.data[k].y = B.data[j].y; C.data[k].e = B.data[j].e; C.rpos[B.data[j].x]++;
            j++; k++;
        }
    }
    while (i <= A.tu)
    {
        C.data[k].x = A.data[i].x; C.data[k].y = A.data[i].y; C.data[k].e = A.data[i].e; C.rpos[A.data[i].x]++;
        i++; k++;
    }
    while (j <= B.tu)
    {
        C.data[k].x = B.data[j].x; C.data[k].y = B.data[j].y; C.data[k].e = B.data[j].e; C.rpos[B.data[i].x]++;
        j++; k++;
    }
    C.tu = k - 1;
    return ok;

}
#include <iostream>
#include <fstream>
#include <iomanip>
#define maxsize 12500
#define maxrc 1000
#define ok 1
#define error 0
#define size 1000
using namespace std;
typedef int Status;
typedef int ElemType;
typedef struct {
    int x, y;
    ElemType e;
}Triple;
typedef struct {
    Triple data[maxsize];
    int mu, nu, tu;
    int rpos[maxrc + 1]; 
}TSMatrix;
Status InitTSMatrix(TSMatrix& L, ifstream& filein)
{
    filein >> L.mu >> L.nu;
    L.tu = 0;
    ElemType temp;
    int q = 0;
    int num[size];
    for (int i = 1; i <= L.mu; i++)
    {
        for (int j = 1; j <= L.nu; j++)
        {
            q++;
            filein >> temp;
            if (temp) {
                q++;
                L.data[q].x = i;
                L.data[q].y = j;
                L.data[q].e = temp;
                L.tu++;
            }
        }
        for (int k = 1; k <= L.mu; k++)  num[k] = 0;
        for (int k = 1; k <= L.tu; k++) num[L.data[k].x]++;//每一行非零元个数
        L.rpos[1] = 1;
        for (int k = 2; k < L.mu; k++)
        {
            L.rpos[k] = L.rpos[k - 1] + num[k - 1];
        }
        return ok;
    }
Status Display(TSMatrix L,int r,int c)
{
    int q = 1;
    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= c; j++)
        {
            if (q <= L.tu && i == L.data[q].x && j == L.data[q].y)
            {
                cout <<setiosflags(ios::left)<< setw(3) << L.data[q].e;
                q++;
            }
            else
            {
                cout <<setiosflags(ios::left)<< setw(3) << '0';
            }
        }
        cout << endl;
    }
        return ok;
}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 你这里看不出问题呢,把代码贴出来看看。

    打赏 评论

相关推荐 更多相似问题