将整数转化为字符串

来源:互联网 发布:黑马python 编辑:程序博客网 时间:2024/04/27 16:18
 

#include<iostream>
#define abs(a) ((a)>=0?(a):-(a))
using namespace std;
void itostring(int i)
 
 char* str=new char[10];
 int n=0;
 bool zf=1;
 if(i<0) zf=0;
 while(i)
 {
  str[n++]=abs(i%10)+'0';
  i=i/10;
 }
 if(!zf) str[n++]='-';
 for(int j=n-1;j>=0;j--)
  cout<<str[j];
 cout<<endl;
 delete[] str;

}
void main()
{
 int i;
 char key;
 do
 {
  cin>>i;
  itostring(i);
  cin>>key;
 }while(key!='a');
   

}

0 0
原创粉丝点击