#include
using namespace std;
class integer_string{
int num;
char *s;
public:
integer_string(int n);
int f();
void fun();
void show();integer_string();integer_string()
};
integer_string::integer_string(int n)
{
num=n;
}
int integer_string::f()
{
int m=num,i;
for(i=0;m>0;m/=10)
i++;
return i;
}
void integer_string::fun()
{
int m=num;
s=new char[f()+1];
for(int i=0;m>0;m/=10)
{
s[f()-1-i]=m%10+'0';
i++;
}
s[i]='\0';
}
void integer_string::show()
{
cout<<"字符串为:"<<s<<endl;
}
integer_string::
{
delete []s;
}
void main()
{
int b;
cout<<"输入一个正整数:"<<endl;
cin>>b;
integer_string test(b);
test.fun();
test.show();
system("pause");
}
不关注你的代码质量 只是调通你的代码 这里有一些细节要关注 数组不要越界,类的构造函数和析构函数了解一下 ,自己看看代码吧
#pragma warning(suppress : 4996)
#include <iostream>
using namespace std;
class integer_string {
int num;
char* s;
public:
integer_string(int n);
~integer_string();
int f();
void fun();
void show();
};
integer_string::integer_string(int n)
{
num = n;
s = NULL;
}
integer_string::~integer_string()
{
delete[]s;
}
int integer_string::f()
{
int m = num, i;
for (i = 0; m > 0; m /= 10)
i++;
return i;
}
void integer_string::fun()
{
int m = num;
s = new char[f() + 1];
int i = 0;
for (i = 0; m > 0; m /= 10)
{
s[f() - 1 - i] = m % 10 + '0';
i++;
}
s[i] = '\0';
}
void integer_string::show()
{
cout << "字符串为:" << s << endl;
}
void main()
{
int b;
cout << "输入一个正整数:" << endl;
cin >> b;
integer_string test(b);
test.fun();
test.show();
system("pause");
}
卡住是因为你的程序写错了,导致程序无法正常运行