【C++学习】习题记录 第四章 复合类型

第四章 复合类型

第一题:按示例请求显示信息。

#include<iostream>
using namespace std;
int main()
{
	cout << "What's your first name? ";
	char first_name[20];
	cin.getline(first_name,19);
	cout << "What's your last name? ";
	char last_name[20];
	cin.getline(last_name, 19);
	cout << "What letter grade do you deserve ? ";
	char grade;
	cin >> grade;
	cout << "What is your age ? ";
	int age;
	cin >> age;
	cout << "Name: " << last_name << ',' << first_name << endl;
	cout << "Grade: " <<char (grade+1)  << endl;
	cout << "Age: " << age<<endl;
	cin.get();
	return 0;
}

结果显示4.1

第二题:修改程序4.4,使用C++string类

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string dessert;
	string name;
	cout << "Enter your last name:  ";
	getline(cin,name);
	cout << "Enter your favourite dessert: ";
	getline(cin, dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you," << name << ".\n";
	cin.get();
	return 0;
}

结果显示4.2

第三题:使用char数组,和cstring中的函数,按示例格式显示输出

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char f_name[20];
	char l_name[20];
	char name[40] = {0};//初始化为0
	cout << "Enter your first name: ";
	cin >> f_name;
	cout << "Enter your last name: ";
	cin >> l_name;
	strcat_s(name,l_name);
	strcat_s(name,  ", "); 
	strcat_s(name, f_name);
	cout << "Here's the imformation in a string: " << name<<endl;
	cin.get();
	return 0;
}

结果显示4.3

第四题:使用string对象,和string中的函数,按示例格式显示输出

#include<iostream>
#include<string>
int main(void)
{
	using namespace std;
	cout << "Enter your first name: ";
	string f_name;
	cin >> f_name;
	cout << "Enter your last name: ";
	string l_name;
	cin >> l_name;
	string name;
	name = f_name + ", " + l_name;
	cout << "Here's the imformation in a string: "<<name<<endl;
	cin.get();
	return 0;
}

结果显示4.4

第五题:编写一个包含三个成员的结构并声明,再创建一个该结构的变量并显示

#include<iostream>
#include<string>
using namespace std;
struct CandyBar { string name; double weight; int calorie; };
int main(void)
{
	CandyBar snack{ "Mocha Munch" ,2.3,350};
	cout<<snack.name<<" is "<<snack.weight<<"g with " <<snack.calorie<<" calories."<<endl;
	cin.get();
	return 0;
}

结果显示4.5

第六题:编写一个包含三个成员的结构并声明,再创建包含3个该结构的数组并初始化,最后显示每个结构的内容

#include<iostream>
#include<string>
using namespace std;
struct CandyBar { string name; double weight; int calorie; };
int main()
{
	CandyBar  candy[3]={{"One_mocha",2.5,30},{"Two_mocha",3.5,40 },{"Three_mocha",4.5,50}};
	cout << candy[0].name << " is " << candy[0].weight;
	cout<< "g with " << candy[0].calorie << endl;
	cout << candy[1].name << " is " << candy[1].weight;
	cout<< "g with " << candy[1].calorie << endl;
	cout << candy[2].name << " is " << candy[2].weight;
	cout<< "g with " << candy[2].calorie << endl;
	cin.get();
	return 0;
}

结果显示4.6

第七题:编写一个包含三个成员的结构并声明,根据用户输入初始化并显示

#include<iostream>
#include<string>
using namespace std;
struct PizzaBar { char name[30]; double diameter; double weight; };
int main(void)
{
	PizzaBar WW;
	cout << "Enter the name of the company: ";
	cin.getline(WW.name, 29);
	cout << "Enter the pizza diameter: ";
	cin >> WW.diameter;
	cout << "Enter the pizza weight: ";
	cin >> WW.weight;
	cout << WW.name << "'pizza is " << WW.weight << "g with " << WW.diameter << " cm." << endl;
	cin.get();
	cin.get();
	return 0;
}

结果显示4.7

第八题:使用new完成上一题,根据用户输入初始化并显示

#include<iostream>
#include<string>
using namespace std;
struct PizzaBar { char name[30]; double diameter; double weight; };
int main(void)
{
	PizzaBar *p= new PizzaBar;
	cout << "Enter the pizza diameter: ";
	cin>>p->diameter;
	cout << "Enter the name of the company: ";
	cin.get();                                   //取消换行符;
	cin.getline(p->name,29);
	cout << "Enter the pizza weight: ";
	cin >> p->weight;
	cout << (*p).name << "'pizza is " << p->weight << "g with " <<p->diameter << " cm." << endl;
	cin.get();
	cin.get();
	return 0;
}

结果显示4.8

第九题:重新完成第六题,使用new来分配数组,然后初始化并显示

#include<iostream>
using namespace std;
struct CandyBar { char name[30]; double weight; int calorie; };
int main(void)
{
	CandyBar  *p=new CandyBar[3];
	p[0] = {"One_mocha",2.5,30 };
	p[1] = {"Two_mocha",3.5,40 };
	p[2] = {"Three_mocha",4.5,50 };
	cout << p[0].name << " is " << p[0].weight;
	cout << "g with " << p[0].calorie << endl;
	cout << p[1].name << " is " << p[1].weight;
	cout << "g with " << p[1].calorie << endl;
	cout << p[2].name << " is " << p[2].weight;
	cout << "g with " << p[2].calorie << endl;
	cin.get();
	return 0;
}

结果显示4.9

第四章结束。_

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉木渡香

感谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值