c++primer plus 第四章编程练习

15 篇文章 0 订阅
14 篇文章 0 订阅
int main(int argc, char* argv[])
{
	using std::cout;
	using std::cin;
	using std::endl;
	using std::string;
	using std::array;
	using std::get;

	const int SIZE{ 20 };
	//1
	char ch[SIZE];
	cout << "what your first name: ";
	cin.get(ch, SIZE).get();
	cout << ch << endl;;
	cout << "last name: ";
	cin.get(ch, SIZE).get();
	cout << ch << endl;;//省
	//2
	string str;
	cout << "...:  ";
	getline(cin, str);
	cout << str << endl;;
	cout << "...:  ";
	getline(cin, str);
	cout << str << endl;;
	//3
	char arr[SIZE];
	cout << "enter your first name: ";
	cin.getline(arr, SIZE);
	int in = strlen(arr);
	arr[in] = ',';
	arr[in + 1] = ' ';
	cout << "entet your last name: ";
	cin.getline(&(arr[in + 2]), SIZE);
	cout << arr << endl;
	//改正
	char first[SIZE];
	char last[SIZE];
	char name[SIZE * 2 + 2];
	cout << "enter your first name: ";
	cin.getline(first, SIZE);
	cout << "enter your last name: ";
	cin.getline(last, SIZE);
	strcpy_s(name, last);
	strcat_s(name, ", ");
	strcat_s(name, first);
	cout << name << endl;
	//4
	string First;
	string Last;
	string Name;
	cout << "enter your first name: ";
	cin >> First;
	cout << "enter your last name: ";
	cin >> Last;
	Name += Last;
	Name += ", ";
	Name += First;
	cout << Name;
	//5
	struct CandyBar
	{
		string m_P;
		double m_W;
		int m_S;
	};
	CandyBar snack{ "Mocha Munch",2.3,350 };
	//6
	CandyBar arr[3]
	{
		{"hh",1.1,2},
		{"hh",1.1,2},
		{"hh",1.1,2}
	};
	//8,9
	CandyBar* p = new CandyBar[3]
	{
		{"hh",1.1,2},
		{"hh",1.1,2},
		{"hh",1.1,2}
	};
	//10
	array<double, 3> score;
	for (int ix = 0; ix < 3; ++ix)
		cin >> score[ix];

	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值