C++失败题
class BYTE
main(){
BYTE * pByte = new BYTE[4];
for(int i = 0; i < 4; i++)
{
*pByte = i;
pByte++;
}
}
main(){
BYTE * pByte = new BYTE[4];
for(int i = 0; i < 4; i++)
{
*pByte = i;
pByte++;
}
}
内存错误,未初始化?
实现字符串拷贝函数strcpy()
#include <iostream>
using namespace std;
using namespace std;
char* strcpy(char* dest, const char *src )
{
// char* pdest = static_cast<char*>(dest);
// const char* psrc = static_cast<const char*>(src);
if((dest==NULL)||(src==NULL))
throw"error";
char* strdest = dest;
while((*dest++ = *src++)!='\0');
return strdest;
}
{
// char* pdest = static_cast<char*>(dest);
// const char* psrc = static_cast<const char*>(src);
if((dest==NULL)||(src==NULL))
throw"error";
char* strdest = dest;
while((*dest++ = *src++)!='\0');
return strdest;
}
int main()
{
char* sdest = (char*)malloc(strlen(str));
char str[] = "0123456789";
strcpy( sdest, str);
cout<< sdest<<endl;
return 0;
free(sdest);
}
实现内存拷贝函数memcpy()
#include <iostream>
using namespace std;
void* memcpy(void * dest, const void *src, size_t count )
{
char* pdest = static_cast<char*>(dest);
const char* psrc = static_cast<char*>(src);
if(pdest>psrc && pdest<psrc+count)
{
for(size_t i=count-1; i!=1; --i)
pdest[i] = psrc[i];
}
else
{
for(size_t i=0; i<count; ++i)
pdest[i]=psrc[i];
}
return dest;
}
int main()
{
char src[]= "0123456789";
memcpy( str+1; str+0; 9);
cout<< str<<endl;
return 0;
}
{
char* pdest = static_cast<char*>(dest);
const char* psrc = static_cast<char*>(src);
if(pdest>psrc && pdest<psrc+count)
{
for(size_t i=count-1; i!=1; --i)
pdest[i] = psrc[i];
}
else
{
for(size_t i=0; i<count; ++i)
pdest[i]=psrc[i];
}
return dest;
}
int main()
{
char src[]= "0123456789";
memcpy( str+1; str+0; 9);
cout<< str<<endl;
return 0;
}
二、编写一函数实现子字符串查找
char *strstr(char* str, char * substr)
char *strstr(char* str, char * substr)
main()
{
char b[]="0123456789ABCDEF";
int c[64],d,i=0,base=16;
long n;
scanf("%ld",&n);
do{
c[i]= ;i++;n=n/base;
}while(n!=0);
for(--i;i>=0;--i)
{d=c[i];printf("%c",b[d]);}
getch();
}
int AnsiString::Pos(const AnsiString& subStr)
{
int len=subStr.Length();
int p=0;
for(int i=0;i<DataLen;i++)
{
if(i+1+len<=DataLen)
{
if(SubString(i+1,len)==subStr)
{
p=i+1;
break;
}
}
else
{
break;
}
}
return p;
}
{
char b[]="0123456789ABCDEF";
int c[64],d,i=0,base=16;
long n;
scanf("%ld",&n);
do{
c[i]= ;i++;n=n/base;
}while(n!=0);
for(--i;i>=0;--i)
{d=c[i];printf("%c",b[d]);}
getch();
}
int AnsiString::Pos(const AnsiString& subStr)
{
int len=subStr.Length();
int p=0;
for(int i=0;i<DataLen;i++)
{
if(i+1+len<=DataLen)
{
if(SubString(i+1,len)==subStr)
{
p=i+1;
break;
}
}
else
{
break;
}
}
return p;
}
AnsiString AnsiString::SubString(int index, int count)
{
index--;
if(index+count>DataLen)
{
count=DataLen-index;
}
char* tmp=new char[count+1];
memcpy(tmp,Data+index,count);
tmp[count]=0;
AnsiString Result=tmp;
delete []tmp;
return Result;
}
XOR eax,eax(156252207) 22:00:39
pos函数就是
struct A
{
B*pB;
}
struct B
{
A* pA;
}
{
index--;
if(index+count>DataLen)
{
count=DataLen-index;
}
char* tmp=new char[count+1];
memcpy(tmp,Data+index,count);
tmp[count]=0;
AnsiString Result=tmp;
delete []tmp;
return Result;
}
XOR eax,eax(156252207) 22:00:39
pos函数就是
struct A
{
B*pB;
}
struct B
{
A* pA;
}
一、实现编译通过:
预声明
struct B;
struct A
{
B * pB;
};
struct B
{
A* pA;
};
struct A
{
B * pB;
};
struct B
{
A* pA;
};
专注于企业信息化,最近对股票数据分析较为感兴趣,可免费分享股票个股主力资金实时变化趋势分析工具,股票交流QQ群:457394862