栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ Primer Plus第四章复习题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ Primer Plus第四章复习题

//1
//a
char actor[30];
//b
short betsie[100];
//c
float chuck[13];
//d
long double dipsea[64];
//2
#incldue 
array actor;
array betsie;
array chuck;
array dipsea;
//3
int arr[5] = {1,3,5,7,9};
//4
int arr[5] = {1,3,5,7,9};
int even = arr[0] + arr[4];
//5
float ideas[10];
cout << ideas[1] << endl;
//6
char str[] = "cheeseburger";//让编译器自己计算数组的长度
//7
string str = "Waldorf Salad";
//8
struct fish
{
	char kind[20];
	int weight;
	float length;
};
//9
struct fish
{
	char kind[20];
	int weight;
	float length;
};

fish ff = {"BigFish",12,8.8};
//10
enum Response {No,Yes,Maybe};
//11
double ted = 1.8;
double *pt = &ted;
cout << "ted = " << *pt << endl;
//12
float treacle[10];
float *pa = &treacle[0];// float *pa = treacle;
cout << pt[0] << " " << pt[9] << endl;

//13
cout << "Enter a number: " << endl;
cin >> size;

int *pt = new int[size];

vector arr(size);//注意是圆括号不是方括号
//14
cout << (int *)"Home of the jolly bytes";
//该程序可执行,但输出的是字符串的地址不会输出字符串的内容

//15
struct fish
{
	char king[30];
	int weitht;
	float length;
};
fish *pt = new fish;
cout << pt->kind << ", " << pt->weight << ", " << pt->length << endl;

//16
char a[5];
cin.getline(a,5);//最多读取4个字符,最后一个自动填充空字符
cout << a << endl;

char b[5];
cin >> b;     //从第一个不是空格开始,存入往后5个字符,若5个字符内出现空格,则在空格位停止
cout << b << endl;
//17
#include 
#include 
#include 

const int size = 10;
std::vectorarr(size);
std::arrayarray;
//1-1使用字符串数组
#include 
using namespace std;
int main()
{
	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;
	cout << "Age: " << age << endl;
		
}
//1-2使用string
#include 
#include 
using namespace std;
int main()
{
	string first_name,last_name;
	char grade;
	int age;
	
	cout << "What is your first name?";
	getline(cin,first_name);
	cout << "What is your last name?";
	getline(cin,last_name);
	
	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;
	cout << "Age: " << age << endl;
		
}
//2根据程序清单4.4修改
#include//string
#include 
using namespace std;
int main()
{
//	const int Arsize = 20;
//	char name[Arsize];
//	char dessert[Arsize];
	string name;
	string dessert;
	
	cout << "Enter your name: " << endl;
	getline(cin,name);
//	cin.getline(name,Arsize);
	
	cout << "Please enter your favorite dessert: " << endl;
	getline(cin,dessert);
//	cin.getline(dessert,Arsize);
	
	cout << "I have some delicious " << dessert << " for you, " << name <<".n"; 
//相比于数组输入,string的方式更为简单,且不用定义长度
}

//3
#include 
#include 
#include 
using namespace std;
int main()
{
	const int size = 20;
	char first_name[size];
	char last_name[size];
	char full_name[2*size];
	cout << "Enter your first name:";
	cin.getline(first_name,size);
	cout << "Enter your last_name:";
	cin.getline(last_name,size);
	
	strcpy(full_name,last_name);//先把last_name复制到full_name
	strcat(full_name,", ");     //再把需要添加的部分加到full_name
	strcat(full_name,first_name);//最后把first_name添加到full_name
	
	cout << "Here's the information in a single string: " << full_name << endl;	
}
//4
#include 
#include 
#include 
using namespace std;
int main()
{
	string first_name;
	string last_name;
	string s;
	
	cout << "Enter your first name:";
	getline(cin,first_name);
	cout << "Enter your last_name:";
	getline(cin,last_name);
	s = last_name + ", " + first_name; //注意这里双引号是因为逗号后面还跟了空格,而''里面只能跟一个字符
	cout << "Here's the information in a single string: " << s << endl;
}
//5
#include 
using namespace std;
struct CandyBar
{
	char band[20];
	float weight;
	unsigned int calorie;	
};   //结构体定义注意还有分号

int main()
{
	CandyBar snack = {"Mocha Munch",2.3,350};
	
	cout << "Band = " << snack.band << endl;
	cout << "Weight = " << snack.weight << endl;
	cout << "Calorie = " << snack.calorie << endl;
	
}
//6
#include 
using namespace std;
struct CandyBar
{
	char band[20];
	float weight;
	unsigned int calorie;	
};

int main()
{
	CandyBar snack[3] = {{"Mocha Munch",2.3,350},{"Flip Fleming",3.2,500},{"Micheal Bay",4.3,400}};
	
	
	for(int i=0;i<3;i++)
	{
		cout << i+1 << "st Band = " << snack[i].band << endl;
		cout << i+1 << "st Weight = " << snack[i].weight << endl;
		cout << i+1 << "st Calorie = " << snack[i].calorie << endl;
		cout << endl;
	}
//	cout << "1st Band = " << snack[0].band << endl;
//	cout << "1st Weight = " << snack[0].weight << endl;
//	cout << "1st Calorie = " << snack[0].calorie << endl;
//	cout << "2st Band = " << snack[1].band << endl;
//	cout << "2st Weight = " << snack[1].weight << endl;
//	cout << "2st Calorie = " << snack[1].calorie << endl;	
//	cout << "3st Band = " << snack[2].band << endl;
//	cout << "3st Weight = " << snack[2].weight << endl;
//	cout << "3st Calorie = " << snack[2].calorie << endl;	
}
//7
#include 
using namespace std;

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

int main()
{
	pizza dinner;
	cout << "Please, enter the Pizza' company: ";
	cin.getline(dinner.company,20);
	
	cout << "Please, enter the size of the pizza in inches: ";
	cin >> dinner.diameter;
	
	cout << "Please, enter the weight of the pizza: " ;
	cin >> dinner.weight;
	
	cout << "Company: " << dinner.company << endl;
	cout << "diameter: " << dinner.diameter << endl;
	cout << "weight: " << dinner.weight << endl;
}
//8
#include 
using namespace std;

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

int main()
{
	Pizza *pizza = new Pizza;
	
	cout << "Please, enter the Pizza' company: ";
	cin.getline(pizza->company,20);
	
	cout << "Please, enter the size of the pizza in inches: ";
	cin >> pizza->diameter;
	
	cout << "Please, enter the weight of the pizza: " ;
	cin >> pizza->weight;
	
	cout << "Company: " << pizza->company << endl;
	cout << "diameter: " << pizza->diameter << endl;
	cout << "weight: " << pizza->weight << endl;

	delete pizza;//一定不要忘了释放内存空间
}
//9
#include 
#include 
using namespace std;
struct CandyBar
{
	char brand[20];
	float weight;
	unsigned int calorie;	
};

int main()
{
//	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->weight = 2.3; //用指针的方式来访问
//	pt[0].weight = 2.3; //把指针当作数组来访问
	pt[0].calorie = 350;
	
	strcpy(pt[1].brand,"Flip Fleming");
	pt[1].weight = 3.2;
	(pt+1)->calorie = 500;
//	pt[1].calorie = 500;	
	
	strcpy(pt[2].brand,"Micheal Bay");
	pt[2].weight = 4.3;
	pt[2].calorie = 400;	
	
	for(int i=0;i<3;i++)
	{
		cout << i+1 << "st Brand = " << (pt+i)->brand << endl;
		cout << i+1 << "st Weight = " << (pt+i)->weight << endl;
		cout << i+1 << "st Calorie = " << (pt+i)->calorie << endl;
		cout << endl;
	}
	
	delete pt;	
}
//10
#include 
#include 
using namespace std;
int main()
{
	//array<数据类型,数据个数>变量名;
	array record_list;
	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];	
	
	float average = 0.0;
	for(int i=0;i<3;i++) //可以一个一个打印,也可以用for循环输出打印
	{
		average = average + record_list[i];
	}
	cout << "average of three records: " << average/3.0 << endl;
	
	for(int i=0;i<3;i++)
	{
		cout << i+1 << "record: " << record_list[i] << endl;
	}
}

参考视频-嵌入式技术公开课

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879901.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号