(环境Dev-c++)
斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家莱昂纳多·斐波那契(Leonardo Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2)。请编程使用数组给出斐波那契数列前30项。
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int *p=new int[20];
int *temp=p;
if(!p)
{
cout<<"动态分配存储空间失败!";
}
*p=1;
*(p+1)=1;
cout<<setw(8)<<*p<<setw(8)<<*(p+1);
p=p+2;
for(int i=3;i<=20;i++)
{
*p=*(p-1)+*(p-2);
cout<<setw(8)<<*p;
if(i%5==0) cout<<endl;
p++;
}
delete temp;
return 0;
}
使用数组存储10个数据,利用冒泡排序使数据从小到大输出。
#include<iostream>
using namespace std;
int main()
{
const int n = 10;
void select_sort(int *p,int n);
int arr[n] = { -2,122,34,654,4364,32,54,4,65,0 };
select_sort(arr, n);
cout << "The sorted array:" << endl;
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
return 0;
}
void select_sort(int *p, int n)
{
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-1-i; j++)
{
if (p[j]>p[j+1])
{
int tmp = p[j];
p[j] = p[j + 1];
p[j + 1] = tmp;
}
}
}
}
使用数组存储10个数据,利用选择排序使数据从小到大输出。
#include <iostream>
using namespace std;
int main()
{
int a[10] = { 4,12,13,4,3,42,33,1,43,44 };
int i, j, min, pos_min;
int t;
min = a[0]; pos_min = 0;
for (j = 0; j < 9; j++)
{
min = a[j];
pos_min = j;
for (i = j + 1; i < 10;i++)
{
if (min > a[i])
{
min = a[i];
pos_min = i;
}
}
t = a[j]; a[j] = a[pos_min]; a[pos_min] = t;
}
for (i = 0; i < 10; i++)
cout<<a[i]<<" ";
return 0;
}
.定义学生类(Student),成员函数包括构造函数(学号和姓名使用本人信息赋值)、析构函数、显示函数以及其他函数,并在主函数中测试该类对象。
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student (int n, string nam)
{
cout<<"Constructor is called:"<<n<<endl;
num=n;
name=nam;
}
~Student ()
{
cout<<"Destructor is called: "<<num<<endl;
}
void get_data();
private:
int num;
string name;
};
void Student::get_data()
{
if(num==0)
throw num;
else
cout<<"num="<<num<<" name="<<name<<endl;
cout<<"end of get data"<<endl;
}
void fun()
{
Student s1(1001001,"张三");
s1.get_data();
Student s2(0, "Zhang San");
s2.get_data();
}
int main()
{
try
{
fun();
}
catch(int n)
{
cout<<"num="<<n<<" error!"<<endl;
}
return 0;
}
用先定义“高度”类Hight和“圆”类Circle,再由Hight和Circle多重派生出“圆柱体”类Cylinder。在主函数中定义一个圆柱体对象,调用成员函数求出圆柱体的体积和表面积。
int main( )
{
Cylinder s(5,3); //5是圆柱体底圆半径,3是圆柱体高度
s.tiji( ); //此行输出圆柱体体积
s.biaomianji( ); //此行输出圆柱体表面积
return 0;
}
注:main函数无须再写。(类中的数据成员不能定义为public。)
#include<iostream>
using namespace std;
class Hight
{
float h;
public:
Hight(int y) { h = y; }
float geth()
{
return h;
}
};
class Circle
{
float r;
public:
Circle(float x) { r = x; }
float getr()
{
return r;
}
};
class Cylinder:public Hight,public Circle
{
public:
Cylinder(float x, float y):Hight(y),Circle(x){}
void tiji()
{
float S, H, R;
H = geth();
R = getr();
S = 3.14 * R * R * H;
cout << "体积为" << S << endl;
}
void bmj()
{
float H, R;
H = geth();
R = getr();
cout << "表面积为" << 6.28 * R * R + 6.28 * R * H << endl;
}
};
int main()
{
Cylinder s(5, 3);
s.tiji();
s.bmj();
return 0;
}
有两个矩阵a和b,均为2行3列。用面向对象的方法求两个矩阵之和。要求根据main( )函数中的操作去设计M类,在该类中根据运算符操作的要求去重载相应的运算符。两矩阵相加c =a+b :
输入第一个矩阵数据为:1 2 3 第二个矩阵数据为:1 1 1 则两矩阵和为:2 3 4
4 5 6 1 1 1 5 6 7
int main( )
{
M a,b,c;
cin>>a;
cin>>b;
c=a+b;
cout <<c<<endl;
return 0;
}
#include <iostream>
using namespace std;
class qua {
public :
friend ostream& operator<<(ostream&,qua&);
friend istream& operator>>(istream&,qua&);
friend qua operator + (qua & c1,qua & c2);
qua();
private :
int Q[2][3];
};
qua::qua() {
for(int i=0; i<2; i++)
for(int j=0; j<3; j++)
Q[i][j]=0;
}
ostream & operator <<(ostream & output,qua& c) {
for (int i=0; i<2; i++) {
for (int j=0; j<3; j++)
output<<c.Q[i][j]<<" ";
output<<endl;
}
return output;
}
istream & operator >>(istream & input,qua& c) {
for (int i=0; i<2; i++)
for (int j=0; j<3; j++)
input>>c.Q[i][j];
return input;
}
qua operator +(qua & c1,qua & c2) {
qua c3;
for (int i=0; i<2; i++)
for (int j=0; j<3; j++)
c3.Q[i][j]=c1.Q[i][j]+c2.Q[i][j];
return c3;
}
int main() {
qua c1,c2,c3;
cin>>c1>>c2;
c3=c1+c2;
cout<<c3;
return 0;
}