C++ Primer Plus 第六版 第四章编程练习记录

第4章(VS2013)

3.

#include<iostream>
#include<cstring>

int main()
{
	using namespace std;

	char fn[10];
	cout << "Enter your first name: ";
	cin >> fn;
	char ln[10];
	cout << "Enter your last name: ";
	cin >> ln;
	strcat_s(fn, ", ");
	char he[20];
	strcpy_s(he,fn);
	strcat_s(he, ln);
	cout << "Here's the information in a single string: "<<he << endl;

	cin.get();
	cin.get();
	return 0;
}

4.

#include<iostream>
#include<string>

int main()
{
	using namespace std;

	string str1, str2;
	cout << "Enter your first name: ";
	cin >> str1;
	cout << "Enter your last name: ";
	cin >> str2;
	string str;
	str = str2 + ", ";
	str = str + str1;
	cout << "Here's the information in a single string: " << str << endl;

	cin.get();
	cin.get();
	return 0;
}


5.

#include<iostream>

struct CandyBar
{
	char kind[20];
	double weights;
	int k;
};

int main()
{
	using namespace std;

	CandyBar snack = { "Mocha Much", 2.3, 350 };
	cout << "kind: " << snack.kind << endl;
	cout << "weights: " << snack.weights << endl;
	cout << "calorie: " << snack.k << endl;

	//cin.get();
	cin.get();
	return 0;
}

6.

#include<iostream>

struct CandyBar
{
	char kind[20];
	double weights;
	int k;
};

int main()
{
	using namespace std;

	CandyBar snack[3] = { 
		{ "Mocha Much", 2.3, 350 },
		{"BuLaoLin",2.0,400},
		{"DaBaiTu",2.6,300}

	};

	for (int i = 0; i < 3; i++)
	{
		cout << "kind: " << snack[i].kind << endl;
		cout << "weights: " << snack[i].weights << endl;
		cout << "calorie: " << snack[i].k << endl;
	}
	//cin.get();
	cin.get();
	return 0;
}


7或8.

#include<iostream>

struct Piza
{
	char CompName[20];
	double d;
	double weights;
};

int main()
{
	using namespace std;

	Piza* p = new Piza;

	cin.getline(p->CompName,20);
	cin >> (*p).d;
	cin >> p->weights;
	cout << "CompName: " << p->CompName << endl;
	cout << "d: " << p->d << endl;
	cout << "weights: " << p->weights << endl;
        delete p;

	cin.get();
	cin.get();
	return 0;
}

9.

#include<iostream>

struct CandyBar
{
	char kind[20];
	double weights;
	int k;
};

int main()
{
	using namespace std;

	CandyBar* p = new CandyBar[3];
	
	p[0] = { "Mocha Much", 2.3, 350 };
	p[1] = { "BuLaoLin", 2.0, 400 };
	p[2]={"DaBaiTu", 2.6, 300 };


	for (int i = 0; i < 3; i++)
	{
		cout << "kind: " << p[i].kind << endl;
		cout << "weights: " << p[i].weights << endl;
		cout << "calorie: " << p[i].k << endl;
	}
	delete[] p;
	cin.get();
	return 0;
}

9.

#include<iostream>
#include<array>

int main()
{
	using namespace std;

	cout << "Please input thtee grades in seconds\n";
	array<float, 3>a;
	cin >> a[0];
	cin >> a[1];
	cin >> a[2];
	cout << "first " << a[0]<<" s"<<endl;
	cout << "second " << a[1] << " s" << endl;
	cout << "thred " << a[2] << " s" << endl;
	cout << "avgave: " << (a[0] + a[1] + a[2]) / 3 <<" s"<< endl;

	cin.get();
	cin.get();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值