C++编程问

来源:百度知道 编辑:UC知道 时间:2024/07/07 03:35:15
编写两个函数,一个是将不大于9999的整数转化为字符串;另一个是求出转换后的字符串的长度.由主函数输入一个整数,并输出转换后的字符串和长度!

应该没什么问题了。
#include <iostream>
using namespace std;

int convert(long a, char s[])
{
long temp=0;
int length=0;
for(int i=0;a!=0;i++)
{
temp=a%10;
s[4-i-1]=(char)(temp+48);
a=a/10;
length++;
}
return length;
}

void main()
{

long a;
int length;
char s[4];

cout<<"Please enter an integer (0=<a<=9999): "<<endl;
cin>>a;
length=convert(a,s);
for(int i=0;i<length;i++)
cout<<s[4-length+i]<<" ";
cout<<endl;
cout<<"The length is "<<length<<endl;

}

#include <stdio.h>
#include <conio.h>

void Inttochar(int a, char str[])
{
char stack[5];
int b,i=0;
if (a<0) b=-a;
else b=a;
while (1)
{
stack[i++]= (b%10)+48;
if ((b/=1