C++ primer plus(第六版)第四章练习题

</pre><p></p><p></p><pre name="code" class="cpp">/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第一题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;

int main()
{
	const int NameSize = 20;
	string name_first;
	string name_last;
	char m_grade;
	unsigned int m_age;
	cout << "What is your first name: ";	
	getline(cin, name_first);

	cout << "What is your last name: ";
	getline(cin, name_last);
	
	cout << "What letter grade do you deserve? ";
	cin >> m_grade;

	cout << "What is your age: ";
	cin >> m_age;

	m_grade = toupper(m_grade);

	switch (m_grade)
	{
	case 'A': m_grade = 'B'; break;
	case 'B': m_grade = 'C'; break;
	case 'C': m_grade = 'D'; break;
	default: m_grade = 'E'; 
	}

	cout << "Name: " << name_last << ", " << name_first << endl;
	cout << "Grade: " << m_grade << endl;
	cout << "Age: " << m_age << endl;

	return 0;
}


/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第二题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

int main()
{
	using namespace std;
	string name;
	string dessert;

	cout << "Enter your name:\n";
	getline(cin, name);
	cout << "Enter your favorite dessert:\n";
	getline(cin, dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";
	return 0;
}


/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第三题
/********************************************************************************************************************************************************/
#include<iostream>
#include<cstring>
#include<string>

#pragma warning(disable:4996)
using namespace std;

int main(void)
{
	const int nameSize = 20;
	char name_first[nameSize], name_last[nameSize], fullname[nameSize*2];
	cout << "Enter your first name: ";
	cin.getline(name_first, nameSize);

	cout << "Enter your last name: ";
	cin.getline(name_last, nameSize);

	strcpy(fullname, name_last);
	strcat(fullname, ",");
	strcat(fullname, name_first);

	cout << "Here's the information in a single string: " << fullname;

	return 0;

}


/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第四题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;
int main(void)
{
	string name_first, name_last, fullname;

	cout << "Enter your first name: ";
	getline(cin, name_first);
	cout << "Enter your last name: ";
	getline(cin, name_last);

	fullname = name_last;
	fullname += ",";
	fullname += name_first;

	cout << "Here's the information in a single string: " << fullname << endl;

	return 0;
}

/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第五题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;

struct CandyBar
{
	string brand;
	float weight;
	int kalor;
};

int main(void)
{
	CandyBar snack = { "Mocha Munch", 2.3, 350 };
	cout << "The brand name is: " << snack.brand << endl;
	cout << "The brand weight is: " << snack.weight << endl;
	cout << "The brand kalor is: " << snack.kalor << endl;

	return 0;
}

/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第六题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;

struct CandyBar
{
	string brand;
	float weight;
	int kalor;
};

int main(void)
{
	CandyBar xufuji[3] = { { "first", 1.1, 1 }, { "second", 2.2, 2 }, { "third", 3.3, 3 } };
	for (int i = 0; i < 3; i++)
	{
		cout.width(10);
		cout << xufuji[i].brand;
		cout.width(5);
		cout << xufuji[i].weight;
		cout.width(5);
		cout << xufuji[i].kalor;
		cout << endl;
	}

	return 0;

}

/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第七题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;

struct pizza{
	string company;
	float diameter;
	float weight;
};

int main(void)
{
	pizza pizza1;
	cout << "Enter the pizza's company: ";
	getline(cin, pizza1.company);

	cout << "Enter the pizza's diameter: ";
	cin >> pizza1.diameter;

	cout << "Enter the pizza's weight: ";
	cin >> pizza1.weight;

	cout << "Now display: " << pizza1.company << "  " << pizza1.diameter << "  "
		<< pizza1.weight << endl;

	return 0;
}

/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第八题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;

struct pizza
{
	string company;
	float diameter;
	float weight;
};

int main(void)
{
	pizza* p_pizza1 = new pizza;

//	cout << "Enter pizza's company: ";
//	getline(cin, p_pizza1->company);

	cout << "Enter pizza's diameter: ";
	(cin >> p_pizza1->diameter).get(); //读取一个数值和其后的回车键

	cout << "Enter pizza's company: ";
	getline(cin, p_pizza1->company);

	cout << "Enter pizza's weight: ";
	cin >> p_pizza1->weight;

	cout << "Now display :" << (*p_pizza1).company << "  " <<  (*p_pizza1).diameter << "  "
		<< (*p_pizza1).weight << endl;

	delete p_pizza1;

	return 0;
}

/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第九题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;
struct CandyBar
{
	string brand;
	float kalor;
	int weight;
};

int main(void)
{
	CandyBar * p_candybar = new CandyBar[3];
	p_candybar[0] = { "Candy1", 1.1, 11 };
	p_candybar[1] = { "Candy2", 2.2, 22 };
	p_candybar[2] = { "Candy3", 3.3, 33 };
	
	for (int i = 0; i < 3; i++)
	{
		cout << p_candybar[i].brand << "  " << p_candybar[i].kalor << "  " << p_candybar[i].weight
			<< endl;
	}

	delete[] p_candybar;
	return 0;
}

/********************************************************************************************************************************************************/
Author  :  hongye_05
Blog      :  blog.csdn.net/hongye_05
Time     :   2014.10.12

C++ primer plus(第六版)第四章 第十题
/********************************************************************************************************************************************************/
#include<iostream>
#include<array>

int main(void)
{
	using namespace std;
	array<double, 3> grade;
	double aver = 0;
	for (int i = 0; i < 3; ++i)
	{
		cout << "Enter the grade of " << i + 1 << "th	time: ";
		cin >> grade[i];
		aver += grade[i];
	}
	aver /= 3;
	cout << "The averge grade is " << aver << endl;
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值