Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program that reads from a file specified in the command line as an argument. The program should read from the file words

Write a C++ program that reads from a file specified in the command line as an argument. The program should read from the file words until the end of file. In case the file is empty, print out on a new line the message "File is empty." and then exit. However, if no file name is provided, the program should print on a new line "NO SPECIFIED INPUT FILE NAME.", and exit. If the file cannot be opened, print on a new line "CANNOT OPEN THE FILE ", followed by the file name, and exit. A word is defined as a sequence of one or more non-whitespace characters separated by whitespace.

The program should recognize words that represent integer numbers, fixed-point numbers and special names. An integer is defined as a word that starts by a digit, or a sign (i.e., - or +) followed by a digit, that may be followed by zero or more digits. A fixed-point number is defined as a word that starts by a digit, or an optional sign followed by a digit, and followed by zero or more digits, a decimal point (i.e., a dot) and one or more digits. For example, 0.4, 2.5, -3.75 are valid fixed-point numbers, but 5., .8, 5.6.7 are not valid fixed-point numbers. A special name is defined as a word that starts by $ and followed by a letter which may be followed by zero or more letters, digits or underscores. For example, $value, $num_234_ten, and are valid names, but $9val, and $_num are not.

The program should count and display the number of recognized integers, fixed-point numbers and special names. Then the program should display the value of each word category and the number of occurrences of that word. The program should print out the results according to the format and order given by the example below, as the list of integers first, followed by the list of reals second, then the list of special names third.

My Code:

#include #include #include #include #include

using namespace std;

int main(int argc, char *argv[]) { map ints, reals, names;

// Check if file name was specified if (argc != 2) { cout

// Open the file ifstream file(argv[1]); if (!file.is_open()) { cout

// Read from file and store words in respective map string word; while (file >> word) { if (regex_match(word, regex("^[+-]?[0-9]+$"))) ints[word]++; else if (regex_match(word, regex("^[+-]?[0-9]+(\\.[0-9]+)?$"))) reals[word]++; else if (regex_match(word, regex("^\\$[a-zA-Z]+[a-zA-Z0-9_]*$"))) names[word]++; }

// Check if file was empty if (ints.empty() && reals.empty() && names.empty()) { cout

// Display results cout

cout

cout

return 0; }

For example The output should look like this for infile 5

image text in transcribed

//infile3

image text in transcribed

//infile4

image text in transcribed

//infile5

image text in transcribed

//infile6

image text in transcribed

12345678910111213141516171819Numberofintegersinthefile:12Numberofrealsinthefile:2Numberofspecialnamesinthefile:0Listofintegervaluesandtheirnumberofoccurrences:125:145:3100:1125:1278:11025:11234:13456:24321:1Listofrealvaluesandtheirnumberofoccurrences:0.45:12.375:1 2.525.60+16.255.0.70.22.512.6.716.2525.62.52.525.6.52.5 3456+1025123445=???4321>125

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions

Question

Why do we need scientific methods?

Answered: 1 week ago

Question

5. Describe the relationship between history and identity.

Answered: 1 week ago