C++ primer Plus(第六版)_课后编程练习_第八章

练习代码为本人所写,欢迎大家学习交流若有疑问请留言,我将尽快回复!!!!题8.1:#include <iostream>#include <ctime>using namespace std;void ShowStr(const char * str, int print_time = 1);int main(){ char str[100];...
摘要由CSDN通过智能技术生成

练习代码为本人所写,欢迎大家学习交流

若有疑问请留言,我将尽快回复!!!!

题8.1:

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

void ShowStr(const char * str, int print_time = 1);

int main()
{
	char str[100];
	int time;
	cout << "Enter the string: ";
	cin.getline(str, 100);
	cout << "Enter the time of show: ";
	cin >> time;

	ShowStr(str, time);
	cout << endl;

	ShowStr(str);

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 50 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

void ShowStr(const char * str, int print_time)    //记住定义函数的时候不写默认参数,而是在原型上默认
{
	if(print_time > 0)   //输入0次的时候则不显示
	{
		cout << str << " / ";
		print_time--;
		ShowStr(str, print_time);
	}
}

题8.2:

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

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

void fill(CandyBar & candy_n, char * name_n, double weight_n, int calorie_n);
void show(const CandyBar & candy_n);

int main()
{
	CandyBar candy1;
	fill(candy1, "Millennium Munch", 2.85, 350);
	show(candy1);

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 50 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

void fill(CandyBar & candy_n, char * name_n, double weight_n, int calorie_n)
{
	strcpy(candy_n.name, name_n);    //实验证明C++使用strcpy函数是不用包含头文件<cstring>的
	candy_n.weight = weight_n;
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值