Visual C++ 2012入门经典(第6版) 课后练习(第05章)

本文通过五个具体的C++编程示例介绍了如何实现阶乘计算、数值交换、三角函数转换、输入输出控制及字符串处理等功能。这些示例不仅涵盖了基本的算法实现,还涉及到了面向对象的编程思想。

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

一、

1.main.cpp

#include <iostream>

using namespace std;

int fun(int nNum);

int main()
{
	//输出0到5的阶乘
	for(int i=0;i<6;i++)
	{
		cout<<i<<"! = "<<fun(i)<<endl;
	}

	return 0;
}

// 0!=1
int fun(int nNum)
{
	if(0 == nNum)
		return 1;

	return nNum*fun(nNum-1);
}

二、

1.main.cpp

#include <iostream>

using namespace std;

void SwapTwoNum(int* a,int* b);

int main()
{
	int a(8),b(5);

	cout<<"a = "<<a<<",b = "<<b<<endl;

	SwapTwoNum(&a,&b);

	cout<<"->"<<"a = "<<a<<",b = "<<b<<endl;

	return 0;
}

void SwapTwoNum(int* a,int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

三、

1.main.cpp

#include <iostream>
#include <cmath>

using namespace std;

#define PI 3.141592653

double sind(double angle);
double cosd(double angle);
double tand(double angle);

int main()
{
	double angle = 30.0;

	cout<<"angle = "<<angle<<endl;
	cout<<"sind("<<angle<<") = "<<sind(angle)<<endl;
	cout<<"cosd("<<angle<<") = "<<cosd(angle)<<endl;
	cout<<"tand("<<angle<<") = "<<tand(angle)<<endl;

	return 0;
}

double sind(double angle)
{
	return sin(angle/180.0*PI);
}

double cosd(double angle)
{
	return cos(angle/180.0*PI);
}

double tand(double angle)
{
	return sind(angle)/cosd(angle);
}

四、

1.main.cpp

#include <iostream>

using namespace std;

bool InPut(int* nNum,char* words);
void OutPut(const int nNum,const char* const words);

int main()
{
	int num(-1);
	char words[16] = "";

	while (true)
	{
		if(!InPut(&num,words))
			break;

		OutPut(num,words);
	}

	return 0;
}

//获得输入值
bool InPut(int* nNum,char* words)
{
	cin>>*nNum;
	if(0 == *nNum)
		return false;

	cin>>words;

	return true;
}

//输出值
void OutPut(const int nNum,const char* const words)
{
	cout<<"Num = "<<nNum<<",Words = "<<words<<endl;
}

五、

1.main.cpp

#include <iostream>
#include <cstring>

using namespace std;

char* GetWord(char* sent);

int main()
{
	char* sentence= "There may be a good reason to house";
	cout<<"Sentence:"<<sentence<<endl;
	cout<<GetWord(sentence)<<endl;
	cout<<GetWord(nullptr)<<endl;
	cout<<GetWord(nullptr)<<endl;
	cout<<GetWord(nullptr)<<endl;
	cout<<GetWord(nullptr)<<endl;
	sentence = "The Website has channels as China";
	cout<<"Sentence:"<<sentence<<endl;
	cout<<GetWord(sentence)<<endl;
	cout<<GetWord(nullptr)<<endl;
	cout<<GetWord(nullptr)<<endl;
	cout<<GetWord(nullptr)<<endl;
	cout<<GetWord(nullptr)<<endl;
	cout<<GetWord(nullptr)<<endl;
	cout<<GetWord(nullptr)<<endl;

	return 0;
}

char* GetWord(char* sent)
{
	static int nPos = 0;						//保存当前字符串已检查的位置
	static char* nNowSent = sent;				//保存当前字符串
	static char word[20] = "";					//保存从当前字符串截取的单词

	//如果不是nullptr,则重置nPos为0、nNowSent为新句子
	if(sent != nullptr)
	{
		nPos = 0;
		nNowSent = sent;
	}

	word[0] = '\0';								//每次调用先清空返回的单词

	for (int i = nPos,j=0;nNowSent[i] != '\0';i++,j++)
	{
		nPos++;

		if(nNowSent[i]!=' ' )
		{
			word[j] = nNowSent[i];
			word[j+1] = '\0';
		}
		else
			break;
	}

	return word;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值