本来想做一做后面的练习题,后来发现跟前面有牵连,我只看不动手看不懂,又把前面的内容梳理了一遍,代码实现如下
/*第二章的内容*/
/*编写一个函数该函数返回F数列中用户指定的某个位置的元素
例如用该数列的第八个元素是什么?
我们的程序回答:21*/
int fibon_elem(int pos);
int fibon_elem(int pos)
{
int 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 elem;
}
//考虑若用户输入不合理的值比如0或者负数该咋办
int fibon_elem(int pos)
{
int elem=1;//持有欲返回的值
int n_2=1,n_1=1;
if(pos<=0)
exit(-1);//应该加头文件#include<cstdlib>
//这是方式书上说太激烈,为嘛?
for(int ix=3;ix<=pos;++ix)
{
elem=n_2+n_1;
n_2=n_1;n_1=elem;
}
return elem;
}
//考虑异常处理,但是还没讲到
//重新修正函数模型,还要考虑需要返回值的大小不能溢出了。设定上限为1024
bool fibon_elem(int pos,int &elem)//这第二个参数很妙,我赶脚啊
{
//检查位置值是否合理
if(pos<=0||pos>1024)
{
elem=0;
return false;
}
//位置为1和2时,elem的值为1;
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;
}
//下面的小程序来练习fibon_elem()
#include<iostream>;
using namespace std;}
bool fibon_elem(int,int &);//此为函数声明
int main()
{
int pos;
cout<<"please enter a positon";
cin>>pos;
int elem;
if(fibon_elem(pos,elem))
cout<<"element#"<<pos<<"is"<<elem<<endl;
else cout<<"sorry。Could not caculate element#"
<<pos<<endl;
}
boo1 print_sequence(int pos)
{
if(pos<=0||pos>1024)
{
cerr<<"invaild position:"<<pos<<"--cannot handle request!\n";
return false;
}
cout<<"the fibon sequence for"<<pos<<"positions:\n\t";
switch(pos)//pos=1那么只打印一个1,若pos为其他值那么打印两个1
{
defalut:
case 2:
cout<<"1";//注意此处没有break,没有break的话,后面的case继续执行并且忽略判断条件,这儿写的也非常好
case 1:
cout<<"1";
break;
}
int elem;
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;
cout<<ele,<<(!(ix%10)?"\n\t":" ");
}
cout<<endl;
return true;//此函数有两个推出点,必须保证都有return
}
练习2.1 先前的main()只让用户输入一个位置值,然后便结束程序,如果想让用户取得两个甚至多个元素值,他必须执行这个程序两次或者多次,请改写main(),使它允许用户不断输入位置值,直到用户希望停止为止。
我的想法是用while(cin>>pos)循环来输入所有pos值然后将这些值存入一个数组gpos[size]。此处size我感觉可以提前计数。将gpos数组的值都作为函数的参数带入,最后将所有的数字重新替代位置存入这个数组。
以上是我的想法,现在来实现看看。
昨天晚上思考了一下如何写这个程序,但是我对题目的理解是错误的,上述的想法是用户一次性输入所有想知道的值,实际上输入5这个位置,然后立刻出现此位置的数,然后继续输入,直到用户不想输入。第一种想法已经实现代码如下:
#include<iostream>;
#include<vector>;
using namespace std;
bool fibon_elem(int,int &);
int main()
{
int pos;
int i=0;
vector<int> vpos;
cout<<"please enter the positions you want to know:\n";
while(cin>>pos)
{
vpos.push_back(pos);
}
vector<int> elem(vpos.size());
for(int j=0;j<vpos.size();j++)
{
if(fibon_elem(vpos[j],elem[j]))
cout<<"element#"<<pos
<<"is"<<elem[j]<<endl;
}
}
bool fibon_elem(int pos,int &elem)//这第二个参数很妙,我赶脚啊
{
//检查位置值是否合理
if(pos<=0||pos>1024)
{
elem=0;
return false;
}
//位置为1和2时,elem的值为1;
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;
}
接下来思考如何真正的解出该题0_0::
#include<iostream>;
#include<vector>;
using namespace std;
bool fibon_elem(int,int &);
int main()
{
int pos;
int i=0;
int elem;
//vector<int> vpos;
cout<<"please enter a position:\n";
while(cin>>pos)
{
if(fibon_elem(pos,elem))
cout<<"element#"<<pos
<<"is"<<elem<<endl;
elem=0;
cout<<"if you do not want to know the number please enter any letter!\n"
<<"if not,please enter a position:"
<<endl;
}
}
bool fibon_elem(int pos,int &elem)//这第二个参数很妙,我赶脚啊
{
//检查位置值是否合理
if(pos<=0||pos>1024)
{
elem=0;
return false;
}
//位置为1和2时,elem的值为1;
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;
}
“`
写的有点牵强大概这个意思,就是一直输入想知道的位置。这俩个程序都属于比较简单的。