ACM常用函数整理

不定期更新ing~


      1,strtol()

       strtol() 函可将n进制数转换为10进制,用来将字符串转换为长整型数(long),其原型为:long int strtol (const char* str, char** endptr, int base);

    error存储第一个转换失败以及以后的的字符串;例如:把11221由二进制转换为十进制:num=strtol(s,&error,2);则num等于3,error='221'。

<span style="font-size:14px;">#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;

int main(){
	char *error;
	char s[20];
	while(cin>>s){
		
		printf("2 to 10 : %ld\n",strtol(s,&error,2));
		if(strlen(error)!=0) cout<<"error: "<<error<<endl; 
		
		printf("8 to 10 : %ld\n",strtol(s,&error,8));
		if(strlen(error)!=0) cout<<"error: "<<error<<endl;
		 
		printf("16 to 10 : %ld\n",strtol(s,&error,16));
		if(strlen(error)!=0) cout<<"error: "<<error<<endl; 
		
		printf("\n");
	}
	return 0;
} </span>
     2,十进制数转换为n进制字符串

          使用itoa函数,itoa(num,str,n),将num转化为n进制数存到str字符串中,但是itoa并不是一个标准c函数,时window平台特有的函数,

3,sprintf,

sprintf可将整数转化为字符串,sptintf(str,"%d",num)

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值