1、编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了 cctype函数系列)。
#include <iostream>
#include <cctype>
int main()
{
using std::cout;
using std::cin;
using std::endl;
char ch;
cout << "Enter text for analysis, and type @"
<< "to terminate input.\n";
cin.get(ch);
while(ch != '@')
{
if(!isdigit(ch))
if(islower(ch))
cout << char(toupper(ch));
else if(isupper(ch))
cout << char(tolower(ch));
else
cout << ch;
cin.get(ch);
}
return 0;
}
2.编写一个程序,最多将10个 donation值读入到一个 double数组中(如果您愿意,也可使用模板类aray).程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include <iostream>
#include <array>
const int MaxArray = 10;
int main()
{
using std::cout;
using std::cin;
using std::endl;
std::array<double, MaxArray> array;
double sum = 0, ave = 0;
int num = 0;
cout << "#1:";
while(num < MaxArray && cin >> array[num])
{
sum += array[num];
if(++num < MaxArray)
cout << "#"<<num+1<<":";
}
ave = sum/num;
int Mnum = 0;
for (int i = 0; i < num; i++)
{
if(array[i]>ave)
Mnum++;
}
cout << "这些数字的平均值:"<< ave << endl;
cout << "有"<< Mnum << "个数字大于平均值"<<endl;
if(!cin)
{
cin.clear();
cin.get();
}
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.
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
cout << "Please enter one of the following choices:"<< endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" <<endl;
char ch;
cout << "Please enter a c, p, t, or g:";
cin >> ch;
while((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g'))
{
cout << "Please enter a c, p, t, or g:";
cin >> ch;
}
string word;
switch(ch)
{
case 'c': word = "carnivore"; break;
case 'p': word = "pianist";break;
case 't': word = "tree"; break;
case 'g': word = "game"; break;
}
cout << "A maple is a "<< word << "." << endl;
return 0;
}
4.加入 Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她).请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
}
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name
b. display by title
c. display by bopname d. display by preference
g. quit
注意,“ display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:
#include <iostream>
const int strsize = 20;
const int BopArraySize = 3;
struct bop{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void DisByName(bop [], const int size);
void DisByTitle(bop [], const int size);
void DisByBopname(bop [], const int size);
void DisByPreference(bop [], const int size);
int main()
{
using namespace std;
cout << "a. display by name b. display by title"<<endl;
cout << "c. display by bopname d. display by preference" << endl;
cout << "q. quit" << endl;
bop bopArray[BopArraySize] = {
{ "aaaa",
"atitle",
"abopname",
1
},
{ "bbbb",
"btitle",
"bbopname",
2
},
{ "cccc",
"ctitle",
"cbopname",
3
}};
char choice;
cout << "Enter your choice :_\b";
while (cin >> choice)
{
if (choice == 'q')
{
break;
}
switch (choice)
{
case 'a':DisByName(bopArray, 3);break;
case 'b':DisByTitle(bopArray, 3);break;
case 'c':DisByBopname(bopArray, 3);break;
case 'd':DisByPreference(bopArray, 3);break;
default:break;
}
cout << "Next choice:";
}
cout << "Bye!" << endl;
return 0;
}
void DisByName(bop * array, const int size)
{
using namespace std;
for(int i = 0; i < size; i++)
{
cout << array[i].fullname <<endl;
}
}
void DisByTitle(bop * array, const int size)
{
using namespace std;
for(int i = 0; i < size; i++)
{
cout << array[i].title <<endl;
}
}
void DisByBopname(bop * array, const int size)
{
using namespace std;
for(int i = 0; i < size; i++)
{
cout << array[i].bopname <<endl;
}
}
void DisByPreference(bop * array, const int size)
{
using namespace std;
for(int i = 0; i < size; i++)
{
if(array[i].preference == 0)
cout << array[i].fullname <<endl;
else if (array[i].preference == 1)
cout << array[i].title <<endl;
else if (array[i].preference == 2)
cout << array[i].bopname <<endl;
}
}
5.在 Neutronic王国,货币单位是 tarp,收入所得税的计算方式如下:
#include <iostream>
int main()
{
using namespace std;
cout << "税收系统" << endl;
double money;
cout << "请输入您的收入: ";
while(cin >> money && money >= 0)
{
double ShuiShou = 0;
int n = int(money/5000);
if (n > 9)
n = 9;
switch(n)
{
case 9:
case 8:
case 7:
ShuiShou = (money - 35000) * 0.20 + 20000 * 0.15 + 10000 * 0.10;
break;
case 6:
case 5:
case 4:
case 3:
ShuiShou = (money - 15000) * 0.15 + 10000 * 0.10;
break;
case 2:
case 1:
ShuiShou = (money - 5000) * 0.10;
case 0: ;
}
cout << "税收: "<< ShuiShou << endl;
cout << "请输入您的收入: ";
}
return 0;
}
6.编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或 string对象)和用来存储款项的 double成员。读取所有的数据后程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人( Grand Patrons).然后,程序将列出其他的捐款者,该列表要以 Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”.该程序只显示这两种类别,而不进行排序。
#include <iostream>
#include <cstring>
struct Person{
std::string name;
double money;
};
int main()
{
using namespace std;
cout << "请输入捐款人的数目: ";
int num;
cin >> num;
Person * pt = new Person [num];
for (int i = 0; i < num; i++)
{
cout << "#"<< i + 1 << "Person: "<< endl;
cout << "name: ";
cin >> pt[i].name;
cout << "money: ";
cin >> pt[i].money;
}
cout << "Grand Patrons"<< endl;
int numPer = 0;
for (int i = 0; i < num; i++)
{
if(pt[i].money > 10000)
{
cout << pt[i].name << " ";
numPer++;
}
}
if (numPer == 0)
cout << "none" << endl;
cout << endl;
cout << "Patrons"<< endl;
int LnumPer = 0;
for (int i = 0; i < num; i++)
{
if(pt[i].money < 10000)
{
cout << pt[i].name << " ";
LnumPer++;
}
}
if (LnumPer == 0)
cout << "none" << endl;
delete [] pt;
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>
#include<cstring>
#include<cctype>
int main()
{
using namespace std;
cout << "输入一段语句,使用该程序确定元音字母"<< endl;
string word;
int YuanYin = 0, FuYin = 0, QiTa = 0;
while(cin >> word && word != "q")
{
if(isalpha(word[0]))
{
tolower(word[0]);
switch(word[0])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': YuanYin += 1;break;
default : FuYin += 1;
}
}
QiTa += 1;
}
cout << "元音字母:" << YuanYin << endl;
cout << "辅音字母:" << FuYin << endl;
cout << "其他字母:" << QiTa << endl;
return 0;
}
8.编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include<iostream>
#include <fstream>
int main()
{
using namespace std;
ifstream input;
input.open("jlm.txt");
if(!input)
{
cerr << "文件未打开!!!"<<endl;
exit(0) ;
}
char ch;
int num = 0;
while(input >> ch )
num++;
cout << "文件中的字符数:"<<num <<endl;
return 0;
}
9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
Sam stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
#include <iostream>
#include <fstream>
#include <cstring>
struct Person{
std::string name;
double money;
};
int main()
{
using namespace std;
fstream outFile;
outFile.open("jlm.txt");
if(!outFile.is_open())
{
cerr<< "文件未打开!!!";
return 0;
}
int num;
outFile >> num;
outFile.get();
Person * pt = new Person [num];
for (int i = 0; i < num; i++)
{
getline(outFile,pt[i].name);
outFile >> pt[i].money;
outFile.get();
}
cout << "Grand Patrons"<< endl;
int numPer = 0;
for (int i = 0; i < num; i++)
{
if(pt[i].money > 10000)
{
cout << pt[i].name << endl;
numPer++;
}
}
if (numPer == 0)
cout << "none" << endl;
cout << endl;
cout << "Patrons"<< endl;
int LnumPer = 0;
for (int i = 0; i < num; i++)
{
if(pt[i].money < 10000)
{
cout << pt[i].name << endl;
LnumPer++;
}
}
if (LnumPer == 0)
cout << "none" << endl;
delete [] pt;
return 0;
}