1.编写一个c++风格的程序,用动态分配空间的方法计算Fibonacci数列的前20项并存储到动态分配的空间中
1.代码如下
#include <iostream>
using namespace std;int main()
{int *a = new int[25];a[0] = 0;a[1] = 1;for (int i = 2; i <= 20; i++){a[i] = a[i - 1] + a[i - 2];}for (int i = 0; i <= 20; i++){cout << a[i] << " ";}cout << endl;return 0;
}
2.代码如下:
#include<iostream> using namespace std;int main(){ int *p,*q=new int[20]; //动态分配20个整型内存空间p=q;*p=1;*(p+1)=1; //对前面2个内存空间赋值1cout<<*p<<"\t"<<*(p+1)<<"\t";p=p+2; //p指向第3个内存空间for (int i=3;i<=20;i++){ *p=*(p-1)+*(p-2);cout<<*p<<"\t";if (i%5==0) cout<<endl;p++; //p指向下一个内存空间;}return 0;}
2.编写一个c++风格的程序,建立一个被称为sroot()的函数,返回其函数的二次方根。重载函数sroot()3次,让它返回整数,长整数和双精度数的二次方根(计算二次方根是,可以使用标准库函数 sqrt()).
1.代码如下:
#include <iostream>
#include <cmath>
using namespace std;double sroot(int a)
{return sqrt(1.0*a);
}double sroot(long a)
{return sqrt(1.0*a);
}double sroot(double a)
{return sqrt(1.0*a);
}int main(){ int i=12;long l=1234;double d=12.34;cout<<"i的二次方根是:"<<sroot(i)<<endl;cout<<"l的二次方根是:"<<sroot(l)<<endl;cout<<"d的二次方根是:"<<sroot(d)<<endl;return 0;}
3.编写一个C++风格的程序,解决百钱问题:将一元人民币兑换成1、2、5分的硬币,有多少种换法?
1.代码如下:
#include <iostream>
using namespace std;int main()
{int cnt = 0;for (int i = 0; i * 1 <= 100; i++)for (int j = 0; j * 2 + i * 1 <= 100; j++)for (int k = 0; k * 5 + j * 2 + i * 1 <= 100; k++){if (i * 1 + j * 2 + k * 5 == 100) cnt++;}cout << cnt << endl;return 0;
}
2.代码如下:
#include<iostream> using namespace std;int main(){ int i,j,sum=0;;for(i=0;i<=20;i++)for (j=0;j<=50;j++)if (100-5*i-2*j>=0){ sum++;cout<<100-5*i-2*j<<"\t"<<j<<"\t"<<i<<endl;}cout<<"sum is "<<sum<<endl;return 0;}
4.下面是一个计算器类的定义,请完成该类成员函数的实现。
class counter {public:counter(int number);void increment(); //给原值加1void decrement(); //给原值减1int getvalue(); //取得计数器值int print(); //显示计数private:int value;};
1.代码如下:
#include <iostream>
using namespace std;class counter{public:counter(int number) :value(number) {};void increment(){value++;}//给原值加1void decrement(){value--;}//给原值减1int getvalue(){return value;}//取得计数器值int print(){cout << "value = " << value << endl;}//显示计数private:int value;};
5.根据注释语向的提示,实现类Date的成员函数
#include<iostream>using namespace std;class Date{public:void printDate(); //显示日期void setDay(int d); //设置日的值void setMonth(int m); //设置月的值void setYear(int y); //设置年的值private:int day,month, year;};int main(){ Date testDay;testDay.setDay(5);testDay.setMonth (10);testDay.setYear(2003);testDay.printDate();return 0;}
1.代码如下:
#include<iostream>using namespace std;class Date{public:void printDate(){cout << "year = " << year << endl;cout << "month = " << month << endl;cout << "day = " << day << endl;}//显示日期void setDay(int d){day = d;}//设置日的值void setMonth(int m){month = m;}//设置月的值void setYear(int y){year = y;}//设置年的值private:int day, month, year;};int main(){Date testDay;testDay.setDay(5);testDay.setMonth(10);testDay.setYear(2003);testDay.printDate();return 0;}
6.建立cylinder,类cylinder的构造函数被传递了两个 double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个 double变量中。在类cylinder包含一个成员函数vol(),用来显示每个 cylinder 对象的体积。
1.代码如下:
#include <iostream>
using namespace std;const double PI = 3.1415926;class cylinder
{
public:cylinder(double h, double r):h(h),r(r){}void calV(){v = PI * r*r*h;}void vol(){cout << "v = " << v << endl;}private:double h;double r;double v;
};int main()
{cylinder c1(21.2, 2.3);c1.calV();c1.vol();return 0;
}
7.构建一个类Stock,含字符数组stockcode[]及整型数据成员 quantity,双精度型数据price。构造函数含3个参数,含字符数组na[]、q和p,当定义Stock的类对象时,将对象的第1个字符串参数赋给成员 stockcode,第2和第3个参数分別赋給 quantity和price。未设置第2和第3个参数时 ,quantity的值为1000,price的值为8.98。成员函数 print()没有形参,需使用this指针,显示对象为成员的内容。假设类Stock第1个对象的3个参数分別为"600001”,3000和5.67;第2个对象的第1个数据成员的值是"600001”,第2和第3个数据成员的值取默认值。编写程序分别显示对象数据成员的值。
1.代码如下:
#include <iostream>
#include <cstring>
#include <string>using namespace std;
const int N = 200;class Stock
{
public:Stock(const char na[], int q = 1000, double p = 8.98){strcpy(stockcode, na);quantity = q;price = p;}void print(){cout << "stockcode = " << this->stockcode << endl;cout << "quantity = " << this->quantity << endl;cout << "price = " << this->price << endl;}private:char stockcode[N];int quantity;double price;
};int main()
{Stock s1("600001", 3000, 5.67);Stock s2("600001");s1.print();s2.print();return 0;
}
8.编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。
1.代码如下:
#include <iostream>
#include <string>using namespace std;class Student
{
public:Student(string n, int hao, double g):name(n),xuehao(hao),grade(g){studentNum++;gradeSum += g;}void print(){cout << "xuehao = " << xuehao << endl;cout << "name = " << name << endl;cout << "grade = " << grade << endl;}static void calNum(){cout <<"studentNum = "<< studentNum << endl;}static void averageGrade(){cout << 1.0*gradeSum / studentNum << endl;}private:int xuehao;string name;double grade;static int studentNum;static double gradeSum;
};int Student::studentNum = 0;
double Student::gradeSum = 0;int main()
{Student s1("xiaom", 210, 340);Student s2("xiaol", 211, 450);Student s3("bibi", 212, 370);Student::calNum();Student::averageGrade();return 0;
}
9.编写一个有关股票的程序,其中有两个类:一个是深圳类shen_stock,另一个是上海类shang_stock。类中有三项私有数据成员:普通股票个数general、ST股票个数st和PT股票个数pt,每一个类分别有自己的友元函数来计算并显示深圳或上海的股票总数(三项的和)。两个类还共用一个count(),用来计算深圳和上海总共有多少股票并输出。
1.代码如下:
#include <iostream>
using namespace std;
class shang_stock;class shen_stock
{
public:shen_stock(int a,int b,int c):general(a),st(b),pt(c){}friend void calSum_shen(shen_stock &);friend void calSum_two(shen_stock &, shang_stock &);
private:int general;int st;int pt;};void calSum_shen(shen_stock &p)
{cout << "stockNum_shen = "<<p.general + p.st + p.pt << endl;
}class shang_stock
{
public:shang_stock(int a,int b,int c):general(a),st(b),pt(c){}friend void calSum_shang(shang_stock &);friend void calSum_two(shen_stock &, shang_stock &);
private:int general;int st;int pt;
};void calSum_shang(shang_stock &p)
{cout <<"stockNum_shang = "<< p.general + p.pt + p.st << endl;
}void calSum_two(shen_stock &a, shang_stock &b)
{cout << "shang and shen stockSum = "<<a.general + a.st + a.pt + b.general + b.pt + b.st << endl;
}int main()
{shen_stock s1(12, 13, 14);shang_stock s2(7, 23, 12);calSum_shen(s1);calSum_shang(s2);calSum_two(s1, s2);return 0;
}
10.构建一个类Stock,含字符数组stockcode[]及整型数据成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设置第2和第3个参数时,quan的值为1000,price的值为8.98。成员函数 print()使用this指针,显示对象内容。
1.代码如下:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 200;
class Stock
{
public:Stock(const char s[], int a = 1000, double b = 8.98) :quan(a), price(b){strcpy(stockcode, s);}void print(){cout << "stockcode = " << this->stockcode << endl;cout << "quan = " << this->quan << endl;cout << "price = " << this->price << endl;}private:char stockcode[N];int quan;double price;
};int main()
{Stock s1("xiaom");Stock s2("xiaph", 12, 3.42);s1.print();s2.print();return 0;
}
11.构建一个类book,其中含有两个私有数据成员qu和price, 建立一个有5个元素的数组对象,将qu初始化为1~5,将price 初始化为qu的10倍。显示每个对象的qu*price。
1.代码如下:
#include <iostream>
using namespace std;class book
{
public:book(int a) :qu(a), price(10 * a) {}void print(){cout << "qu*price = " << qu * price << endl;}private:int qu;double price;
};int main()
{book b[5] = { 1,2,3,4,5 };/*book b[5] = { book(1),book(2),book(3),book(4),book(5) };*/for (int i = 0; i < 5; i++){b[i].print();}return 0;
}
12.已有类Time和Date,要求设计一个派生类 Birthtime,它继承类Time和类Date,并且增加一个数据成员 Childname用于表示小孩的名字,同时设计主程序显示一个小孩的出生时间和名字。
class Time{public:Time(int h,int m,int s){ hours=h;minutes=m;seconds=s;}void display(){cout<<"出生时间:"<<hours<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;}protected:int hours,minutes,seconds;};class Date{public:Date(int m,int d,int y){ month=m;day=d;year=y;}void display(){cout<<"出生年月:"<<year<<"年"<<month<<"月"<<day<<"日"<<end1;}protected:int month,day,year;};
1.代码如下:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100;class Time {public:Time(int h, int m, int s){hours = h;minutes = m;seconds = s;}void display(){cout << "出生时间:" << hours << "时" << minutes << "分" << seconds << "秒" << endl;}protected:int hours, minutes, seconds;
};class Date {public:Date(int m, int d, int y){month = m;day = d;year = y;}void display(){cout << "出生年月:" << year << "年" << month << "月" << day << "日" << endl;}protected:int month, day, year;
};class Birthtime:public Time,public Date
{
public:Birthtime(const char name[], int y, int m, int d, int h, int m1, int s) :Date(m,d,y),Time(h,m1,s) {strcpy(Childname, name);}void print(){cout << "name = " << Childname << endl;Date::display();Time::display();}private:char Childname[N];};int main()
{Birthtime b1("xiaom", 2001, 3, 5, 8, 9, 12);b1.print();return 0;
}
13.给出下面的基类:
class area_cl {protected:double height;double width;public:area_cl(double r,double s){ height=r;width=s;}virtual double area()=0;};
要求:
(1)建立基类area_cl的两个派生类rectangle与isosceles,让每一个派生类都包含一个函数area(),分别用来返回矩形与三角形的面积。用构造函数对height与width进行初始化。
(2)写出主程序,用来求height与width分别为10.0与5.0的矩形面积,以及求height与width分别为4.0与6.0的三角形面积
(3)要求通过使用基类指针访问虚函数的方法(即运行时的多态性)分别求出矩形和三角形面积。
1.代码如下:
#include <iostream>
using namespace std;class area_cl {protected:double height;double width;public:area_cl(double r, double s){height = r; width = s;}virtual double area() = 0;};class rectangle :public area_cl
{
public:rectangle(double h, double w) :area_cl(h, w) {}double area(){return width * height;}};class isosceles :public area_cl
{
public:isosceles(double h, double w) :area_cl(h, w) {}double area(){return width * height*0.5;}
};int main()
{rectangle r1(10.0, 5.0);isosceles s1(4.0, 6.0);area_cl *p = nullptr;p = &r1;cout << p->area() << endl;p = &s1;cout << p->area() << endl;return 0;
}
定义基类Base其数据成员为高h,定义成员函教disp()为虚函数。然后,再由基类派生出长方体类Cuboid与圆柱体类Cylinder。并在两个派生类中定义成员函数disp()为虚函数。在主函教中,用基类Base定义指针变量pc,然后用指针pc动态调用基类与派生类中的虚函教disp(),显示长方体与圆柱体的体积。
1.代码如下:
#include <iostream>
using namespace std;
const double PI = 3.1415926;class Base
{
public:Base(double h):h(h){}virtual void disp() = 0;
protected:double h;
};class Cuboid :public Base
{
public:Cuboid(double l,double w,double h):Base(h),l(l),w(w){}void disp(){cout << "Cuboid's v = " << l * w*h << endl;}
private:double l;double w;
};class Cylinder :public Base
{
public:Cylinder(double r,double h):Base(h),r(r){}void disp(){cout << "Cylinder's v = " << PI * r*r*h << endl;}
private:double r;
};int main()
{Base *p = nullptr;Cuboid c1(2, 3, 4);Cylinder c2(3, 5);p = &c1;p->disp();p = &c2;p->disp();return 0;
}
15.给出下面的抽象基类container:
class container{ //声明抽象类containerprotected:double radius;public:container(double radius1); //抽象类container的构造函数virtual double surface_area()=0; //纯虚函数surface_area virtual double volume()=0; //纯虚函数volume};
要求建立三个继承container 的派生类cube 、sphere与cylinder,让每一个派生类都包含虚函数surface_area()和volume(),分别用来计算正方体、球体和圆柱体的表面积及体积。要求写出主程序,应用C++的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积和体积。
1.代码如下:
#include <iostream>
using namespace std;
const double PI = 3.1415926;
class container { //声明抽象类containerprotected:double radius;public:container(double radius1) :radius(radius1) {}; //抽象类container的构造函数virtual double surface_area() = 0; //纯虚函数surface_area virtual double volume() = 0; //纯虚函数volume};class cube :public container
{
public:cube(double a):container(a){}double surface_area(){return 6 * radius*radius ;}double volume(){return radius * radius*radius ;}
private:
};class sphere :public container
{
public:sphere(double a):container(a){}double surface_area(){return 4 * PI*radius*radius ;}double volume(){return 4.0/3.0*PI*radius * radius*radius ;}
private:
};class cylinder :public container
{
public:cylinder(double a,double b):container(a),h(b){}double surface_area(){return 2*PI*radius*radius+2*PI*radius*h ;}double volume(){return PI*radius*radius*h ;}
private:double h;
};int main()
{container *p = nullptr;cube s1(6.0);sphere s2(5.0);cylinder s3(5.0, 6.0);p = &s1;cout<<p->surface_area()<<endl;cout<<p->volume()<<endl;p = &s2;cout << p->surface_area() << endl;cout << p->volume() << endl;p = &s3;cout << p->surface_area() << endl;cout << p->volume() << endl;return 0;
}