本文介绍了如何防止用户在语言C中输入超过2个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,由于超过2.dp,这是不允许2.00000,但这是2.00
我有一个代码,它打破了价值,只有.000或.00取决于数字。而我正在考虑创造一个条件,如果它大于.99。
但它不起作用
我尝试过:
int Substring(char string [])
{
int i; int lengt; int index;
// char string [5],
char sub [1000];
int position,length,c = 0;
// printf(输入字符串\ n);
//得到(字符串);
printf(%s,string);
lengt = strlen(string);
for(i = 1; i< lengt; i ++)
{
// if(。== string)
// {
// printf(错误请用dot \\\
重新输入你的值);
//休息;
//}
if('。'== string [i])
{
index = i;
printf(%d:%d \ n,index,lengt);
//休息;
}
}
// printf(%d,lengt);
position = index;
length = lengt;
// printf(输入子串的位置和长度\ n);
// scanf(%d%d,& position,& length);
while(c< length){
sub [c] = string [position + c];
c ++;
}
sub [c] ='\'';
printf(必需的子串是\%s \\ n,sub);
system(暂停);
返回0;
}
解决方案
For example this is not allowed 2.00000 due to its over 2.d.p but this is 2.00
I have a code below that breaks the value and only how .000 or .00 depends on the number. And i was thinking of creating a condition such as if its greater than .99.
But it doesnt work
What I have tried:
int Substring(char string[]) { int i; int lengt; int index; //char string[5], char sub[1000]; int position, length, c = 0; //printf("Input a string\n"); //gets(string); printf("%s",string); lengt=strlen(string); for(i=1;i<lengt;i++) { // if("."==string) // { //printf("error please re-type your value with a dot\n"); //break; //} if('.'==string[i]) { index=i; printf("%d : %d \n",index,lengt); //break; } } //printf("%d",lengt); position=index; length=lengt; //printf("Enter the position and length of substring\n"); //scanf("%d%d", &position, &length); while (c < length) { sub[c] = string[position+c]; c++; } sub[c] = '\0'; printf("Required substring is \"%s\"\n", sub); system("pause"); return 0; }
解决方案
这篇关于如何防止用户在语言C中输入超过2个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!