C++常用庫函數atoi,itoa,strcpy,strcmp的實現

 C++常用庫函數atoi,itoa,strcpy,strcmp的實現

 view plaincopy to clipboardprint?

1.//整數轉換成字符串itoa函數的實現  

#include "stdafx.h"  

#include <iostream>  

using namespace std;  

void itoaTest(int num,char str[] )  

{  

       int sign = num,i = 0,j = 0;  

       char temp[11];  

       if(sign<0)//判斷是否是一個負數  

10        {  

11               num = -num;  

12        };  

13        do  

14        {  

15               temp[i] = num%10+'0';          

16               num/=10;  

17               i++;  

18        }while(num>0);  

19        if(sign<0)  

20        {  

21               temp[i++] = '-';  

22        }  

23        temp[i] = '\0';  

24        i--;  

25        while(i>=0)  

26        {  

27               str[j] = temp[i];  

28               j++;  

29               i--;  

30        }  

31        str[j] = '\0';  

32 }  

33 2. //字符串轉換成整數atoi函數的實現  

34 int atoiTest(char s[])  

35 {  

36        int i = 0,sum = 0,sign;    //輸入的數前面可能還有空格或製表符應加判斷  

37        while(' '==s[i]||'\t'==s[i])  

38        {  

39               i++;  

40        }  

41        sign = ('-'==s[i])?-1:1;  

42        if('-'==s[i]||'+'==s[i])  

43        {  

44               i++;  

45        }  

46        while(s[i]!='\0')  

47        {  

48               sum = s[i]-'0'+sum*10;  

49               i++;  

50        }      

51        return sign*sum;  

52 }  

53    

54    

55 3.//字符串拷貝函數  

56 #include "stdafx.h"  

57 #include <assert.h>  

58 #include <string.h>  

59 #include <iostream>  

60 using namespace std;  

61 char *srcpy(char *dest,const char *source)  

62 {  

63        assert((dest!=NULL)&&(source!=NULL));  

64        char *address = dest;  

65        while(*source!='\0')  

66        {  

67               *dest++=*source++;  

68        }  

69        *dest = '\0';  

70        return address;  

71 }  

72    

73 4.//判斷輸入的是否是一個迴文字符串  

74 #include "stdafx.h"  

75 #include <string.h>  

76 #include <iostream>  

77 using namespace std;  

78 //方法一:藉助數組  

79 bool isPalindrome(char *input)  

80 {  

81        char s[100];  

82        strcpy(s,input);  

83        int length = strlen(input);  

84        int begin = 0,end = length-1;  

85        while(begin<end)  

86        {  

87               if(s[begin]==s[end])  

88               {  

89                      begin++;  

90                      end--;  

91               }  

92               else  

93               {  

94                      break;  

95               }             

96        }  

97        if(begin<end)  

98        {  

99               return false;  

100        }      

101        else  

102        {  

103               return true;  

104        }        

105 }  

106 //方法二:使用指針  

107 bool isPalindrome2(char *input)  

108 {  

109        if(input==NULL)  

110               return false;  

111        char *begin = input;  

112        char *end = begin+strlen(input)-1;  

113        while(begin<end)  

114        {  

115               if(*begin++!=*end--)  

116                      return false;  

117        }  

118        return true;  

119 }  

120    

121 int main(int argc, char* argv[])  

122 {  

123        char *s ="1234554321";  

124        if(isPalindrome(s))  

125        {  

126               cout<<"True"<<endl;  

127        }  

128        else  

129        {  

130               cout<<"Fasle"<<endl;  

131        }  

132    

133        if(isPalindrome2(s))  

134        {  

135               cout<<"True"<<endl;  

136        }  

137        else  

138        {  

139               cout<<"Fasle"<<endl;  

140        }  

141        cin.get();  

142    

143        return 0;  

144 }  

145    

146    

147 5.//不使用庫函數,編寫函數int strcmp(char *source, char *dest),若相等返回0,否則返回-1  

148 int strcmp(char *source, char *dest)  

149 {  

150        assert(source != NULL && dest != NULL);  

151        while(*source++==*dest++)  

152        {  

153               if(*source=='\0'&&*dest=='\0')  

154                      return 0;          

155        }  

156        return -1;  

157 }  

 

view plaincopy to clipboardprint?

158 #include <stdio.h>  

159 void strcat(char *string1, char *string2){  

160     while(*string1 != '\0')  

161         string1++;  

162     while(*string2)  

163     {  

164         *string1++ = *string2++;  

165     }  

166     *string1++ = '\0';  

167 }  

168 int strlen(char *string1){  

169     int count = 0;  

170     while(*string1++ != '\0')  

171         count++;  

172     return count;  

173 }  

174 int main(void)  

175 {  

176     char name[100]="wangfeng";  

177     char *mesg = " is a student!";  

178     strlen(name);  

179     puts(name);  

180     return 0;  

181 }  

182 #include <stdlib.h>  

183 /* 

184    這個函數調用的是庫函數中的 

185    strtol()函數,關於這個函數的 

186    源代碼後面將會給出。 

187 */  

188 int my_atoi(char *str)  

189 {  

190    return (int) strtol(str, NULL, 10);  

191 }  

192 /* 

193    下面的兩個函數沒有調用strtol()函數, 

194    而是直接給出了該函數的實現。 

195 */  

196 int my_atoi01(const char *str)  

197 {  

198    long int v=0;  

199    int sign = 0;  

200    

201    while ( *str == ' ')  str++;  

202    

203    if(*str == '-'||*str == '+')  

204       sign = *str++;  

205    

206    while (isdigit(*str))  

207    {  

208       v = v*10 + *str - '0';  

209       str++;  

210    }  

211    return sign == '-' ? -v:v;  

212 }  

213 int my_atoi02(char *str)  

214 {  

215    int sign;  

216    int n;  

217    unsigned char *p;  

218    

219    p=str;  

220    while (isspace(*p) ) p++;  

221    

222    sign = (*p == '-' ) ? -1 : 1;  

223    

224    if (*p=='+' || *p=='-' ) p++;  

225    

226    for (n=0; isdigit(*p); p++)  

227       n = 10*n + (*p - '0');  

228    

229    return sign*n;  

230 }  

231    

232 int main()  

233 {  

234    char * str = "2147483647";  

235    printf("%d\n",my_atoi(str));  

236     

237    str = "-2147483648";  

238    printf("%d\n",my_atoi(str));  

239     

240    str = "2147483647";  

241    printf("%d\n",my_atoi01(str));  

242     

243    str = "-2147483648";  

244    printf("%d\n",my_atoi01(str));  

245     

246    str = "2147483647";  

247    printf("%d\n",my_atoi02(str));  

248     

249    str = "-2147483648";  

250    printf("%d\n",my_atoi02(str));  

251     

252    system("pause");  

253    return 0;  

254 }  

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章