C++ primer plus Chapter4--复合类型 程序练习

本文通过多个示例介绍了C++中使用字符串和结构体的方法,包括字符串的输入输出、结构体成员的初始化与访问等核心内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//1(string)

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

int main()
{
	string FirstName;
	string LastName;
	string grade;
	int age;

	cout << "What is your first name?" << endl;
	//cin >> FirstName;
	//cin.getline(FirstName);
	getline(cin,FirstName);
	cout << "What is your last name?" << endl;
	//cin >> LastName;
	//cin.getline(LastName);
	getline(cin,LastName);
	cout << "What letter grade do you deserve?" << endl;
	cin >> grade;
	cout << "What is your age?" << endl;
	cin >> age;

	cout << "Name: " << LastName << "," << FirstName << endl;
	cout << "Grade: " << grade << endl;
	cout << "Age: " << age << endl;

	system("pause");
	return 0;
	
}

输出:

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: B
Age: 22

问题:

使用string时,如果输出为Betty Sue 样式,需要使用getline(cin, FirstName)进行输入,否则只能读取空格前的字符,默认空格为结束符,并自动将Sue赋值给LastName.

 

//1(char)

#include <iostream>
        
using namespace std;

int main()
{
	char FirstName[128];
	char LastName[128];
	char grade[16];
	int age;

	cout << "What is your first name?" << endl;
	//cin >> FirstName;
	//cin.getline(FirstName);
	//getline(cin,FirstName);
	cin.get(FirstName,128).get();
	cout << "What is your last name?" << endl;
	//cin >> LastName;
	//cin.getline(LastName);
	//getline(cin,LastName);
	cin.get(LastName,128).get();
	cout << "What letter grade do you deserve?" << endl;
	cin >> grade;
	cout << "What is your age?" << endl;
	cin >> age;

	cout << "Name: " << LastName << "," << FirstName << endl;
	cout << "Grade: " << grade << endl;
	cout << "Age: " << age << endl;

	system("pause");
	return 0;
	
}
#include <iostream>
#include<cstring>       
using namespace std;

int main()
{
	char FirstName[128];
	char LastName[128];

	cout << "Enter your first name:";
	cin.getline(FirstName,128);
	cout << "Enter your last name:";
	cin.getline(FirstName,128);

	cout << "Here is the information in a single string: " << LastName << ", " << FirstName << endl;


	system("pause");
	return 0;
	
}

 

//2

#include <iostream>
#include<string>       
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;


	system("pause");
	return 0;
	
}

输出:

Enter your name:
Dirk Hammernose
Enter your favorite dessert:
Radish Torte
I have some delicious Radish Torte for you, Dirk Hammernose.

 

 

//3(使用char数组和头文件cstring)

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

int main()
{
	char FirstName[128];
	char LastName[128];

	cout << "Enter your first name:";
	cin.getline(FirstName,128);
	cout << "Enter your last name:";
	cin.getline(LastName,128);

	cout << "Here is the information in a single string: " << LastName << ", " << FirstName << endl;


	system("pause");
	return 0;
	
}

输出:

Enter your first name:Flip
Enter your last name:Fleming
Here is the information in a single string: Fleming, Flip

注:

cin.getline 读取换行符并保存到第一个参数中;

cin.get 会将换行符留在输入队列中,所以会导致下一个读取的内容是换行符。

 

//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);

	cout << "Here is the information in a single string: " << LastName << ", " << FirstName << endl;


	system("pause");
	return 0;
	
}

输出:

Enter your first name:Flit
Enter your last name:Fleting
Here is the information in a single string: Fleting, Flit

 

//5

#include <iostream>    
using namespace std;

struct CandyBar
{
	char name[128];
	double weight;
	int calorie;
};

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

	cout << "The snack's name is " << snack.name << ", weight is " << snack.weight << ", calorue is " << snack.calorie << "." << endl;
	


	system("pause");
	return 0;
	
}

输出:

The snack's name is Mocha Munch, weight is 2.3, calorue is 350.

 

//6(结构数组)

#include <iostream>    
using namespace std;

struct CandyBar
{
	char name[128];
	double weight;
	int calorie;
};

int main()
{
	CandyBar snack[3] = 
	{
		{"Mocha Munch", 2.3, 350},
		{"Apple", 2.5, 400},
		{"Banana", 2.6, 450}
	};

	for(int i = 0; i < 3; i++)
	cout << "The snack's name is " << snack[i].name << ", weight is " << snack[i].weight << ", calorue is " << snack[i].calorie << "." << endl;
	


	system("pause");
	return 0;
	
}

输出:

The snack's name is Mocha Munch, weight is 2.3, calorue is 350.
The snack's name is Apple, weight is 2.5, calorue is 400.
The snack's name is Banana, weight is 2.6, calorue is 450.

 

//7

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

struct Pizza
{
	char name[128];
	double weight;
	double diameter;
};

int main()
{
	Pizza snack = 
	{
		"Mocha Munch",
		400,
		2.6
	};
//int len = snack.name.size();
	int len = strlen(snack.name) - 1;
	
	cout << "The company's name is " << snack.name << ",it contact " << len << " characters" ;
	cout << ", weight is " << snack.weight << ", diameter is " << snack.diameter << "." << endl;
	


	system("pause");
	return 0;
	
}

输出:

The company's name is Mocha Munch,it contact 10 characters, weight is 400, diameter is 2.6.

注:

sizeof运算符指出整个数组的长度,

strlen( )函数返回的是存储在数组中的字符串的长度,只计算可见的字符。

//7

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

struct Pizza
{
	string name;
	double weight;
	double diameter;
};

int main()
{
	Pizza snack;
	cout << "Enter Pizza company's name:";
	getline(cin,snack.name);
	cout << "Enter Pizza's weight:";
	cin >> snack.weight;
	cout << "Enter Pizza's diameter:";
	cin >> snack.diameter;

    int len = snack.name.size() - 1;

	
	cout << "The company's name is " << snack.name << ",it contact " << len << " characters" ;
	cout << ", weight is " << snack.weight << ", diameter is " << snack.diameter << "." << endl;
	


	system("pause");
	return 0;
	
}
Enter Pizza company's name:Delicious Pizza
Enter Pizza's weight:12
Enter Pizza's diameter:450
The company's name is Delicious Pizza,it contact 14 characters, weight is 12, diameter is 450.

//8(new动态分配)

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

struct Pizza
{
	string name;
	double weight;
	double diameter;
};

int main()
{
	Pizza *snack = new Pizza;

	cout << "Enter Pizza company's name:";
	getline(cin,snack->name);
	cout << "Enter Pizza's weight:";
	cin >> snack->weight;
	cout << "Enter Pizza's diameter:";
	cin >> snack->diameter;

    int len = snack->name.size() - 1;

	
	cout << "The company's name is " << snack->name << ",it contact " << len << " characters" ;
	cout << ", weight is " << snack->weight << ", diameter is " << snack->diameter << "." << endl;
	delete snack;


	system("pause");
	return 0;
	
}
Enter Pizza company's name:Delicious Pizza
Enter Pizza's weight:12
Enter Pizza's diameter:450
The company's name is Delicious Pizza,it contact 14 characters, weight is 12, diameter is 450.

最后,不要忘了要释放内存。

//9

#include <iostream>    
using namespace std;

struct CandyBar

{
	char name[128];
	double weight;
	int calorie;
};

int main()

{
	CandyBar * snack = new CandyBar[3]{

		{"Mocha Munch", 2.3, 350},

		{"Apple", 2.5, 400},

		{"Banana", 2.6, 450}

	};

	for(int i = 0; i < 3; i++)
		cout << "The snack's name is " << snack[i].name << ", weight is " << snack[i].weight << ", calorie is " << snack[i].calorie << "." << endl;

	delete[] snack;

	system("pause");

	return 0;

}

该程序有错误,不知该如何修改。

 

//10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值