C++ Primer Plus 第四章之编程练习题

4.1按要求显示信息

#include<iostream>
#include<string> 
using namespace std;
int main(){
	string fname,lname;
	char grade;
	int age;
	cout<<"What is your first name?";
	getline(cin,fname);
	cout<<"\nWhat is your last name?";
	getline(cin,lname);
	cout<<"\nWhat letter grade do you deserve?";
	cin>>grade;
	cout<<"\nWhat is your age?";
	cin>>age;
	cout<<"\nname:"<<lname<<","<<fname<<endl;
	cout<<"\nGrade:"<<char(int(grade)+1)<<endl;
	cout<<"\nAge:"<<age;
	return 0;
	
} 

运行结果
在这里插入图片描述

4.2 修改程序清单4.4

#include<iostream>
#include<string>
using namespace std;
int main(){
	string name,dessert;
	
	cout<<"Enter your name: \n";
	getline(cin,name);
	cout<<"Enter your favorite dessert: \n";
	getline(cin,dessert);
	cout<<"Your name is "<<name<<",and your favorite dessert is "<<dessert<<endl;
	return 0;
} 

4.3 用char组合姓名

#include<iostream>
#include<cstring>
//里面有strcat()函数

using namespace std;
int main(){
	char fname[80];
	char lname[80];
	cout<<"Enter your first name: ";
	cin.getline(fname,80);
	cout<<"Enter your last name: ";
	cin.getline(lname,80);
	
	strcat(fname,", ");
	strcat(fname,lname);
	cout<<"Here is "<<fname<<endl;
	return 0;
} 

strcat()函数是专门用来将两个char类型变量连接起来形成新的char类型变量的函数,该函数在头文件cstring中被定义,拼接中间不会有间隔,将后一个连接到前一个的后面,形成新的char数组赋值,覆盖前面的char数组。

4.4 用string组合姓名

#include<iostream>
#include<string>

using namespace std;
int main(){
	string fname;
	string lname;
	string wname;
	cout<<"Enter your first name: ";
	getline(cin,fname);
	cout<<"Enter your last name: ";
	getline(cin,lname);
	
	wname = fname+", "+lname;
	cout<<"Here is "<<wname<<endl;
	return 0;
}

直接用string的拼接在一起就可以了。只不过跟 string的输入跟char的输入不一样。

4.5 结构体

#include<iostream>
using namespace std;

//主张结构体声明定义在外
//结构体变量定义在内 
struct CandyBar{
	string brand;
	double weight;
	int calorie;
}; 
int main(){
	CandyBar snack ={"Mocha Munch",2.3,350};
	cout<<snack.brand<<endl;
	cout<<snack.weight<<endl;
	cout<<snack.calorie<<endl;
	return 0;
} 

4.6 结构体 用数组

#include<iostream>
using namespace std;

//主张结构体声明定义在外
//结构体变量定义在内 
struct CandyBar{
	string brand;
	double weight;
	int calorie;
}; 
int main(){
	CandyBar snack[3] ={{"Mocha Munch",2.3,350},
	{"Mocha",3.3,450},
	{"Munch",4.3,550}};
	cout<<snack[0].brand<<endl;
	cout<<snack[0].weight<<endl;
	cout<<snack[0].calorie<<endl;
	cout<<snack[1].brand<<endl;
	cout<<snack[1].weight<<endl;
	cout<<snack[1].calorie<<endl;
	cout<<snack[2].brand<<endl;
	cout<<snack[2].weight<<endl;
	cout<<snack[2].calorie<<endl;
	return 0;
} 

4.7 披萨公司

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

//主张结构体声明定义在外
//结构体变量定义在内 
struct Biza{
	string company;
	double size;
	double weight;
}; 
int main(){
	Biza b;
	getline(cin,b.company);
	cin>>b.size;
	cin>>b.weight;
	
	cout<<b.company<<endl;
	cout<<b.size<<endl;
	cout<<b.weight<<endl;
	return 0;
} 

4.8 结构体用new

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

//主张结构体声明定义在外
//结构体变量定义在内 
struct Biza{
	string company;
	double size;
	double weight;
}; 
int main(){
	//Biza * b = new Biza;
	Biza * b;
	b = new Biza;
	getline(cin,b->company);
	cin>>b->size;
	cin>>b->weight;
	
	cout<<b->company<<endl;
	cout<<b->size<<endl;
	cout<<b->weight<<endl;
	delete b;
	return 0;
} 

注意是用 delete b;还是delete [] b;

4.9 new动态分配数组

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

//主张结构体声明定义在外
//结构体变量定义在内 
struct CandyBar{
	string brand;
	double weight;
	int calorie;
}; 
int main(){
//	CandyBar snack[3] ={{"Mocha Munch",2.3,350},{"Mocha",3.3,450},{"Munch",4.3,550}};
	CandyBar * snack = new CandyBar[3];
	snack[0] = {"Mocha Munch",2.3,350};
	snack[1] = {"Mocha",3.3,450};
	snack[2] = {"Munch",4.3,550};
	cout<<snack[0].brand<<endl;
	cout<<snack[0].weight<<endl;
	cout<<snack[0].calorie<<endl;
	cout<<snack[1].brand<<endl;
	cout<<snack[1].weight<<endl;
	cout<<snack[1].calorie<<endl;
	cout<<snack[2].brand<<endl;
	cout<<snack[2].weight<<endl;
	cout<<snack[2].calorie<<endl;
	
	delete [] snack;
	return 0;
} 

用new要注意用delete

4.10 存储40米跑成绩

#include<iostream>
using namespace std;

int main() {
	const int num = 3;
//	array<double,num> a;
	double a[3];
	cin>>a[0];
	cin>>a[1];
	cin>>a[2];
	
	cout<<"平均成绩:"<<(a[0]+a[1]+a[2])/3<<endl;
	
	return 0;
}
发布了9 篇原创文章 · 获赞 0 · 访问量 1800

猜你喜欢

转载自blog.csdn.net/weixin_46021869/article/details/104076592