(原創) 写一个字符串相加产生整数的function (C/C++)

Homework 6 & Midterm 1
Write a function int AtoiPlus(const char*, const char*) that takes two C-style strings containing int digit and return the corresponding int. For example, AtoiPlus("123", "4") return 127. You should write a main routine to test your function. Use at least the following code fragment to test your code.

1 None.gif string  s  =   " 1236 " ;
2 None.gif char  ca[]  =   " 123 " ;
3 None.gif //  pass these into your AtoiPlus function and output 1359   


Demo code :

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2006 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : AtoiPlus.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0
 6InBlock.gifDescription : Homework 6 & Midterm 1
 7InBlock.gif              Write a function int AtoiPlus(const char*, const char*) 
 8InBlock.gif              that takes two C-style strings containing int digit and
 9InBlock.gif              return the corresponding int. For example, 
10InBlock.gif              AtoiPlus("123", "4") return 127. You should write a main
11InBlock.gif              routine to test your function. Use at least the following 
12InBlock.gif              code fragment to test your code.
13InBlock.gif
14InBlock.gif              string s = "1236";
15InBlock.gif              char ca[] = "123";
16InBlock.gif              // pass these into your AtoiPlus function and output 1359
17InBlock.gif
18InBlock.gifRelease     : 11/29/2006
19ExpandedBlockEnd.gif*/

20 None.gif#include  < iostream >
21 None.gif#include  < string >
22 None.gif#include  < sstream >
23 None.gif
24 None.gif int  AtoiPlus( const   char * const   char * );
25 None.gif
26 None.giftemplate  < class  T >  
27 None.gif void  ConvertFromString(T & const  std:: string & );
28 None.gif
29 ExpandedBlockStart.gifContractedBlock.gif int  main()  dot.gif {
30InBlock.gif    std::string s = "1236";
31InBlock.gif    char ca[] = "123";
32InBlock.gif    int result = AtoiPlus(s.c_str(),ca);
33InBlock.gif
34InBlock.gif    std::cout << result << std::endl;
35ExpandedBlockEnd.gif}

36 None.gif
37 None.giftemplate  < class  T >  
38 ExpandedBlockStart.gifContractedBlock.gif void  ConvertFromString(T &  val,  const  std:: string &  str)  dot.gif {
39InBlock.gif    std::stringstream ss(str);
40InBlock.gif    ss >> val;
41ExpandedBlockEnd.gif}

42 None.gif
43 ExpandedBlockStart.gifContractedBlock.gif int  AtoiPlus( const   char *  s1,  const   char *  s2)  dot.gif {
44InBlock.gif    std::string str1 = s1;
45InBlock.gif    std::string str2 = s2;
46InBlock.gif
47InBlock.gif    int i = 0;
48InBlock.gif    ConvertFromString(i, str1);
49InBlock.gif
50InBlock.gif    int j = 0;
51InBlock.gif    ConvertFromString(j, str2);
52InBlock.gif
53InBlock.gif    return i + j;
54ExpandedBlockEnd.gif}

See Also
(原創)如何将std::string转int,double? (C/C++)
(原創)如何将int,double转std::string? (C/C++)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值