C++ Primer Plus(第6版)---编程作业完成(第四,五章)

本篇博客通过多个实际案例,详细介绍了使用C++进行结构化编程的方法,包括如何利用结构体和数组解决具体问题,如成绩管理、糖果条信息处理及披萨订单分析等。

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

第四章

#include <iostream>
#include <string>
#include <cstring>
#include <array>
using namespace std;
//函数原型
void grade(void);
void code(void);
void name(void);
void name_string(void);
void candy(void);
void candy_array(void);
void pizza_analy(void);
void pizza_plus(void);
void candy_plus(void);
void run_grade(void);

//主函数

int main()
{
	//grade();

	//code();

	//name();

	//name_string();

	//candy();

	//candy_array();

	//pizza_analy();

	//pizza_plus();

	//candy_plus();

	run_grade();
	return 0;
}

//-------------作业一--------------------------------
struct student
{
	char first_name[20];
	char last_name[20];
	char grade;
	int age;
};
void grade(void)
{
	student s1;
	cout << "What is your first name? " ;
	cin.getline(s1.first_name, 20);
	cout << "What is your last name? " ;
	cin.getline(s1.last_name, 20);
	cout << "What letter grade do you deserve? ";
	cin >> s1.grade;
	cout << "What is your age? ";
	cin >> s1.age;

	
	cout << "Name: " << s1.last_name << " , " << s1.first_name << endl;
	cout << "Grade: " <<char( s1.grade + 1) << endl;
	cout << "Age: " << s1.age << endl;
	
	return;
}

//--------------作业二--------------------------------------
//string替换程序4.4
void code(void)
{
	string name, dessert;
	cout << "Enter your name: ";
	//cin >> name;//只能读取一个单词
	getline(cin, name);//使用getline获取一行string
	cout << "Enter the dessert: ";
	getline(cin, dessert);
	cout << "name: " << name << " dessert: " << dessert << endl;

	return;
}

//----------------作业三-------------------------------------------
//使用char数组
void name(void)
{
	const int Size = 40;
	char first_name[Size];
	char last_name[Size];
	
	cout << "Enter your first name: ";
	cin.getline(first_name, Size);
	cout << "Enter your last name: ";
	cin.getline(last_name, Size);
	
	char name[100];
	strcpy_s(name, strlen(last_name)+1,last_name);
	strcat_s(name, strlen({ ", " }) +strlen(name)+ 1, { ", " });
	strcat_s(name, strlen(first_name) +strlen(name)+ 1,first_name);

	cout << name << endl;
	return;
}

//--------------作业四------------------------------
//使用string
void name_string(void)
{
	string first_name, last_name;
	cout << "Enter your first name: ";
	getline(cin,first_name);
	cout << "Enter your last name: ";
	getline(cin,last_name);

	last_name += ", ";
	last_name += first_name;
	cout << last_name << endl;
	return;
}


//---------作业五------------------------------------------
struct CandyBar
{
	char name[20];
	float weight;
	int carlo;
};
void candy(void)
{
	CandyBar snack = { "Mocha Munch",2.3f,350 };
	cout << snack.name << endl;
	cout << snack.weight << endl;
	cout << snack.carlo << endl;
	return;
}


//-----------作业六---------------------------------
//上面结构体类型定义后,都可以使用
//重复定义会报错
void candy_array(void)
{
	CandyBar candys[3] = { {"A",1.0f,10},{"B",2.0f,20},{"C",3.0f,30}};
	cout << "candy1: " << candys[0].name << candys[0].weight << candys[0].carlo << endl;
	cout << "candy2: " << candys[1].name << candys[1].weight << candys[1].carlo << endl;
	cout << "candy3: " << candys[2].name << candys[2].weight << candys[2].carlo << endl;
	return;
}

//----------------作业七--------------------------
struct pizza
{
	string company_name;
	double diameter;
	double weight;
};
void pizza_analy(void)
{
	pizza pa;
	cout << "Enter your companey: ";
	getline(cin, pa.company_name);
	cout << "Enter the diameter: ";
	cin >> pa.diameter;
	cout << "Enter the weight: ";
	cin >> pa.weight;

	cout << pa.company_name << endl;
	cout << pa.diameter << endl;
	cout << pa.weight << endl;

	return;
}


//------------------作业八-----------------------------
void pizza_plus(void)
{
	pizza* ps = new pizza;
	cout << "Enter the diameter: ";
	cin >> ps->diameter;//cin按空格,读取数字之后的换行符留在了输入队列中
	cin.get();//单独一个get把换行符读取走
	cout << "Enter your companey: ";
	getline(cin, ps->company_name);
	cout << "Enter the weight: ";
	cin >> ps->weight;

	cout << ps->diameter << endl;
	cout << ps->company_name << endl;
	cout << ps->weight << endl;
	//!!!delete
	delete ps;
	return;
}

//-------------作业九----------------------------------
void candy_plus(void)
{
	CandyBar* ps = new CandyBar[3];
	ps[0] = { "A",1.0f,10 };
	ps[1]={ "B",2.0f,20 };
	ps[2] = { "C",3.0f,30 };
	
	cout << "candy1: " << ps[0].name << ps[0].weight << ps[0].carlo << endl;
	cout << "candy2: " << ps[1].name << ps[1].weight << ps[1].carlo << endl;
	cout << "candy3: " << ps[2].name << ps[2].weight << ps[2].carlo << endl;

	delete[] ps;
	return;
}

//----------------作业十-------------------------------------------
void run_grade(void)
{
	array<double, 3> run;
	cout << "Enter the first :";
	cin >> run[0];
	cout << "Enter the second :";
	cin >> run[1];
	cout << "Enter the third :";
	cin >> run[2];
	cout << "The num is: 3 " << "average is:" << (run[0] + run[1] + run[2]) / 3 << endl;
	return;
}

第五章

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

//函数原型
void int_sum(void);
void array_factorial(void);
void input_sum(void);
void invest_cmp(void);
void book(void);
void book_plus(void);
void car_record(void);
void word_count(void);
void word_sum_plus(void);
void star(void);
//主函数
int main()
{
	//int_sum();

	//array_factorial();

	//input_sum();

	//invest_cmp();

	//book();

	//book_plus();

	//car_record();

	//word_count();

	//word_sum_plus();

	star();
	return 0;
}


//--------------------作业一-----------------------------------------
void int_sum(void)
{
	int min_integer, max_integer;
	cout << "Enter the min integer: ";
	cin >> min_integer;
	cout << "Enter the max integer: ";
	cin >> max_integer;
	int sum = 0;
	for (int i = min_integer;i <= max_integer;i++)
	{
		sum += i;
	}
	cout << "the sum is: " << sum << endl;
	return;
}


//----------------作业二----------------------------------------
//使用array和long double计算0-15!
void array_factorial(void)
{
	const int ArSize = 101;
	array<long double, ArSize>arr;//使用类array代替定长数组
	arr[0] = arr[1] = 1;
	for (int i = 2;i < ArSize;i++)
	{
		arr[i] = arr[i - 1] * i;
	}
	for (int j = 0;j < ArSize;j++)
	{
		cout << j << " ! = " << arr[j] << endl;
	}
	cout << sizeof(long double) << endl;//8
	cout << sizeof(long long) << endl;//8
	return;
}


//------------作业三--------------------------------------------
void input_sum(void)
{
	int sum = 0;
	int ch ;
	cin >> ch;//cin.get()是读取字符,会返回数字的ASCII码值
	while (ch != 0)
	{
		sum += ch;
		cout << sum << endl;
		cin >> ch;
	};
	return;
}


//--------------作业四--------------------------------------
void invest_cmp(void)
{
	int year = 0;
	long Daphne = 100;
	long Cleo=100;
	do
	{
		Daphne += 10;
		Cleo = Cleo * (1.05f);
		year++;
	}while (Cleo<=Daphne);
	cout << "Daphne= " << Daphne << endl;
	cout << "Cleo= " << Cleo << endl;
	cout << "year= " << year << endl;
	return;
}


//--------------作业五-----------------------
void book(void)
{
	const char* month[12] = {"Jan","Feb","Mar","Apr","Ay","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	int data[12];
	cout << "Enter the data following the month: " << endl;
	for (int i = 0;i < 12;i++)
	{
		cout << month[i] << "\t: ";
		cin >> data[i];
	}
	int sum = 0;
	for (int j = 0;j < 12;j++)
	{
		sum += data[j];
		cout << month[j] << " : " << data[j] << endl;
	}
	cout << "sum:\t" << sum << endl;
	return;
}


//----------------------作业六-------------------------
void book_plus(void)
{
	const char* month_plus[12] = { "Jan","Feb","Mar","Apr","Ay","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
	int year_data[3][12];
	cout << "Enter the data following the tips: " << endl;
	for (int j = 0;j < 3;j++)
	{
		cout << "the " << j+1 << " year:\t" << endl;
		for (int i = 0;i < 12;i++)
		{
			cout << month_plus[i] << "\t: ";
			cin >> year_data[j][i];
		}
	}
	int sum_year[3];
	int all_sum = 0;
	for (int j = 0;j < 3;j++)
	{
		int sum = 0;
		for (int i = 0;i < 12;i++)
		{
			sum=sum+year_data[j][i];
		}
		sum_year[j] = sum;
		cout << "the " << j+1 << " year of sum:\t" << sum_year[j]<<endl;
		all_sum += sum_year[j];
	}
	cout << "all_sum:\t" << all_sum << endl;
	return;
}


//-------------作业七--------------------------------------
struct car
{
	string brand;
	int year;
};
void car_record(void)
{
	int num;
	cout << "How many cars do you wish to catalog? ";
	cin >> num;
	//此处cin不读取回车符,回车符被留在输入行,需要读取掉
	cin.get();
	car* pc = new car[num];
	for (int i = 0;i < num;i++)
	{
		cout << "Car #" << i + 1 << ":" << endl;
		cout << "Please enter the make: ";
		//cin没有针对于string类的IO接口,不能用cin.get()
		getline(cin,pc[i].brand);
		cout << "Please enter the year made: ";
		//cin.get()是用来接收字符的
		cin >> (pc[i].year);
		cin.get();//cin跳过回车符,因此需要cin.get()读取掉多余的回车符
	}
	cout << "Here is your collection: " << endl;
	for (int j = 0;j < num;j++)
	{
		cout << pc[j].year << "\t" << pc[j].brand << endl;
	}

	delete[] pc;
	return;
}


//--------------------作业八----------------------------------------
void word_count(void)
{
	unsigned int count = 0;
	char input[128];
	cout << "Enter words(to stop,type the word done): " ;
	do
	{
		cin >> input;
		count++;
	} while (strcmp(input, "done"));//相同为0
	cout << input << endl;//空格,cin就开始重新读取了
	//input=done
	cout << "You enterd a total of " << count-1 << endl;
	return;
}


//----------------作业九-----------------------------
void word_sum_plus(void)
{
	//使用string和关系运算符
	//cin。get()没有string接口,使用getline(cin,string)
	unsigned int count = 0;
	string str;
	cout << "Enter words(to stop,type the word done): ";
	do
	{
		//getline(cin,str);
		cin >> str;//cin可以接收string
		count++;
		cout << str << endl;
	} while (str!="done");//相同为0
	cout <<str << endl;//空格,cin就开始重新读取了
	//input=done
	cout << "You enterd a total of " << count - 1 << endl;
	//getline,string是按行计算,无法确定单词数量
	//cin>>str,就可以了
	return;
}


//--------------------作业十-------------------------------------
void star(void)
{
	int rows;
	cout << "Enter the num of rows: ";
	cin >> rows;
	for (int i = 0;i < rows;i++)
	{
		for (int j = rows - i - 1;j > 0;j--)
		{
			cout << " . ";
		}
		for (int k = 0;k < i + 1;k++)
		{
			cout << " * ";
		}
		cout << endl;
	}
	return;
}

如有错误,欢迎指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值