字符串:You will pay $687.71 every month
请问如何如何输出687.71的浮点数?
字符串:You will pay $687.71 every month
请问如何如何输出687.71的浮点数?
参考代码如下,将你的字符串输入到窗口即可输出:
#include <cstdlib>
#include <math.h>
#include <stdio.h>
int main()
{
char str[1000];
gets(str);
char num[100] = {0};
int i = 0;
for (; str[i] != '\0'; i++)
{
if (str[i] == '$')
{
int j = i + 1;
int k = 0;
for (; str[j] != '\0'; j++)
{
if (str[j] != ' ')
{
num[k++] = str[j];
}
else
{
num[k] = '\0';
break;
}
}
}
}
double dnum = 0.0;
i = 0;
int j = 0;
for ( i = 0; num[i] != '\0'; i++)
{
if (num[i] == '.')
{
j = i;
}
}
i = j - 1;
int k = 0;
for (; i >= 0; i--)
{
dnum += (num[i] - '0') * pow(10, k);
k++;
}
i = j + 1;
k = 1;
for (; num[i] != '\0'; i++)
{
dnum += (num[i] - '0') / pow(10, k);
k++;
}
printf("%f", dnum);
return 0;
}