如何防止用户在语言C中输入超过2个小数位 [英] How do I prevent users from entering more than 2 decimal places in language C

查看:71
本文介绍了如何防止用户在语言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;
}

解决方案

使用 strchr [ ^ ]找到小数点, strlen 获取后面的位数。

这是你的作业,所以没有代码!

但这是一个简单的任务:你只需要四个信息:

1 )字符串中的字符数。

2)字符串中的点数。

3)最后一个点的位置。

4)如果除了数字之外还有其他数据和点。



如果(4)为真,或2)大于1,那就不好了。

否则,减去3 1,如果它大于3,则不好。


函数 dec2 就地更改输入字符串。提供测试程序。

  #include   <   stdio.h  >  

char * dec2( char * s)
{
char * p = s;
int decpoint = - 1 ;

while (* p&& decpoint< 2
{
if (decpoint> = 0
++ decpoint;
if (* p == ' 。 '
{
decpoint = 0 ;
}
else if (* p< ' 0' || * p> ' 9'
{
return s; // 输入数字错误
}
++ p;
}
* p = ' \0';
return s;
}

int main()
{
char a [] = 12.02;
char b [] = 。123456789 ;
char c [] = 12567.0 ;
char d [] = 123.456 ;
char e [] = 12。 F4\" ;

printf( in'%s',a);
dec2(a);
printf( ,out'%s'\ n,a);

printf( in'%s',b);
dec2(b);
printf( ,out'%s'\ n,b);

printf( in'%s',c);
dec2(c);
printf( ,out'%s'\ n,c);

printf( in'%s',d);
dec2(d);
printf( ,out'%s'\ n,d);

printf( in'%s',e);
dec2(e);
printf( ,out'%s'\ n,e);

return 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;
}

解决方案

Use strchr[^] to find the decimal point and strlen to get the number of digits after it.


This is your homework, so no code!
But this is a simple task: all you need is four pieces of info:
1) Number of characters in the string.
2) Number of dots in the string.
3) Position of the last dot.
4) If there is anything other than digits and dots.

If (4) is true, or 2) is greater than 1, it's bad.
Otherwise, subtract 3 from 1 and if it's greater than 3, it's bad.


The function dec2 changes the input string in-place. A test program is provided.

#include <stdio.h>

char * dec2(char * s)
{
  char * p = s;
  int decpoint = -1;

  while ( *p && decpoint < 2)
  {
    if ( decpoint >= 0)
      ++decpoint;
    if ( *p == '.')
    {
      decpoint = 0;
    }
    else if (*p < '0' || *p > '9')
    {
      return s; // error in input number
    }
    ++p;
  }
  *p = '\0';
  return s;
}

int main()
{
  char a[] = "12.02";
  char b[] = ".123456789";
  char c[] = "12567.0";
  char d[] = "123.456";
  char e[] = "12.F4";

  printf("in '%s'", a);
  dec2(a);
  printf(", out '%s'\n", a);

  printf("in '%s'", b);
  dec2(b);
  printf(", out '%s'\n", b);

  printf("in '%s'", c);
  dec2(c);
  printf(", out '%s'\n", c);

  printf("in '%s'", d);
  dec2(d);
  printf(", out '%s'\n", d);

  printf("in '%s'", e);
  dec2(e);
  printf(", out '%s'\n", e);

  return 0;
}


这篇关于如何防止用户在语言C中输入超过2个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆