第六章 分支语句和逻辑运算符

本文介绍了C++中的逻辑or(&&)、and(||)运算符的优先级,字符函数库cctype的应用,条件运算符(??)的用法,switch语句的选择与枚举类型处理,以及错误输入处理和文件I/O。实例展示了如何处理字符串、菜单选择和税务计算。
摘要由CSDN通过智能技术生成

1、逻辑表达式
or(||)、and(&&)、not(!);
在c++中,逻辑or和逻辑and运算符都低于关系运算符。
还有,!运算符的优先级高于所有的关系运算符和算数运算符。
逻辑and 运算符的优先级高于逻辑or运算符。
2、字符函数库cctype
如果ch是一个字母,则isalpha(ch)函数返回一个非零值,否则返回0。同样,如果ch是标点符号(如逗号或句号),函数ispunct(ch)将返回true。(这些函数的返回类型是int,而不是bool,但通常bool转换让我们能够将他们视为bool类型。)
请添加图片描述
3、?:运算符(也就是条件运算符)
常被用来代替if else 语句的运算符,它是c++中唯一一个需要三个操作数的运算符。
expression1 ? expression2 :expression3
如果experssion1为true,这整个条件表达式的值为experssion2 的值;否则,整个表达式的值为expression3的值。
例如:
5>3 ? 10 : 12 //因为5>3,所有表达式的值为10;反之,则表达式的值为12;
4、switch语句
在大型列表中进行选择,创建屏幕菜单,是最好的选择。

switch( )
{
   case  1:   
   {
     ...
     break;
   }
   ....
   default:
   break;
}

showmenu()函数显示一组选项。再使用switch语句,根据用户的反应执行相应的操作。
5、将枚举量用作标签
程序在使用enum定义一组相关的常量,在switch语句中使用这些常量,通常,cin无法识别枚举类型,因此该程序要求用户在选择选项时输入一个整数,当switch语句将int值和枚举量标签进行比较时,将枚举量提升为int,还有,在while循环测试条件中,也会将枚举量提升为int类型。
6、读取数字的循环

#include <iostream>
#include <string>
#include <ctime>
#include <istream>
using namespace std;
const int max = 5;
int main()
{
	using namespace std;
	int i=0,n,sum=0;
	int weight[max];
	cout << "fish  #1:";
	while( i< max&&cin >> weight[i])
	{
		i++;
		if (i < max)
		{
			cout << "fish  #" << i + 1 << ";";
		}
	}
	for (i = 0; i < max; i++)
	{
		sum = sum + weight[i];
	}
	if (i == 0)
		cout << "暂无鱼!" << endl;
	else
	{
		sum = sum / i;
       cout << "捕鱼数量:" << sum<< endl;
	}
	
	return 0;
}

7、c++中发现输入了错误的内容时,采取以下步骤:
重置cin用来接受新的输入。
删除错误输入。
提示用户再输入。
程序必须先重置cin,然后才能删除错误输入。
8、简单文件输入和输出
请添加图片描述

声明对象:
ofstream outFile;
ofstream fout;
将这种对象与特定的文件关联起来:
outFole.open("fish.txt");
char filoename[50];
cin >> filename;
fout.open(filename);

请添加图片描述
8.1读取文本文件
请添加图片描述

将对象与特定的文件相关联起来:
inFile.open("bowing.txt");
char filename[50];
cin>>filename;
fin.open(filename);

第一题:

#include <iostream>
#include <string>
#include <ctime>
#include <istream>
using namespace std;

int main()
{
	char ch = 0;
	while ((ch = cin.get()) != '@')
	{
		if (isdigit(ch))
		{
			continue;
		}
		else if (islower(ch))
		{
			cout << (char)toupper(ch);
		}
		else if (isupper(ch))
		{
			cout << (char)tolower(ch);
		}
	}
	cout << endl;
	cout << "Done!" << endl;
	return 0;
}

第四题:

#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const unsigned int strsize = 100;
struct bop
{
	char fullname[strsize];//名字
	char title[strsize];//职务头衔
	char bopname[strsize];
	int preference;     //0=fullname,1=title,2=bopname;偏爱
};
void display_by_name(const struct bop *appdata, unsigned int size)
{
	for (size_t i = 0; i < size; i++)
	{
		cout << appdata[i].fullname << endl;
	}
}
void display_by_title(const struct bop *appdata, unsigned int size)
{
	for (size_t i = 0; i < size; i++)
	{
		cout << appdata[i].title << endl;
	}
}
void display_by_bopname(const struct bop *appdata, unsigned int size)
{
	for (size_t i = 0; i < size; i++)
	{
		cout << appdata[i].bopname << endl;
	}
}
void display_by_preference(const struct bop *appdata, unsigned int size)
{
	for (size_t i = 0; i < size; i++)
	{
		if (appdata[i].preference == 0)
		{
			cout << appdata[i].fullname << endl;
		}
		else if (appdata[i].preference == 1)
		{
			cout << appdata[i].title << endl;
		}
		else if (appdata[i].preference == 2)
		{
			cout << appdata[i].bopname << endl;
		}
	}
}
int main()
{
	const struct bop appdata[5] =
	{
		{"wimp macho","aaaa","bbbb",0},
		{"Raki Rhodes","cccc","dddd",1},
	    {"Celia Laiter","eeee","ffff",2},
	    {"Hoppy Hipman","gggg","hhhh",0},
		{"Pat Hand","iiii","jjjj",1}
	};
	char ch=0;
	int floats = 0;
	using namespace std;
	cout << "benebloent order of programmers report:";
	cout << "a.display by name" << endl;
	cout << "b.display by title" << endl;
	cout << "c.display by bopname" << endl;
	cout << "d.display by preference" << endl;
	cout << "q.quit" << endl;
	cout << "Enter your choice:";
	while (cin >> ch)
	{
		if (ch == 'q')
		{
			break;
		}
		switch (ch)
		{
		case 'a':
			display_by_name(appdata, 5);
			break;
		case 'b':
			display_by_title(appdata, 5);
			break;
		case 'c':
			display_by_bopname(appdata, 5);
			break;
		case 'd':
			display_by_preference(appdata, 5);
			break;
		default:
			break;
		}
		cout << "Next choice: ";
	}
	cout << "Bye!" << endl;
	return 0;
}

第五题:

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int main()
{
	int income;
	int tax;
	while (cin >> income)
	{
		if (income >= 0)
		{
			if (income <= 5000)
			{
				cout << "您不需缴纳税款!" << endl;
			}
			else if (income > 5000 && income <= 15000)
			{
				tax = 5000 * 0 + (income - 5000)*0.1;
				cout << tax << endl;
			}
			else if (income > 15000 && income <= 35000)
			{
				tax = 5000 * 0 + 10000 * 0.1 + (income-15000)*0.15;
				cout << tax << endl;
			}
			else if (income > 35000)
			{
				tax = 5000 * 0 + 10000 * 0.1 + 20000 * 0.15 + (income - 35000)*0.2;
				cout << tax << endl;
			}
		}
		else
		{
			break;
		}
	}
	return 0;
}

第六题:

#include <iostream>
#include <string>
#include <ctime>
#include <istream>
using namespace std;
struct informa
{
	string name;
	double money;
};
int main()
{
	unsigned int num=0;
	cout << "捐献者的数量:";
	cin >> num;
	cin.get();
	struct informa * data = new struct informa[num];//创建动态分配的结构数组中
	for(int i=0;i<num;i++)
	{
		cout << "第" << i+1 << "个捐献者姓名:";
		getline(cin,data[i].name);

		cout << "捐款金额:";
		cin >> data[i].money;
		cin.get();
	}
	int temp = 0;
	cout << "重要捐款人:" << endl;
	for (int i = 0; i < num; i++)
	{
		if (data[i].money > 10000)
		{
			cout << "捐款者姓名:" << data[i].name << endl;
			cout << "捐款金额:" << data[i].money << endl;
			temp++;
		}
	}
	if (temp == 0)
		cout << "none!" << endl;
	int tmp = 0;
	cout << "Patrons:" << endl;
	for (int i = 0; i < num; i++)
	{
		if (data[i].money < 10000)
		{
			cout << "捐款者姓名:" << data[i].name << endl;
			cout << "捐款金额:" << data[i].money << endl;
			tmp++;
		}
	}
	if (tmp == 0)
		cout << "none!" << endl;
	return 0;
}

第三题:

#include <iostream>
#include <string>
#include <ctime>
#include <istream>
using namespace std;

int main()
{
	using namespace std;
	cout << "please enter one of the following choices:"<<endl;
	cout << "c) carnivore"<<endl;
	cout << "p) pianist" << endl;
	cout << "t) tree" << endl;
	cout << "g) game" << endl;
	char ch=0;
	bool floats = 0;
	while (cin >> ch&&!floats)
	{
			switch(ch)
			{
				case 'c':
				{
					cout << "A maple is  a carnivore." << endl;
					floats = 1;
					break;
				}
				case 'p':
				{
					cout << "A maple is  a pianist." << endl;
					floats = 1;
					break;
				}
				case 't':
				{
					cout << "A maple is  a tree." << endl;
					floats = 1;
					break;
				}
				case 'g':
				{
					cout << "A maple is  a game." << endl;
					floats = 1;
					break;
				}
				default:
				{
					cout << "Please enter a c,  p,  t,  or  g:" << endl;
					break;
				}
			}
	}
	return 0;
}

第七题:

#include <iostream>
#include <string>
#include <ctime>
#include <istream>
using namespace std;

int main()
{
	using namespace std;
	int vowels = 0;
	int consonants = 0;
	int others = 0;
	string input;
	cout << "Enter words (q to quit):" << endl;

	while ((cin >> input))
	{
		if (input.length() == 1 && input[0] == 'q')
		{
			break;
		}

		if (isalpha(input[0]))
		{
			if (input[0] == 'a' || input[0] == 'e' || input[0] == 'i' || input[0] == 'o' || input[0] == 'u')
			{
				vowels++;
			}
			else
				consonants++;
		}
		else
			others++;
	}
	cout << vowels << " words beginning with vowels" << endl;
	cout << consonants << " words beginning with consonants" << endl;
	cout << others << " otners" << endl;
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值