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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_24994943/article/details/82290620

1、

#include <iostream>
#include <cctype>
using namespace std;
/*
1、读取键盘输入,遇到@符号为止
2、回显输入(数字除外)
3、将大写改为小写,小写改为大写
*/
int main()
{
	char c[100];
	int n = 0;
	while (cin >> c[n])
	{
		if (c[n] == '@') break;
		n++;
	}
	for (int i = n - 1; i >= 0; i--)
	{
		if (isalpha(c[i]))
		{
			if (c[i] >= 'a' && c[i] <= 'z')
				c[i] = toupper(c[i]);
			if (c[i] >= 'A' && c[i] <= 'Z')
				c[i] = tolower(c[i]);
			cout << c[i];
		}
	}
	return 0;
}

2、

#include <iostream>
using namespace std;
//求平均值
double average(const double a[], const int &n);
//求数组中大于平均值的数字的个数
int aboveAverage(const double a[], const int &n);


int main()
{
	double a[10];
	int n = 0;
	while (cin >> a[n])
	{
		n++;
		if (n == 10) break;
	}
	cout << "该数组的平均值为: " << average(a, n) << endl;
	cout << "该数组中有" << aboveAverage(a, n) << "个数均大于平均值" << endl;

	return 0;
}

double average(const double a[], const int & n)
{
	double count = 0;
	for (int i = 0; i < n; i++)
	{
		count += a[i];
	}
	return count / n;
}

int aboveAverage(const double a[], const int & n)
{
	double ave = average(a, n);
	int x = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] > ave) x++;
	}
	return x;
}

3、

#include <iostream>
using namespace std;
//switch的练习
void printSomething(const char &c);

int main()
{
	cout << "Please enter one of the following choices:" << endl;
	cout << "w) word \t\t p) powerpoint" << endl;
	cout << "e) excel \t\t g) game!" << endl;
	char c;
	cin >> c;
	printSomething(c);
	return 0;
}

void printSomething(const char & c)
{
	switch (c)
	{
	case 'w':
		cout << "Open Word.." << endl;
		break;
	case 'p':
		cout << "Open Powerpoint.." << endl;
		break;
	case 'e':
		cout << "Open Excel.." << endl;
		break;
	case 'g':
		cout << "Open Game!!!" << endl;
		break;
	default:
		cout << "Please enter a w, p, e, or g:" << endl;
		char cc;
		cin >> cc;
		printSomething(cc);
		break;
	}
}

5、

#include <iostream>
using namespace std;
double IncomeTax(const double &earning);

int main()
{
	double earning;
	while (cin >> earning)
	{
		if (earning < 0) break;
		cout << earning << " tvarps应该支付所得税:" << IncomeTax(earning) << endl;
	}
	return 0;
}

double IncomeTax(const double & earning)
{
	if (earning <= 5000)
		return 0.0;
	else if (earning <= 15000)
		return (earning - 5000)*0.1;
	else if (earning <= 35000)
		return 10000 * 0.1 + (earning - 15000)*0.15;
	else
		return 10000 * 0.1 + 20000 * 0.15 + (earning - 35000)*0.2;
}

6、

#include <iostream>
#include <string>
using namespace std;
struct Patron
{
	string name;
	double money;
};

//显示捐款人
void Patrons(const Patron p[], const int &n);

int main()
{
	int n;
	cin >> n;
	Patron *p = new Patron[n];
	for (int i = 0; i < n; i++)
	{
		cin >> p[i].name >> p[i].money;
	}
	Patrons(p, n);
	delete[] p;
	return 0;
}

void Patrons(const Patron p[], const int &n)
{
	bool flag = false;
	cout << "Grand Patrons" << endl;
	for (int i = 0; i < n; i++)
	{
		if (p[i].money >= 10000)
		{
			flag = true;
			cout << p[i].name << "\t" << p[i].money << endl;
		}
	}
	if (!flag) cout << "none" << endl;
	flag = false;
	cout << "Patrons" << endl;
	for (int i = 0; i < n; i++)
	{
		if (p[i].money < 10000)
		{
			flag = true;
			cout << p[i].name << "\t" << p[i].money << endl;
		}
	}
	if (!flag) cout << "none" << endl;
}

7、

#include <iostream>
#include <vector>
using namespace std;
void WhichOne(const char &c, int &vow, int &con, int &oth);

int main()
{
	vector<char> c;
	char temp;
	bool flag = false;
	cin.get(temp);
	while (!flag || !(temp == ' ' || temp == '\n'))
	{
		if (temp == 'q') flag = true;
		else flag = false;
		c.push_back(temp);
		cin.get(temp);		
	}
	c.pop_back();
	int vow = 0, con = 0, oth = 0;
	WhichOne(c[0], vow, con, oth);
	for (int i = 0; i < c.size() - 1; i++)
	{
		if (c[i] == ' '||c[i] == '\n')
		{
			WhichOne(c[i + 1], vow, con, oth);
		}
	}
	cout << vow << " words beginning with vowels" << endl;
	cout << con << " words beginning with consonants" << endl;
	cout << oth << " others" << endl;
	return 0;
}

void WhichOne(const char & c, int & vow, int & con, int & oth)
{
	if (!isalpha(c)) oth++;
	else
	{
		switch (c)
		{
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
		case 'A':
		case 'E':
		case 'I':
		case 'O':
		case 'U':
			vow++;
			break;
		default:
			con++;
			break;
		}
	}
}

8、

9、

猜你喜欢

转载自blog.csdn.net/sinat_24994943/article/details/82290620
今日推荐