Essential C++第二章习题

本文介绍了C++编程中的一些关键概念,包括使用while循环处理用户输入,计算斐波那契数列和潘多拉数列,以及函数模板的应用。在示例中,展示了如何检查输入的有效性,计算特定位置的数列元素,并在用户交互中实现再次尝试的功能。此外,还展示了如何定义和使用函数模板来处理不同类型的数据,例如整数、浮点数和字符串,以实现最大值计算的通用性。
摘要由CSDN通过智能技术生成

调用函数

2-1(又有问题) while循环那里不能识别输入的char.

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
bool fibon_elem(int pos, int &elem)
{
    if(pos <= 0 || pos > 1024)
      {elem = 0; return false;}

    elem = 1;
    int n_2 = 1, n_1 = 1;

    for(int ix = 3; ix <= pos; ++ix)
    {
        elem = n_2 + n_1;
        n_2 = n_1;
        n_1 = elem;
    }
    return true;
}

int main()
{
    int pos;
    // 其次,这个elem变量又是没有初始化,也没有赋值
    int elem = 1;
    char ch;
    bool more = true;
    while(more)
    {
    cout << "Please enter a position: ";
    cin >> pos;

   
    if (fibon_elem(pos, elem))
    cout <<"element # " <<pos
         <<"is" << elem <<endl;
    else cout << "Sorry, Could not calculate element #"
              << pos <<endl;    

    cout << "would you like to try again? (y/n) ";
       cin >> ch;
       if ( ch != 'y' || ch != 'Y' )
		   { more = false;}        

    }    
    return 0;                                                                                      
}

2-2

#include <iostream>
#include <vector>
#include <string>
using namespace std;
void cal_element(vector<int> &vec, int pos)
{
    if(pos <= 0||pos > 1024)
    {
        cout << "this number can not "<< endl;
    }
    for(int i = 1; i <= pos;i++)
    {   
        int sum = i*(3*i-1)/2;
        vec.push_back(sum);
    }
}
void print(vector<int> &vec, const string &str)
{
    cout << "the Pentagonal sequence is: ";
    for(int i = 0; i < vec.size(); i++)
    {
        cout<<vec[i]<<' ';
    }
    cout <<"the type of the vector is: "
         <<str<<endl;
}


int main()
{
  vector<int> vec;
  int num = 1;
  string str = "int";
  cout << "please input a number: ";
  cin >> num;
  cal_element(vec, num);
  print(vec, str);
  return 0;

}

2-3

#include <iostream>
#include <vector>
#include <string>
using namespace std;

inline bool CheckNum(const int pos)
{
	if (pos <= 0 || pos > 1024)
	{
		cout << "Oops:requested num is not supported" << endl;
		return false;
	}
	return true;
}
void cal_element(vector<int> &vec, int pos)
{
    if (!CheckNum)
	{
		return;
	}
    for(int i = 1; i <= pos;i++)
    {   
        int sum = i*(3*i-1)/2;
        vec.push_back(sum);
    }
}
void print(vector<int> &vec, const string &str)
{
    cout << "the Pentagonal sequence is: ";
    for(int i = 0; i < vec.size(); i++)
    {
        cout<<vec[i]<<' ';
    }
    cout <<"the type of the vector is: "
         <<str<<endl;
}


int main()
{
  vector<int> vec;
  int num = 1;
  string str = "int";
  cout << "please input a number: ";
  cin >> num;
  cal_element(vec, num);
  print(vec, str);
  return 0;

}

2-4

#include <iostream>
#include <vector>
#include <string>
using namespace std;

inline bool CheckNum(const int pos)
{
	if (pos <= 0 || pos > 1024)
	{
		cout << "Oops:requested num is not supported" << endl;
		return false;
	}
	return true;
}
const vector<int>* cal_element(int pos)
{
    static vector<int> vec1;
    if (pos <= 0 || pos > 1024)
	{
		cout << "Oops:requested num is not supported" << endl;
		return 0;
	}
    for(int i = 1; i <= pos; i++)
    {   
        int sum = i*(3*i-1)/2;
        vec1.push_back(sum);
    }
    return &vec1;
}
void BackNum(int num)
{
	const vector<int> *vec = cal_element(num);        //将pentagonal函数返回的值赋予*vec  即 *vec=&vec1
	cout << "The value of this number in Pentagonal sequence is: ";
	cout << (*vec)[num-1] << endl;    //记得此时vec是指针,需要用 (*vec)。
	
}



int main()
{
  int num = 1;
  cout << "please input a number: ";
  cin >> num;
  cal_element(num);
  BackNum(num);
 
  return 0;

}

2-5

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;


void max(int, int)
{}
void max(float, float)
{}
void max(string &, string&)
{}
int main()
{
	int num1, num2;
	float num3, num4;
	string str1, str2;
	cin >> num1 >> num2 >> num3 >> num4 >> str1 >> str2;
	max(num1, num2);
	max(num3, num4);
	max(str1, str2);
	system("pause");
}

2-6

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

template <class T>
void max(T &x, T&y)
{
    
}
int main()
{
	int num1, num2;
	float num3, num4;
	string str1, str2;
	cin >> num1 >> num2 >> num3 >> num4 >> str1 >> str2;
	max(num1, num2);
	max(num3, num4);
	max(str1, str2);
	system("pause");
}

关于函数模板:function template以关键字template开场,其后紧接着以成对尖括号<>包围起来的一个或者多个标识符。这些标识符用来表示我们希望推迟决定的数据类型。用户每次利用这个模板产生函数,都必须退工确实的类型信息。这些标识符事实上扮演者占位符的角色,用来放置函数参数列表以及函数体中的某些实际数据类型。
举例:

#include <iostream>
using namespace std;
template<class T>
void Swap(T & x, T & y)
{
    T tmp = x;
    x = y;
    y = tmp;
}
int main()
{
    int n = 1, m = 2;
    Swap(n, m);  //编译器自动生成 void Swap (int &, int &)函数
    double f = 1.2, g = 2.3;
    Swap(f, g);  //编译器自动生成 void Swap (double &, double &)函数
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值