1、编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换成小写,将小写字符转换成大写。
#include<iostream>
#include<cctype>
using namespace std;
int main(){
char c;
cin >> c;
while (c != '@'){
if (islower(c)){
c = toupper(c);
}
else if (isupper(c)){
c = tolower(c);
}
cout << c << endl;
cin >> c;
}
return 0;
}
2、编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类array)。程序遇到非数字输入时结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include<iostream>
#include<cctype>
using namespace std;
int main(){
double donation[10];
int num = 0;
double sum = 0;
for (int i = 0; i < 10; i++){
if (cin >> donation[i]){
num++;
sum += donation[i];
}
else{
break;
}
}
if (num != 0){
double average = sum / num;
int bigger = 0;
for (int i = 0; i < num; i++){
if (donation[i]>average)
bigger++;
}
cout << "平均值为: " << average << "\t共有" << bigger << " 个数比平均值大" << endl;
}
return 0;
}
3、编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序提示用户输入一个有效的字母,直到用户这样做为止,然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。
#include<iostream>
using namespace std;
int main(){
cout << "Please enter one of the following choices:" << endl;
cout << "c) carnivore \t p) pianist" << endl;
cout << "t) tree \t g) game" << endl;
char in;
cin >> in;
bool finish = false;
while (!finish){
switch (in){
case 'c':
cout << "A maple is a carnivore." << endl;
finish = true;
break;
case 'p':
cout << "A maple is a pianist." << endl;
finish = true;
break;
case 't':
cout << "A maple is a tree." << endl;
finish = true;
break;
case 'g':
cout << "A maple is a game." << endl;
finish = true;
break;
default:
cout << "Please enter c , p , t or g : ";
cin >> in;
}
}
return 0;
}
4、加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔、秘密姓名或成员偏好来列出成员。/*题目过长,详见人民邮电出版社出版的《C++ Primer Plus》第六版中文版第200页*/
#include<iostream>
using namespace std;
const int strsize = 25;
const int num = 5;
struct bop{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main(){
bop peoples[num] = {
{ "曹操", "丞相", "孟德",1 },
{ "刘备", "汉中王", "玄德",0 },
{ "关羽", "汉寿亭侯", "云长",1 },
{ "赵云", "顺平侯", "子龙",2 },
{ "周瑜", "将军", "公瑾",2 }
};
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name \t b. display by title" << endl;
cout << "c. display by bopname \t d. display by preference" << endl;
cout << "q. quit" << endl;
cout << "Enter your choice: ";
char c;
cin >> c;
bool finish = false;
while (!finish){
switch (c){
case 'a':
for (int i = 0; i < num; i++){
cout << peoples[i].fullname << endl;
}
cout << "Next choice: ";
cin >> c;
break;
case 'b':
for (int i = 0; i < num; i++){
cout << peoples[i].title << endl;
}
cout << "Next choice: ";
cin >> c;
break;
case 'c':
for (int i = 0; i < num; i++){
cout << peoples[i].bopname << endl;
}
cout << "Next choice: ";
cin >> c;
break;
case 'd':
for (int i = 0; i < num; i++){
switch (peoples[i].preference){
case 0:
cout << peoples[i].fullname << endl;
break;
case 1:
cout << peoples[i].title << endl;
break;
case 2:
cout << peoples[i].bopname << endl;
break;
}
}
cout << "Next choice: ";
cin >> c;
break;
case 'q':
cout << "Bye!" << endl;
finish = true;
break;
default:
cout << "Next choice: ";
cin >> c;
break;
}
}
return 0;
}
5、在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000tvarp: 不收税
5001——15000tvarp: 10%
15001——35000tvarp: 15%
35000tvarp以上: 20%
请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。#include<iostream>
using namespace std;
int main(){
double salary;
cout << "请输入工资: ";
while (cin >> salary){
if (salary < 0){
break;
}
double tax = 0;
if (salary>5000){
tax += (salary - 5000)*0.1;
if (salary > 15000){
tax += (salary - 15000)*0.05;
if (salary > 35000){
tax += (salary - 35000)*0.05;
}
}
}
cout << "应交税费: " << tax << " tvarp" << endl;
cout << "请输入工资: ";
}
return 0;
}
6、编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来存储姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者姓名及捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(GrandPatrons)。然后,程序列出其他的捐款者,该列表要以Patron开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include<iostream>
#include<string>
using namespace std;
struct donation{
string name;
double money;
};
int main(){
int num;
cout << "Please enter the number of donors: ";
cin >> num;
donation *d = new donation[num];
int bigger = 0;
for (int i = 0; i < num; i++){
cout << "Please enter the name and money:" << endl;
cin >> d[i].name;
cin >> d[i].money;
if (d[i].money>10000){
bigger++;
}
}
if (bigger > 0 && bigger < num){
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++){
if (d[i].money>10000){
cout << d[i].name << "\t" << d[i].money << endl;
}
}
cout << "Patrons" << endl;
for (int i = 0; i < num; i++){
if (d[i].money <= 10000){
cout << d[i].name << "\t" << d[i].money << endl;
}
}
return 0;
}
if (bigger == 0){
cout << "Grand Patrons" << endl;
cout << "none" << endl;
cout << "Patrons" << endl;
for (int i = 0; i < num; i++){
if (d[i].money <= 10000){
cout << d[i].name << "\t" << d[i].money << endl;
}
}
return 0;
}
if (bigger == num){
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++){
if (d[i].money>10000){
cout << d[i].name << "\t" << d[i].money << endl;
}
}
cout << "Patrons" << endl;
cout << "none" << endl;
}
delete[]d;
return 0;
}
7、编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音大头,有多少个单词以辅音打头,还有多少单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
string str;
int kind1 = 0;//元音开头
int kind2 = 0;//辅音开头
int kind3 = 0;//其他开头
cout << "Enter words (q to quit):" << endl;
cin >> str;
while (str != "q"){
if (isalpha(str[0])){
if (str[0] == 'a' || str[0] == 'A' || str[0] == 'o' || str[0] == 'O'
|| str[0] == 'e' || str[0] == 'E' || str[0] == 'i' || str[0] == 'I'
|| str[0] == 'u' || str[0] == 'U'){
kind1++;
}
else{
kind2++;
}
}
else{
kind3++;
}
cin >> str;
}
cout << kind1 << " words beginning with vowels" << endl;
cout << kind2 << " words beginning with consonants" << endl;
cout << kind3 << " others" << endl;
return 0;
}
8、编写一个程序,它打开一个文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include<iostream>
#include<fstream>
using namespace std;
int main(){
char c;
long num = 0;
ifstream read;
read.open("F:\\Wine.txt");
while (read >> c){
num++;
}
cout << "文件中共有 " << num << " 个字符" << endl;
return 0;
}
9、完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct donation{
char name[30];
double money;
};
int main(){
ifstream read;
read.open("F:\\donation.txt");
int num;
read >> num;
char str[30];
read.getline(str,1);
donation *d = newdonation[num];
int bigger = 0;
for (int i = 0; i< num; i++){
read.getline(d[i].name,30);
read >>d[i].money;
read.getline(str,1);
if(d[i].money>10000){
bigger++;
}
}
if (bigger > 0&& bigger < num){
cout <<"Grand Patrons" << endl;
for (int i =0; i < num; i++){
if(d[i].money>10000){
cout<< d[i].name << "\t" << d[i].money << endl;
}
}
cout <<"Patrons" << endl;
for (int i =0; i < num; i++){
if(d[i].money <= 10000){
cout<< d[i].name << "\t" << d[i].money << endl;
}
}
return 0;
}
if (bigger == 0){
cout <<"Grand Patrons" << endl;
cout <<"none" << endl;
cout <<"Patrons" << endl;
for (int i =0; i < num; i++){
if(d[i].money <= 10000){
cout<< d[i].name << "\t" << d[i].money << endl;
}
}
return 0;
}
if (bigger ==num){
cout <<"Grand Patrons" << endl;
for (int i =0; i < num; i++){
if(d[i].money>10000){
cout<< d[i].name << "\t" << d[i].money << endl;
}
}
cout <<"Patrons" << endl;
cout <<"none" << endl;
}
delete[]d;
return 0;
}