天天看點

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. using namespace std;                                                              
  5. int main()                                                                        
  6. {                                                                                 
  7.     char *offset;     // 接收解析結束位址
  8.     string s1 = "520";                                                            
  9.     cout << strtol(s1.c_str(), &offset, 0) << endl;                               
  10.     string s2 = "0xA";                                                            
  11.     cout << strtol(s2.c_str(), &offset, 16) << endl;                              
  12.     return 0;                                                                     

問題02:如何将int或float類型的數值轉換為某種格式的字元串

    使用stringstream類來存儲字元串資料。stringstream是一種把資料轉換成字元串的簡便方法,因為它允許使用由标準輸入輸出流類提供的格式化工具。

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

問題03:如何把用科學計數法表示的數值字元串存儲到double變量中

    要解析用科學計數法表示的數值,最直接的方法是使用C++函數庫在<sstream>中内置的streamstring類。

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

繼續閱讀