C++ primer第3章作业

习题3.1

用适当的using声明,而不是用std::访问标准库的名字,给定次与幂,求结果

#include<iostream>
using std::cin;
using std::cout;
class cimi
{
	double di;
	int mi;
public:

	cimi(double a,int b)
	{
		di = a;
		mi = b;
		double temp=1;
		for (int i = 1; i <= b; i++)
		{
			temp *= this->di;
		}
		std::cout << temp << std::endl;
	}
	

};

int main()
{
	cout << "请输入底数与幂" << std::endl;
	
	double di;
	int mi;
	cin >> di;
	cin >> mi;
	cimi d(di, mi);


	system("pause");


	return 0;
}



习题3.5

编写程序实现从标准输入每次读入一行文本,然后改写程序,每次读入一个单词

#include<iostream>
#include<string>

int main()
{
	std::string z;
	while (getline(std::cin, z))//一次一行
		std::cout << z << std::endl;

	return 0;
}


int main1()
{
	std::string z;
	while (std::cin >> z)//一次一个单词
	{
		std::cout << z << std::endl;
	}
	return 0;
}


3.7 

编写一个程序读入2个String对象测试他们是否相等,若不相等指出哪个大,在测试他们长度

#include<iostream>
#include<string>
//测试是否相等,谁大
int main()
{


	std::string s1, s2;
	getline(std::cin, s1);
	getline(std::cin, s2);
	if (s1 == s2)
	{
		std::cout << "equal" << std::endl;
	}
	else if (s1 > s2)
		std::cout << "s1>s2" << std::endl;
	else
		std::cout << "s2>s1" << std::endl;
	system("pause");
  return 0;
}



#include<iostream>
#include<string>

int main()
{


	std::string s1, s2;
	getline(std::cin, s1);
	getline(std::cin, s2);
	if (s1.size() == s2.size())
	{
		std::cout << "equal" << std::endl;
	}
	else if (s1.size()>s2.size())
		std::cout << "s1>s2" << std::endl;
	else
		std::cout << "s2>s1" << std::endl;
	system("pause");
  return 0;
}


习题3.8编写一个程序,从标准读入多个string对象.连到一起,然后改写程序把连接后的string以空格隔开


#include<iostream>
#include<string>

int main()
{


	std::string s1, s2;
	while (std::cin >> s1)
		s2 += s1;
	std::cout << s2 << std::endl;

	system("pause");
	return 0;

}


以空格隔开

#include<iostream>
#include<string>

int main()
{


	std::string s1, s2;
	while (std::cin >> s1)
	{
		s2 += s1;
		s2 += " ";
	}
	
	std::cout << s2 << std::endl;

	system("pause");
	return 0;

}



编写程序,从string对象中去掉标点符号,

#include<iostream>
#include<string>

int main()
{
	std::string s;
	getline(std::cin, s);
	for (std::string::size_type j=0; j != s.size()-1; j++)
	{
		if (ispunct(s[j]))
		{
			std::cout << "木有此字符串" << std::endl;
			system("pause");
			return 0;
		}
	}
	std::cout << "此字符串为" << s << std::endl;
	system("pause");

}

习题3.13

读入一组整数到vector对象,计算并输出每对相邻元素的和,如果读入元素个数是奇数则提示用户最后一个元素没有求和


#include<iostream>
#include<vector>
int main()
{
	std::vector<int> num;
	int a;
	while (std::cin >> a)
		num.push_back(a);
	if (num.size() == 0)
	{
		std::cout << "无元素" << std::endl;
	}
	//下一节
	/*
	for(std::vector<int>::iterator q=num.begin();q!=num.end()-1;q+=2)
	{
	std::cout<<*q+*(q+1)<<std::endl;
	}
	*/
	for (std::vector<int>::size_type ix = 0; ix != num.size()-1; ix+=2)
	{
		std::cout << num[ix] + num[ix + 1] << std::endl;
	}
	if (num.size() % 2 != 0)
	{
		std::cout << "最后一个元素" << num[num.size() - 1] <<"没有相加"<< std::endl;
	}


	system("pause");

	return 0;
}


修改程序,头尾配对

#include<iostream>
#include<vector>
int main()
{
	std::vector<int> num;
	int a;
	while (std::cin >> a)
		num.push_back(a);
	std::vector<int>::size_type first = 0;
	std::vector<int>::size_type last = num.size() - 1;
	for (first, last; first < last; last--, first++)
	{
		std::cout << num[first] + num[last] << std::endl;
	}
	if (first == last)
	{
		std::cout << "居中这个数"<<num[first]<<"木有被加" << std::endl;
	}

	system("pause");

	return 0;
}



读入一段文本到vector对象,每个单词都存储为vector的一个元素,把vector对象中的字母转换为大写字母,输出vector对象中转换的元素

#include<iostream>
#include<string>
#include<vector>
#include<cctype>

int main()
{

	std::vector<std::string> str1;
	std::string str;
	while (std::cin >> str)
	{
		str1.push_back(str);
	}
	if (str1.size() == 0)
	{
		std::cout << "没有元素" << std::endl;
		return 0;
	}
	for (std::vector<std::string>::size_type ix = 0; ix != str1.size(); ++ix)
	{
		for (std::string::size_type index = 0; index != str1[ix].size(); ++index)
		{
			if (islower(str1[ix][index]))  //测试字符是否为小写字母
				str1[ix][index] = toupper(str1[ix][index]); // 将字符c转换为大写英文字母
		}
		if ((ix + 1) % 8 == 0)
		{
			std::cout << std::endl;
		}
	}



	return 0;
}

习题3.18

编写程序来创建10个元素的vector对象,用迭代器把每个元素值改成当前值2倍


#include<iostream>
#include<string>
#include<vector>
#include<cctype>

int main()
{
	std::vector<int> a;
	int num;
	while (std::cin >> num)
		a.push_back(num);
	for (std::vector<int>::iterator p = a.begin(); p != a.end(); ++p)
	{
		*p = (*p) * 2;
	}
	system("pause");
	return 0;
}


习题3.21

何时使用const 迭代器?又在何时使用const_iterator?解释两者的区别

const 迭代器是迭代器常量,该迭代器本身的值不能修改,即该迭代器在定义时

需要初始化,而且初始化之后,不能再指向其他元素。若需要指向固定元素的

迭代器,则可以使用const 迭代器。

    const_iterator 是一种迭代器类型,对这种类型的迭代器解引用会得到一个指

向const 对象的引用,即通过这种迭代器访问到的对象是常量。该对象不能修

改,因此,const_iterator 类型只能用于读取容器内的元素,不能修改元素的

值。若只需遍历容器中的元素而无需修改它们,则可以使用const_iterator。




习题3.22 
如果采用下面的方法来计算mid 会产生什么结果? 
vector<int>::iterator mid = (vi.begin() + vi.end())/2; 

迭代器没有初始化,肯定错误


习题3.23 
解释下面每个bitset 对象包含的位模式: (

a) bitset<64> bitvec(32);

 (b) bitset<32> bv(1010101); 
(c) string bstr; cin >> bstr; bitset<8> bv(bstr);

(a)第5个位置为1,从0开始,32=2的5次方,其他都为0

(b)0、2、4、5、7、8、11、13、 14、16、17、18、19 位置为1,其余位置均为0。因为十进制数1010101 对应的 

二进制数为000000000000011110110100110110101。
注意: (c)bv 有8 个二进制位,(位编号从0 开始)用读入的字符串的从右至左的8 个字符对bv 的0~7 位进行初始化



习题3.24 
考虑这样的序列1,2,3,5,8,13,21,并初始化一个将该序列数字所对应的位置设 置为1 的bitset<32>对象。然后换个方法,给定一个空的bitset 对象,编写一 小段程序把相应的数位设置为1。

#include<iostream>
#include<bitset>

int main()
{
	std::bitset<32> bv;
	int x = 0, y = 1, z;
	z = x + y;
	while (z <= 21)
	{
		bv.set(z);
		x = y;
		y = z;
		z = x + y;
	}
	
}
斐波那契定律





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值