第四章复合类型 编程练习

本文展示了几个C++程序示例,包括请求用户输入名字、年级、年龄等信息并显示,以及使用char数组和C++string类将姓和名用逗号连接并显示。此外,还演示了如何初始化结构体CandyBar,包括品牌、重量和卡路里,并展示其内容。
摘要由CSDN通过智能技术生成

1.编写一个C++程序,如下述输出示例所示的那样请求并显示信息。

What is your first name? Betty Sue

What is your last name? Yewe

What letter grade do you deserve? B

What is your age? 22

Name: Yewe, Betty Sue

Grade: C

Age: 22

#include<iostream>
using namespace std;
int main()
{
	const int a = 50 ;
	char name[a];
	char lname[a];
	char grade[a];
	char age[a];
	cout << "What is your first name?";
	cin.get(name, a).get();
	cout << "What is your last name?";
	cin.get(lname, a).get();
	cout << "What letter grade do you deserve?";
	cin.get(grade, a).get();
	cout << "What is your age?";
	cin.get(age, a).get();
	cout << "Name:" << lname << "," << name <<endl;
	cout << "Grade:" << grade << endl;
	cout << "Age:" << age << endl;
	return 0;
}

2.修改程序清单4.4,使用C++string类而不是char数组。

//程序清单4.4
#include<iostream>
using namespace std;
int main()
{
	const int size = 100;
	char name[size];
	char dessert[size];
	cout << "Enter your name:" << endl;
	cin.getline(name, size);
	cout << "Enter your favorite dessert " << endl;
	cin.getline(dessert, size);
	cout << "I have some delicious " << dessert;
	cout << "for you, " << name << endl;
	return 0;
}

修改后:

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

3.编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。下面是该程序运行时的情形。

Enter your first name: Flip

Enter your last name: Fleming

Here's the information in a single string: Fleming Flip

#include<cstring>
#inlcude<iostream>
using namespace std;
int main()
{
	const int size = 100;
	char fname[size];
	char lname[size];
	cout << "Enter your first name: ";
	cin.getline(fname, size);
	cout << "Enter your last name: ";
	cin.getline(lname, size);
	cout << "Here's the information in a single string: ";
	strcat_s(lname, ", ");
	strcat_s(lname, fname);
	cout << lname;
	return 0;
}

4.

编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形。

Enter your first name: Flip

Enter your last name: Fleming

Here's the information in a single string: Fleming Flip

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string fname;
	string lname;
	cout << "Enter your first name: ";
	getline(cin,fname);
	cout << "Enter your last name: ";
	getline(cin,lname);
	cout << "Here's the information in a single string: ";
	fname = lname + ", " + fname;
	cout << fname;
	return 0;
}

5. 结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量并将其成员分别初始化为“Mocha Munch”、2.3和350.初始化应在声明snack时进行。最后,程序显示snack变量的内容。

#include<iostream>
using namespace std;
struct CandyBar
{
	char brand[1000];
	double weight;
	int colorie;
};
int main()
{
	CandyBar snack =
	{
		"Mocha Munch",
		2.3,
		350
	};
	cout << "牌子:" << snack.brand << endl;
	cout << "重量:" << snack.weight << endl;
	cout << "卡路里:" << snack.colorie << endl;
	return 0;
}

6.结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为选择的值,然后显示每个结构的内容。

#include<iostream>
using namespace std;
struct CandyBar
{
	char brand[1000];
	double weight;
	int colorie;
};
int main()
{
	CandyBar snack[3] =
	{
		{"Mocha Munch",2.3,350},
		{"special ll",2.5,450},
		{"lily whow",3.5,756},
	};
	cout << "牌子:" << snack[0].brand << endl;
	cout << "重量:" << snack[0].weight << endl;
	cout << "卡路里:" << snack[0].colorie << endl;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值