C++ Primer Plus课后复习题及编程练习第四章-复合类型

本文介绍了C++编程中的基本概念,包括字符数组、数组定义、结构体、枚举类型、指针使用以及动态内存分配,通过实例展示了如何处理不同类型的数据和输入输出操作。
摘要由CSDN通过智能技术生成

 4.12 复习题

1.a:char actor[30];

b:short betsie[100];

c:float chunk[13];

d:long double dipsea[64];

2.#include<array>//要包含头文件

a:array<char,30> actor;

b:array<short,100> betsie;

c:array<float,13> chunk;

d:array<long double,64> dipsea;

3.int array[5]={1,3,5,7,9};

4.int even = array[0] + array[4];

5.float ideas[] = {1, 2, 3, 4};

cout << ideas[1] << " is the second element of this array." << endl;

6.char st[] = "cheeseburger" ;

7.string st = "Waldorf Salad" ;

8.struct Fish{

        string kind;

        int weight;

        double length;

};

9.Fish a_fish = {"carp" , 2 , 27.3}; 

10.enum Response{ //默认从0开始,逐一递增

        No , Yes , Maybe

}; //or

enum Response{

        Yes = 1 , No = 0 , Maybe = 2 

};

11.double ted = 5.2 ;

double* p = &ted ;

cout << ted << endl ;

cout << *p << endl;

12.float treacle[10] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10};

float* p = treacle ;

cout << p[9]  << endl ;

13.unsigned int size ;

cout << "Please enter a number:" ;

cin >> size ;

int* p = new int[size] ;

15.Fish* b_fish = new Fish ;

cout << "Please enter a fish:" << endl ;

cin >> b_fish->kind ;

17.

#include<iostream>
#include<array>
#include<vector>

const int size = 10;
std::vector<std::string> vest(size);
std::array<std::string, size> arst;

4.13 编程练习

1.

#include<iostream>
using namespace std;

int main() {
    
    //1.
	char first_name[20], last_name[20];
	char grade;
	int age;

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

	cout << "Name: " << last_name << " , " << first_name << endl;
	cout << "Grade: " << grade << endl;
	cout << "Age: " << age << endl;


	system("pause");
	return 0;
}

2.

#include<iostream>
#include<string>
using namespace std;

int main() {

	//2.
	//const int Arsize = 20;
	//char name[Arsize];
	//char dessert[Arsize];

	//cout << "Enter your name:" << endl;
	//cin.getline(name, Arsize);//reads through newline
	//cout << "Enter your favouite dessert:" << endl;
	//cin.getline(dessert, Arsize);
	//cout << "I have some delicious " << dessert;
	//cout << " for you, " << name << "." << endl;

	string name;
	string dessert;

	cout << "Enter your name:" << endl;
	getline(cin, name);//需要头文件<string>
	cout << "Enter your favouite dessert:" << endl;
	cin >> dessert;
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << "." << endl;


	system("pause");
	return 0;
}

3.

#include<iostream>
#include<cstring>
using namespace std;

int main() {

	//3.
	char f_name[10], l_name[10], full_name[20];
	cout << "Enter your first name: ";
	cin.getline(f_name, 10);
	cout << "Enter your last name: ";
	cin.getline(l_name, 10);
	cout << "Here's the information in a single string: ";
	strcpy_s(full_name, l_name);
	strcat_s(full_name, " , ");
	strcat_s(full_name, f_name);
	cout << full_name << endl;

	system("pause");
	return 0;
}

4.

#include<iostream>
#include<string>
using namespace std;

int main() {

	//4.
	string f_name, l_name, full_name;
	cout << "Enter your first name: ";
	getline(cin, f_name);
	cout << "Enter your last name: ";
	getline(cin, l_name);
	cout << "Here's the information in a single string: ";
	full_name = l_name + " , " + f_name;
	cout << full_name << endl;

	system("pause");
	return 0;
}

5.

#include<iostream>
using namespace std;

struct CandyBar {
	string brand;
	double weight;
	int calorie;
};

int main() {

    //5.
	CandyBar snake;
	CandyBar* p = &snake;
	p->brand = "Mocha Munch";
	p->weight = 2.3;
	p->calorie = 350;
	cout << "snake's brand:" << p->brand << endl;
	cout << "snake's weight:" << p->weight << endl;
	cout << "snake's calorie:" << p->calorie << endl;

	system("pause");
	return 0;
}

6.

#include<iostream>
using namespace std;

struct CandyBar {
	string brand;
	double weight;
	int calorie;
};

int main() {

	//6.
	CandyBar snake[3] = {
		{"Mocha Munch",2.3,350},
		{"Mocha",6.5,500},
		{"Mocha Munch",3.6,450},
	};

	for (int i = 0; i < 3; i++) {
		cout << "snake's brand:" << snake[i].brand << endl;
		cout << "snake's weight:" << snake[i].weight << endl;
		cout << "snake's calorie:" << snake[i].calorie << endl;
		cout << endl;
	}

	system("pause");
	return 0;
}

7.

#include<iostream>
using namespace std;

struct Pizza {
	char name[30];
	double weight;
	int diameter;
};

int main() {

	//7.
	int num;
	cout << "How many Pizza company? ";
	cin >> num;
	cin.ignore();//不加这一行代码,下面getline会被跳过

	Pizza* p = new Pizza[num];

	for (int i = 0; i < num; i++) {
		cout << "Please enter the Pizza's name:";
		cin.getline(p[i].name, 30);
		//cin >> p[i].name;

		cout << "Please enter the Pizza's weight:";
		cin >> p[i].weight;

		cout << "Please enter the Pizza's diameter:";
		cin >> p[i].diameter;
		cin.ignore();
		cout << endl;
	}

    delete p;

	system("pause");
	return 0;
}

10.

#include<iostream>
#include<array>
using namespace std;

int main() {

	array<double, 3> run_score;
	double sum = 0;

	for (int i = 0; i < 3; i++) {
		cout << "Please enter the No." << i+1 << "'s score: ";
		cin >> run_score[i];
		sum += run_score[i];
	}

	cout << "The average time is " << sum / 3 << endl;

	system("pause");
	return 0;
}

  • 35
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值