CMSC 202

Publish in

Documents

4 views

Please download to get full document.

View again

of 25
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Share
Description
CMSC 202. Computer Science II for Majors. Topics. C++ stream input / output Formatted Output Project 1 description Project 1 posted on web. Stream I/O in C++. Stream is a sequence of bytes C++ introduces objects for I/O cout which replaces ‘printf’ cin which replaces ‘scanf’
Transcript
CMSC 202Computer Science II for MajorsTopics
  • C++ stream input / output
  • Formatted Output
  • Project 1 description
  • Project 1 posted on web
  • Stream I/O in C++
  • Stream is a sequence of bytes
  • C++ introduces objects for I/O
  • cout which replaces ‘printf’
  • cin which replaces ‘scanf’
  • cerr : unbuffered standard error output
  • clog : buffered standard error output
  • Include iostream header file
  • Stream I/O in C++ …cont
  • cout
  • Denotes the output stream (usually console)
  • Uses insertion operator << to output values to stream
  • cout << “C++ is better than C”;
  • Stream I/O in C++ …cont
  • cout
  • Screen<<“C++”coutObjectInsertion OperatorVariableStream I/O in C++ …cont
  • cin
  • Denotes input stream (usually keyboard)
  • Uses extraction operator >> to input values from stream
  • Skips white spaces
  • cin >> number1;
  • Stream I/O in C++ …cont
  • cin
  • Extraction OperatorObjectVariable>>cin45Key boardExample
  • # include <iostream>
  • using std :: cout;
  • using std :: cin;
  • using std :: endl;
  • int main()
  • {
  • float num1, num2, avg;
  • cout << “Enter two numbers” ;
  • cin >> num1;
  • cin >> num2;
  • avg = (num1+num2) / 2;
  • cout << “Average = “ << avg << endl;
  • return 0;
  • }
  • endl is a Manipulator, causes linefeed to be inserted. Just like “/n”
  • Stream I/O operations
  • Streams are used for File I/O operations
  • Common file operations
  • Open file
  • Process file
  • Close file
  • Include fstream header file
  • Stream I/O operations … cont
  • Streams are typed by operations to be performed on them
  • ifstream denotes a file used for input
  • ofstream denotes a file used for output
  • fstream denotes a file used for both
  • File I/O used << and >> operators – similar to cin and cout
  • Stream I/O operations … cont
  • Creating streams
  • Declare variable for each stream
  • ifstream Inputfile;
  • ofstream Outputfile;
  • Associate stream with a file
  • Inputfile.open (“file1”);
  • Outputfile.open (“file2”);
  • Also possible in single step
  • ifstream Inputfile (“file1”);
  • ofstream Outputfile (“file2”);
  • Stream I/O operations … cont#include <fstream>using std::ifstream;using std::ofstream;int main(){ ifstream InputFile ("input.txt"); // input stream ofstream OutputFile ("output.txt");// output stream int value = 0; while(InputFile >> value) // similar to cin >> value { OutputFile << "Value =" << value <<endl; } InputFile.close(); OutputFile.close();return 0;}Formatted Output
  • Setting field width
  • Member Function width
  • cin.width(5); // input only 5 characters
  • cout.width(5); // sets # of character positions // in which value should be output
  • Manipulator setw
  • cin >> setw(5);
  • cout << setw(5) << ... ;
  • Formatted Output … cont
  • Setting precision
  • Manipulators
  • double pi = 3.14159 cout << setprecision(3) <<pi; // 3.142
  • Member Function
  • double pi = 3.14159;cout.precision(3);cout << pi; // 3.142Formatted Output … contFormatted Output … cont
  • Example
  • #include<iostream>#include<iomanip>using namespace std;int main(){ float a,b,c; a = 9; b = 99.099; c = 9.901; cout << "Before formatting:" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl << endl; cout << "After formatting:" << endl; cout << fixed; cout << setprecision(2); cout << "a = " << setw(7) << a << endl; cout << "b = " << setw(7) << b << endl; cout << "c = " << setw(7) << c << endl; return 0;}Formatted Output … cont
  • Example
  • Before formatting:a = 9b = 99.099c = 9.901After formatting:a = 9.00b = 99.10c = 9.90Formatted Output … cont
  • Justification
  • Uses stream manipulators left and right
  • left : left-justify, padding characters on right
  • right: right-justify, padding characters on left
  • cout << left << setw(10) << x ;
  • cout << right << setw(10) << x ;
  • Project 1 description
  • Objective : To use string and vector and to experience OOP as user of objects
  • To use C++ stream operators
  • Input : Text file
  • Command line argument
  • Assume all words are lowercase
  • Project 1 description … cont
  • Output : an analysis of text in file
  • Print File name
  • Total number of words in file
  • Total number of distinct words
  • For each alphabet, print # of words, # of distinct words and # of occurrences
  • Most frequently occurring word
  • Project 1 description … cont
  • Output … cont
  • 6. Longest Word and # of characters in it
  • Letter that begins most number of words
  • Letter that begins most distinct words and # of distinct words with that letter
  • Letters that do not begin any words
  • Project 1 description … cont
  • Sample Input (taken from Project description)
  • a very angry and pesky antelope ate all of my angelhair pasta i really really really dislike a very pesky antelope Project 1 description … cont
  • Sample Output (taken from Project description)
  • linux3[33]% Proj1 p1.dat Processing file "p1.dat" The file contains 21 words. The file contains 15 distinct words. Words that begin with 'a': a:2 angry:1 and:1 antelope:2 ate:1 all:1 angelhair:1 Total words: 9, Distinct words: 7 Words that begin with 'd': dislike:1 Total words: 1, Distinct words: 1 Project 1 description … cont
  • Sample Output …cont
  • Words that begin with 'i': i:1 Total words: 1, Distinct words: 1Words that begin with 'm': my:1 Total words: 1, Distinct words: 1 Words that begin with 'o': of:1 Total words: 1, Distinct words: 1 Words that begin with 'p': pesky:2 pasta:1 Total words: 3, Distinct words: 2 Project 1 description … cont
  • Sample Output …cont
  • Words that begin with 'r': really:3 Total words: 3, Distinct words: 1 Words that begin with 'v': very:2 Total words: 2, Distinct words: 1 The most frequent word is "really" which occurred 3 times. The longest word is "angelhair" which contains 9 characters. The letter 'a' had the most words - 9 The letter 'a' had the most distinct words - 7 No words begin with the letters b, c, e, f, g, h, j, k, l, n, q, s, t, u, w, x, y, z
    Related Documents
    Eng 202 Tech Def
    Sep 20, 2017

    Eng 202 Tech Def

    CMSC 203
    Sep 21, 2017

    CMSC 203

    p193-202
    Sep 21, 2017

    p193-202

    10.1.1.202.8858
    Sep 21, 2017

    10.1.1.202.8858

    202
    Sep 21, 2017

    202

    CNET_202.pdf
    Sep 23, 2017

    CNET_202.pdf

    CNET_202.pdf
    Sep 23, 2017

    CNET_202.pdf

    CMSC 202
    Mar 16, 2018

    CMSC 202

    View more...
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks