1.编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype系列)
using namespace std;
int main()
{
char ch;
cout << "Please enter a string: ";
while (cin >> ch && ch != '@')
{
if (!isdigit(ch))//判断数字
{
if (islower(ch))//判断小写字母
{
ch -= 32;//转换成小写字母
}
else if (isupper(ch))//判断大写字母
{
ch += 32;//转换成小写字母
}
cout << ch;
}
}
cout << endl;
system("pause");
return 0;
}
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cout << "请输入一些字符,以“@”为结束符,若输入字母,大小写将会转换:" << endl;
cin >> ch;
for (; ch != '@';)//遇到第一个字符'@'后结束输入
{
if (isalpha(ch))//如果是字母
{
if (islower(ch))//如果是小写字母
{
ch -= 32;
cout << ch;
}
else if (isupper(ch))
{
ch += 32;
cout << ch;
}
}
else if (!isdigit(ch))//如果是数字
continue;
cin >> ch;
}
system("pause");
return 0;
}
2.编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include <iostream>
using namespace std;
const int MAX = 10;
int main()
{
double arr[MAX];
double number;
double total = 0;
int i = 0;
cout << "Please enter a number: ";
while (i<MAX&&cin >> number) {
arr[i] = number;
total += number;
i++;
if (i < MAX)
cout << "Please enter a number: ";
}
double average = total / i;
cout << "The average of those numbers is " << average << endl;
int high = 0;
for (int j = 0; j < i; j++)
{
if (arr[j] > average)
high++;
}
cout << "The number of higher than average is " << high << endl;
system("pause");
return 0;
}```
3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提供用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
Please enter a c , p , t , or g: q
Please enter a c , p , t , or g: t
A maple is a tree.
```cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
string c = "carnivore";
string p = "pianist";
string t = "tree";
string g = "game";
cout << "Please enter one of the following choices: " << endl;
cout << "c) " << c << " " << "p)" << p << endl;
cout<< "t) " << t << " " << "g)" << g << endl;
while (cin >> ch && ch != 'c'&&ch != 'p'&&ch != 't'&&ch != 'g')
{
cout << "Please enter a c,p,t,or g: ";
}
switch (ch) {
case'c':cout << "A maple is a " << c << endl;
break;
case'p':cout << "A maple is a " << p << endl;
break;
case't':cout << "A maple is a " << t << endl;
break;
case'g':cout << "A maple is a " << g << endl;
break;
}
system("pause");
return 0;
}
4.加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
/ Benevolent Order of Programmers name structure
struct bop{
char fullname[strsize]; // real name
char title[strsize]; // job title
char bopname[strsize]; // secret BOP name
int preference; // 0 = fullname , 1 = title , 2 = bopname
};
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
注意,“display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:
Benevolent Order of Programmers Report
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
Enter your choice: a
Wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice: d
Wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOPY
Next choice: q
Bye!
程序
#include <iostream>
using namespace std;
const int strsize = 20;
const int human_num = 5;
struct bop {
char fullname[strsize];//real name
char title[strsize];//job title
char bopname[strsize];//secret BOP name
int preference; //0=fullname,1=title,2=bopname;
};
int main()
{
bop human[human_num] = {
{"Wimp Macho","Teacher","Caption America",0},
{"Raki Rhodes","Junior Programmer","Raytheon",1},
{"Celia Laiter","Doctor","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","Black widow",1},
{"Pat Hand","Student","LOOPY",2}
};
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a.display by name b.display by title" << endl;
cout << "c.display by bopname d.display by preference" << endl;
cout << "q.quit\n";
char ch;
cout << "Enter your choice: ";
while (cin >> ch && ch != 'q')
{
switch (ch) {
case'a':
{
for (int i = 0; i < human_num; i++)
cout << human[i].fullname << endl;
break;
}
case'b':
{
for (int i = 0; i < human_num; i++)
cout << human[i].title << endl;
break;
}
case'c':
{
for (int i = 0; i < human_num; i++)
cout << human[i].bopname << endl;
break;
}
case'd':
{
for (int i = 0; i < human_num; i++) {
if (human[i].preference == 0)
cout << human[i].fullname << endl;
else if (human[i].preference == 1)
cout << human[i].title << endl;
else
cout << human[i].bopname << endl;
}
break;
}
}
cout << "Next choice: ";
}
cout << "Bye!" << endl;
system("pause");
return 0;
}
- 在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps: 不收税
5001~15000 tvarps: 10%
15001~35000 tvarps: 15%
35000 tvarps 以上: 20%
例如,收入为38000 tvarps时,所得税为5000 * 0.00 + 10000 * 0.10 + 20000 * 0.15 + 3000 * 0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。
#include <iostream>
using namespace std;
const float Rate0 = 0;
const float Rate1 = 0.1;
const float Rate2 = 0.15;
const float Rate3 = 0.2;
int main()
{
float income, tax;
cout << "Please enter your income: ";
while (cin >> income && income > 0)
{
if (income < 5000)
tax = 0;
else if (income < 15000)
tax = 5000 * Rate0 + (income - 5000)*Rate1;
else if (income < 35000)
tax = 5000 * Rate0 + 10000 * Rate1 + (income - 15000)*Rate2;
else
tax = 5000 * Rate0 + 10000 * Rate1 + 20000 * Rate2 + (income - 35000)*Rate3;
cout << "The tax that you should pay is " << tax << endl;
cout << "Please enter your income: ";
}
system("pause");
return 0;
}
- 编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来储存款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include <iostream>
#include <string>
using namespace std;
struct information {
string name;//储存姓名
double money;//储存款项
};
void show_grand(information *info, int number);
void show_other(information *info, int number);
int main()
{
int number;
cout << "Please enter the number of people: ";
cin >> number;
cin.get();
information *info = new information[number];
for (int i = 0; i < number; i++)
{
cout << "Please enter the " << i + 1 << "-th name: ";
getline(cin, info[i].name);
cout << "Please enter the " << i + 1 << "-th money: ";
cin >> info[i].money;
cin.get();
}
cout << "Patrons" << endl;
show_grand(info, number);
show_other(info, number);
system("pause");
return 0;
}
void show_grand(information *info, int number)//重要捐款人
{
cout << "Grand Patrons: " << endl;
int count_grand = 0;
for (int i = 0; i < number; i++)
{
if (info[i].money > 10000)
{
cout << info[i].name << "\t" << info[i].money << endl;
count_grand++;
}
}
if (count_grand == 0)
cout << "none" << endl;
}
void show_other(information *info, int number)//其他捐款人
{
cout << "other Patrons:" << endl;
int count_other = 0;
for (int i = 0; i < number; i++)
{
if (info[i].money <= 10000)
{
cout << info[i].name << "\t" << info[i].money << endl;
count_other++;
}
}
if (count_other == 0)
cout << "none" << endl;
}
#include <iostream>
using namespace std;
const int namesize = 20;
const double stand = 10000.0;
struct patron
{
char name[namesize];
double money;
};
int main()
{
//动态数组
int count;//捐款者总数
cout << "Enter the number of patrons: " << endl;//请输入捐款者总数
cin >> count;
cin.get();
patron *peoplelist = new patron[count];
//输入数据
cout << "Enter the patrons: " << endl;
for (int i = 0; i < count; i++)
{
cout << "Patrons name:" << endl;
cin.getline(peoplelist[i].name, namesize);
cout << "donation:" << endl;
cin >> peoplelist[i].money;
cin.get();
}
//输出数据
cout << "\n*********" << endl;
cout << "\nGrand Patrons: " << endl;
int n = 0;
for (int i = 0; i < count; i++)
{
if (peoplelist[i].money > stand)
{
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
n++;
}
}
if (n == 0)
cout << "none";
int m = 0;
cout << "\nPatrons: " << endl;
for (int i = 0; i < count; i++)
{
if (peoplelist[i].money <= stand)
{
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
m++;
}
}
if (m == 0)
cout << "none";
delete peoplelist;
system("pause");
return 0;
}
7.编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words (q to quit) :
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others
#include <iostream>
using namespace std;
int main()
{
char str[20];
int number_vowel = 0;//元音
int number_consonants = 0;//辅音
int number_others = 0;//其他单词
cout << "Enter words(q to quit): " << endl;
while (cin >> str)
{
if (strcmp(str, "q") == 0)
break;
char ch = str[0];
if (isalpha(ch))
{
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
number_vowel++;
break;
default:
number_consonants++;
}
}
else
number_others++;
}
cout << number_vowel << " words beginning with vowels\n";
cout << number_consonants << " words beginning with consonants\n";
cout << number_others << " others\n";
system("pause");
return 0;
}
- 编写一个程序,它打开一个文件,逐个字符的读取该文件,直到到达文件末尾,然后指出该文件包含多少个字符。
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 60;
int main()
{
//打开测试文件
char filename[SIZE];
ifstream inFile;
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file" << filename << endl;
cout << "Program terminating.\n";//程序终止
exit(EXIT_FAILURE);//表示异常退出
}
//开始读取数据
char value;
int count = 0;
inFile >> value;
inFile.get();
while (inFile.good())
{
++count;
inFile >> value;
inFile.get();
}
//文件读取末尾
if (inFile.eof())//判断是否到达文件尾部
cout << "End of file reached.\n";//已到达文件结尾
else if (inFile.fail())//打开文件失败
cout << "Input terminated by data mismatch.\n";//因数据不匹配而终止输入
else
cout << "Input terminated for unknown reason.\n";//输入因未知原因而终止
if (count == 0)
cout << "No data processed.\n";//没有处理数据
else
{
cout << "chars: " << count << endl;
}
inFile.close();
system("pause");
return 0;
}
- 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int SIZE = 60;
const double stand = 10000.0;
struct patron {
string name;
double money;
};
int main()
{
char filename[SIZE];
ifstream inFile;
//打开文件
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
//读取数据
int count;
inFile >> count;
inFile.get();//读取文件数据
patron *peoplelist = new patron[count];
for (int i = 0; i < count; i++)
{
getline(inFile, peoplelist[i].name);
inFile >> peoplelist[i].money;
inFile.get();
}
//输出数据
cout << "\nGrand Patrons:" << endl;
int n = 0;
for (int i = 0; i < count; i++)
{
if (peoplelist[i].money > stand)
{
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
n++;
}
}
if (n == 0)
cout << "none";
int m = 0;
cout << "\nPatrons: " << endl;
for (int i = 0; i < count; i++)
{
if (peoplelist[i].money <= stand)
{
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
m++;
}
}
if (m == 0)
cout << "none";
//释放内存,关闭文件
delete peoplelist;
inFile.close();
return 0;
}