C++ prime plus 第六章

第六章…

1.编写一个程序,读取键盘输入,知道遇到@符号位置,并显示输入(数字除外),同时大写变成小写,小写变成大写

#include 
using namespace std;
int main()
{
	char ch;
	while((ch = cin.get()) != '@') //首先声明一个char类型的ch,在输入之后进行判断
    {  
		if(ch >= 'A' && ch <= 'Z')
			cout << (char)(ch + ('a' - 'A'));
		else
            cout << (char)(ch - ('a' - 'A'));
	}
	cout << endl;
	return 0;
}

实验结果:
C++ prime plus 第六章_第1张图片

2.将10个donation值读入到一个double数组中,程序遇到非数字输入时将结束输入,显示这些数字的平均值以及数组中有多好个数字大于平均值

#include 
using namespace std;
int main()
{
	double donation[10];
	double sum;
	int count = 0;
	cout<<"请输入10个值:";
	for(int i=0;i<10;i++)
       {
           if(cin>>donation[i])
            sum +=donation[i];
           else
            break;
       }
    cout<<"平均值为:"<(sum/10))
            count++;
    }
    cout<<"一共有"<

实验结果:
C++ prime plus 第六章_第2张图片

3.编写一个菜单驱动程序的雏形,显示4个提供选项的菜单,如果使用非有效选项,系统则会给提示,使用switch语句,运行结果如书所示

#include 
using namespace std;
int main()
{
	cout << "Please enter one of the following choices:" << endl << "c) carnivore                     p) pianist" << endl << "t) tree                          g) game" << endl;
	cout << "Please enter a c , p , t , or g: ";
	char ch;
	while(cin.get(ch).get() && (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g'))    //注意cin.get(ch).get()
	{
		cout << "Please enter a c , p , t , or g: ";
	}
	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;
}

实验结果:
C++ prime plus 第六章_第3张图片

4.创建一个程序,可以按照要求显示成员,并使用书中的结构

#include 
using namespace std;
struct bop
{
    char fullname[50];
    char title[50];
    char bopname[50];
    int preference;
};

void display_by_name();
void display_by_title();
void display_by_bopname();
void diaplay_by_preference();
char ch;
bop people[5] = {
		{
			"Wimp Macho",
			"BOSS",
			"WM",
			0
		},
		{
			"Raki Rhodes",
			"Manager",
			"Junior Programmer",
			2
		},
		{
			"Celia Laiter",
			"MIPS",
			"CL",
			1
		},
		{
			"Hoppy Hipman",
			"Analyst Trainee",
			"AT",
			1
		},
		{
			"Pat Hand",
			"Student",
			"LOOPY",
			2
		}
	};
int main()
{

	cout<<"a. display by name         b. display by title" << endl << "c. display by bopname      d. display by preference" << endl << "q. quit" << endl << "Enter your choice: ";
	while(cin>>ch&&ch!='q')
    {
        switch(ch){
        case'a':display_by_name();break;
        case'b':display_by_title();break;
        case'c':display_by_bopname();break;
        case'd':diaplay_by_preference();break;
        }
        cout<<"Enter your choice:"<

实验结果:
C++ prime plus 第六章_第4张图片

5.写一个程序,使用循环输入来要求用户输入收入,并报告所得税,用户输入负数或者非数字时,循环将结束

#include 
using namespace std;
double money;
int main()
{
    for(int i=0;i<10;i++)
    {
        while(cin>>money&&(money>0)){
            if(money<=5000)
                cout<5000&&money<=15000)
                cout<<(money-5000) * 0.1 + 5000 * 0<15000&&money<=35000)
                cout<<(money-15000) * 0.15 + 10000 * 0.1 + 5000*0<

实验结果:
C++ prime plus 第六章_第5张图片

6.编写一个程序,记录捐赠基金,要求用户输入捐赠者数目,然后要求用户输入每一个捐赠者的姓名和款项。这些信息被动态分配到一个结构数组中。(结构包括名字,存款),读取所有数据之后,记录捐款超过10000的捐款者的名字和捐款数额。

#include 
#include 
using namespace std;
struct people

{
	string name;
	double money;
	int flag;
};
int main()
{
	cout << "请输入捐献者数目: ";
	int num , GP = 0;
	cin >> num;   //以这种方式输入完了数字之后后面要加上cin.get()
    cin.get();
	people *team = new people[num];
	for(int i = 0 ; i < num ; ++ i)
    {
		getline(cin , team[i].name);
		cin >> team[i].money;
		cin.get();
		if(team[i].money > 10000){
			team[i].flag = 1;   //使用另一个变量标记捐款超过10000的人 
			++ GP;
		}
		else
			team[i].flag = 0;
	}
	cout << "Patrons: ";
	for(int i = 0 ; i < num ; ++ i){
		if(team[i].flag)
			cout << team[i].name << " ";
	}
	cout << endl;
	cout << "Others: ";
	if(GP == num)
		cout << "none" << endl;
	else{
		for(int i = 0 ; i < num ; ++ i){
			if(!(team[i].flag))
				cout << team[i].name << " ";
		}
	}
	cout << endl;
	return 0;
}

7.编写一个程序,每次读取一个单词,指导用户输入q为止,改程序指出有多少个单词以元音开头,有多少个以辅音开头,还有多少个单词不属于这两类使用if和switch来确定哪些以元音打头

#include 
#include 
using namespace std;
int main()
{
	cout << "Enter words (q to quit) :" << endl;
	string ch;
	int num1 = 0 , num2 = 0 , num3 = 0;
	while(cin >> ch)    //确保每次可以读取一个单词,string可以自动读取每一个单词,可以使用ch[i]的方式来选择一个单词中的第几个词
    {
		if(ch[0] == 'q' && ch[1] == 0) //确保不是以q为开头的单词而只是一个q
			break;
		if(ch[0] == 'a' || ch[0] == 'A' || ch[0] == 'e' || ch[0] == 'E' || ch[0] == 'i' || ch[0] =='I' || ch[0] == 'o' || ch[0] == 'O' || ch[0] == 'u' || ch[0] == 'U')
			++ num1;
		else if(ch[0] >= 'a' && ch[0] <='z' || ch[0] >= 'A' && ch[0] <='Z')
			++ num2;
		else ++ num3;
	}
	cout << num1 <<" words beginning with vowels" << endl << num2 <<" words beginning with consonants" << endl << num3 <<" others" << endl;
	return 0;
}

实验结果:
C++ prime plus 第六章_第6张图片

8.编写一个程序,可以打开一个文件,逐个字符读取该文件,直到到达文件末尾,指出该文件中包含多少个字符

#include 
#include 
using namespace std;
int main()
{
	ifstream infile;
	infile.open("in.txt");
	char ch;
	int num = 0;
	while(infile >> ch) //用于读取对应文件中的字符
	{
		if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
			++ num; 
	}
	cout << num << endl;
	return 0;
}

9.在完成上述编程之后


#include 
#include 
#include 
using namespace std;
struct people{
	string name;
	double money;
	int flag;
};
int main()
{
	fstream infile;
	infile.open("in.txt");
	int num , GP = 0;
	infile >> num;
	infile.get();
	people *p = new people[num];
	for(int i = 0 ; i < num ; ++ i){
		getline(infile , (p + i) -> name);
		infile >> (p + i) -> money;
		infile.get();
		if((p + i) -> money > 10000){
			(p + i) -> flag = 1;
			++ GP;
		}
		else
			(p + i) -> flag = 0;
	}
	cout << "Patrons: ";
	for(int i = 0 ; i < num ; ++ i){
		if((p + i) -> flag)
			cout << (p + i) -> name << " ";
	}
	cout << endl;
	cout << "Others: ";
	if(GP == num)
		cout << "none" << endl;
	else{
		for(int i = 0 ; i < num ; ++ i){
			if(!((p + i) -> flag))
				cout << (p + i) -> name << " ";
		}
	}
	cout << endl;
	return 0;
}

文中部分参考原文:https://blog.csdn.net/acm_yuuji/article/details/37889391

有一部分需要在熟悉一下

你可能感兴趣的:(C++ prime plus 第六章)