6.11编程练习
1. 编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大小写字符转换为小写,将小写字符转换为大写。
#include <iostream>
#include <cctype>
using namespace std;
int main(){
char ch;
while (cin.get(ch) && ch != '@'){
if (isalpha(ch))
{
if (isupper(ch))
{
char temp = tolower(ch);
cout << temp << endl;
}
else
{
char temp = toupper(ch);
cout << temp << endl;
}
}
}
system("pause");
return 0;
}
2. 编写一个程序,最多将10个donation值读入到一个double数组中。程序遇到非数字输入时将结束输入,并告诉这些数字的平均值以及数组中有多少个数字大于平均值。
#include <iostream>
using namespace std;
int main(){
double donation[10] = {0};
double temp;
int i = 0;
while (i<10 ){
cout << "#" << i << ": ";
if (!(cin >> temp)){
break;
}
donation[i] = temp;
i++;
}
double sum = 0;
for (int j = 0; j < 10; j++)
{
sum = sum + donation[j];
}
double pingjun = sum / (i*1.0);
int dabiao = 0;
for (int k = 0; k < 10; k++)
{
if (donation[k] > pingjun){
dabiao++;
}
}
cout << "平均值:" << pingjun << ",达标数:" << dabiao << endl;
system("pause");
return 0;
}
3. 编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。
#include <iostream>
#include <string>
int main(){
using namespace std;
cout << "Please enter one of the following choices:" << endl;
cout << "c) carnivore\tp) pianist" << endl;
cout << "t) tree \tg) game" << endl;
cout << "Please enter a,c,p,t, or g:";
char ch;
cin.get(ch);
string temp;
bool flag = true;
while (flag){
switch (ch)
{
case 'c':
temp = "carnivore";
flag = false;
break;
case 'p':
temp = "pianist";
flag = false;
break;
case 't':
temp = "tree";
flag = false;
break;
case 'g':
temp = "game";
flag = false;
break;
default:
cout << "Please enter a,c,p,t, or g:";
flag = true;
//清除输入的'\n'缓存
cin.sync();
cin.get(ch);
break;
}
}
cout << "A maple is a: "<<temp<<endl;
system("pause");
return 0;
}
4. 请编写一个程序,可以使用真实姓名,头衔,秘密姓名或成员偏好列出成员。编写该程序时,请使用下面的结构:
5. 在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
~5000tvarps: 不收税
5001~15000tvarps: 10%
15001~35000tvarps: 15%
35000~tvarps: 20%
#include <iostream>
using namespace std;
int main(){
const int a = 0;
const int b = 10000 * 0.1;
const int c = 20000 * 0.15;
int sum;
float result;
while (true && cin >> sum){
if(sum > 35000)
{
result = (sum - 35000)*0.2 + c + b + a;
}
else if (sum > 15000){
result = (sum - 15000)*0.15 + b + a;
}
else if (sum > 5000){
result = (sum - 5000)*0.1 + a;
}
else{
result = 0;
}
cout << "个人所得税: " << result << endl;
}
system("pause");
return 0;
}
6. 编写一个程序,记录捐助给“维护合法权利团体”的资金。该陈旭要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被存储在衣蛾动态分配的结构数组中。每个结构有两个成员:用来存储姓名的字符数组和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题。
#include <iostream>
#include <string>
using namespace std;
struct juanzhu{
string name;
double number;
};
int main(){
int sum;
cout << "Enter sum: ";
cin >> sum;
juanzhu* jz = new juanzhu[sum];
for (int i = 0; i < sum; i++)
{
string tempname;
double tempnumber;
cout << "第" << i + 1 << "个\n姓名:";
cin >> tempname;
cout << "捐款数:";
cin >> tempnumber;
jz[i].name = tempname;
jz[i].number = tempnumber;
}
for (int i = 0; i < sum; i++)
{
if (jz[i].number > 10000)
{
cout << "Grand Patrons:\n" << "name:" << jz[i].name<< "\nmoney:" << jz[i].number << endl;
}
}
system("pause");
return 0;
}
7.
8. 编写一个程序,它打开一个文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
string filename = "solution8.txt";
ifstream inFile;
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << endl;
exit(EXIT_FAILURE);
}
char value;
int count = 0;
while (inFile >> value && inFile.good()){
count++;
}
cout << "count:" << count;
system("pause");
return 0;
}
9. 完成编程练习6,但从文件中读取所需的信息。该文件的第一项为捐款人数,余下的内容应为成对的行。在每一行中,第一行为捐款人姓名,第二行为捐款数额。
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
struct juanzhu{
string name;
double number;
};
int main(){
string filename = "solution9.txt";
ifstream inFile;
inFile.open(filename);
int sum;
inFile >> sum;
juanzhu* jz = new juanzhu[sum];
string name;
double number;
int i = 0;
while (inFile >> name && inFile >> number >> number && inFile.good()){
jz[i].name = name;
jz[i].number = number;
}
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << endl;
exit(EXIT_FAILURE);
}
for (int i = 0; i < sum; i++)
{
if (jz[i].number > 10000)
{
cout << "Grand Patrons:\n" << "name:" << jz[i].name<< "\nmoney:" << jz[i].number << endl;
}
}
system("pause");
return 0;
}