c++ Primer Plus(第六版)第六章习题,写代码之路

c++ Primer Plus(习题6.1)

//获取输入,小写变成大写,大写变小写,一个函数的应用
#include<iostream>
#include<cctype>
int main()
{
	using namespace std;
	char ch;
	cout << "Enter a string,put @ to exit:\n";
	while (cin.get(ch)&&ch != '@')
	{
		if (islower(ch))			//判断是否为小写
			ch = toupper(ch);		//变成大写
		else
			ch = tolower(ch);
		cout << ch;
	}
	cout << "\nBye!\n";
	return 0;
}
c++ Primer Plus(习题6.2)

//输入double值10个,直到输入非数字
//并计算平均值和超过平均值的个数
#include<iostream>
int main()
{
	using std::cout;
	using std::endl;
	using std::cin;
	double d[10];
	int i=0;
	double num = 0;
	
	int count=0;
	cout << "Enter 10 double value: \n";
	while (cin >> d[i] && i < 10)
	{
		num += d[i];
		i++;
	}
	double average = num / i;
	cout << "According to you input,the average is: " << average << endl;
	while (i>0)
	{
		if (d[i] > average)
			count++;
		i--;
	}
	cout << "The number of pass the average is : " << count << endl;
	cout << "Bye!\n";
	return 0;

}
c++ Primer Plus(习题6.3)

//一个菜单的程序,显示内容自己设计
#include<iostream>
int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	cout << "Please enter one of the following choose: \n"
		<< "c) carnivore                p)planist\n"
		<< "t) tree                     g)game\n"
		<< "q) exit\n";
	char choose;
	while (cin >> choose&&choose != 'q')
	{
		switch (choose)
		{
		case 'c':cout << "Cat is a dog.\n"; break;
		case'p':cout << "Planet is a plant.\n"; break;
		case't':cout << "A map is a tree.\n"; break;
		case'g':cout << "Game is another game.\n"; break;
		default:cout << "Please a c,p,t,g,or q: "; break;
		}
		cout << "\nPlease enter one of the following choose: \n"
			<< "c) carnivore                p)planist\n"
			<< "t) tree                     g)game\n"
			<< "q) exit\n";
	}
	cout << "Bye!\n";
	return 0;
}
c++ Primer Plus(习题6.4)

//BOP的成员名字问题,一个结构,,根据选项下来选择怎么输出
//
#include<iostream>
using namespace std;
const int strsize = 20;

//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
};
void showmenu();				//显示菜单
void showname(bop *n);			//显示全名的,其他类似,这里是按指针传递参数的
void showtitles(bop *n);		//
void showbop(bop (&n)[5]);
void showpre(bop *n);
int main()
{
	bop b[5]={					//结构数组一定要用常量创建,外面声明一个const int size=5不行
		{"Wimp Macho","title 1","Mouth",0},
		{"Raki Rhodes","title 2","Mi",2},
		{"Celia Laiter","title 3","Wow",1},
		{"Hoppy Hipman","title 4","Lol",1},
		{"Pat Hand","title 5","Rxh",2}
	};
	char choose;
	showmenu();
	while (cin >> choose&&choose != 'q')
	{
		switch (choose)
		{
		case'a':showname(&b[0]); break;
		case'b':showtitles(&b[0]); break;
		case'c':showbop(b); break;
		case'd':showpre(&b[0]); break;
		default:
			cout << "Please choose rightly:!";
			break;
		}
		showmenu();
	}
	cout << "Bye!\n";
	return 0;
}

void showmenu()
{
	cout << "\nPlease enter one of the following choices: \n"
		<< "a. display by name             b. dispaly by title\n"
		<< "c. display by bopname          d. display by prefence\n"
		<< "q. exit\n";
}

void showname(bop *n)
{
	for (int i = 0; i < 5; i++)
		cout<<(n+i)->fullname<<endl;
}

void showtitles(bop *n)
{

	for (int i = 0; i < 5; i++)
		cout <<n[i].title << endl;
}

void showbop(bop (&n)[5])
{
	for (int i = 0; i < 5; i++)
		cout << n[i].bopname << endl;
		
}
void showpre(bop *n)						//0=fullname,1=title,2=bopname
{
	for (int i = 0; i < 5; i++)
	{
		if (n[i].preference == 0)
			cout << n[i].fullname << endl;
		else if (n[i].preference == 1)
			cout << n[i].title << endl;
		else if (n[i].preference == 2)
			cout << n[i].bopname << endl;
		else
			cout << "Error preference!\n";
	}
}

c++ Primer Plus(习题6.5)

//典型的税率问题,用用判断就ok了
//数学不怎么好的人会弄错,偶就是
#include<iostream>
const int money_1 = 5000;					//为了方便以后调整
const int money_2 = 15000;
const int money_3= 35000;
const double tax_1 = 0.1;
const double tax_2 = 0.15;
const double tax_3 = 0.2;
int main()
{
	using namespace std;
	double income;
	cout << "Enter your income: ";
	while (cin >> income && income>0)
	{
		double taxs = 0;
		cout << "Pay for the taxs: ";
		if (income <=money_1)				//小于5000
			cout << taxs << " Remain:  " << income - taxs << endl;
		else if (income <=money_2)			//5000~15000
		{
			taxs = (income - money_1)*tax_1;		
			cout << taxs << " Remain:  " << income - taxs << endl;
		}
		else if (income <=money_3)				//15000~35000
		{
			taxs = (income - money_2)*tax_2 + (money_2-money_1)*tax_1;	//范围内收的税固定了
			cout << taxs << " Remain:  " << income - taxs << endl;
		}
		else
		{												
			taxs = (income - money_3)*tax_3 + (money_3- money_2)*tax_2 + (money_2 - money_1)*tax_1;
			cout << taxs << " Remain:  " << income - taxs << endl;
		}
		cout << "Enter your income: ";
	}
	cout << "Bye!\n";
	return 0;
}
c++ Primer Plus(习题6.6)

//一个捐钱的结构,谁捐的多,就有成就
#include<iostream>
struct menbers {
	char fullname[20];
	double money;
};
int main()
{
	using namespace std;
	int count;
	cout << "Enter the numbers of menbers: ";
	cin >> count;
	cin.get();					//数字和字符混合输入,记得要处理换行符
	menbers *m = new menbers[count];
	for (int i = 0; i < count; i++)
	{
		cout << "Enter the #"<<i+1<<"fullname: ";
		cin.getline(m[i].fullname, 20);
		cout << "And money: ";
		cin >> m[i].money;
		cin.get();
	}
	cout << "Grand Patrons:	\n";
	int many = 0;								//用来判断是否有这么好心的人
	for (int i = 0; i < count; i++)
	{
		if (m[i].money > 10000)
		{
			cout << m[i].fullname <<"\t"
				<< m[i].money << endl;
			many++;
		}
	}
	if (many == 0)
		cout << "		None		\n";
	many = 0;											//重置,判断平凡人
	cout << "Patrons:		\n";
	for (int i = 0; i < count; i++)
	{
		if (m[i].money<=10000)
		{
			cout << m[i].fullname << "\t"
				<< m[i].money << endl;
			many++;
		}
	}
	if (many == 0)
		cout << "		None		\n";
	delete[]m;
	cout << "Bye!\n";
	return 0;

}
c++ Primer Plus(习题6.7)

//每次读取一个单词,判断多少是元音开头,多少辅音\
//读取单词可以用数组或string类,简单就好,不要纠结
#include<iostream>
#include<cstring>
#include<cctype>
int main()
{
	using namespace std;
	int vowel = 0;		//元音个数
	int cons = 0;		//辅音个数
	int others = 0;
	char q[20];
	cout << "Please enter words(q to exit): ";
	cin >> q;
	while (strcmp(q,"q"))
	{
		if(isalpha(q[0]))
			switch (q[0])
			{
			case 'a':
			case 'A':
			case'e':
			case'E':
			case'i':
			case'I':
			case'o':
			case'O':
			case'u':
			case'U':vowel++; break;
			default:cons++; break;
			}
		else 
			others++;
		cin >> q;
	}
	cout << vowel << " words beginning with vowels.\n"
		<< cons << " words beginning with consonants.\n"
		<< others << " others.\n";
	cout << "Bye!\n";
	return 0;
}
c++ Primer Plus(习题6.8)

//从文件中读字符,这里会显示文件内容
//文件自己提供
#include<fstream>
#include<iostream>
#include<cstdlib>
int main()
{
	using namespace std;
	ifstream read_in;						//建立一个文件流对象
	char filename[10];
	char ch;
	int num = 0;
	cout << "Enter the file name: ";
	cin.getline(filename,10);
	read_in.open(filename);
	if (!read_in.is_open())
	{
		cout << "Can't open the file " << filename << endl;
		cout << "Program terminating!\n";
		exit(EXIT_FAILURE);
	}
	read_in.get(ch);
	while (read_in.good())
	{
		cout << ch;
		num++;
		read_in.get(ch);
	}
	if (read_in.eof())
		cout << "End of file reached!\n";
	cout << "The file " << filename << " contains " << num << " characters.\n";
	read_in.close();
	cout << "Bye!\n";
	return 0;


}
c++ Primer Plus(习题6.9)

//和第六题一样的,不过是从文件中读取数据,并且显示

#include<fstream>
#include<iostream>
#include<cstdlib>
int main()
{
	using namespace std;
	ifstream money;
	char filename[10];
	char ch;
	cout << "Enter the file name: ";
	cin.getline(filename, 10);
	money.open(filename);
	if (!money.is_open())
	{
		cout << "Can't open the file " << filename << endl;
		cout << "Program terminating!\n";
		exit(EXIT_FAILURE);
	}
	int num;
	money >> num;
	cout << num;
	money.get(ch);
	while (money.good())
	{	
		cout << ch;
		money.get(ch);				//实际上可以用结构体的,这里偷了一下懒
	}
	if (money.eof())
		cout << "\nEnd of file reached!\n";
	money.close();
	cout << "Bye!\n";
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值