
刚开始的时候我竟然想徒手写进制转换的代码,直到我发现了一个函数itoa()真的是太香啦~~~~
头文件:#include<stdlib.h>
函数:itoa(n,s,r),其中n为转换前的数,s为转换后存入的字符串,r为转换为几进制。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std;
int main()
{
int n,r;
char s[105];
while(cin>>n>>r)
{
if(n<0)
{
cout<<"-";
n=-n;
}
itoa(n,s,r);
if(r>10)
for(int i=0;s[i]!='\0';i++)
{
if(s[i]>='a'&&s[i]<='z')
s[i] = s[i] -32;
}
cout<<s<<endl;
}
return 0;
}
本文介绍了一种在C++中利用itoa函数快速进行整数进制转换的方法,简化了手动编写转换代码的过程。代码示例展示了如何处理负数,并将转换后的字符串按指定进制输出,同时在输出时对大于10的进制转换结果自动进行大写处理。
1320

被折叠的 条评论
为什么被折叠?



