//6.3
#include<iostream>
using namespace std;
int fact(int val)
{
int ret = 1;
while (val > 1)
ret *= val--;
return ret;
}
int main()
{
int jc;
jc = fact(5);
cout << "5!=" << jc << endl;
}
//6.4
#include<iostream>
using namespace std;
int fact(int val)
{
int ret = 1;
while (val > 1)
ret *= val--;
return ret;
}
int main()
{
int jc;
cout << "Please input a unsigned int:>0:" << endl;
cin >> jc;
jc = fact(jc);
cout << jc<<"的阶乘是:" << jc << endl;
}
//6.5
#include<iostream>
using namespace std;
int abss(int val)
{
int ret = 1;
if (val >= 0)
ret = val;
else
ret = -val;
return ret;
}
int main()
{
int ab;
cout << "Please input a unsigned int:>0:" << endl;
cin >> ab;
ab = abss(ab);
cout << "绝对值是:" << ab << endl;
}
//6.6
#include<iostream>
using namespace std;
int abss(int val)
{
static int cont = 0;
int ret = 1;
if (val >= 0)
ret = val;
else
ret = -val;
cont++;
cout << "cont = " << cont << endl;
return ret;
}
int main()
{
int ab;
cout << "Please input a unsigned int:>0:" << endl;
while (1)
{
cin >> ab;
ab = abss(ab);
cout << "绝对值是:" << ab << endl;
}
}
//6.7
#include<iostream>
using namespace std;
int jia()
{
static int cont = -1;
cont++;
return cont;
}
int main()
{
int val;
for (int i = 0;i < 10;++i)
{
val = jia();
cout << "val = " << val << endl;
}
}
//6.8
#ifndef CHAPTER6_H_
#define CHAPTER6_H_
int jia();
#endif
//6.10
#include<iostream>
using namespace std;
void exchange(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
int main()
{
int v1, v2;
cout << "input two numbers:" << endl;
cin >> v1 >> v2;
exchange(&v1,&v2);
cout << v1 << " " << v2 << endl;
}
//6.11
#include<iostream>
using namespace std;
void reset(int &i)
{
i = 0;
}
int main()
{
int m;
cin >> m;
reset(m);
cout << "m = " << m << endl;
}
//6.12
#include<iostream>
using namespace std;
void exchange(int &i,int &j)
{
int temp;
temp = i;
i = j;
j = temp;
}
int main()
{
int m,n;
cin >> m>>n;
exchange(m,n);
cout << "m = " << m << endl<<"n = "<<n<<endl;
}
//6.17
#include<iostream>
#include<string>
using namespace std;
bool FT(const string &s);
void B2S(string &s);
int main()
{
string ss;
cin >> ss;
if (FT(ss))
{
cout << "有大写字母" << endl;
B2S(ss);
cout << ss << endl;
}
else
cout << "没有大写字母" << endl;
}
//判断是否含有大写字母
bool FT(const string &s)
{
for (auto it = s.begin();it != s.end();it++)
{
if (!isupper(*it))
continue;
else
{
return true;
}
}
return false;
}
//大写字母变为小写字母
void B2S(string &s)
{
for (auto it = s.begin();it != s.end();it++)
{
*it = tolower(*it);
}
}
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int cmp(int num, int *p)
{
return (num > *p) ? num : *p;
}
int main()
{
srand((unsigned)time(NULL));
int a[10];
for (auto &i : a)
{
i = rand() % 100;
}
int val;
cout << "请输入一个数:" << endl;
cin >> val;
cout << "较大的数是:"<<cmp(val,a) << endl;
cout << "随机数组是:" << endl;
for (auto &i : a)
{
cout << i << " ";
}
return 0;
}
//6.25
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
string str;
for (int i = 0;i != argc;++i)
{
str += argv[i];
}
cout << str << endl;
}
//6.26
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
string str;
for (int i = 0;i != argc;++i)
{
cout << "argv["<<i<<"] :"<<argv[i] << endl;
}
}
//6.33
#include<iostream>
#include<vector>
using namespace std;
void out(vector<int> val,int i)
{
unsigned sz = val.size();
if (!val.empty() && i < sz)
{
cout << val[i] << endl;
out(val, i + 1);
}
}
int main()
{
vector<int> v = {1,2,3,4,5,6,7,8,9,1,0};
out(v, 0);
}
//6.36正常形式
string (&func(string str)) [10]
{
}
//6.37类型别名
typedef string arr[10];
arr& func(string str);
//位置类型
auto func(string str)->string(&)[10];
//使用decltype
string str1[10];
decltype(str1) &func(string str);
//6.37
int odd = {1,3,5,7};
int even = {2,4,6,8};
decltype(odd) &arr(int i)
{
return (i%2)? odd:even;
}