atoi和itoa函數(shù)的實(shí)現(xiàn)方法
//atoi的實(shí)現(xiàn)
#include<iostream>
using namespace std;
int atio1(char *s)
{
int sign=1,num=0;
if(*s=='-')
sign=-1;
s++;
while((*s)!='\0')
{
num=num*10+(*s-'0');
s++;
}
return num*sign;
}
//itoa的實(shí)現(xiàn)
char *itoa(int num, char *str, int radix)
{
char* ptr = str;
int i;
int j;
while (num)
{
*ptr++ = string[num % radix];
num /= radix;
if (num < radix)
{
*ptr++ = string[num];
*ptr = '\0';
break;
}
}
j = ptr - str - 1;
for (i = 0; i < (ptr - str) / 2; i++)
{
int temp = str[i];
str[i] = str[j];
str[j--] = temp;
}
return str;
}
int main()
{
char *s="-123567890";
cout<<atio1(s);
system("pause");
}
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++?opencv圖像處理實(shí)現(xiàn)圖片邊緣檢測(cè)示例
這篇文章主要為大家介紹了C++?opencv實(shí)現(xiàn)圖片邊緣檢測(cè)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05一文詳解C++關(guān)鍵字nullptr及與NULL的區(qū)別
這篇文章主要給大家詳細(xì)介紹了C++關(guān)鍵字nullptr,及?NULL與nullptr的區(qū)別,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06