Arduino函数库03.Arduino 字符函数

新手入门是每个应用程序最重要的元素之一。很多时候,它的难易程度决定了用户是否会继续使用应用程序。在用户界面上,新手入门不仅仅是简单的5个介绍软件的页面或说明性的工具提示内容。下面我会使用最有趣的例子来描述新手入门过程的设计原则。

所有数据都以字符形式输入计算机,包括字母,数字和各种特殊符号。在本章节中,我们讨论C++检查和操作单个字符的功能。

字符处理库包括几个函数,执行有用的测试和字符数据的操作。每个函数接收一个字符,表示为int或EOF作为参数。字符通常作为整数操作。

记住,EOF通常具有值-1,而一些硬件架构不允许负值存储在char变量中。因此,字符处理函数将字符作为整数来操作。

下表总结了字符处理库的函数。使用字符处理库中的函数时,请包含<cctype>标题。

序号原型和描述
1int isdigit(int c)
如果c是数字,则返回1,否则返回0。
2int isalpha(int c)
如果c是字母,则返回1,否则返回0。
3int isalnum(int c)
如果c是数字或字母,则返回1,否则返回0。
4int isxdigit(int c)
如果c是十六进制数字字符,则返回1,否则返回0。
5int islower(int c)
如果c是小写字母,则返回1,否则返回0。
6int isupper(int c)
如果c是大写字母,则返回1;否则返回0。
7int isspace(int c)
如果c是空白字符:换行符(’\n’)、空格符(’ ‘)、换页符(’\f’)、回车符(’\r’)、水平制表符(’\t’)或垂直制表符(’\v’),则返回1,否则返回0。
8int iscntrl(int c)
如果c是控制字符,如换行符(’\n’)、换页符(’\f’)、回车符(’\r’)、水平制表符 (’\t’)、垂直制表符(’\v’)、alert(’\a’)或退格(’\b’),则返回1,否则返回0。
9int ispunct(int c)
如果c是除空格,数字或字母以外的打印字符,则返回1,否则返回0。
10int isprint(int c)
如果c是包含空格(’ ‘)的打印字符,则返回1,否则返回0。
11int isgraph(int c)
如果c是除空格(’ ‘)之外的打印字符,则返回1,否则返回0。

例子

以下示例演示如何使用函数 isdigit,isalpha,isalnum  isxdigit 。函数 isdigit 确定其参数是否为数字(0-9)。函数 isalpha 确定其参数是大写字母(A-Z)还是小写字母(a-z)。函数 isalnum 确定其参数是大写,小写字母还是数字。函数 isxdigit 确定其参数是否为十六进制数字(A-F,a-f,0-9)。

例1

void setup () {
   Serial.begin (9600);
   Serial.print ("According to isdigit:\r");
   Serial.print (isdigit( '8' ) ? "8 is a": "8 is not a");
   Serial.print (" digit\r" );
   Serial.print (isdigit( '8' ) ?"# is a": "# is not a") ;
   Serial.print (" digit\r");
   Serial.print ("\rAccording to isalpha:\r" );
   Serial.print (isalpha('A' ) ?"A is a": "A is not a");
   Serial.print (" letter\r");
   Serial.print (isalpha('A' ) ?"b is a": "b is not a");
   Serial.print (" letter\r");
   Serial.print (isalpha('A') ?"& is a": "& is not a");
   Serial.print (" letter\r");
   Serial.print (isalpha( 'A' ) ?"4 is a":"4 is not a");
   Serial.print (" letter\r");
   Serial.print ("\rAccording to isalnum:\r");
   Serial.print (isalnum( 'A' ) ?"A is a" : "A is not a" );

   Serial.print (" digit or a letter\r" );
   Serial.print (isalnum( '8' ) ?"8 is a" : "8 is not a" ) ;
   Serial.print (" digit or a letter\r");
   Serial.print (isalnum( '#' ) ?"# is a" : "# is not a" );
   Serial.print (" digit or a letter\r");
   Serial.print ("\rAccording to isxdigit:\r");
   Serial.print (isxdigit( 'F' ) ?"F is a" : "F is not a" );
   Serial.print (" hexadecimal digit\r" );
   Serial.print (isxdigit( 'J' ) ?"J is a" : "J is not a" ) ;
   Serial.print (" hexadecimal digit\r" );
   Serial.print (isxdigit( '7' ) ?"7 is a" : "7 is not a" ) ;

   Serial.print (" hexadecimal digit\r" );
   Serial.print (isxdigit( '$' ) ? "$ is a" : "$ is not a" );
   Serial.print (" hexadecimal digit\r" );
   Serial.print (isxdigit( 'f' ) ? “f is a" : "f is not a");
   
}

void loop () {

}

结果

According to isdigit:
8 is a digit
# is not a digit
According to isalpha:
A is a letter
b is a letter
& is not a letter
4 is not a letter
According to isalnum:
A is a digit or a letter

8 is a digit or a letter
# is not a digit or a letter
According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit

$ is not a hexadecimal digit
f is a hexadecimal digit

我们对每个函数使用条件运算符(?:)来确定字符串“is a”或字符串“is not a”是否应该打印在每个测试字符的输出中。例如,行a表示如果“8”是数字,即如果isdigit返回真(非零)值,则打印字符串“8 is a”。如果“8”不是数字(即,如果isdigit返回0),则打印字符串“8 is not a”。

例2

以下示例演示了函数 islower  isupper 的使用。函数 islower 确定其参数是否为小写字母(a-z)。函数 isupper 确定其参数是否为大写字母(A-Z)。

int thisChar = 0xA0;

void setup () {
   Serial.begin (9600);
   Serial.print ("According to islower:\r") ;
   Serial.print (islower( 'p' ) ? "p is a" : "p is not a" );
   Serial.print ( " lowercase letter\r" );
   Serial.print ( islower( 'P') ? "P is a" : "P is not a") ;
   Serial.print ("lowercase letter\r");
   Serial.print (islower( '5' ) ? "5 is a" : "5 is not a" );
   Serial.print ( " lowercase letter\r" );
   Serial.print ( islower( '!' )? "! is a" : "! is not a") ;
   Serial.print ("lowercase letter\r");

   Serial.print ("\rAccording to isupper:\r") ;
   Serial.print (isupper ( 'D' ) ? "D is a" : "D is not an" );
   Serial.print ( " uppercase letter\r" );
   Serial.print ( isupper ( 'd' )? "d is a" : "d is not an") ;
   Serial.print ( " uppercase letter\r" );
   Serial.print (isupper ( '8' ) ? "8 is a" : "8 is not an" );
   Serial.print ( " uppercase letter\r" );
   Serial.print ( islower( '$' )? "$ is a" : "$ is not an") ;
   Serial.print ("uppercase letter\r ");
}

void setup () {

}

结果

According to islower:
p is a lowercase letter
P is not a lowercase letter
5 is not a lowercase letter
! is not a lowercase letter

According to isupper:
D is an uppercase letter
d is not an uppercase letter
8 is not an uppercase letter
$ is not an uppercase letter

例3

以下示例演示如何使用函数 isspace,iscntrl,ispunct,isprint  isgraph 

  • 函数 isspace 确定其参数是否为空白字符,例如空格(’ ‘),换页符(’\f’),换行符(’\n’),回车符(’\r’),水平制表符(’\t’)或垂直制表符(’\v’)。
  • 函数 iscntrl 确定其参数是否为控制字符,如水平制表符(’\t’),垂直制表符(’\v’),换页符(’\f’),alert(’\a’),退格符(’\b’),回车符(’\r’)或换行符(’\n’)。
  • 函数 ispunct 确定其参数是否是除空格,数字或字母以外的打印字符(例如$,#,(,),[,],{,},;,:或%)。
  • 函数 isprint 确定其参数是否为可以在屏幕上显示的字符(包括空格字符)。
  • 函数 isgraph 测试与isprint相同的字符,但不包括空格字符。
void setup () {
   Serial.begin (9600);
   Serial.print ( " According to isspace:\rNewline ") ;
   Serial.print (isspace( '\n' )? " is a" : " is not a" );
   Serial.print ( " whitespace character\rHorizontal tab") ;
   Serial.print (isspace( '\t' )? " is a" : " is not a" );
   Serial.print ( " whitespace character\n") ;
   Serial.print (isspace('%')? " % is a" : " % is not a" );
   
   Serial.print ( " \rAccording to iscntrl:\rNewline") ;
   Serial.print ( iscntrl( '\n' )?"is a" : " is not a" ) ;
   Serial.print (" control character\r");
   Serial.print (iscntrl( '$' ) ? " $ is a" : " $ is not a" );
   Serial.print (" control character\r");
   Serial.print ("\rAccording to ispunct:\r");
   Serial.print (ispunct(';' ) ?"; is a" : "; is not a" ) ;
   Serial.print (" punctuation character\r");
   Serial.print (ispunct('Y' ) ?"Y is a" : "Y is not a" ) ;
   Serial.print ("punctuation character\r");
   Serial.print (ispunct('#' ) ?"# is a" : "# is not a" ) ;
   Serial.print ("punctuation character\r");

   Serial.print ( "\r According to isprint:\r");
   Serial.print (isprint('$' ) ?"$ is a" : "$ is not a" );
   Serial.print (" printing character\rAlert ");
   Serial.print (isprint('\a' ) ?" is a" : " is not a" );
   Serial.print (" printing character\rSpace ");
   Serial.print (isprint(' ' ) ?" is a" : " is not a" );
   Serial.print (" printing character\r");
   
   Serial.print ("\r According to isgraph:\r");
   Serial.print (isgraph ('Q' ) ?"Q is a" : "Q is not a" );
   Serial.print ("printing character other than a space\rSpace ");
   Serial.print (isgraph (' ') ?" is a" : " is not a" );
   Serial.print ("printing character other than a space ");
}

void loop () {

}

结果

According to isspace:
Newline is a whitespace character
Horizontal tab is a whitespace character
% is not a whitespace character
According to iscntrl:
Newline is a control character
$ is not a control character
According to ispunct:
; is a punctuation character
Y is not a punctuation character
# is a punctuation character
According to isprint:
$ is a printing character
Alert is not a printing character
Space is a printing character
According to isgraph:
Q is a printing character other than a space
Space is not a printing character other than a space

发布者:suiyublg,转转请注明出处:https://huibian.net/1023-2/

(36)
suiyublg的头像suiyublg
上一篇 2017年4月1日 17:27
下一篇 2017年4月3日 11:00

相关推荐

  • Arduino教程23.Arduino switch case语句

    类似于if语句, switch … case 通过允许程序员指定应在各种条件下执行的不同代码来控制程序的流程。特别是, switch 语句将变量的值与 case 语句中指定的值进行比较。当发现一个case语句的值与变量的值匹配时,运行case语句中的代码。 switch语句使用&nbsp…

    2017年3月24日
    41000
  • Arduino教程14.Arduino 数组

    数据已然成为企业管理不可缺少的因素,过去企业在用的更多是ERP、CRM之类的内部数据,其实外部数数组是连续的一组相同类型的内存位置。要引用数组中的特定位置或元素,我们指定数组的名称和数组中特定元素的位置编号。 下图给出了一个名为C的整数数组,它包含11个元素。通过给出数组名称,后面跟特定元素的位置编号:方括号([]),你可以引用这些元素中的任何一个。位置编号…

    2017年3月30日
    52001
  • Arduino传感器01.Arduino 湿度传感器

    在本节中,我们将学习如何使用不同的传感器连接我们的Arduino板。我们将讨论以下传感器: 湿度传感器(DHT22) DHT-22(也称为AM2302)是一个数字输出,相对湿度和温度传感器。它使用电容式湿度传感器和热敏电阻来测量周围空气,并在数据引脚上发送数字信号。 在本例中,你将学习如何将此传感器与Arduino UNO一起使用。室温和湿度将打印到串口监视…

    2017年4月20日
    51500
  • Arduino函数库06.Arduino pinMode()

    描述 将指定的引脚配置为输入或输出。有关引脚功能的详细信息,请参阅数字引脚的说明。从Arduino 1.0.1开始,可以使用INPUT_PULLUP模式启用内部上拉电阻。此外,INPUT模式显式禁止内部上拉。 语法 pinMode(pin, mode) 参数 pin:你希望设置模式的引脚的编号mode:INPUT,OUTPUT或INPUT_PULLUP。(有…

    2017年3月31日
    53700
  • Arduino项目06.Arduino 键盘消息

    在此示例中,当按下按钮时,文本字符串作为键盘输入发送到计算机。字符串报告按钮被按下的次数。一旦你完成了Leonardo版的程序化和接线,打开你最喜欢的文本编辑器来查看结果。 警告 – 当你使用 Keyboard.print()命令时,Arduino将接管你的计算机键盘。为确保在使用此功能运行草图时不会失去对计算机的控制,请在调…

    2017年4月17日
    56400

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
欢迎大家来到大雄学编程!