2004年北理复试上机题

历年北京理工大学复试上机题题目汇总:

http://blog.csdn.net/u014552756/article/details/78505845


1、建立一个角类,在这个类中重载减号运算符,并实现求出角度的正弦值的函数。

#include <iostream>
#include <math.h>
using namespace std;

double const pi =3.1415;

class angle
{
private:
    double x;
public:
    angle() {};
    angle(int c):x(c) {};

    angle operator-(angle a)
    {
        return angle(x-a.x);
    }
    void show()
    {
        cout<<sin(x/180*pi)<<endl;
    }
};

int main()
{
    angle a(60),b(30),c;
    a.show();
    b.show();

    c=a-b;
    c.show();

    return 0;
}

2、建 立一 个 求 一 元二 次 方 程 解的 类( a*x^2+b*x+c=0 ),输入系数 a,b,c 的值后打印出这个方程的解来,也比较简单。 需要注意的是系数 a 不能为零以及方程有无解,单解还是双解的情况。

#include <iostream>
#include<math.h>
using namespace std;

class fun
{
private:
    double a, b, c;
public:
    fun() {};
    fun(int i,int j,int k):a(i),b(j),c(k) {};
    void assign(int i,int j,int k)
    {
        a=i,b=j,c=k;
    }
    void show()
    {
        double t;
        t=pow(b,2)-4*a*c;
        if(t<0)
            cout<<"此方程无解。"<<endl;
        if(t==0)
            cout<<"此方程有唯一解:"<<(-1*b)/(2*a)<<endl;
        if(t>0)
            cout<<"此方程有两个解,第一个解为:"<<(-1*b-sqrt(t))/(2*a)<<" 第二个解为:"<<(-1*b+sqrt(t))/(2*a)<<endl;
    }
};

int main()
{
    double a,b,c;
    fun s;
    cout<<"请输入一元二次方程组的系数,a不可以为0:"<<endl;
    cin>>a>>b>>c;
    s.assign(a,b,c);
    s.show();

    return 0;
}

3、实现一个多项式的类(a+b*x+c*x^2+d*x^3+...+),要求输入该多项式的系数和 x 的值后打印出这个多项式的值。这道题本身并不难,但他要求用好的算法(实际上就是递归)。

#include <iostream>
#include <cmath>
using namespace std;

class fun
{
private:
    int a,exp;
public:
    fun() {};
    fun(int i,int k):a(i),exp(k) {};
    void assign(int i,int k)
    {
        a=i,exp=k;
    }
    int show(int x)
    {
        return a*pow(x,exp);
    }
};

int main()
{
    int s=0,a,x,exp;
    fun h;
    cout<<"请输入x的值:"<<endl;
    cin>>x;

    cout<<"请输入系数及其幂,格式:系数幂,以99 99结束:"<<endl;
    while(cin>>a>>exp)
    {
        if(a==99&&exp==99)
            break;
        h.assign(a,exp);
        s=s+h.show(x);
    }

    cout<<"该多项式的结果为:"<<s<<endl;

    return 0;
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值