记录一些库函数的实现,主要考查代码风格、程序的健壮性、通用性。例子都是用C++语言实现。
请编写一个 函数,该函数将给定的一个数字字符串转换成整数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cassert>
5 using namespace std;
6 //转换函数
7 int StrToNum( const char *str )
8 {
9 assert( str != NULL );
10 int num = 0;
11 while ( *str != '\0' ) {
12 num = num * 10 + ( *str - '0' );
13 str = str + 1;
14 }
15
16 return num;
17 }
18
19 int main() {
20
21 char ch[8];
22 while ( scanf( "%s", ch ) != EOF )
23 printf( "%d\n", StrToNum( ch) );
24
25 return 0;
26 }
27
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cassert>
5 using namespace std;
6 //转换函数
7 int StrToNum( const char *str )
8 {
9 assert( str != NULL );
10 int num = 0;
11 while ( *str != '\0' ) {
12 num = num * 10 + ( *str - '0' );
13 str = str + 1;
14 }
15
16 return num;
17 }
18
19 int main() {
20
21 char ch[8];
22 while ( scanf( "%s", ch ) != EOF )
23 printf( "%d\n", StrToNum( ch) );
24
25 return 0;
26 }
27
请编写一个 C 函数,该函数将给定的一个整数转换成字符串