第五章主要讲了函数
#include<iostream>
#include<string>
#include<vector>
#include<stdexcept>
using namespace std;
void test_5_9() {
char c;
int cnt = 0;
while (cin >> c)
{
switch (c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cnt++;
default:
break;
}
}
std::cout << "元音字母个数为:\t" << cnt << endl;
}
void test_5_10() {
char c;
int cnt_a = 0;
int cnt_e = 0;
int cnt_i = 0;
int cnt_o = 0;
int cnt_u = 0;
int cnt_space = 0;
int cnt_t = 0;
int cnt_n = 0;
while (cin>>std::noskipws >> c)
{
//一定要加上std::noskipws,表示不忽略任何地方的空白
switch (c)
{
case 'a':
case 'A':
cnt_a++;
break;
case 'e':
case 'E':
cnt_e++;
break;
case 'i':
case 'I':
cnt_i++;
break;
case 'o':
case 'O':
cnt_o++;
break;
case 'U':
case 'u':
cnt_u++;
break;
case ' ':
cnt_space++;
break;
case '\t':
cnt_t++;
break;
case '\n':
cnt_n++;
break;
default:
break;
}
}
std::cout << "元音字母a的个数为:\t" << cnt_a << endl;
std::cout << "元音字母e的个数为:\t" << cnt_e << endl;
std::cout << "元音字母i的个数为:\t" << cnt_i << endl;
std::cout << "元音字母o的个数为:\t" << cnt_o << endl;
std::cout << "元音字母u的个数为:\t" << cnt_u << endl;
std::cout << "空格的个数为:\t" << cnt_space << endl;
std::cout << "制表符的个数为:\t" << cnt_t << endl;
std::cout << "换行符的个数为:\t" << cnt_n << endl;
}
void test_5_12() {
char c1;
char c2;
int cnt_f = 0;
int cnt_l = 0;
int cnt_i = 0;
while (cin >> c1)
{
if (c1 == 'f')
{
cin >> c2;
switch (c2)
{
case 'f':
cnt_f++;
break;
case 'l':
cnt_l++;
break;
case 'i':
cnt_i++;
break;
default:
break;
}
}
}
std::cout << "ff个数为:\t" << cnt_f<< endl;
std::cout << "fl个数为:\t" << cnt_l << endl;
std::cout << "fi个数为:\t" << cnt_i << endl;
}
void test_5_14() {
string str1,str2;
string max_str;
int max_cnt = 0, cnt = 1;
while (cin >> str2)
{
if (str1 == str2)
{
cnt++;
}
else
{
if (cnt > max_cnt)
{
max_cnt = cnt;
max_str = str2;
}
str1 = str2;
cnt = 1;
}
}
if (cnt > max_cnt)
{
max_cnt = cnt;
max_str = str2;
}
cout << max_str << "连续出现了" << max_cnt << "次" << endl;
}
void test_5_17() {
vector<int> a = { 0,1,1,2 };
vector<int> b = { 0,1,1,2,3,5 };
vector<int> duan = a.size() < b.size() ? a : b;
vector<int> chang = a.size() > b.size() ? a : b;
decltype(duan.size()) i = 0;
cout << duan.size() << endl;
cout << chang.size() << endl;
for (; i < duan.size(); i++)
{
if (duan[i] != chang[i])
break;
}
if (i == duan.size())
{
cout << "真" << endl;
}
else
{
cout << "假" << endl;
}
}
void test_5_19() {
string str1, str2;
do
{
cout << "请输入两个字符串:" << endl;
cin >> str1 >> str2;
cout << "比较短的字符串是:"<<(str1.size() < str2.size() ? str1 : str2) << endl;
} while (cin);
}
void test_5_20() {
string str1, str2;
int flag = 0;
while (cin>>str2)
{
if (str2 == str1)
{
flag = 1;
cout << str2 << endl;
break;
}
else
{
str1 = str2;
}
}
if (flag == 0)
{
cout << "没有重复的单词" << endl;
}
}
void test_5_25() {
int a, b;
while (cin >> a >> b)
{
try {
if (b == 0) throw runtime_error("被除数不能是0");
cout << static_cast<double>(a) / b << endl;
}
catch (runtime_error err)
{
cout << err.what()
<< "\n 是否需要重新输入?Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n')
break;
}
}
}
int main() {
//test_5_9();
//test_5_10();
//test_5_12();
//test_5_14();
//test_5_17();
//test_5_19();
//test_5_20();
test_5_25();
return 0;
}