C++初学(17)

17.1、字符函数库cctype

C++有一个与字符相关的、非常方便的函数软件包,它可以简化如确定字符是否为大写字母、数字或标点符号等工作。这些函数的原型是在头文件cctype中定义的。如果ch是一个字母,则isalpha(ch)函数返回一个非零值,否则返回0。

使用这些函数要比使用AND或者OR运算符更方便,例如:

if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))

这句话等价于这句:

if(isalpha(ch))

是不是更通用,更方便。

下面这个程序演示一些ctype库函数。isalpha()来检查字符是否为字母字符;isdigit()来测试字符是否为数字字符;isspace()来测试字符是否为空白,如换行符、空格;ispunct()来测试字符是否为标点符号,顺便复习一下if else结构:

#include <iostream>
#include<cctype>
int main()
{
	using namespace std;
	cout << "Enter text for analysis,and type @"
		<< "to terminate input." << endl;
	char ch;
	int whitespace = 0;
	int digits = 0;
	int chars = 0;
	int punct = 0;
	int others = 0;

	cin.get(ch);
	while (ch != '@')
	{
		if (isalpha(ch))
			chars++;
		else if (isspace(ch))
			whitespace++;
		else if (isdigit(ch))
			digits++;
		else if (ispunct(ch))
			punct++;
		else
			others++;
		cin.get(ch);
	}
	cout << chars << " letters, "
		<< whitespace << " whitespace, "
		<< digits << " digits, "
		<< punct << " punctuations,"
		<<  others << " others." << endl;
	   
	return 0;
}

下面对cctype软件包的函数进行总结:

17.2、?:运算符

C++有一个常用来代替if else语句的运算符,这个运算符被称为条件运算符(?:),它是C++中唯一一个需要3个操作数的运算符。该运算符的通用格式如下:

expression1 ? expression2 : expression3 

如果expression1为true,则整个条件表达式的值为expression2的值,否则为expression3的值。

#include<iostream>
int main()
{
	using namespace std;
	int a, b;
	cout << "Enter two integers: ";
	cin >> a >> b;
	cout << "The larger of " << a << " and " << b;
	int c = a > b ? a : b;
	cout << " is " << c << endl;
	return 0;
}

与if else相比,条件运算符更简洁(当然如果条件很复杂还用if else吧),条件运算符生成一个表达式,因此是一个值,可以将其赋给变量或将其放到一个更大的表达式中。

17.3、switch语句

如果需要从几个项目中选择一个,使用if else可能很麻烦,而switch语句能够很容易的在大型列表进行选择。下面是switch的通用格式:

switch (integer-expression)
{
    case label1 : statement(s)
    case label2 : statement(s)
    …
    defalut : statement(s)
}

C++的switch语句就像路牌,告诉计算机应执行哪行代码。执行到switch语句时,程序将跳到integer-expression的指标记的那一行,如果integer-expression的值为4,则程序将执行标签为case  4:那一行。注意,integer-expression必须是一个结果为整数值的表达式,每个标签也必须时整数常量表达式。如果integer-expression不与任何标签匹配,则程序将跳到标签为default的那一行。如果没有default标签,将跳过switch后面的语句执行。

C++的case标签仅仅是行标签,而不是选项之间的界线。也就是说,程序跳到switch中特定代码行后,将以此执行之后的所有语句,除了break语句(),都会让程序执行完一组特定语句后停止。

下面有一段超绝长的程序:

#include <iostream>
using namespace std;
void showmenu();
void report();
void comfort();
int main()
{ 
	showmenu();
	int choice;
	cin >> choice;
	while (choice != 5)
	{
		switch (choice)
		{
		case 1:cout << "\a\n";
			break;
		case 2:report();
			break;
		case 3:"The boss was in all day.";
			break;
		case 4:comfort();
			break;
		default:cout << "That's not a choice." << endl;
		}
		showmenu();
		cin >> choice;
	}
	cout << "Bye!" << endl;
	return 0;
}
void showmenu()
{
	cout << "Please enter 1,2,3,4 or 5: " << endl
		<< " 1) alarm                 2) report" << endl
		<< " 3) alibi                 4) comfort" << endl
		<< " 5) quit" << endl;
}
void report()
{
	cout << "It's been an excellent week for bussiness." << endl
		<< "Sales are up 120%.Expenses are down 35%." << endl;
}
void comfort()
{
	cout << "Your employees think you are the finest CEO" << endl
		<< "in the industry.The board of directors think " << endl
		<< "you are the finest CEO in the industry." << endl;
}

注:为了让程序正常运行,输入必须是整数,如果你输入一个字母,输入语句将会失效,导致程序陷入死循环。该程序需要break语句来确保只执行switch语句中的特定部分。

switch语句和if else语句都允许程序从选项中进行选择。相比之下,if else更通用,它能处理取值范围;但是switch无法处理浮点测试,因为它的case标签必须是整数,标签值必须是常数。当然如果是选择多个选项,switch还是优先于if else语句。

14.4、break和continue语句

break和continue语句都使程序能够跳过部分代码。break语句是直接结束整个循环,无论后面还有进行几次;而continue语句则是读到它时跳过当前批次的循环,进入下一个循环。

#include <iostream>
const int ArSize = 80;
int main()
{
	using namespace std;
	char line[ArSize];
	int spaces = 0;

	cout << "Enter a line of text:" << endl;
	cin.get(line,ArSize);
	cout << "Complete line: " << endl << line << endl;
	cout << "Line through first period: " << endl;
	for (int i = 0; line[i] != '\0'; i++)
	{
		cout << line[i];
		if (line[i] == '.')
			break;
		if (line[i] != ' ')
			continue;
		spaces++;	
	}
	cout << endl << spaces << " spaces" << endl;
	cout << "Done!" << endl;
	return 0;
}

虽然continue语句导致该程序跳过循环体的剩余部分,但不会跳过循环体的更新表达式。

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值