您的位置:首页 > 编程语言 > C语言/C++

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

2006-11-29 18:10 375 查看
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.

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

Demo code :

1#include <iostream>
21#include <string>
22#include <sstream>
23
24int AtoiPlus(const char*, const char*);
25
26template <class T>
27void ConvertFromString(T&, const std::string&);
28
29
37template <class T>
38
43int AtoiPlus(const char* s1, const char* s2) std::string str1 = s1;
45 std::string str2 = s2;
46
47 int i = 0;
48 ConvertFromString(i, str1);
49
50 int j = 0;
51 ConvertFromString(j, str2);
52
53 return i + j;
54}
See Also
(原創)如何将std::string转int,double? (C/C++)
(原創)如何将int,double转std::string? (C/C++)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐