免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 6247 | 回复: 26
打印 上一主题 下一主题

此程序如何限制输入字符的类型呀... [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-02-07 22:44 |只看该作者 |倒序浏览
#include<iostream>;
using namespace std;
int main()
{
    &nbsp&nbsp&nbsp&nbspconst int max=2;
    &nbsp&nbsp&nbsp&nbspint no[max];
    &nbsp&nbsp&nbsp&nbspint a,maxno;
    &nbsp&nbsp&nbsp&nbspfor (a=0;a<=max;a++)     &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp                 //将键盘输入的三个将放入整数数组no
        &nbsp&nbsp&nbsp&nbsp{
           &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspcout<<"请输入第"<<a+1<<"个数:";
           &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspcin>;>;no[a];
        &nbsp&nbsp&nbsp&nbsp}
    &nbsp&nbsp&nbsp&nbspfor (a=0,maxno=0;a<=max;a++)            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp   //取出数组no中最大的数,并赋值给maxno
        &nbsp&nbsp&nbsp&nbsp{
           &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspif (no[a]>;maxno)
              &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspmaxno=no[a];
        &nbsp&nbsp&nbsp&nbsp}
    &nbsp&nbsp&nbsp&nbspcout<<"你输入的三个数:";            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp        //输出数组no中的三个数
    &nbsp&nbsp&nbsp&nbspfor (a=0;a<=max;a++)
        &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspcout<<no[a]<<"\t";
    &nbsp&nbsp&nbsp&nbspcout<<"最大者是:"<<maxno<<endl;    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp         //输出最大数maxno的值
    return 0;
}
           
           /***********************************************************/
在从键盘读入数据给数组no时,我该怎么样控制数据类型呢?这个代码从键盘接受整形数字.没有问题...可是只要接受的类型是字符或者其它.运行就乱套了...
哪个大哥哥教教...我想了好久了....因为是自学.又没朋友会..所以只好请大家指教了....

论坛徽章:
0
2 [报告]
发表于 2005-02-10 18:53 |只看该作者

此程序如何限制输入字符的类型呀...

isdigit

论坛徽章:
0
3 [报告]
发表于 2005-02-10 19:56 |只看该作者

此程序如何限制输入字符的类型呀...

具体一点,好吗?我才开始学....
说出来我不能理解没有关系.我会努力去理解的...

论坛徽章:
0
4 [报告]
发表于 2005-02-10 20:25 |只看该作者

此程序如何限制输入字符的类型呀...

没人帮忙吗?我换了好多方法都不行....先将输入的值赋给一个数组,再对数组内的值进行判断.也不行.谁能帮帮我呀....

论坛徽章:
0
5 [报告]
发表于 2005-02-10 20:28 |只看该作者

此程序如何限制输入字符的类型呀...

你要的程序是不是:
要求用户输入N个数,输出其中最大的数
对不对?

论坛徽章:
0
6 [报告]
发表于 2005-02-10 20:52 |只看该作者

此程序如何限制输入字符的类型呀...

[quote]原帖由 "akaaron"]没人帮忙吗?我换了好多方法都不行....先将输入的值赋给一个数组,再对数组内的值进行判断.也不行.谁能帮帮我呀....[/quote 发表:

如果输入一个子母,实际也就是输入了一个数字……
你可以做的就是从标准输入按照字符串的形式读入,然后用isdigit检验~

论坛徽章:
0
7 [报告]
发表于 2005-02-11 04:27 |只看该作者

此程序如何限制输入字符的类型呀...

ISALPHA(3)          Linux Programmer's Manual          ISALPHA(3)

NAME
       isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit - character classification routines

SYNOPSIS
       #include <ctype.h>;

       int isalnum(int c);
       int isalpha(int c);
       int isascii(int c);
       int isblank(int c);
       int iscntrl(int c);
       int isdigit(int c);
       int isgraph(int c);
       int islower(int c);
       int isprint(int c);
       int ispunct(int c);
       int isspace(int c);
       int isupper(int c);
       int isxdigit(int c);

DESCRIPTION
       These  functions  check  whether  c,  which  must have the value of an unsigned char or EOF, falls into a certain character class according to the current
       locale.

       isalnum()
              checks for an alphanumeric character; it is equivalent to (isalpha(c) || isdigit(c)).

       isalpha()
              checks for an alphabetic character; in the standard "C" locale, it is equivalent to (isupper(c) || islower(c)).  In  some  locales,  there  may  be
              additional characters for which isalpha() is true--letters which are neither upper case nor lower case.

       isascii()
              checks  whether  c  is  a  7-bit  unsigned char value that fits into the ASCII character set.  This function is a BSD extension and is also an SVID
              extension.

       isblank()
              checks for a blank character; that is, a space or a tab.  This function is a GNU extension.

       iscntrl()
              checks for a control character.

       isdigit()
              checks for a digit (0 through 9).

       isgraph()
              checks for any printable character except space.

       islower()
              checks for a lower-case character.

       isprint()
              checks for any printable character including space.

       ispunct()
              checks for any printable character which is not a space or an alphanumeric character.

       isspace()
              checks for white-space characters.  In the "C" and "OSIX" locales, these are: space, form-feed ('\f'), newline  ('\n'),  carriage  return  ('\r'),
              horizontal tab ('\t'), and vertical tab ('\v').

       isupper()
              checks for an uppercase letter.

       isxdigit()
              checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F.

RETURN VALUE
       The values returned are nonzero if the character c falls into the tested class, and a zero value if not.

CONFORMING TO
       ANSI - C, BSD 4.3.  isascii() is a BSD extension and is also an SVID extension.  isblank() is a GNU extension.

NOTE
       The details of what characters belong into which class depend on the current locale.  For example, isupper() will not recognize an A - umlaut as an upper-
       case letter in the default C locale.

SEE ALSO
       tolower(3), toupper(3), setlocale(3), ascii(7), locale(7)

GNU                         1995-09-02                 ISALPHA(3)

论坛徽章:
0
8 [报告]
发表于 2005-02-11 07:49 |只看该作者

此程序如何限制输入字符的类型呀...

谢谢...我回去琢磨一下...
虽然对函数的用法还是很模糊.....但知道各个函数的功能.谢谢各位的帮助.,我会查这方面的资料的.....

论坛徽章:
0
9 [报告]
发表于 2005-02-11 07:52 |只看该作者

此程序如何限制输入字符的类型呀...

原型:extern int isascii(int c);
  
  用法:#include <ctype.h>;
  
  功能:判断字符c是否为ascii码
  
  说明:当c为ascii码时,返回非零值,否则返回零。ascii码指0x00-0x7F之间的字符
  
  举例:

      // isascii.c
      
      #include <syslib.h>;
      #include <ctype.h>;

      main()
      {
        char s[]="文曲星-BJGGV";
        int i=12;            // length of string s
        
        clrscr();            // clear screen
        textmode(0xE0);      // make sure LCD mode is 3 big line
        printf("%s\n",s);
        for(i=0;i<12;i++)
        {
          if(isascii(s)) putchar('^');
          else putchar('.');
        }
        getchar();
        return 0;
      }
      
  相关函数:无

论坛徽章:
0
10 [报告]
发表于 2005-02-11 07:53 |只看该作者

此程序如何限制输入字符的类型呀...

原型:extern int isalnum(int c);
  
  用法:#include <ctype.h>;
  
  功能:判断字符c是否为字母或数字
  
  说明:当c为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零。
  
  举例:

      // isalnum.c
      
      #include <syslib.h>;
      #include <ctype.h>;

      main()
      {
        int c;
        
        clrscr();        // clear screen
        c='a';
        printf("%c:%s\n",c,isalnum(c)?"yes":"no";
        c='7';
        printf("%c:%s\n",c,isalnum(c)?"yes":"no";
        c='@';
        printf("%c:%s\n",c,isalnum(c)?"yes":"no";
        getchar();
        return 0;
      }
      
  相关函数:isalpha,isdigit,isxdigit,iscntrl,isgraph,isprint,ispunct,isspace
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP