题目code(C++):总结
基本类型转换
1、整型和浮点型之间的转换2、字符串和数字之间的转换3、字符串和字符之间的转换
题目
读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式
每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10 ^100 。
输出格式
在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。
输入样例
1234567890987654321123456789
输出样例
yi san wucode(C++):
//万能头文件 //#include#include #include #include using namespace std; int main(int argc, char** argv) { string n; //输入 cin>>n; //length()计算数字长度 int m = n.length(); //cout< 0; i*=10) { d += 1; } //cout<<"d="< 总结 基本类型转换 1、整型和浮点型之间的转换https://www.cnblogs.com/xiaodangxiansheng/p/12697278.html
(1)普通的强制转换
double d_Temp = 1000; int i_Temp; i_Temp = (int)d_Temp; cout<(2)使用标准的强制转换
static_castdynamic_castconst_castreinterpret_cast
double d_Temp; int i_Temp = 323; d_Temp = static_cast2、字符串和数字之间的转换(i_Temp); (1)用sprintf_s函数将数字转换成字符串
int i_temp = 2020; std::string s_temp; char c_temp[20]; sprintf_s(c_temp, "%d", i_temp); s_temp = c_temp; std::cout << s_temp << std::endl;(2)用sscanf函数将字符串转换成数字
double i_temp; char c_temp[20] = "15.234"; sscanf_s(c_temp, "%lf", &i_temp); std::cout << i_temp << std::endl;(3)atoi, atof, atol, atoll(C++11标准) 函数将字符串转换成int,double, long, long long 型
std::string s_temp; char c_temp[20] = "1234"; int i_temp = atoi(c_temp); s_temp = c_temp; std::string s_temp; char c_temp[20] = "1234.1234"; double i_temp = atof(c_temp); s_temp = c_temp;(4)strtol, strtod, strtof, strtoll,strtold 函数将字符串转换成int,double,float, long long,long double 型
std::string s_temp; char c_temp[20] = "4.1234"; double a = strtod(c_temp, nullptr); //后面的参数是如果遇到字符串则是指向字符串的引用(5)用to_string把数字转化成字符串
double d_temp = 1.567; std::string s_temp = to_string(d_temp);3、字符串和字符之间的转换(1)用c_str从字符串转换char*
string s_temp = "this is string"; const char* c_temp; c_temp = s_temp.c_str();(2)char数组转换成字符串
string s_temp; char c_temp[20]; c_temp[0] = 'H'; c_temp[1] = 'L'; c_temp[2] = 'L'; c_temp[3] = 'O'; c_temp[4] = ' '; s_temp = c_temp;



