C++ Primer Plus 编程练习ch4

4-1

#include<iostream>
#include<string>
#include<cstring>


using namespace std;

int main() {
    cout << "What is your first name? ";
    char fname[20];
    cin.getline(fname, 20);
    cout << "What is your last name? ";
    char lname[20];
    cin.getline(lname, 20);
    cout << "What letter grade do you deserve? ";
    char grade;
    cin >> grade;
    cout << "What is your age? ";
    int age;
    cin >> age;
    cout << "Name: " << lname << "," << fname << endl;
    cout << "Grade: " << ++grade << endl;
    cout << "Age: " << age << endl;




    system("pause");
    return 0;

}

4-2


#include<iostream>
#include<string>
#include<cstring>


using namespace std;

int main() {
    cout << "What is your first name? ";
    string fname;
    getline(cin, fname);
    cout << "What is your last name? ";
    string lname;
    getline(cin, lname);
    cout << "What letter grade do you deserve? ";
    char grade;
    cin >> grade;
    cout << "What is your age? ";
    int age;
    cin >> age;
    cout << "Name: " << lname << "," << fname << endl;
    cout << "Grade: " << ++grade << endl;
    cout << "Age: " << age << endl;




    system("pause");
    return 0;
}


4-3


#include<iostream>
#include<string>
#include<cstring>


using namespace std;

int main() {
	cout << "Enter your first name:";
	char fname[20];
	cin >> fname;
	cout << "Enter your last name:";
	char lname[20];
	cin >> lname;
	cout << "Here's the information in a single string:";

	strcat_s(lname, ", ");
	strcat_s(lname, fname);
	cout << lname << endl;




	system("pause");
	return 0;
}

4-4


#include<iostream>
#include<string>
#include<cstring>


using namespace std;

int main() {
	cout << "Enter your first name:";
	string fname;
	cin >> fname;
	cout << "Enter your last name:";
	string lname;
	cin >> lname;
	cout << "Here's the information in a single string:";
	lname += ", ";
	lname += fname;
	
	cout << lname << endl;




	system("pause");
	return 0;
}


4-5


#include<iostream>
#include<string>
#include<cstring>


using namespace std;

int main() {
	struct CandyBar {
		string brand;
		double weight;
		int calorie;
	};
	CandyBar snack = { "Mocha Munch", 2.3, 350 };
	cout << "snack.brand = " << snack.brand << endl;
	cout << "snack.weight = " << snack.weight << endl;
	cout << "snack.calorie = " << snack.calorie << endl;



	system("pause");
	return 0;
}


4-6


#include<iostream>
#include<string>
#include<cstring>


using namespace std;

int main() {
	struct CandyBar {
		string brand;
		double weight;    //pound
		int calorie;
	};
	CandyBar renko[3];
	renko[0] = { "Mocha Munch", 2.3, 350 };
	renko[1] = { "Orio Strawberry Cake", 1.1,1390 };
	renko[2] = { "Egg Pudding", 0.2, 174 };
	for (int i = 0; i < 3; i++) {
		cout << "renko[" << i << "].brand = " << renko[i].brand << endl;
		cout << "renko[" << i << "].weight = " << renko[i].weight << endl;
		cout << "renko[" << i << "].calorie = " << renko[i].calorie << endl;

	}



	system("pause");
	return 0;
}


4-9


#include<iostream>
#include<string>
#include<cstring>


using namespace std;

int main() {
	struct CandyBar {
		string brand;
		double weight;    //pound
		int calorie;
	};
	CandyBar * renko = new CandyBar[3];
	renko[0] = { "Mocha Munch", 2.3, 350 };
	renko[1] = { "Orio Strawberry Cake", 1.1,1390 };
	renko[2] = { "Egg Pudding", 0.2, 174 };
	for (int i = 0; i < 3; i++) {
		cout << "renko[" << i << "].brand = " << renko[i].brand << endl;
		cout << "renko[" << i << "].weight = " << renko[i].weight << endl;
		cout << "renko[" << i << "].calorie = " << renko[i].calorie << endl;

	}
	delete[] renko;


	system("pause");
	return 0;
}

4-10


#include<iostream>
#include<string>
#include<cstring>
#include<array>


using namespace std;

int main() {
	array<double, 3>run;
	cout << "The first test: ";
	cin >> run[0];
	cout << "The seond test: ";
	cin >> run[1];
	cout << "The third test: ";
	cin >> run[2];
	double ave = (run[0] + run[1] + run[2]) / 3.0;
	cout << "The average is " << ave << endl;



	system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值