1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
// common.h
// DO NOT COPY/PASTE ME INTO counts.cpp, BECAUSE
// I AM ALREADY #included THERE ON LINE 5. JUST BE
// SURE A COPY OF ME IS IN YOUR WORKING DIRECTORY.
//
// Use the constants, symbols and functions herein to
// insure that your printed results exactly match our
// solution's results. Do not print by any other means.
//
// Do not change this file, and especially do not rely on any
// changes to it. You will not turn this file in.
#ifndef COMMON_H
#define COMMON_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
// global constants: number of different characters, and
// first and last printable characters
const int NUM = 128;
const int FIRST = '!';
const int LAST = '~';
// symbols for special characters, corresponding to codes 0 through FIRST-1
const char symbols[][4] = {"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK",
"BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1",
"DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC",
"FS", "GS", "RS", "US", "SPC" };
// symbol for DEL character, code LAST+1 (same as NUM-1)
const char symbolDel[4] = "DEL";
// the following four functions must be used to print results
// use prHeader at the start to print header row (titles)
void prHeader(std::ostream& out) {
out << "Code\tChar\tCount\n----\t----\t-----\n";
}
// use prCountStr to print count for one of the special symbols
void prCountStr(std::ostream& out, int code, const char str[], int count) {
out << std::setw(3) << code << '\t' << str << '\t'
<< std::setw(5) << count << std::endl;
}
// use prCountChr to print count for one of the printable characters
void prCountChr(std::ostream& out, int code, const char chr, int count) {
out << std::setw(3) << code << '\t' << chr << '\t'
<< std::setw(5) << count << std::endl;
}
// use prTotal at the end to print total character count
void prTotal(std::ostream& out, int count) {
out << "\t\t-----\nTotal\t\t"
<< std::setw(5) << count << std::endl;
}
// use the following three functions for part 2 error messages
// ignore these functions for part 1
// use badFile(name) to exit if a file (name) cannot be opened
void badFile(char name[]) {
std::cout << "bad file: " << name << std::endl;
std::exit(1);
}
// use badOption(op) if an invalid option (not '-o') is on command line
void badOption(char op[]) {
std::cout << "bad option: " << op << std::endl;
std::exit(2);
}
// use missing() if output filename is missing
void missing() {
std::cout << "missing output file\n";
std::exit(3);
}
#endif
|