에서 삭제 나는 엄청난 수사업자 새로운 C++
#include <iostream>
using namespace std;
class big
{
protected:
char *a;
long lenght,maxlenght;
public:
big()
{
maxlenght=256;
a=new char[maxlenght];
memset(a,'\0',maxlenght);
}
void read();
void set_maxlenght(long x);
long msize();
long size();
char* return_big();
long at(long poz);
char* operator[](long poz);
void operator=(big x);
void modify (long poz,long val);
void dec (long poz);
void inc (long poz);
void operator-(big x);
int compare(big x);
};
//functions
void write(big x)
{
char *ptr;
ptr=x.return_big();
cout<<ptr;
}
//class big
void big::read()
{
char *buffer;
buffer=new char[maxlenght];
memset(buffer,'\0',maxlenght);
cin>>buffer;
delete[] a;
a=new char[strlen(buffer)];
memset(a,'\0',maxlenght);
for(long i=0;i<(long)strlen(buffer);i++)
a[i]=buffer[i];
lenght=strlen(buffer);
delete[] buffer;
}
void big::set_maxlenght(long x)
{
maxlenght=x;
}
long big::msize()
{
return maxlenght;
}
long big::size()
{
return lenght;
}
char* big::return_big()
{
char *x;
x=a;
return x;
}
long big::at(long poz)
{
if(poz>=lenght)
return -1;
else
return this->a[poz];
}
char* big::operator[](long poz)
{
if(poz>=lenght)
return NULL;
else
return a+poz;
}
void big::operator=(big x)
{
a=new char[x.size()];
for(long i=0;i<(long)strlen(x.a);i++)
this->modify(i,x.at(i));
this->lenght=strlen(x.a);
}
void big::modify (long poz,long val)
{
a[poz]=(char)val;
}
void big::dec (long poz)
{
if(a[poz])
a[poz]--;
}
void big::inc (long poz)
{
if(a[poz]<255)
a[poz]++;
}
void big::operator-(big z)
{
long n,m,i,j,aux;
big rez,x,y;
if(compare(z))
{
x=*this;
y=z;
}
else
{
y=*this;
x=z;
}
n=x.size();
m=y.size();
i=n;
j=m;
while(j>=0)
{
if(x.at(i)<0)
{
x.modify(i-1,x.at(i-1)+x.at(i));
x.modify(i,0);
}
aux=x.at(i)-y.at(j);
if(aux<0)
{
x.dec(i-1);
rez.modify(i,aux+10);
}
else
rez.modify(i,aux);
}
while(i)
{
i--;
if(x.at(i)<0)
{
x.modify(i-1,x.at(i-1)+x.at(i));
x.modify(i,0);
}
rez.modify(i,x.at(i));
}
}
int big::compare(big x)
{
return strcmp(this->a,x.return_big());
}
int main()
{
big a,b;
a.read();
b.read();
write(a);
endl(cout);
write(b);
return 0;
}
첫 번째 읽기 괜찮 처리 할 큰라는 이름의 클래스를 만들려고 사업자 new
및 delete
와 도움이 필요하지만, 두 번째는있다 이 a=new char[strlen(buffer)]
을 실행하려면이 오류 :
---Windows has triggered an error in MyProject.exe
This may be due to the corruption of the heap, which indicates a bug in MyProject.exe or any DLLs it has loaded.
This may also be due to the user pressing F12 while MyProject.exe has focus.
The output window may have more diagnostic information.---
는 두 개의 버튼이 휴식하고 계속있다. 나는 계속 누르면
그것은 나에게 보여줍니다이 : 나는 매우 포인터 (6 개월) 경험 아니에요
Unhandled exception at 0x77b8380e in Olimpiada a IX-a.exe: 0xC0000374: A heap has been corrupted.
. 나는 어떤 대답을 주셔서 감사합니다.
뒤의'NUL' 문자를 수용하려면 길이에 1을 더해야합니다. 또는'strdup()'또는'std :: string' 객체를 사용하십시오. – trojanfoe
* 참고 : 큰 숫자로 작업하려면 'NTL'과 같은 라이브러리를 사용하십시오. http://www.shoup.net/ntl/ * – Vyktor
이 코드에서 포인터를 사용하면 안됩니다. 'std :: vector'를 사용해야합니다. 나는 당신에게 [좋은 입문 C++ 책] (http://stackoverflow.com/q/388242/46642)을 추천해야하는 것처럼 느낀다. –