调用函数
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;
}