13 stream Input/Output -1

很久没写了,今天是4月13号,难得有时间安静的坐下来写写,挺开心的~

目录:

  1. Using Member Functions eof, get and put

  2. Comparing cin and cin.get

  3. Using Member Function getline

  4. Unformatted I/O Using read, write and gcount

  5. Using stream manipulators hex, oct, dec and setbase.

  6. Controlling precision of floating-point values

  7. Creating and testing user-defined, nonparameterized stream manipulators

  8. Stream manipulator showpoint

  9. Justification (left, right and internal)

  10. padding

  11. scientific format

  12. uppercase

  13. Setting and Resetting the Format State via Member Function flags

  14. restore formatting

  15. stream error state

 


1) Using Member Functions eof, get and put

#include <iostream>
using namespace std;

int main() {
    int character;

    cout << "Before input, cin.eof():" << cin.eof() <<
    "\nEnter a sentence followed by Enter and end-of-file:\n";

    while ((character = cin.get()) != EOF) {
        cout.put(character);
    }

    cout << "\nEOF in this system is :" << character << "\nAfter input of EOP, cin.eof() is" << cin.eof() << endl;
}

mac 的 clion的退出是 command+d 好像


2) Comparing cin and cin.get
#include <iostream>
using namespace std;

//Comparing cin and cin.get
int main(){
    const int SIZE{80};
    char buffer1[SIZE];
    char buffer2[SIZE];

    cout << "Enter a sentence:\n";
    cin >> buffer1; // cin 读取字符直到遇到空白字符(white-space character)就中断了
    cout << "\nThe string read with cin was :\n" << buffer1 << "\n\n";


    cin.get(buffer2, SIZE);  // gin.get() 正常需要3个参数,built-in array of chars, size limit, delimiter(这里没写、默认是"\n")
    cout << "The string read with cin.get was:\n" << buffer2 << endl;
    

}


3) Using Member Function getline

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

//Using Member Function getline
int main(){
    const int SIZE{80};
    char buffer[SIZE];

    cout << "Enter a sentence:\n";
    cin.getline(buffer, SIZE);

    cout << "\nThe sentence entered is :\n" << buffer << endl;

}


 

4) Unformatted I/O Using read, write and gcount

//13.5 Unformatted I/O Using read, write and gcount
int  main(){
    const int SIZE{80};
    char buffer[SIZE];

    cout << "Enter a sentence:\n";
    cin.read(buffer, 20);

    cout << "\nThe sentence entered was:\n";
    cout.write(buffer, cin.gcount());
    cout << endl;

}


 

5)Using stream manipulators hex, oct, dec and setbase.

//Using stream manipulators hex, oct, dec and setbase.
int main() {
    int number;
    cout << "Enter a decimal number:";
    cin >> number;

    cout << number << "in hexadecimal is :" << hex << number << "\n"; // 16进制
    cout << dec << number << "in octal is :" << oct << number << "\n"; // 8进制

    cout << setbase(10) << number << "in decimal is :" << number << endl;

}

 


6) Controlling precision of floating-point values.

 

// Controlling precision of floating-point values.
// the setprecision stream manipulator or the precision
//member function of ostream.

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

int main(){
    double root2{sqrt(2.0)};
    cout << "Square root of 2 with precisions 0-9.\n" << "Precision set by ostream member function precision:\n";
    cout << fixed;

    for (int places{0}; places <= 9; ++places){
        cout.precision(places);
        cout << root2 << "\n";
    }

    cout << "\nPrecision set by stream maniplulator setprecision:\n";
    for (int places{0}; places <= 9; ++places) {
        cout << setprecision(places) << root2 << "\n";
    }
    
}

 

7) Creating and testing user-defined, nonparameterized stream manipulators


// Creating and testing user-defined, nonparameterized stream manipulators.
#include <iostream>
using namespace std;

ostream& bell(ostream& output){
    return output << "\a";
}

ostream& carriageReturn(ostream& output){
    return output << "\r";
}

ostream& tab(ostream& output){
    return output << "\t";
}

ostream& endLine(ostream& output){
    return output << "\n" << flush;
}

int main(){
    cout << "Testing the tab manipulator:"<< endLine << "a" << tab << "b" << tab << "c" << endLine;

    //cout << "Testing the carriageReturn and bell manipulator:" << endLine << "........";


}


 

8) Stream manipulator showpoint

#include <iostream>
using namespace  std;

int main(){
    cout << "Before using showpoint"
    << "\n9.9900 prints as:" << 9.9900
    << "\n9.9000 prints as:" << 9.9000
    << "\n9.0000 prints as:" << 9.0000;

    cout << showpoint
            << "\n\nAfter using showpoint"
            << "\n9.9900 prints as:" << 9.9900
            << "\n9.9000 prints as:" << 9.9000
            << "\n9.0000 prints as:" << 9.0000;
}


 

9) Justification (left, right and internal)


//13.7.2 Justification (left, right and internal)
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int x{12345};

    cout << "Default is right justified:\n\"" << setw(10) << x << "\"";

    cout << "\n\nUse left to left justfify x:\n\""
    << left << setw(10) << x << "\"";  // 东西在左边就行

    cout << "\n\nUse right to right justify x:\n\""
    << right << setw(10) << x << "\""; // 东西在右边就ok
}


10) padding

// using member funciton fill and stream manipulator setfill to change
// the padding character for fields larger than the printed value

#include <iostream>
#include <iomanip>

using namespace std;

int main() {

    int x{10000};
    cout << x << "printed as int right and left justified\n" <<
         "and as hex with internal justifications.\n" <<
         "Using the default pad character(space):\n";

    //display x
    cout << setw(10) << x << "\n";

    // display x with left justification
    cout << left << setw(10) << x << "\n";

    // display x with base as hex with internal justification
    cout << showbase << internal << setw(10) << hex << x << "\n\n";

    // display x using padded (right justification)
    cout << "using various padding chars" << endl;
    cout << right;
    cout.fill('*');
    cout << setw(10) << dec << x << "\n";

    // display x using padded chars (left justification)
    cout << left << setw(10) << setfill('%') << x << "\n";

    // display x using padded chars (internal justification)
    cout << internal << setw(10) << setfill('^') << hex << x << endl;


}




11)scientific format


// Floating-point values displayed in system default
// scientific and fixed formats.


#include <iostream>
using namespace  std;


int main() {
    double x{0.001234567};
    double y{1.946e9};

    cout << "Displayed in default format:\n" << x << "\t" << y;

    cout << "\n\nDisplayed in scientific format:\n" << scientific << x << "\t" << y;

    cout << "\n\nDisplayed in fixed format:\n" << fixed << x << "\t" << y << endl;

}




12)uppercase

// Stream manipulator uppercase
#include <iostream>
using namespace  std;

int main() {
    cout << "Printing uppercase letters in scientific\n" <<
    "notation exponents and hexadecimal values:\n";

    cout << uppercase << 4.345e10 << "\n"
    << hex << showbase << 123456789 << endl;

}

 


14) restore formatting

// flag member function , restore format

#include <iostream>
using namespace std;

int main() {
    int integerValue{100};
    double doubleValue{0.0947628};

    cout << "The value of the flags variable is :" << cout.flags()
    << "\nPrint int and double in original format:\n" << integerValue << "\t" << doubleValue;

    // use cout flags function to save original format
    ios_base::fmtflags originalFormat{cout.flags()};
    cout << showbase << oct << scientific; // 8进制

    // display flags value, int and double values(new format)
    cout << "\n\nThe value of the flags variables is :" << cout.flags()
    << "\nPrint int and double in a new format:\n"
    << integerValue << "\t" << doubleValue;

    //restore format
    cout.flags(originalFormat);
    //  display flags value, int and double values(old format)
    cout << "\n\nThe value of the flags variables is :" << cout.flags()
         << "\nPrint int and double in a original format:\n"
         << integerValue << "\t" << doubleValue;

}


15) stream error state

//stream error states
#include <iostream>
using namespace std;

int main() {
    int integerValue;

    cout << "Before a bad input operation:"
    << "\ncin.rdstate():" << cin.rdstate()
    << "\n cin.eof():" << cin.eof()
    << "\n cin.fail():" << cin.fail()
    << "\n cin.bad():" << cin.bad()
    << "\n cin.good():" << cin.good()
    << "\n\n Expects an integer, but enter a character: ";

    cin >> integerValue;  // 这里故意输入一个字符串

    cout << "After a bad input operation:"
         << "\ncin.rdstate():" << cin.rdstate()
         << "\n cin.eof():" << cin.eof()
         << "\n cin.fail():" << cin.fail()
         << "\n cin.bad():" << cin.bad()
         << "\n cin.good():" << cin.good()
         << "\n\n Expects an integer, but enter a character: ";

    cin.clear();

}

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值