习题 6.3
#include<iostream> #include<string> using namespace std; #include <vector> #include <stdexcept> string fact(char Your_answer) { switch (Your_answer) { case 'A': return "猜错了 加油噢"; break; case 'B': return "猜错了 加油噢"; break; case 'C': return "猜对了"; break; default: break; } } int main() { cout << "猜猜我的微信号" << endl; cout << "A : 65165165" << '\n' << "B : 684864" << '\n' << "C :19817465899" << endl; char a; cin >> a; fact(a); string final_answer = fact(a); cout << final_answer << endl; cin.get(); }
习题6.4
#include<iostream> #include<string> using namespace std; #include <vector> #include <stdexcept> int fact(int number) { int total = 1; for (; number != 1 ; --number) { total *= number; } return total; } int main() { cout << "该程序用来计算数字的阶乘" << endl; cout << "请输入要计算的数字" << endl; int a; cin >> a; fact(a); int output = fact(a); cout << a << "的阶乘为" << output << endl; cin.get(); }
习题6.5
#include<iostream>
#include<string>
using namespace std;
#include <vector>
#include <stdexcept>
int fact(int number)
{
number = abs(number);
return number;
}
int main()
{
cout << "该程序用来取绝对值" << endl;
cout << "请输入数字" << endl;
int a;
cin >> a;
fact(a);
int output = fact(a);
cout << a << "的绝对值为: " << output << endl;
cin.get();
}
习题 6.10
#include<iostream>
#include<string>
using namespace std;
#include <vector>
#include <stdexcept>
int fact(int *number1 , int *number2)
{
int c = 0;
c = *number1;
*number1 = *number2;
*number2 = c;
return 0;
}
int main()
{
cout << "该程序用来调换两个整数的值" << endl;
cout << "请输入要交换的整数值" << endl;
int a ,b;
cin >> a >> b;
cout << "交换前的值: " << "a= " << a << " b=: " << b << endl;
fact(&a,&b);
cout << "交换后的值: " << "a= " << a << " b=: " << b << endl;
cin.get();
}
6.17
#include<iostream>
#include<string>
using namespace std;
#include <vector>
#include <stdexcept>
bool My_reset(const string& My_string)
{
for (int i = 0; i != My_string.size() ; i++)
{
if (My_string[i] >= 'A' && My_string[i] <= 'Z')
return true;
}
return false;
}
int main()
{
cout << "该程序用来检验字符串中是否含有大写字母" << endl;
cout << "请输入目标字符串" << endl;
string string_1;
cin >> string_1;
if ( My_reset(string_1))
cout << "该字符串含有大写字母" << endl;
else
{
cout << "该字符串不含有大写字母" << endl;
}
cin.get();
}
#include<iostream>
#include<string>
using namespace std;
#include <vector>
#include <stdexcept>
string My_reset( string& My_string)
{
for (int i = 0; i != My_string.size() ; i++)
{
if (My_string[i] >= 'A' && My_string[i] <= 'Z')
{
cout << "该字符串含有大写字母" << endl;
My_string[i] += 32;
return My_string;
}
else
{
cout << "该字符串不含有大写字母" << endl;
return My_string;
}
}
}
int main()
{
cout << "该程序用来检验字符串中是否含有大写字母 并改大写字母为小写字母" << endl;
cout << "请输入目标字符串" << endl;
string string_1;
cin >> string_1;
cout << "修改前的字符串为" << string_1 << endl;
My_reset(string_1);
cout << "修改后的字符串为" << string_1 << endl;
cin.get();
}
6.27
#include<iostream>
#include<string>
using namespace std;
#include <vector>
#include <stdexcept>
int My_sum(initializer_list <int> il)
{
int sum = 0;
for (auto beg = il.begin(); beg != il.end() ; beg++)
{
sum += *beg;
}
return sum;
}
int main(int argc , char *argv[])
{
cout << My_sum({ 1, 2 ,3 ,4 ,5 }) << endl;
cin.get();
}
6.33 递归函数
#include<iostream>
#include<string>
using namespace std;
#include <vector>
#include <stdexcept>
void print(vector<int>::iterator vec_beg , vector<int>::iterator vec_end)
{
if (vec_beg != vec_end)
{
cout << *vec_beg << " ";
return print(++vec_beg, vec_end);
}
}
int main(int argc , char *argv[])
{
vector<int> vec = { 0 , 2, 4, 6, 7 };
print(vec.begin(), vec.end());
cin.get();
}
6.37
string (&func(string i))[10];
typedef string arrT[10];
using arrT = string[10];
arrT& func(arrT& arr);
auto fanc(arrT& arr)->string(&)[10];
string My_string[10];
decltype(My_string)& func(arrT& arr);
6.55
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int multi(int a, int b)
{
return a * b;
}
int divide(int a, int b)
{
return a / b;
}
int main(int argc, char* argv[])
{
vector<int (*) (int, int)> val = {add , sub , multi , divide}; //vector对象中保存 指向函数的指针
for (const auto cc : val)
cout << cc(30, 6) << " ";
cin.get();
}