Class streamManipulator(for hw) cout的应用

题目:

Now you are required to design a class named streamManipulator.The class can receive an integer as input and output the its corresponding value in decimal, octal and hexadecimal forms.And it can also round a floating-point value to a certain precision(保留一定的位数).Finally, it can also transform a temperature(int) in Fahrenheit to a floating-point Celsis temperature.

The details are listed as followed:

(1)The class has a private member named number, which is the integer to be output in decimal, octal and hexadecimal form.

(2)a construtor receives an integer to initialize the number. 

(3) a public function setNumber receives an integer parameter to change the value of number.

(4)a public function showInDecimal outputs the decimal form of number(X).(You should output "As a decimal number X")

(5)a public function showInOctal outputs the octal form of number(X).(You should output "As an octal number X")

(6)a public function showInHexadecimal outputs the hexadecimal form of number(X).(You should output "As a hexadecimal number X")

(7)a public function printInPrecision gets two parameters, a double target and a integer precision(X).It should round the target to the precision given.(You should output "Rounded to X digit(s) is Y")

(8)a public function convertFahrenToCelsis gets a integer temperature(X) and outputs its value in Celsis(Y).(You should output "After converting:\nX===>Y",同时保留三位小数)

Before you get started to code, please read carefully the standard input and output below and the main function given.

Standard Input

12

100.343434343 5

7

Standard Output

As a decimal number 12
As an octal number 014
As a hexadecimal number 0xc
Rounded to 5 digit(s) is 100.34343
After converting:
7===>-13.889

//mian.cpp


    #include <iostream>
    #include "streamManipulator.h"
     
    using std::cout;
    using std::cin;
    int main() {
    int integer;
    cin >> integer;
    StreamManipulator s(integer);
     
    s.showInDecimal();
    s.showInOctal();
    s.showInHexadecimal();
    s.setNumber(10);
    s.showInDecimal();
    s.showInOctal();
    s.showInHexadecimal();
    double target;
    int precision;
    cin >> target >> precision;
     
    s.printInPrecision(target, precision);
    int FahrenTemp;
    cin >> FahrenTemp;
    s.convertFahrenToCelsis(FahrenTemp);
    return 0;
    }
     


//streamManipulator.h


     
    #ifndef Stream_HEAD
    #define Stream_HEAD
    class StreamManipulator {
    private:
    int number;
    public:
    explicit StreamManipulator(int n);
    ~StreamManipulator();
    void setNumber(int s);
    void showInDecimal();
    void showInOctal();
    void showInHexadecimal();
    void printInPrecision(double target, int precision);
    void convertFahrenToCelsis(int temperature);
    };
     
    #endif
     

//streamManipulator.cpp

    #include <iostream>
    #include "streamManipulator.h"
    #include <iomanip>
    using std::cout;
    using std::cin;
    using std::showbase;
    using std::dec;
    using std::hex;
    using std::oct;
    using std::endl;
    using std::setprecision;
    using std::fixed;
    StreamManipulator::StreamManipulator(int n) {
    number = n;
    }
     
    StreamManipulator::~StreamManipulator() {
    }
    void StreamManipulator::setNumber(int s) {
    number = s;
    }
    void StreamManipulator::showInDecimal() {
    cout << showbase << "As a decimal number " << dec << number << endl;
    }
     
    void StreamManipulator::showInOctal() {
    cout << showbase << "As an octal number " << oct << number << endl;
    }
     
    void StreamManipulator::showInHexadecimal() {
    cout << showbase << "As a hexadecimal number " << hex << number << endl;
    }
     
    void StreamManipulator::printInPrecision(double target, int precision) {
    cout << dec;
    cout << fixed;
    cout << setprecision(precision) << "Rounded to ";
    cout << precision << " digit(s) is ";
    cout << target << endl;
    }
     
    void StreamManipulator::convertFahrenToCelsis(int temperature) {
    cout << "After converting:\n";
    double celsis = 5.0 / 9.0 * (temperature - 32);
    cout << dec;
    cout << fixed;
    cout << temperature << "===>" << setprecision(3) << celsis << endl;
    }
     



//需要注意的是输出不同进制时,要对cout进行进制更改。
//PS: 十六进制的是“0(数字)x” 不是“O(字母)x”,解决此问题,直接用showbase即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值