C++ 指针new的问题,是怎么回事,求解

2025-01-05 19:38:24
推荐回答(2个)
回答1:

#include
using namespace std;

int main()
{
char *M=new char[];
char *G=new char[];

cout<<"ÇëÊäÈëM:";
cin>>M;
cout<<"ÇëÊäÈëG:";
cin>>G;

//CRC(M,G);
int ml,gl;
int len=10;

int m;
int g;
int yushu;//ÓàÊý

ml=strlen(M);//Ëù·¢×Ö·û´®µÄ³¤¶È
gl=strlen(G);//УÑéÂëµÄ³¤¶È

char *s=new char[len];
char *ys=new char[gl-1];
int *nm=new int[ml];
int *ng=new int[gl];

for(int j=0;j {
if(M[j]=='0')
{
nm[j]=0;
}
if(M[j]=='1')
{
nm[j]=1;
}
}

for(int i=0;i {
if(M[i]=='0')
{
ng[i]=0;
}
if(M[i]=='1')
{
ng[i]=1;
}
}

m=nm[0];
for(j=1;j {
m=2*(m+nm[j])+nm[j+1];
}
cout<<"m:"<
g=ng[0];
for(i=0;i {
g=2*(g+ng[i])+ng[i+1];
}

yushu=m%g;

//ÓàÊý×Ö·û´®
int ys1=yushu;
for(m=gl-1;m>=0;m--)
{
ys[m]=ys1%2;
ys1=ys1/2;
}

//ºÏ²¢µÃµ½·¢Ë͵Ä×Ö·û´®

cout<<"·¢ËÍ×Ö·ûΪ£º"< for ( i=0;i {
cout< }

delete [] s;
delete [] ys;
delete [] nm;
delete [] ng;

delete [] M;
delete [] G;
return 0;

}
主要是i m 等变量重定义 其他没有语法错误 算法没细看

回答2:

#include
using namespace std;

int main()
{
char *M=new char[];
char *G=new char[];

cout<<"请输入M:";
cin>>M;
cout<<"请输入G:";
cin>>G;

//CRC(M,G);
int ml,gl;
int len;

int m;
int g;
int yushu;//余数

ml=strlen(M);//所发字符串的长度
gl=strlen(G);//校验码的长度

char *s=new char[len];////////////////////////////这个len是不是也输入下,还是从哪来,没初始化
char *ys=new char[gl-1];
int *nm=new int[ml];
int *ng=new int[gl];

int i ,j;/////提到前面就可以了,不用每个循环都定义,后面那个m的也一样

for( j=0;j {
if(M[j]=='0')
{
nm[j]=0;
}
if(M[j]=='1')
{
nm[j]=1;
}
}

for( i=0;i {
if(M[i]=='0')
{
ng[i]=0;
}
if(M[i]=='1')
{
ng[i]=1;
}
}

m=nm[0];

for( j=1;j {
m=2*(m+nm[j])+nm[j+1];/////少了一个分号
}
cout<<"m:"<
g=ng[0];
for( i=0;i {
g=2*(g+ng[i])+ng[i+1];
}

yushu=m%g;

//余数字符串
int ys1=yushu;
for( m=gl-1;m>=0;m--)
{
ys[m]=ys1%2;
ys1=ys1/2;
}

//合并得到发送的字符串

cout<<"发送字符为:"< for ( i=0;i {
cout< }

delete [] s;
delete [] ys;
delete [] nm;
delete [] ng;

delete [] M;
delete [] G;

return 0;
}