Cookbook系列之Cpp:数值计算

问题01:如何将字符串形式的数值转换为诸如int或float之类的数值类型

    定义在<cstdlib>中的strtol、strtod和strtoul函数可以将以null结尾的字符串转换为long int、double或unsigned long类型的数值。你可以使用他们把任意进制的字符串转换为数值(相比之下,atoi函数只能转换十进制数值,并且没有错误返回)。

  1. #include <iostream>                                                               
  2. #include <string>                                                                 
  3. #include <cstdlib>                                                                
  4.                                                                                   
  5. using namespace std;                                                              
  6.                                                                                   
  7. int main()                                                                        
  8. {                                                                                 
  9.     char *offset;     // 接收解析结束地址
  10.                                                                                   
  11.     string s1 = "520";                                                            
  12.     cout << strtol(s1.c_str(), &offset, 0) << endl;                               
  13.                                                                                   
  14.     string s2 = "0xA";                                                            
  15.     cout << strtol(s2.c_str(), &offset, 16) << endl;                              
  16.                                                                                   
  17.     return 0;                                                                     

问题02:如何将int或float类型的数值转换为某种格式的字符串

    使用stringstream类来存储字符串数据。stringstream是一种把数据转换成字符串的简便方法,因为它允许使用由标准输入输出流类提供的格式化工具。

  1. #include <iostream>                                                               
  2. #include <string>                                                                 
  3. #include <sstream>                                                                
  4. #include <iomanip>                                                                
  5.                                                                                   
  6. using namespace std;                                                              
  7.                                                                                   
  8. int main()                                                                        
  9. {                                                                                 
  10.     stringstream ss;                                                              
  11.                                                                                   
  12.     ss << 9;                                                                      
  13.     cout << ss.str() << endl;                                                     
  14.                                                                                   
  15.     ss.str("");                                                                   
  16.     ss << showbase << hex << 16;                                                  
  17.     cout << ss.str() << endl;                                                     
  18.                                                                                   
  19.     ss.str("");                                                                   
  20.     ss << setprecision(3) << 3.1415;                                                
  21.     cout << ss.str() << endl;                                                     
  22.                                                                                   
  23.     return 0;                                                                     

问题03:如何把用科学计数法表示的数值字符串存储到double变量中

    要解析用科学计数法表示的数值,最直接的方法是使用C++函数库在<sstream>中内置的streamstring类。

  1. #include <iostream>                                                               
  2. #include <sstream>                                                                
  3. #include <string>                                                                 
  4.                                                                                   
  5. using namespace std;                      
  6.                                                                                   
  7. int main()                                                                        
  8. {                                                                                 
  9.     stringstream ss("1.234e5");                                                   
  10.     double d = 0;                                                                 
  11.     ss >> d;                                                                      
  12.                                                                                   
  13.     if(ss.fail()) {                                                               
  14.         string e = "Unable to format";                                            
  15.         throw(e);                                                                 
  16.     }                                                                             
  17.                                                                                   
  18.     cout << d << endl;                                                            
  19.                                                                                   
  20.     return 0;                                                                     

问题04:如何获得某种数值类型的最值

    使用<limits>中的numeric_limits类模板可以知道某个数值类型所能表示的最大值和最小值,min和max是numeric_limits的静态成员函数。

  1. #include <iostream>                                                               
  2. #include <limits>                                                                 
  3.                                                                                   
  4. using namespace std;                                                              
  5.                                                                                   
  6. int main()                                                                        
  7. {                                                                                 
  8.     cout << numeric_limits<int>::min() << endl;                                   
  9.     cout << numeric_limits<int>::max() << endl;                                   
  10.                                                                                   
  11.     return 0;                                                                     

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章