求大神指教...运行这些代码,为什么显示的结果没有字符串,是C++primer Plus 第四版222页上面的程序清单8.8
#include<iostream>usingnamespacestd;unsignedlongleft(unsignedlongnum,unsignedct);char...
#include<iostream>
using namespace std;
unsigned long left(unsigned long num,unsigned ct);
char * left(const char * str, int n=1);
int main ()
{
char * trip = "Hawaii!! ";
unsigned long n = 12345678;
int i ;
char * temp;
for(i=1;i<10;i++)
{
cout<<left(n,i)<<"\n";
temp=left(trip,i);
cout<<temp<<"\n";
delete [] temp;
}
return 0;
}
unsigned long left(unsigned long num,unsigned ct)
{
unsigned digits=1;
unsigned long n = num;
if(ct==0||num==0)
return 0;
while (n/=10)
digits++;
if(digits>ct)
{
ct=digits-ct;
while(ct--)
num/=10;
return num;
}
else
return num;
}
char * left(const char * str, int n)
{
if(n<10)
n=0;
char * p = new char[n+1];
int i;
for(i=0;i<n&&str[i];i++)
p[i]=str[i];
while (i<=n)
p[i++]='\0';
return p;
} 展开
using namespace std;
unsigned long left(unsigned long num,unsigned ct);
char * left(const char * str, int n=1);
int main ()
{
char * trip = "Hawaii!! ";
unsigned long n = 12345678;
int i ;
char * temp;
for(i=1;i<10;i++)
{
cout<<left(n,i)<<"\n";
temp=left(trip,i);
cout<<temp<<"\n";
delete [] temp;
}
return 0;
}
unsigned long left(unsigned long num,unsigned ct)
{
unsigned digits=1;
unsigned long n = num;
if(ct==0||num==0)
return 0;
while (n/=10)
digits++;
if(digits>ct)
{
ct=digits-ct;
while(ct--)
num/=10;
return num;
}
else
return num;
}
char * left(const char * str, int n)
{
if(n<10)
n=0;
char * p = new char[n+1];
int i;
for(i=0;i<n&&str[i];i++)
p[i]=str[i];
while (i<=n)
p[i++]='\0';
return p;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询