SDUST C++第一次实验,关于构造函数和简单应用

Problem1:给一个字符串,将其转化成相应的整数(进制天哪,我的C啊!!功底暴漏无遗)

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

class Integer
{
public:
    Integer(int a)
    {
        x_ = a;
    }
    Integer(char *s, int m) //其实主要是纠结在了进制的转换。。
    {
        x_ = 0;
        int num ;
        if(s[0] >= '0' && s[0] <= '9')
                num = s[0] - '0';
            else if(s[0] >= 'A' && s[0] <= 'Z')
                num = s[0] - 'A' + 35;
            else if(s[0] >= 'a' && s[0] <= 'z')
                num = s[0] - 'a' + 10;
            x_ = num;
        for(int i = 1; s[i] != '\0'; i++)
        {
            if(s[i] >= '0' && s[i] <= '9')
                num = s[i] - '0';
            else if(s[i] >= 'A' && s[i] <= 'Z')
                num = s[i] - 'A' + 35;
            else if(s[i] >= 'a' && s[i] <= 'z')
                num = s[i] - 'a' + 10;
            x_ = x_ * m + num;
        }
    }
    int getValue()
    {
        return x_;
    }
private:
    int x_;
};

int main()
{
    char str[100];
    int numOfData, numOfStr;
    int data, i, radix;

    cin>>numOfData;
    for (i = 0; i < numOfData; i++){
        cin>>data;
        Integer anInteger(data);
        cout<<anInteger.getValue()<<endl;
    }

    cin>>numOfStr;
    for (i = 0; i < numOfStr; i++){
        cin>>str>>radix;
        Integer anInteger(str,radix);
        cout<<anInteger.getValue()<<endl;
    }
    return 0;
}

Problem2: 给出一个函数求其解(对于输出格式的控制)

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

class Equation
{
public:
    Equation(double a, double b, double c)
    {
        a_ = a;
        b_ = b;
        c_ = c;
    }
    void solve()
    {
        x = (-b_ + sqrt(b_*b_ - 4*a_*c_)) / 2 / a_;
        y = (-b_ - sqrt(b_*b_ - 4*a_*c_)) / 2 / a_;
    }
    void printRoot()
    {
        if(x >= y)
            cout << fixed << setprecision(2) << x << " " << setprecision(2) << y;
        else
            cout << fixed  << setprecision(2) << y << " " << setprecision(2) << x;
    }
private:
    double a_, b_, c_, x, y;

};




int main()
{
    double a, b, c;
    while (cin>>a>>b>>c)
    {
        Equation equ(a,b,c);
        equ.solve();
        equ.printRoot();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值