(原創) 写一个字符串相加产生整数的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++)
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++)
相关文章推荐
- 1.把一个字符串内的正整数相加
- 输入一个表示整数的字符串,把该字符串转换成整数并输出。
- 10. 编码:写一个c函数,实现将16进制表现形式的字符串转化为整数。例如,输入”1a”,返回26,输入“FE”,返回254
- 用递归法将一个整数n转换成字符串
- 输入一个整数,将这个整数以字符串的形式逆序输出
- 字符串-数组的定义和关系,应用产生一个随机字符串
- 随机产生一个mbit为的长整数
- 实现一个函数int my_atoi(char s[]),可以将一个字符串转换为对应的整数
- 华为机试——通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 输入一个表示整数的字符串,把该字符串转换成整数并输出。
- 请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
- 用递归调用输出 一个N位整数的每一位的数 并转化为一个字符串
- c函数实现将一个整数转为任意进制的字符串输出
- 超大整数相加用字符串解决
- 【C语言】为下面的函数原型编写函数定义: int ascii_to_integer(char *str); 这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。
- 【C语言】编写一个函数,将一个数字字符串转换成该字符串对应的数字(包括正整数、负整数)。
- 随机产生一个跟时间相关的字符串
- 将一个整数(数字或长字符串形式)转换成中文输出
- Java编程题:将一个十六进制的字符串转换成一个十进制的整数
- JAVA中如何判断一个输入是数字(小数和整数)还是字符串?