6.3:
#include <iostream>
using namespace std;
int fact(int val)
{
int ret = 1;
for (int i = 1; i <= val; i++)
{
ret *= i;
}
return ret;
}
int main()
{
int v = fact(10);
cout << v << endl;
return 0;
}
6.4:
#include <iostream>
using namespace std;
int fact(int val)
{
int ret = 1;
for (int i = 1; i <= val; i++)
{
ret *= i;
}
return ret;
}
int main()
{
cout << "请输入一个数字: " << endl;
int a = 0;
cin >> a;
int v = fact(a);
cout << "a! = " << v << endl;
return 0;
}
6.5:
#include <iostream>
using namespace std;
int absoluteValue(int val)
{
if (val >= 0)
{
return val;
}
else
{
return -val;
}
}
int main()
{
cout << "请输入一个数字: " << endl;
int a = 0;
cin >> a;
int v = absoluteValue(a);
cout << "|a| = " << v << endl;
return 0;
}
6.7:
#include <iostream>
using namespace std;
size_t func()
{
static size_t ctr = 0;
return ctr++;
}
int main()
{
for (size_t i = 0; i < 10; ++i)
{
cout << func() << endl;
}
return 0;
}
6.10:
#include <iostream>
using namespace std;
void change(int*v1,int*v2)
{
int temp = *v1;
*v1 = *v2;
*v2 = temp;
}
int main()
{
int a = 10;
int b = 20;
cout << "a = " << a << " b = " << b << endl;
change(&a, &b);
cout << "a = " << a << " b = " << b << endl;
return 0;
}
6.11:
#include <iostream>
using namespace std;
void reset(int& i)
{
i = 0;
}
int main()
{
int i = 42;
reset(i);
cout << i << endl;
return 0;
}
6.12:
#include <iostream>
using namespace std;
void change(int&v1,int&v2)
{
int temp = v1;
v1 = v2;
v2 = temp;
}
int main()
{
int a = 10;
int b = 20;
cout << "a = " << a << " b = " << b << endl;
change(a, b);
cout << "a = " << a << " b = " << b << endl;
return 0;
}
6.17:
#include <iostream>
using namespace std;
void is_upper(const string&s)
{
int i = 0;
for ( ; i < s.size(); ++i)
{
if (isupper(s[i]))
{
cout << "有大写" << endl;
break;
}
}
if (i == s.size())
{
cout << "没有大写字母" << endl;
}
}
void to_lower(string &s)
{
for (int i = 0; i < s.size(); ++i)
{
s[i]=tolower(s[i]);
}
}
int main()
{
string s1 = "sgfsfdFGHdgs";
is_upper(s1);
to_lower(s1);
cout << s1 << endl;
return 0;
}
6.21:
#include <iostream>
using namespace std;
int compare(int a,int*b)
{
return (a > *b ? a : *b);
}
int main()
{
int i1 = 10, i2 = 20;
cout << compare(i1, &i2) << endl;
return 0;
}
6.23:
#include <iostream>
using namespace std;
void print(int a)
{
cout << a << endl;
}
void print(const int arr[], int size)
{
for (int i = 0; i != size; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int i = 10;
print(i);
int j[2] = { 0,1 };
print(j, sizeof(j) / sizeof(j[0]));
return 0;
}
6.26:
#include <iostream>
using namespace std;
#include <string>
int main(int argc, char** argv)
{
string str;
for (int i = 1; i != argc; ++i) {
str += argv[i];
str += " ";
}
cout << str << endl;
return 0;
}
6.27:
偷的别人的,这几节看得云里雾里的QAQ
#include <iostream>
#include <initializer_list>
int sum(std::initializer_list<int> const& il)
{
int sum = 0;
for (auto i : il) sum += i;
return sum;
}
int main(void)
{
auto il = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::cout << sum(il) << std::endl;
return 0;
}
6.33:
第一种:
#include <iostream>
using namespace std;
#include <vector>
void print(vector<int>&v,int i)
{
cout << v[v.size()-i-1] << endl;
i--;
if (i >= 0)
{
print(v, i);
}
}
int main()
{
vector<int>v1 = { 1,2,3,4,5 };
print(v1, v1.size() - 1);
return 0;
}
第二种:
#include <iostream>
using namespace std;
#include <vector>
void print(vector<int>::iterator beg, vector<int>::iterator end)
{
if (beg != end)
{
cout << *beg << " ";
print(++beg, end);
}
}
int main()
{
vector<int>v1 = { 1,2,3,4,5 };
print(v1.begin(), v1.end());
return 0;
}
6.38:
decltype(arrStr)& arrPtr(int i)
{
return (i % 2) ? odd : even;
}
6.42:
#include <iostream>
using namespace std;
string make_plural(size_t ctr, const string& word, const string& ending = "s")
{
return(ctr > 1) ? word + ending : word;
}
int main()
{
string s1 = "success";
string s2 = "failure";
cout << make_plural(1, s1, "es") << " " << make_plural(2, s1, "es") << endl;
cout << make_plural(1, s2) << " " << make_plural(2, s2) << endl;
return 0;
}
6.47:
#include <iostream>
using namespace std;
#include<vector>
void print(vector<int>&v)
{
#ifndef NDEBUG
cout << "vector size: " << v.size() << endl;
#endif // !NDEBUG
if (!v.empty())
{
auto temp = v.back();//返回最末一个元素
v.pop_back();//删掉最后一个元素
print(v);
cout << temp << " ";
}
}
int main()
{
vector<int>v1 = { 1,2,3,4,5,6 };
print(v1);
return 0;
}
6.51:
#include <iostream>
using namespace std;
void f()
{
cout << "f()" << endl;
}
void f(int)
{
cout << "f(int)" << endl;
}
void f(int, int)
{
cout << "f(int, int)" << endl;
}
void f(double, double = 3.14)
{
cout << "f(double,double = 3.14)" << endl;
}
int main()
{
//f(2.56, 42);
f(42);
f(42, 0);
f(2.56, 3.6546);
return 0;
}
6.54:
int func(int, int);
using p1 = int(int a, int b);
vector<p1*>v1;
using p2 = int(*)(int a, int b);
vector<p2>v2;
using p3 = decltype(func)*;
vector<p3>v3;
typedef decltype(func)* p4;
vector<p4>v4;
typedef int(*p5)(int a, int b);
vector<p5>v5;
using p6 = decltype(func);
vector<p6*>v6;
6.56:
#include <iostream>
using namespace std;
#include <vector>
int func1(int a, int b)
{
return a + b;
}
int func2(int a, int b)
{
return a - b;
}
int func3(int a, int b)
{
return a * b;
}
int func4(int a, int b)
{
return a / b;
}
int main()
{
using p2 = int(*)(int a, int b);
vector<p2>v2 = { func1,func2,func3,func4 };
for (auto f : v2)
{
cout << f(2, 2) << endl;
}
return 0;
}