Problem Description
编写函数atof(char s[])和read_line(char str[], int n),将字符串转换为对应数值的小数。
测试程序为:
#include <stdio.h>
#include <ctype.h>
#define STR_LEN 20
int read_line(char str[], int n); //n为字符串的长度,返回实际读入的字符个数
double atof(char s[]);
int main()
{
char str[STR_LEN + 1];
read_line(str, STR_LEN);
printf("%f\n", atof(str));
return 0;
}
/* 你的代码将被嵌在这里 */
Input Description
在一行中输入一个字符串。
Output Description
输出字符串对应数值的小数。
Sample Input
+123.456
Sample Output
123.456000
答案:
#include <stdio.h>
#include <ctype.h>
#define STR_LEN 20
int read_line(char str[], int n); //n为字符串的长度,返回实际读入的字符个数
double atof(char s[]);
int main()
{
char str[STR_LEN + 1];
read_line(str, STR_LEN);
printf("%f\n", atof(str));
return 0;
}
int read_line(char str[], int n)
{
gets(str);
}
#include <stdio.h>
#include <ctype.h>
#define STR_LEN 20
int read_line(char str[], int n); //n为字符串的长度,返回实际读入的字符个数
double atof(char s[]);
int main()
{
char str[STR_LEN + 1];
read_line(str, STR_LEN);
printf("%f\n", atof(str));
return 0;
}
int read_line(char str[], int n)
{
gets(str);
}