【问题描述】 本题要求实现一个拆分实数的整数与小数部分的简单函数。在main里完成数据读取和结果输出。 函数原型为 void splitfloat( float x, int *intpart, float *fracpart );
【问题描述】
本题要求实现一个拆分实数的整数与小数部分的简单函数。在main里完成数据读取和结果输出。
函数原型为
void splitfloat( float x, int *intpart, float *fracpart );
void splitfloat(float x, int *intpart, float *fracpart)
{
std::string srcData =std:: to_string(x);
std::string temp;
int position=-1;
for (int i = 0; i < srcData.length(); i++)
{
if (srcData[i]=='.')
{
position = i;
break;
}
}
for (int i = 0; i < position; i++)
{
temp = temp + srcData[i];
}
int tempValue;
tempValue = stoi(temp);
*intpart=tempValue;
temp = "0";
for (int i = position; i < srcData.length(); i++)
{
temp = temp + srcData[i];
}
float tempData;
tempData = stof(temp);
*fracpart = tempData;
}
你试试看