📜  C / C++中的strtod()函数

📅  最后修改于: 2021-05-26 00:28:28             🧑  作者: Mango

strtod()是C和C++ STL中的内置函数,该函数将字符串的内容解释为浮点数,并以双精度形式返回其值。
它将指针设置为指向字符串的最后一个有效字符之后的第一个字符(仅当存在时),否则将指针设置为null。

语法

double strtod(str, &end)

参数
str :它指定具有浮点数表示形式的字符串。
end :指定参数,该参数引用char *类型的已分配对象。

返回值:返回一个双精度值,如果不能执行有效的转换,则从字符串转换为double值,然后返回0。

下面的程序说明了上述函数:

程序1

// C++ program to illustrate the
// strtod() function
#include 
#include 
  
using namespace std;
  
int main()
{
    char str[] = "11.03e 0mn";
    char* end;
    double number;
  
    number = strtod(str, &end);
    cout << "number = " << str << endl;
    cout << "double = " << number << endl;
    cout << "end string = " << end << endl;
  
    return 0;
}
输出:
number = 11.03e 0mn
double = 11.03
end string = e 0mn

程序2

// C++ program to illustrate the
// strtod() function without trailing characters
#include 
#include 
  
using namespace std;
  
int main()
{
    char str[] = "4.06";
    char* end;
    double number;
  
    number = strtod(str, &end);
    cout << "number= " << str << endl;
    cout << "double= " << number << endl;
  
    // If end is not Null
    if (*end) {
        cout << end;
    }
    // If end is Null
    else {
        cout << "null";
    }
    return 0;
}
输出:
number= 4.06
double= 4.06
null

程序3
带有指数和十六进制的strtod()函数

// C++ program to illustrate the
// strtod() function with exponents and hexadecimals
#include 
#include 
#include 
  
using namespace std;
int main()
{
    // initialize a exponential value
    char str[] = "-89.04e-3win gfg";
    char* end;
    double number;
  
    number = strtod(str, &end);
    cout << "str = " << str << endl;
    cout << "double = " << number << endl;
    cout << "end string = " << end << endl
         << endl;
  
    // initialize a new hexadecimal value
    strcpy(str, "1998gupta.1204ishwar");
  
    number = strtod(str, &end);
    cout << "str = " << str << endl;
    cout << "double = " << number << endl;
    cout << "end string = " << end << endl;
  
    return 0;
}
输出:
str = -89.04e-3win gfg
double = -0.08904
end string = win gfg

str = 1998gupta.1204ishwar
double = 1998
end string = gupta.1204ishwar

计划4

// C++ program to illustrate the
// strtod() function for Infinity and NaN
#include 
#include 
  
using namespace std;
  
int main()
{
    char* end;
  
    cout << "Infinity"
         << " to double = " 
         << strtod("infinity", &end) << endl;
    cout << "end string = " << end << endl
         << endl;
  
    cout << "Infpqrs"
         << " to double = " << 
         strtod("Infpqrs", &end) << endl;
    cout << "end string = " << end << endl
         << endl;
  
    cout << "NaN11x"
         << " to double = " 
         << strtod("NaN11x", &end) << endl;
    cout << "end string = " << end << endl
         << endl;
  
    return 0;
}
输出:
Infinity to double = inf
end string = 

Infpqrs to double = inf
end string = pqrs

NaN11x to double = nan
end string = 11x

计划5
带前导空格的strtod()函数

// C++ program to illustrate the
// strtod() function with leading whitespace
#include 
#include 
  
using namespace std;
  
int main()
{
    char* end;
  
    cout << "99.99"
         << " to double = " 
         << strtod(" 19.99", &end) << endl;
    // end pointer is set to null
    cout << "end string = " 
         << end << endl
         << endl;
  
    // Returns 0 because of invalid conversion
    cout << "xyz1.80"
         << " to double = " 
         << strtod("xyz1.80", &end) << endl;
    cout << "end string = " << end << endl
         << endl;
  
    return 0;
}
输出:
99.99 to double = 19.99
end string = 

xyz1.80 to double = 0
end string = xyz1.80
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”