1. 第1题
#include <iostream>
#include<cctype>
using namespace std;
int main()
{
char ch;//字符
cout << "请输入字符:";
while ((ch = cin.get() )!= '@')
{
if (!isdigit(ch))//判断是否为数字,不是数字则进行大小写转化
{
if (isupper(ch))
{
ch = tolower(ch);
cout << ch;
}
else if (islower(ch))
{
ch = toupper(ch);
cout << ch;
}
else
cout << ch;
}
}
return 0;
}
使用cctype头文件,对于判断字符是否是数字、大小写就非常容易了。这里要注意的是循环的测试条件,检查输入的字符是否为‘@’字符,如果是‘@’字符,之后的字符就不再显示了。
结果图:
2. 第2题
#include <iostream>
#include<array>
using namespace std;
const int ArrSize = 10;
int main()
{
array<double, ArrSize> donation;//类型为double的array数组
double total = 0;//总数
double avg;//平均值
int i = 0;//数字计数
int count = 0;//大于平均值计数
cout << "请输入第1个数:";
while (i < ArrSize && (cin >> donation[i]))
{
i++;
if (i < ArrSize)
cout << "请输入第" << i + 1 << "个数字:";
}
if (i == 0)
cout << "输入数字个数为0!" << endl;
else if (i == ArrSize)
cout << "数组已满结束输入!" << endl;
else
cout << "程序遇到非数字输入结束输入!" << endl;
for (int j = 0; j < i;j++)
{
total = total + donation[j];
}
avg = total / i;
cout << "平均值为:" << avg << endl;
for (int j = 0; j < i; j++)
{
if (donation[j] > avg)
count++;
}
cout << i << "个数中有" << count << "个数大于平均数" << avg << endl;
return 0;
}
考察点
- 考察的是读取数字的循环,书上第187页
- 直接用
cin>>fonation[i]
可判断输入的是否为数字 - 使用array模板类
结果图:
3. 第3题
#include<iostream>
using namespace std;
int main()
{
char ch;//输入的字符
cout << "Please enter one of the following choices:\n"
"c) carnivore \t\t p) pianist\n"
"t) tree \t\t g) game" << endl;
cin >> ch;//输入第一个字符
//如果输入的字符不是四个字符中的任一个,就进行while循环
while(!(ch == 'c' || ch == 'p' || ch == 'g' || ch == 't'))
{
cout << "Please enter a c, p, t, or g: ";
cin >> ch;
}
//输入字符满足后,就进行switch循环
switch (ch)
{
case 'c':
cout << "A maple is a carnivore." << endl;
break;
case 'p':
cout << "A maple is a pianist." << endl;
break;
case 't':
cout << "A maple is a tree." << endl;
break;
case 'g':
cout << "A maple is a game." << endl;
break;
}
return 0;
}
注意点
- 考察逻辑运算符:我这里的while循环中的测试条件不唯一,只要你的条件是:输入的不是有效字符,就让用户不断重新输入。
!(ch == 'c' || ch == 'p' || ch == 'g' || ch == 't')
就是输入的不是c/p/g/t的话,就循环。 - switch语句:一定要记得加break。
结果图:
4. 第4题
#include<iostream>
using namespace std;
const int strsize = 20;
const int arrsize = 5;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int perference;
};
int main()
{
//创建bop结构数组,并初始化
bop arr[arrsize] = { {"Wimp Macho", "Teacher","Captain America", 0},
{"Raki Rhides", "Junior Programmer", "Raytheon", 1},
{"Celia Laiter", "Doctor","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","Black widow", 1},
{"Pat Hand","Student", "LOOPY", 2}
};
char ch;//输入的字符
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name \t\t b. display by title\n"
"c. display by bopname \t\t d. display by perference\n"
"q. quit" << endl;
cout << "Enter your choice: ";
cin >> ch;
while (ch != 'q')
{
switch (ch)
{
case 'a':
for (int i = 0; i < arrsize; i++)
{
cout << arr[i].fullname << endl;
};
break;
case 'b':
for (int i = 0; i < arrsize; i++)
{
cout << arr[i].title << endl;
};
break;
case 'c':
for (int i = 0; i < arrsize; i++)
{
cout << arr[i].bopname << endl;
};
break;
case 'd':
for (int i = 0; i < arrsize; i++)
{
int index = arr[i].perference;//得到0、1、2
switch (index)
{
//0=fullname
case 0:
cout << arr[i].fullname << endl;
break;
//1=title
case 1:
cout << arr[i].title << endl;
break;
//2=bopname
case 2:
cout << arr[i].bopname << endl;
break;
}
};
break;
}
cout << "Next choice: ";
cin >> ch;
}
cout << "Bye!" << endl;
return 0;
}
注意点
- 访问结构成员的方法,使用句点运算符
- switch语句:当输入字符是d时,首先得到每个数组元素的perference值:0、1、2;再根据这个值输出这个数组元素对应的结构成员,我这里在switch语句中又使用了一个switch语句。
结果图:
5. 第5题
#include<iostream>
using namespace std;
int main()
{
double tax;//所得税
int salary;//收入
cout << "请输入您的收入: ";
//当输入是数字且不为负数时,计算所得税
while (cin >> salary && salary >= 0)
{
if (salary <= 5000)
cout << "不收税!" << endl;
else if (salary > 5000 && salary <= 15000)
{
tax = (salary - 5000)*0.10;
cout << "所得税为:" << tax << endl;
}
else if (salary > 15000 && salary <= 35000)
{
tax = 10000 * 0.10 + (salary - 15000)*0.15;
cout << "所得税为:" << tax << endl;
}
else
{
tax = 10000 * 0.10 + 20000 * 0.15 + (salary - 35000)*0.20;
cout << "所得税为:" << tax << endl;
}
cout << "请输入您的收入: ";
}
cout << "Bye!" << endl;
return 0;
}
注意点
- 关键点在于while的循环条件,只有输入是数字且不为负数时才计算所得税!
结果图:
6. 第6题
#include<iostream>
#include<string>
using namespace std;
struct Partron
{
string name;//姓名
double money;//捐款数
};
int main()
{
int num;//捐款人数
int count1 = 0;//计数
int count2 = 0;//计数
cout << "请输入捐款人数:";
cin >> num;
Partron *arr = new Partron[num];//根据人数创建动态的Partron结构数组
cout << "请输入捐献者的姓名和款项:" << endl;
for (int i = 0; i < num; i++)
{
cout << "#" << i + 1 << ": " << endl;
cout << "姓名:";
cin >> arr[i].name;
cout << "款项:";
cin >> arr[i].money;
}
cout << "输入完成!" << endl << endl;
cout << "Grand Patrons" << endl;
cout << "姓名\t款项" << endl;
for (int i = 0; i < num; i++)
{
if (arr[i].money > 10000)
{
count1++;
cout << arr[i].name << "\t" << arr[i].money << endl;
}
}
if (count1 == 0)
cout << "None!" << endl;
cout << endl<< "Patrons" << endl;
cout << "姓名\t款项" << endl;
for (int i = 0; i < num; i++)
{
if (arr[i].money <= 10000)
{
count2++;
cout << arr[i].name << "\t" << arr[i].money << endl;
}
}
if (count2 == 0)
cout << "None!" << endl;
delete[] arr;
return 0;
}
注意点
- 用new创建动态结构数组,数组大小可以根据用户自定义输入。
- 最后一定要用delete[]释放内存,否则会造成内存泄漏!
- 用句点运算符访问结构成员。
结果图:
7. 第7题
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main()
{
int count1 = 0;//元音的个数
int count2 = 0;//辅音的个数
int other = 0;//其他开头的个数
string str;
cout << "Enter words (q to quit) :" << endl;
while (cin>> str)
{
if (str == "q")
break;//如果输入q,直接跳出循环
char ch = str[0];
if (isalpha(ch))
{
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count1++;
break;
default:
count2++;
break;
}
}
else
other++;
}
cout << count1 << " words beginning with vowels" << endl;
cout << count2 << " words beginning with consonants" << endl;
cout << other << " others" << endl;
return 0;
}
注意点
- 如何读取一个单词:(1)可以使用char []数组来存储一个单词(2)可以直接用string型来存储,直接
cin>>str;
就可以读入一个单词。 - 如何得到一个单词的开头:无论是char数组还是string型,直接用索引【0】就可以得到。
- 元音字母有5个:a/e/i/o/u,其余全是辅音字母,所以在switch语句中case的字符采用个数较少的元音,将辅音全置于default中。
结果图: