c++ primer plus chapter

第一题

​
#include <iostream>

int main(void)
{
	using namespace std;
	const int size = 20;

	char first_name[size], last_name[size];
	char grade;
	int age;

	cout << "What is your first name?";
	cin.getline(first_name, size);
	cout << "What is your last name?";
	cin.getline(last_name, size);
	cout << "What letter grade do you deserver?";
	cin >> grade;
	cout << "What is your age?";
	cin >> age;

	cout << "Name: " << last_name << ", " << first_name << endl;
	cout << "Grade: " << char(grade+1) << endl;
    //字符变量加一等于ASCALL码值加一,将被整型提升故需强制类型转换回char类型
	cout << "Age: " << age << endl;

	return 0;
}

​

第二题

#include <iostream>
#include <string>

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

	cout << "Enter your name: " << endl;
	getline(cin, name);//用字符数组接收时用cin.getline(array,size);因为字符数组需要指定输入大小
	//用string类对象接收时用getline函数,因为string类对象会自动调整大小
	cout << "Enter your favorite dessert: " << endl;
	getline(cin, dessert);

	cout << "I have some delicious " << dessert << " for you, " << name << ". " << endl;

	return 0;
}

第三题

#include <iostream>
#include <cstring>

int main(void)
{
	using namespace std;
	const int SIZE = 20;

	char first_name[SIZE], last_name[SIZE];
	char full_name[2*SIZE];

	cout << "Please enter your first name:" << endl;
	cin.getline(first_name, SIZE);
	cout << "Please enter your last name:" << endl;
	cin.getline(last_name, SIZE);
	
	strcpy(full_name, last_name);//拷贝
	strcat(full_name, ", ");//追加
	strcat(full_name, first_name);

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

	return 0;
}

第四题

#include <iostream>
#include <string>

int main(void)
{
	using namespace std;
	string first_name, last_name, full_name;

	cout << "Please enter your first name:" << endl;
	getline(cin, first_name);
	cout << "Please enter your last name:" << endl;
	getline(cin, last_name);
	
	full_name = last_name + ", " + first_name;

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

	return 0;
}

第五题

#include <iostream>

struct CandyBar
{
	char brand[20];
	float weight;
	unsigned int calorie;
};

int main(void)
{
	using namespace std;

	CandyBar snack = {"Mocha Munch", 2.3, 350};

	cout << "Band = " << snack.brand << endl;
	cout << "Weight = " << snack.weight << endl;
	cout << "Calorie = " << snack.calorie << endl;

	return 0;
}

第六题

#include <iostream>

struct CandyBar
{
	char brand[20];
	float weight;
	unsigned int calorie;
};

int main(void)
{
	using namespace std;

	CandyBar snack[3] = {{"Mocha Munch", 2.3, 350}, {"Flip Fleming", 3.2, 500}, {"Micheal Bay", 4.3, 400}};

	cout << "1st Band = " << snack[0].brand << endl;
	cout << "1st Weight = " << snack[0].weight << endl;
	cout << "1st Calorie = " << snack[0].calorie << endl;
	cout << "*************************************" << endl;

	cout << "2nd Band = " << snack[1].brand << endl;
	cout << "2nd Weight = " << snack[1].weight << endl;
	cout << "2nd Calorie = " << snack[1].calorie << endl;
	cout << "*************************************" << endl;

	cout << "3th Band = " << snack[2].brand << endl;
	cout << "3th Weight = " << snack[2].weight << endl;
	cout << "3th Calorie = " << snack[2].calorie << endl;
	cout << "*************************************" << endl;

	return 0;
}

第七题

#include <iostream>

struct Pizza
{
	char company[20];
	float diameter;
	float weight;
};

int main(void)
{
	using namespace std;

	Pizza dinner;

	cout << "Please enter the Pizza's company:";
	cin.getline(dinner.company, 20);
	cout << "Please enter the size of pizza in inches:";
	cin >> dinner.diameter;
	cout << "Please enter the weight of pizza in pounds:";
	cin >> dinner.weight;

	cout << "Pizza company: " << dinner.company << ". "<< "diameter in inches: " << dinner.diameter << ". " << "Weight in pounds: " << dinner.weight << endl;


	return 0;
}

第八题

#include <iostream>

struct Pizza
{
	char company[20];
	float diameter;
	float weight;
};

int main(void)
{
	using namespace std;

	Pizza *pizza = new Pizza;

	cout << "Please enter the Pizza's company:";
	cin.getline(pizza->company, 20);
	cout << "Please enter the size of pizza in inches:";
	cin >> pizza->diameter;
	cout << "Please enter the weight of pizza in pounds:";
	cin >> pizza->weight;

	cout << "Pizza company: " << pizza->company << ". "<< "diameter in inches: " << pizza->diameter << ". " << "Weight in pounds: " << pizza->weight << endl;

	delete pizza;

	return 0;
}

第九题

#include <iostream>
#include <cstring>

struct CandyBar
{
	char brand[20];
	float weight;
	unsigned int calorie;
};

int main(void)
{
	using namespace std;

	//CandyBar snack[3] = {{"Mocha Munch", 2.3, 350}, {"Flip Fleming", 3.2, 500}, {"Micheal Bay", 4.3, 400}};
	CandyBar *pt = new CandyBar[3];

	strcpy(pt[0].brand, "Mocha Munch");
	pt[0].weight = 2.3;
	pt[0].calorie = 350;

	strcpy(pt[1].brand, "Flip Fleming");
	pt[1].weight = 3.2;
	pt[1].calorie = 500;

	strcpy(pt[2].brand, "Micheal Bay");
	pt[2].weight = 4.3;
	pt[2].calorie = 400;

	cout << "1st Band = " << pt->brand << endl;
	cout << "1st Weight = " << pt->weight << endl;
	cout << "1st Calorie = " << pt->calorie << endl;
	cout << "*************************************" << endl;

	cout << "2nd Band = " << (pt+1)->brand << endl;
	cout << "2nd Weight = " << (pt+1)->weight << endl;
	cout << "2nd Calorie = " << (pt+1)->calorie << endl;
	cout << "*************************************" << endl;

	cout << "3th Band = " << (pt+2)->brand << endl;
	cout << "3th Weight = " << (pt+2)->weight << endl;
	cout << "3th Calorie = " << (pt+2)->calorie << endl;
	cout << "*************************************" << endl;

	delete [] pt;

	return 0;
}

第十题

#include <iostream>
#include <array>

int main(void)
{
	using namespace std;

	array<float, 3> record_list;
	float average;

	cout << "Please enter three records of 40 meters:" << endl;
	cout << "First record:";
	cin >> record_list[0];
	cout << "Second record:";
	cin >> record_list[1];
	cout << "Third record:";
	cin >> record_list[2];

	cout << "1st: " << record_list[0] << "; " << "2nd: " << record_list[1] << "; " << "3rd: " << record_list[2] << endl;

	average = (record_list[0] + record_list[1] + record_list[2]) / 3;
	cout << "Average: " << average << endl;

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值