一、
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;
}