Question

In: Computer Science

C++ - De Morgan’s laws commonly apply to text searching using Boolean operators AND, OR, and...

C++ -

De Morgan’s laws commonly apply to text searching using Boolean operators AND, OR, and NOT. Consider a set of documents containing the words “cars” and “trucks”. De Morgan’s laws hold that these two searches will return the same set of documents:

Search A: NOT (cars OR trucks)

Search B: (NOT cars) AND (NOT trucks)

The corpus of documents containing “cars” or “trucks” can be represented by four documents:

Document 1: Contains only the word “cars”.

Document 2: Contains only “trucks”.

Document 3: Contains both “cars” and “trucks”.

Document 4: Contains neither “cars” nor “trucks”.

To evaluate Search A, clearly the search “(cars OR trucks)” will hit on Documents 1, 2, and 3. So the negation of that search (which is Search A) will hit everything else, which is Document 4.

Evaluating Search B, the search “(NOT cars)” will hit on documents that do not contain “cars”, which is Documents 2 and 4. Similarly the search “(NOT trucks)” will hit on Documents 1 and 4. Applying the AND operator to these two searches (which is Search B) will hit on the documents that are common to these two searches, which is Document 4.

Please write a C++ program to De Morgan's Law via document search.

You can use four text files as document 1, 2 3 and 4.

Using this Assignment, read a document and create frequency map. Take the frequency map and display histogram for each word in the document.

Draw histogram in horizontal direction.

This (6) **********

my (4) ******

Truck (6) *********

Solutions

Expert Solution

Sol:

This is the program for De Morgan's law with given conditions

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Searches in the file for word Car and return true if its there else returns false
bool hasCar(string fileName) {
//opening file to check
   ifstream file( fileName+".txt");
   string line;
//initially setting result false, changes only when word is found
   bool result = false;
   //going through file line by line
   while (getline(file, line)) {
       //if cars i written then set result to true and stop loop
       if (line == "cars") {
           result = true;
           break;
       }
   }
  
   return result;
}

//Searches in the file for word Truck and return true if its there else returns false
bool hasTruck(string fileName) {
   //opening file to check
   ifstream file( fileName+".txt");
   string line;
//initially setting result false, changes only when word is found
   bool result = false;
   //going through file line by line
   while (getline(file, line)) {
       //if trucks is written then set result to true and stop loop
       if (line == "trucks") {
           result = true;
           break;
       }
   }
   //cout << result<<"\n";
   return result;
}

int main()
{
   cout << "Search A: NOT (cars OR trucks)\n";
   //iterating through every file
   for (int i = 1;i <= 4;i++) {
      
       //dynanamically creating file name
       string fileName = "Doc" + to_string(i);
      
       //use these to know condition foe every file
       //cout << "for " << fileName<<"\n";
       //cout << "if (!(" << hasCar(fileName) << " || " << hasTruck(fileName) << "))\n";
      
       //writing search A comdition as it is
       if (!(hasCar(fileName) || hasTruck(fileName))) {
           cout << "Document " << i << "\n";
       }
   }

   cout << "Search B: (NOT cars) AND (NOT trucks)\n";
   //iterating through every file
   for (int i = 1;i <= 4;i++) {
      
       //dynanamically creating file name
       string fileName = "Doc" + to_string(i);
      
       //use these to know condition foe every file
       //cout << "for " << fileName<<"\n";
       //cout << "if (!" << hasCar(fileName) << " && !" << hasTruck(fileName) << "))\n";
      
       //writing search B condition as it is
       if (!hasCar(fileName) && !hasTruck(fileName)) {
           cout << "Document " << i << "\n";
       }
   }

   return 0;
}

----------------------------

The output is:


Related Solutions

USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub...
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub language to be as follows (NOTE: 5 has highest precedence and 0 lowest): 5 *, /, % 4 +, - 3 <, <=, >, >=, !=, == 2 not 1 and 0 or 1.  Infix-Postfix Conversion and Evaluation with Logical and Relational operators – Convert the following infix expression to a postfix expression and evaluate the result (assume that true=1 and false=0). Provide both the...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
3. Simplify the following expressions using the properties of boolean algebra : 3A)    S(A,B,C) = A'B'C...
3. Simplify the following expressions using the properties of boolean algebra : 3A)    S(A,B,C) = A'B'C + A'BC + AB'C + ABC 3B) F(A,B,C) = A'B'C' + A'B'C + AB'C' + AB'C + ABC' + ABC
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
Using newtons gravitation laws, design a program in C++ that determine the position of n=3 number...
Using newtons gravitation laws, design a program in C++ that determine the position of n=3 number of bodies relative to each other, and outputs the results in a ongoing table that constantly updates as long as the simulation runs, and a pgPlot. Implement the newtonian equations using the LEAPFROG Method. Show how the you solved the equations. Make sure to describe each section of your code to describe what each part of your code is responsible for (i.e use //)...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Using C language: (Note: If a family of 4, apply family of 3 cost plus the...
Using C language: (Note: If a family of 4, apply family of 3 cost plus the cost of 1 participant. K800 is the fixed cost for a family of 3.) A bus company organizes a tour. The tour is open for 4 days and participants can register for the number of days they would like to participate in the tour. Each participant would pay K 300 for the tour per day, a family of 3 would pay K 800 per...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Write a short text based game using C++ were you can chose the gender of the...
Write a short text based game using C++ were you can chose the gender of the character, than choose between two location, desert or forest. If either location is chosen than you can choose to either stay at your spot or travel to find help. If player chose desert, than choosing either to stay or travel can be led to death by heat exhaustion or saved randomized, or if forest is chosen, than it can be either death of starvation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT