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;                                                                     

 

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