栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ prime 第六章

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ prime 第六章

习题 6.3

#include
#include
using namespace std;
#include 
#include 

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
#include
using namespace std;
#include 
#include 

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
#include
using namespace std;
#include 
#include 

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
#include
using namespace std;
#include 
#include 

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
#include
using namespace std;
#include 
#include 

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
#include
using namespace std;
#include 
#include 

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
#include
using namespace std;
#include 
#include 

int My_sum(initializer_list  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
#include
using namespace std;
#include 
#include 

void print(vector::iterator vec_beg , vector::iterator vec_end)
{
	if (vec_beg != vec_end)
	{
		cout << *vec_beg << "  ";
		return print(++vec_beg, vec_end);
	}
}


int main(int argc , char *argv[])

{	
	vector 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 
#include
#include
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 val = {add , sub , multi , divide}; //vector对象中保存 指向函数的指针
	for (const auto cc : val)
		cout << cc(30, 6) << "   ";


	cin.get();
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348299.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号