|
|
#include<iostream>
using namespace std;
int main()
{
    const int max=2;
    int no[max];
    int a,maxno;
    for (a=0;a<=max;a++)              //将键盘输入的三个将放入整数数组no
    {
        cout<<"请输入第"<<a+1<<"个数:";
        cin>>no[a];
    }
    for (a=0,maxno=0;a<=max;a++)              //取出数组no中最大的数,并赋值给maxno
    {
        if (no[a]>maxno)
        maxno=no[a];
    }
    cout<<"你输入的三个数:";              //输出数组no中的三个数
    for (a=0;a<=max;a++)
        cout<<no[a]<<"\t";
    cout<<"最大者是:"<<maxno<<endl;              //输出最大数maxno的值
return 0;
}
/***********************************************************/
在从键盘读入数据给数组no时,我该怎么样控制数据类型呢?这个代码从键盘接受整形数字.没有问题...可是只要接受的类型是字符或者其它.运行就乱套了...
哪个大哥哥教教...我想了好久了....因为是自学.又没朋友会..所以只好请大家指教了....
| 默难 回复于:2005-02-10 18:53:00
| isdigit
| | akaaron 回复于:2005-02-10 19:56:59
| 具体一点,好吗?我才开始学....
说出来我不能理解没有关系.我会努力去理解的...
| | akaaron 回复于:2005-02-10 20:25:02
| 没人帮忙吗?我换了好多方法都不行....先将输入的值赋给一个数组,再对数组内的值进行判断.也不行.谁能帮帮我呀....
| | 默难 回复于:2005-02-10 20:28:59
| 你要的程序是不是:
要求用户输入N个数,输出其中最大的数
对不对?
| | 默难 回复于:2005-02-10 20:52:04
| [quote:a05949336a="akaaron"]没人帮忙吗?我换了好多方法都不行....先将输入的值赋给一个数组,再对数组内的值进行判断.也不行.谁能帮帮我呀....[/quote:a05949336a]
如果输入一个子母,实际也就是输入了一个数字……
你可以做的就是从标准输入按照字符串的形式读入,然后用isdigit检验~
| | kert_t8 回复于:2005-02-11 04:27:48
| 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 "POSIX" 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)
| | akaaron 回复于:2005-02-11 07:49:07
| 谢谢...我回去琢磨一下...
虽然对函数的用法还是很模糊.....但知道各个函数的功能.谢谢各位的帮助.,我会查这方面的资料的.....
| | akaaron 回复于:2005-02-11 07:52:30
| 原型: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[i])) putchar('^');
else putchar('.');
}
getchar();
return 0;
}
相关函数:无
| | akaaron 回复于:2005-02-11 07:53:41
| 原型: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
| | akaaron 回复于:2005-02-11 07:54:39
| 原型:extern int isalpha(int c);
用法:#include <ctype.h>
功能:判断字符c是否为英文字母
说明:当c为英文字母a-z或A-Z时,返回非零值,否则返回零。
举例:
// isalpha.c
#include <syslib.h>
#include <ctype.h>
#include <stdio.h>
main()
{
int c;
clrscr(); // clear screen
printf("Press a key");
for(;;)
{
c=getchar();
clrscr();
printf("%c: %s letter",c,isalpha(c)?"is":"not");
}
return 0; // just to avoid warnings by compiler
}
相关函数:isalnum,isdigit,isxdigit,iscntrl,isgraph,isprint,ispunct,isspace
| | akaaron 回复于:2005-02-11 07:55:02
| 原型:extern int iscntrl(int c);
用法:#include <ctype.h>
功能:判断字符c是否为控制字符
说明:当c在0x00-0x1F之间或等于0x7F(DEL)时,返回非零值,否则返回零。
举例:
// iscntrl.c
#include <syslib.h>
#include <ctype.h>
main()
{
int c;
clrscr(); // clear screen
c='a';
printf("%x:%s\n",c,iscntrl(c)?"yes":"no");
c=0x0d;
printf("%x:%s\n",c,iscntrl(c)?"yes":"no");
c=0x7f;
printf("%x:%s\n",c,iscntrl(c)?"yes":"no");
getchar();
return 0;
}
相关函数:isalnum,isalpha,isdigit,isxdigit,isgraph,isprint,ispunct,isspace
| | akaaron 回复于:2005-02-11 07:55:45
| 原型:extern int isdigit(int c);
用法:#include <ctype.h>
功能:判断字符c是否为数字
说明:当c为数字0-9时,返回非零值,否则返回零。
举例:
// isdigit.c
#include <syslib.h>
#include <ctype.h>
main()
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isdigit(c)?"yes":"no");
c='9';
printf("%c:%s\n",c,isdigit(c)?"yes":"no");
c='*';
printf("%c:%s\n",c,isdigit(c)?"yes":"no");
getchar();
return 0;
}
相关函数:isalnum,isalpha,isxdigit,iscntrl,isgraph,isprint,ispunct,isspace
| | akaaron 回复于:2005-02-11 07:56:14
| 原型:extern int islower(int c);
用法:#include <ctype.h>
功能:判断字符c是否为小写英文字母
说明:当c为小写英文字母(a-z)时,返回非零值,否则返回零。
举例:
// islower.c
#include <syslib.h>
#include <ctype.h>
main()
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,islower(c)?"yes":"no");
c='A';
printf("%c:%s\n",c,islower(c)?"yes":"no");
c='7';
printf("%c:%s\n",c,islower(c)?"yes":"no");
getchar();
return 0;
}
相关函数:isupper
| | akaaron 回复于:2005-02-11 07:56:44
| 原型:extern int isgraph(int c);
用法:#include <ctype.h>
功能:判断字符c是否为除空格外的可打印字符
说明:当c为可打印字符(0x21-0x7e)时,返回非零值,否则返回零。
举例:
// isgraph.c
#include <syslib.h>
#include <ctype.h>
main()
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isgraph(c)?"yes":"no");
c=' '; // 0x20
printf("%c:%s\n",c,isgraph(c)?"yes":"no");
c=0x7f;
printf("%c:%s\n",c,isgraph(c)?"yes":"no");
getchar();
return 0;
}
相关函数:isprint
| | akaaron 回复于:2005-02-11 07:57:36
| 原型:extern int isprint(int c);
用法:#include <ctype.h>
功能:判断字符c是否为可打印字符(含空格)
说明:当c为可打印字符(0x20-0x7e)时,返回非零值,否则返回零。
举例:
// isprint.c
#include <syslib.h>
#include <ctype.h>
main()
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isprint(c)?"yes":"no");
c=' ';
printf("%c:%s\n",c,isprint(c)?"yes":"no");
c=0x7f;
printf("%c:%s\n",c,isprint(c)?"yes":"no");
getchar();
return 0;
}
相关函数:isgraph
| | akaaron 回复于:2005-02-11 07:57:53
| 原型:extern int ispunct(int c);
用法:#include <ctype.h>
功能:判断字符c是否为标点符号
说明:当c为标点符号时,返回非零值,否则返回零。
标点符号指那些既不是字母数字,也不是空格的可打印字符。
举例:
// ispunct.c
#include <syslib.h>
#include <ctype.h>
#include <string.h>
main()
{
char s[]="Hello, Rain!";
int i;
clrscr(); // clear screen
printf("%s\n",s);
for(i=0;i<strlen(s);i++)
{
if(ispunct(s[i])) printf("^");
else printf(".");
}
getchar();
return 0;
}
相关函数:isalnum,isalpha,isdigit,isxdigit,iscntrl,isgraph,isprint,isspace
| | akaaron 回复于:2005-02-11 07:58:30
| 原型:extern int isspace(int c);
用法:#include <ctype.h>
功能:判断字符c是否为空白符
说明:当c为空白符时,返回非零值,否则返回零。
空白符指空格、水平制表、垂直制表、换页、回车和换行符。
举例:
// isspace.c
#include <syslib.h>
#include <ctype.h>
main()
{
char s[]="Test Line 1\tend\nTest Line 2\r";
int i;
clrscr(); // clear screen
for(i=0;i<strlen(s);i++)
{
if(isspace(s[i])) putchar('.');
else putchar(s[i]);
}
getchar();
return 0;
}
相关函数:isalnum,isalpha,isdigit,isxdigit,iscntrl,isgraph,isprint,ispunct
| | akaaron 回复于:2005-02-11 07:58:50
| 原型:extern int isupper(int c);
用法:#include <ctype.h>
功能:判断字符c是否为大写英文字母
说明:当c为大写英文字母(A-Z)时,返回非零值,否则返回零。
举例:
// isupper.c
#include <syslib.h>
#include <ctype.h>
main()
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isupper(c)?"yes":"no");
c='A';
printf("%c:%s\n",c,isupper(c)?"yes":"no");
c='7';
printf("%c:%s\n",c,isupper(c)?"yes":"no");
getchar();
return 0;
}
相关函数:islower
| | akaaron 回复于:2005-02-11 07:59:19
| 原型:extern int isxdigit(int c);
用法:#include <ctype.h>
功能:判断字符c是否为十六进制数字
说明:当c为A-F,a-f或0-9之间的十六进制数字时,返回非零值,否则返回零。
举例:
// isxdigit.c
#include <syslib.h>
#include <ctype.h>
main()
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isxdigit(c)?"yes":"no");
c='9';
printf("%c:%s\n",c,isxdigit(c)?"yes":"no");
c='*';
printf("%c:%s\n",c,isxdigit(c)?"yes":"no");
getchar();
return 0;
}
相关函数:isalnum,isalpha,isdigit,iscntrl,isgraph,isprint,ispunct,isspace
| | akaaron 回复于:2005-02-11 08:00:02
| 原型:extern int toascii(int c);
用法:#include <ctype.h>
功能:将字符c转换为ascii码
说明:toascii函数将字符c的高位清零,仅保留低七位。返回转换后的数值。
举例:
// toascii.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++)
{
putchar(toascii(s[i]));
}
getchar();
return 0;
}
相关函数:无
| | akaaron 回复于:2005-02-11 08:00:23
| 原型:extern int tolower(int c);
用法:#include <ctype.h>
功能:将字符c转换为小写英文字母
说明:如果c为大写英文字母,则返回对应的小写字母;否则返回原来的值。
举例:
// tolower.c
#include <syslib.h>
#include <ctype.h>
main()
{
char *s="Hello, World!";
int i;
clrscr(); // clear screen
printf("%s\n",s);
for(i=0;i<strlen(s);i++)
{
putchar(tolower(s[i]));
}
getchar();
return 0;
}
相关函数:toupper
| | akaaron 回复于:2005-02-11 08:00:40
| 原型:extern int toupper(int c);
用法:#include <ctype.h>
功能:将字符c转换为大写英文字母
说明:如果c为小写英文字母,则返回对应的大写字母;否则返回原来的值。
举例:
// toupper.c
#include <syslib.h>
#include <ctype.h>
main()
{
char *s="Hello, World!";
int i;
clrscr(); // clear screen
printf("%s\n",s);
for(i=0;i<strlen(s);i++)
{
putchar(toupper(s[i]));
}
getchar();
return 0;
}
相关函数:tolower
| | akaaron 回复于:2005-02-11 08:01:14
| 我把我找到的资料贴出来了...方便大家查询......谢谢各位的帮助...
| | sinboy2002 回复于:2005-02-28 22:54:23
| 辛苦akaaron兄了
| | Benson_linux 回复于:2005-04-06 00:32:10
| :shock:
提供一个我自己编的函数来控制输入的类型:
int P_p(char get[20])
{
int a, b;
int back = 0;
int i,j = 0;
int n[20];
for (i = 0; get[i] != '\0'; i++)
{
if ((a = (get[i]-48>=0))&&(b = (get[i]-48<=9)))
{
n[j++] = (get[i]-48);
}
else
{
return (0);
}
}
n[j] = -1;
j = 0;
while (n[j] != -1)
{
back = back*10+n[j];
j++;
}
return back;
}
让你主函数中的scanf函数这样写:scanf("%s", p);
char *p = (char *)malloc(20);
然后调用在main()中这样调用:
scanf("%s",p);
while ( !(choice = P_p(p)))
{
printf("/***Error***/Enter error!\n");
printf("What do you want to do?");
scanf("%s",p);
}
| | flyer_8601 回复于:2005-04-06 14:29:32
| 程序本身就不正确,const int max=2;
int no[max];
你定义的no数组长度只有2,你却往里面放3个数
| |
|