c语言 写一个程序,取一位有效数字

对一个输入的数字取一位有效数字,且只进不舍(即若第一个有效数字后有非0数字,第一个有效数字+1)。
例如,输入一个小数,0.0123,则经过处理后,得0.02.这个小数的位是不确定的。能实现吗?

第1个回答  2012-04-23
不太清楚你的问题,在这里帮你编了个要求只能输入两位数的程序,然后判断其中的有效数据。不知道和你提的问题有没有搭边。呵呵...
#include<stdio.h>
main()
{int x,y=0;
printf("Please input x:"); /*请输入两位数据x的值*/
scanf("%d",&x); /*输入两位的数据*/
if(x%10==0) /*判断个位上的数是否为0*/
y=x/10; /*如果个位上的数据为0,有效数字为十位上的数字*/
else
y=x/10+1;/*否则有效数字为十位上的数字加1*/
printf("%d",y);
}
第2个回答  2012-04-23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

float getRMS(char *ch) {
int len = strlen(ch), index = 0;
float temp = 0;
for (; index < len; index++) {
if (ch[index] == '.')
break;
}
for (int i = 0; i < len; i++) {
if (ch[i] == '.')
continue;
if (atoi(&ch[0]) == 0) {
temp = (float)ch[i] - 48;
if (temp != 0) {
if (atoi(&ch[i + 1]) != 0) {
temp++;
}
} else {
continue;
}
return temp * pow(0.1, i - index);
} else {
if (ch[1] == '.') {
i = 1;
} else {
i = 0;
}
char c1 = ch[1 + i];
if (atoi(&c1) != 0) {
temp = (float)ch[0] - 47;
} else {
temp = (float)ch[0] - 48;
}
return temp * pow(10, index - 1);
}
}
return -1;
}

int main() {
float RMS = 0;
char *ch = new char;
printf("请输入数字:");
gets(ch);
RMS = getRMS(ch);
if(RMS == -1){
printf("计算出错");
}else{
printf("%s%f","有效数字为:\n",RMS);
}
system("PAUSE");
return 0;
}
支持小数计算
哥啊,我都已经实现了,给分吧!

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网