c++考试程序题

六、程序设计题

1. 定义一个长度为5的实型数组,元素值通过键盘输入。实现以下功能:

(1)用下标法输出数组中的元素;

(2)将数组中下标为奇数的元素的值改为原值的三分之一,其余元素的值改为原值的平方根。将修改以后的数组中的元素以指针法输出。(7分)

#include<iostream.h>

#include<math.h>

class shuzu

{

floata[5];

public:

voidshuru();//输入数组元素的值

voidshuchu();//输出数组元素的值

voidjisuan();//如果数组中下标为奇数的元素的值改为原值的三分之一,其余元素的值改为原值的平方根

};

void shuzu::shuru()

{  

    cout<<"输入数组的值:";

     for(inti=0;i<5;i++)

     {  

        cin>>a[i];

     }

}

void shuzu::shuchu()

{      cout<<"数组的值为:";

     for(inti=0;i<5;i++)

     {  

        cout<<a[i]<<'\t';

     }

     cout<<endl;

}

void shuzu::jisuan()

{

     for(inti=0;i<5;i++)

     {

        if(i%2!=0)

        {

            a[i]=a[i]/3;

        }

        else

            a[i]=sqrt(a[i]);

     }

     cout<<"计算后数组的值为:";

     for(float*p=a;p<a+5;p++)//使用指针输出数组元素

     {

          

        cout<<*p<<'\t';

     }

     cout<<endl;

}

void main()

{

shuzu s;

s.shuru();

s.shuchu();

s.jisuan();

}2. 定义一个圆类(Circle),私有数据成员为半径(radius)、圆周长和面积,共有成员函数为输入半径;计算周长、面积;输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0,周长和面积在构造函数中生成)和复制构造函数。再主函数中创建2个对象,一个通过构造函数初始化,一个通过复制构造函数初始化,并输出三个圆对象的半径,周长和面积。(9分)

#include<iostream.h>

#define PI 3.14159

class Circle

{

private:

floatradius,girth,area;

public:

Circle(floatr=0);//构造函数(以半径为参数,缺省值为0)

Circle(Circle&c);//复制构造函数

voidinput();//输入半径

voidjisuan();//计算周长、面积

voidoutput();//输出半径、周长和面积

};

Circle::Circle(float r)

{

radius=r;

}

Circle::Circle(Circle &c)

{

radius=c.radius;

}

void Circle::input()

{

cout<<"输入圆的半径:";

cin>>radius;

}

void Circle::jisuan()

{

   girth=2*PI*radius;

   area=PI*radius*radius;

}

void Circle::output()

{

cout<<"圆的半径:"<<radius<<endl;

cout<<"圆的周长:"<<girth<<endl;

cout<<"圆的面积:"<<area<<endl;

}

void main()

{

  Circle  c1;

  Circle c2(c1);

  c1.input();

  c1.jisuan();

  c1.output();

  c2.input();

  c2.jisuan();

  c2.output();

 

}

3.设计一个学生类,包含姓名,性别,年龄,籍贯,班级数据信息,定义3个类对象,请以友元函数实现输出年龄大于20的学生信息。

#include<iostream.h>

#include<string.h>

class Student

{

char*name;

char*sex;

int age;

char*home;

char*grade;

public:

Student(char*n,char *s,int a,char *h,char *g);//构造函数

voiddisplay();//输出学生信息

friendvoid student_age(Student s);//友元函数,判断年龄大于20的学生,并输出该学生姓名

};

Student::Student(char *n,char *s,int a,char*h,char *g)

{

name=newchar[strlen(n)+1];

strcpy(name,n);

sex=newchar[strlen(s)+1];

strcpy(sex,s);

age=a;

home=newchar[strlen(h)+1];

strcpy(home,h);

grade=newchar[strlen(g)+1];

strcpy(grade,g);

}

void Student::display()

{

cout<<"姓名:"<<name<<endl;

cout<<"性别:"<<sex<<endl;

cout<<"年龄:"<<age<<endl;

cout<<"籍贯:"<<home<<endl;

cout<<"班级:"<<grade<<endl;

}

void student_age(Student s)

{

if(s.age>20)

     s.display();

else

     cout<<"学生"<<s.name<<"年龄小于20"<<endl;

}

void main()

{

Students1("李四","男",30,"保定市","软件13班");

Students2("小红","女",15,"石家庄","软件8班");

Students3("张明","男",21,"北京","软件7班");

student_age(s1);

   student_age(s2);

   student_age(s3);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值