哪位高手帮我看看这个C++的程序问题出在哪里吧?小弟刚开始学习Vc++

2024年11月28日 14:50
有3个网友回答
网友(1):

你的代码有错,更改如下,
头文件bcmul.h
#include //矩阵的乘法
#include "math.h"
class cbcmul
{
public:
//t m,n,k;
void ccbcmul (double **ar,double **ai,double **br,double **bi, int m,int n,int k ,double **cr,double **ci)
{
int i,j,d;
double p,q,r,s;
p=0,q=0;
for (i=0; i<=m-1; i++)
{
for (j=0; j<=n-1; j++)
{
for (d=0; d<=k-1; d++)
{
p=ar[i][d]*br[d][j];
q=-1*ai[i][d]*bi[d][j];
r=ar[i][d]*bi[d][j];
s=ai[i][d]*br[d][j];

cr[i][j]=p+q;
ci[i][j]=r+s;
}
}
}
//return;
}
};
cpp文件
#include "bcmul.h" //复数的N次方根
using namespace std;

int main()
{
double cr[2][1],ci[2][1];
double ar[2][5]={
{1,3,2,2,3},
{0,4,5,5,1}
};
double ai[2][5]={
{1,0,2,2,3},
{0,4,-4,5,1}
};
double br[5][1]={
{5},{0},{0},{2},{1}
};
double bi[5][1]={
{5},{0},{0},{3},{-1}
};
cbcmul cal;
cal.ccbcmul (&(*ar),&(*ai),2,5,1,&(*cr),&(*ci)); //米这里的引用有错误
cout < system("pause");
return 0;
};

网友(2):

class cbcmul
{
public:
//t m,n,k;
void ccbcmul (double **ar,double **ai,double **br,double **bi, int m,int n,int k ,double **cr,double **ci)
{
int i,j,d;
double p,q,r,s;
p=0,q=0;

for (i=0; i<=m-1; i++)
{
for (j=0; j<=n-1; j++)
{
for (d=0; d<=k-1; d++)
{
p=ar[i][d]*br[d][j];
q=-1*ai[i][d]*bi[d][j];
r=ar[i][d]*bi[d][j];
s=ai[i][d]*br[d][j];

cr[i][j]=p+q;
ci[i][j]=r+s;
}
}
}
return;
}///少了
};

////////////////
///main 函数}后的";"去掉

///////////////
///cal.ccbcmul (&(*ar),&(*ai),2,5,1,&(*cr),&(*ci));
这样的调用可能会参数类型不一致的

网友(3):

下面的矩阵类实现了+,-,++,--运算,你可参考写出×的运算
#include
#include
#include

using namespace std;
class matrix
{
public:
matrix()
{
m=0;n=0;
pmatrix=NULL;
}
matrix(int im,int in)
{
m=im;n=in;
pmatrix=new int[m*n];
}
matrix(matrix& c)
{
m=c.m;n=c.n;
pmatrix=new int[m*n];
for (int i=0;i*(pmatrix+i)=*(c.pmatrix+i);
}
~matrix() { delete [] pmatrix; }
void input();
void showmatrix();
matrix& operator++();
matrix& operator++(int);
matrix& operator--();
matrix& operator--(int);
matrix& operator=(matrix c);
private:
int m,n,*pmatrix;
};
void matrix::input()
{
cout<<"请输入 "<for (int i=0;ifor (int j=0;jcin>>*(pmatrix+i*n+j);
}
void matrix::showmatrix()
{
cout<<"当前矩阵元素为:"<for (int i=0;i{
for (int j=0;jcout<cout<}
}
matrix& matrix::operator ++()
{
for (int i=0;ifor (int j=0;j++(*(pmatrix+i*n+j));
return *this;
}

matrix& matrix::operator ++(int)
{
matrix temp(*this);
for (int i=0;ifor (int j=0;j++(*(pmatrix+i*n+j));
return temp;
}

matrix& matrix::operator --()
{
for (int i=0;ifor (int j=0;j--(*(pmatrix+i*n+j));
return *this;
}

matrix& matrix::operator --(int)
{
matrix temp(*this);
for (int i=0;ifor (int j=0;j--(*(pmatrix+i*n+j));
return temp;
}

matrix& matrix::operator=(matrix c)
{
m=c.m;n=c.n;
delete [] pmatrix;
pmatrix=new int[m*n];
for(int i=0;i*(pmatrix+i)=*(c.pmatrix+i);
return *this;
}

int _tmain(int argc, _TCHAR* argv[])
{
int m,n;
cout<<"请输入矩阵的行和列数:"<cin>>m>>n;
matrix A(m,n);
matrix B;
A.input();
A.showmatrix();
cout<<"++A后,";
++A;
A.showmatrix();
cout<<"A++后,";
A++;
A.showmatrix();
cout<<"--A后,";
--A;
A.showmatrix();
cout<<"A--后,";
A--;
A.showmatrix();

B=A;
cout<<"B=A后,B";
B.showmatrix();
return 0;
}