In: Computer Science
Note: The built-in character analysis functions
(isdigit(), islower(),
etc.) may not be used.
Exercise 6.1 Write to an output file
Open an output file (stream) and write your identifying information
to the
associated output file using a function. Put your standard output
informa-
tion in a function named ShowHeader(). The file stream must be
passed by
reference to the function. To do this, an & (ampersand) is used
between the
data type (ofstream) and the name of the argument (I like to use
fOut for
output file stream objects and fIn for input file stream object
names).
Exercise 6.2 Read from an input file
Read/Process an input file a character at a time. Open an input
file (stream)
and read a single character on each pass through a loop (a while()
loop is
probably easiest) until the end of file is reached. After reading
the character,
write it to the output file created earlier. The output should
contain the
same characters as the input file.
Exercise 6.3 Character Analysis
Suggestion: Develop the remainder of this program in piecewise
manner–
specifically, add one character analysis operation at a time,
before adding
the next.
Analyze the characters in a file using functions to display their
charac-
teristics, e.g., lower case, digit etc. The logic for analysis will
be inside the
loop above.
Specifically, test if the character:
• is a letter.
• test if it is lower case or upper case.
• if the character is a letter, test if it is a vowel or
consonant.
• is a digit.
• if the character is a digit, test if the value is odd or
even.
• is a punctuation character.
• is a logical operator symbol.
Write a short function for most operations in the list above. If a
character
is a letter, then it is either upper or lower case, so we only need
to write one
function to test the case of the character. Do not use the built-in
functions.
Note: there are no I/O (input/output) operations in
the functions, only a
single character is examined.
Exercise 6.1
Answer :
#include <iostream>
#include <fstream>
using namespace std;
void ShowHeader(ifstream& fileOut){
int data;
while (fileOut >> data) {
fileOut << data
<< endl;
}
}
int main()
{
ofstream fOut;
char *fileOut = fOut.open("output data
file");
ShowHeader(fileOut);
fOut.close();
}
Exercise 6.2
Answer :
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fIn;
ofstream fOut;
fIn.open("input data file");
fOut.open("output data file");
int data;
while (fIn >> data) {
fOut << data
<< endl;
}
fIn.close();
fOut.close();
}
Excercise 6.3
#include <iostream>
#include <fstream>
using namespace std;
void calculateAverage(ifstream& inp, ifstream& iop)
{
int data;
while (inp >> data)
{
if (isalpha(inp >>
data)){
iop << data <<"is Letter" << endl;
}
if (tolower(inp >>
data)){
iop << data <<"is LowerCase" << endl;
}
if (toupper(inp >>
data)){
iop << data <<"is Upper Case" << endl;
}
if(inp >>
data=='a' || inp >> data=='e' || inp >> data=='i'
||
inp
>> data=='o' || inp >> data=='u' || inp >>
data=='A' ||
inp
>> data=='E' || inp >> data=='I' || inp >>
data=='O' ||
inp
>> data=='U'){
iop
<< data <<"is Vowel" << endl;
}
else
if((inp << data>='a'&& inp << data<='z')
|| (inp << data>='A'&& inp <<
data<='Z'))
{
iop << data <<" is Consonants" << endl;
}
if(inp <<
data >='0' && inp << data <='9')
{
iop << data <<"is Digit" << endl;
if( inp >> data % 2 == 0){
iop << data <<" and is Even" << endl;
}
else{
iop << data <<" and is Odd" << endl;
}
}
if (ispunct(inp <<
data)){
iop << data
<<" is punctuation character" << endl;
}
if(inp << data
>='&&' || inp << data <='||' || inp <<
data <='!'){
iop << data <<" logical operator symbol " <<
endl;
}
}
}
int main()
{
ofstream fIn;
ofstream fOut;
char *fIn = fOut.open("output data
file");
char *fOut = fOut.open("output data
file");
ShowHeader(fIn, fOut);
fOut.close();
}