C++ Primer Plus笔记: 2023.07.04

第四章课后习题:

(4)

#include <iostream>
#include <string>
using namespace std ;

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

	return 0 ;
}

(5)
解法1:

#include <iostream>
using namespace std ;

const int NameSize = 30 ;

struct CandyBar{
	char brand[NameSize] ;
	double weight ;
	int calorie ;
} ;

int main()
{
	CandyBar snack = {
		"Mocha Munch" ,
		2.3 ,
		350 
	} ;
	
	cout << "This kind of snack's brand: " << snack.brand << endl ;
	cout << "This kind of snack's weight: " << snack.weight << endl ;
	cout << "How many calories does this kind of snack have: " << snack.calorie << endl ;
	
	return 0 ;
}

解法2:

#include <iostream>
using namespace std ;

struct CandyBar {
	string brand ;
	double weight ;
	int calorie ;
} ;

int main()
{
	CandyBar snack = {
		"Mocha Munch" ,
		2.3 ,
		350
	} ;

 	cout << "This kind of snack's brand: " << snack.brand << endl ;
    cout << "This kind of snack's weight: " << snack.weight << endl ;
    cout << "How many calories does this kind of snack have: " << snack.calorie << endl ;
	
	return 0 ;	
}

刚刚无意发现,在我这Ubuntu上不添加string头文件也能编译成功

(9)

#include <string>
#include <iostream>
using namespace std ;

struct CandyBar {
	std::string brand ;
	double weight ;
	int calorie ;
} ;
	
CandyBar * snack ;

int main()
{
	snack = new CandyBar [3] ;
	snack[0] = {
		"Mocha Munch" ,
		2.3 ,
		350 
	} ;
	snack[1] = {
                "New York" ,
                5.6 ,
                950
        } ;
	snack[2] = {
                "Betty Sue" ,
                7.3 ,
                560
        } ;
	
	cout << "The first kind of snack: " << endl ;
	cout << "Brand: " << snack->brand << endl ;
	cout << "Weight: " << snack->weight << endl ;
	cout << "Calorie: " << snack->brand << endl << endl ;
	
	cout << "The second kind of snack: " << endl ;
    cout << "Brand: " << (snack+1)->brand << endl ;
    cout << "Weight: " << (snack+1)->weight << endl ;
    cout << "Calorie: " << (snack+1)->brand << endl << endl ;
	
	cout << "The third kind of snack: " << endl ;
    cout << "Brand: " << (snack+2)->brand << endl ;
    cout << "Weight: " << (snack+2)->weight << endl ;
    cout << "Calorie: " << (snack+2)->brand << endl << endl ;

	delete [] snack ;
	return 0 ;
}

这里要注意如何定义结构类型:

struct CandyBar {
	std::string brand ;
	double weight ;
	int calorie ;
} ;

需要注意两点,一是结构的成员后面要加分号,二是如果想用string的成员并且还想带上string头文件,要写成std::string,否则就别用string头文件,像这样也行:

#include <iostream>
using namespace std ;

struct CandyBar {
	string brand ;
	double weight ;
	int calorie ;
} ;

(7)

#include <iostream>
using namespace std ;

const int NameSize = 100 ;
struct Pizza {
	char company[NameSize] ;
	double diameter ;
	double weight ;
} ;

int main()
{
	Pizza william ;
	
	cout << "What is the pizza company? " ;
	cin.get(william.company, NameSize).get() ;
	cout << "How long is the pizza's diameter? " ;
 	cin >> william.diameter ;
	cout << "What is the pizza's weight? " ;
    cin >> william.weight ;

	cout << endl << endl ;
	cout << "Company's Name: " << william.company << endl ;
	cout << "Diameter: " << william.diameter << endl ;
	cout << "Weight: " << william.weight << endl ;
	
	return 0 ;
}

(8)

#include <iostream>
using namespace std ;

const int NameSize = 100 ;

struct Pizza {
	char company[NameSize] ;
	double diameter ;
	double weight ;
} ;

Pizza * william ;

int main()
{
	william = new Pizza ;
	
	cout << "How long is the pizza's diameter? " ;
	(cin >> william->diameter).get() ;
	cout << "What is the pizza company's name? " ;
    cin.get(william->company, NameSize).get() ;
	cout << "What is the weight of this kind of pizza? " ;
    cin >> william->weight ;

	cout << endl << "--------------------------------------------------------------" << endl ;
	
	cout << "Company's Name: " << william->company << endl ;
	cout << "Diameter: " << william->diameter << endl ;
	cout << "Weight: " << william->weight << endl ;

	delete william ;
}

(6)

#include <iostream>
#include <string>
using namespace std ;

struct CandyBar {
	std::string brand ;
	double weight ;
	int calorie ;
} ;

int main()
{
	CandyBar candy[3] = {
		{
			"Mocha Munch" ,
			2.3 ,
			350
		} ,
		{
                        "New York" ,
                        8.9 ,
                        700
                } ,
		{
                        "New York" ,
                        18.7 ,
                        900
                } 
	} ;
	
	cout << "The first kind of candy: " << endl ;
	cout << "Brand: " << candy->brand << endl ;
	cout << "Weight: " << candy->weight << endl ;
	cout << "calorie: " << candy->calorie << endl << endl ;

	cout << "The second kind of candy: " << endl ;
    cout << "Brand: " << (candy+1)->brand << endl ;
    cout << "Weight: " << (candy+1)->weight << endl ;
    cout << "calorie: " << (candy+1)->calorie << endl << endl ;

	cout << "The third kind of candy: " << endl ;
    cout << "Brand: " << (candy+2)->brand << endl ;
    cout << "Weight: " << (candy+2)->weight << endl ;
    cout << "calorie: " << (candy+2)->calorie << endl ;

	return 0 ;
}

(10)

#include <array>
#include <iostream>
using namespace std ;

int main()
{
	cout << "Please enter your grade of 40 meters' running of three times: " << endl ;
	
	array<double, 3> grade ;
	
	cin >> grade[0] >> grade[1] >> grade[2] ;
	
	cout << endl << "----------------------------------------------------------" << endl ;
	 
	cout << "Times of running: " << grade.size() << endl ;
	cout << "Average Grade: " << (grade[0] + grade[1] + grade[2])/3 << endl ;
}

接下来到了第五章

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值