Question

1. Define Structure UNIT as described in the list: ınit char codease complete the question int numberOfCreditHours int semest
pluginfile.php/94428/mod resource/content/1/lab 6202 %20 %28file %20and%20parsing % 29.pdf Expected C++Skills to finish this
Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours Unit semester of study L
1. Define Structure UNIT as described in the list: ınit char codease complete the question int numberOfCreditHours int semester int numberOfPreReq; pointer to number of pre-requisites eg:like char preRequisite[numberOfPreReq][10]; char* preReq int numberOfPostReq; pointer to number of post-requisites eg:like char postRequisite[numberOfPostReq] [1 0]; char* postReq NOTE: this is a BIG question and has to be asked seperately, answering only one question here.
pluginfile.php/94428/mod resource/content/1/lab 6202 %20 %28file %20and%20parsing % 29.pdf Expected C++Skills to finish this activity Simple data types C++ structs C++ string class Conversion of string to values C++ arrays C++ loops C++ branching C++ in/output Background The skills that you use in this lab, will help you in accomplishing your final project. So, it is very useful if you give it enough attention and time. In the previous lab you learned how to use C++ struct to define and work with complex data structures. In fact structs are the first step to the world of object oriented programming, wherein everything is treated as a standalone object with a mumber of attributes (like the ones you create for sstruct) and routines (which is missing from the struct) In this lab, you will read information from a file line by line and then parse the strings that contain multiple information relating to a unit including the unit code, credit hours, semester of the unit, a list of prerequisites, and a list of post-requisite units. You need to extract all this information and then store it inside a struct. For this activity you need to get comfortable with some of the useful string data type functions. Therefore, I would suggest you review the string class in C++ and some of its useful functions from the following linke http://www.cplusplus.com/reference/string/string/Pkw-string You also need to learn to work with files, you can find a link quick review of file operation in the following http://www.cplusplus.com/doc/tutorial/files/ 1/2
Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours Unit semester of study List of prerequisites Number of prerequisites List of post requisites Number of post requisites Inside your main function declare an array of type Uni t called units with size 20 elements. This array will contain the information related to all the units in our diploma program. Each of the elements of this array correspond with one our diploma units. IL III Inside your main function define a string named sample. This string will be used to read information from a file and then parse the information into the elements of the array units []. IV. Create and ifstream object, and then open the input file "diloma-flowchart.txt" Read each line of the input file into the string sample. The organization of each line is given as the example below V. "15FELE120; 3;2; 15FELE111; 15FELE210,15FELE 214;" This string contains the following information: 15FELE120: it is the unit code followed by a; (semicolon) to indicate the end of the unit code 3: mumber of credit hours followed by the ; (semicolon) to indicate the end of credit hours 2: semester of study followed by the; (semicolon) to indicate the end of semester of study 15FELE111: prerequisite followed by; to indicate end of prerequisites, if there were more than 1 prerequisite, then the values would be separate by a, (comma) 15FELE210,15FELE214: list of post-requisites separated with, (comma) then followed by; to indicate the end of the list VI Write a program to extract the above information from the sample string and then fill out the corresponding fields inside the fields of the array units [] In the end, write all the information contained inside the array units [], on the output
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>

using namespace std;

struct Unit
{
string code;
int numberOfCreditHours;
int semester;
int numberOfPreReq;
vector<string> prerequisties;
int numberOfPostReq;
vector<string> postrequisties;
};

vector<string> split (const string &s, char delim) {
vector<string> result;
stringstream ss (s);
string item;

while (getline (ss, item, delim)) {
result.push_back (item);
}

return result;
}

int main() {

vector<string> v;
vector<string> requesties;
Unit units[20];
int credithours;
int semester;
int line = 0;
string req;

ifstream file("thefile.txt");

// Processing the data in the text file to the vector of String.

// Opening the file

if (file.is_open()) {
  
string sample;
while (getline(file, sample)) {
  
v = split (sample, ';');
units[line].code = v[0];
units[line].numberOfCreditHours = stoi(v[1]);
units[line].semester = stoi(v[2]);
req = v[3];
requesties = split(req,',');
units[line].numberOfPreReq = requesties.size();
units[line].prerequisties = requesties;
req = v[4];
requesties = split(req,',');
units[line].numberOfPostReq = requesties.size();
units[line].postrequisties = requesties;

line++;
  

}

}

// Closing the file

file.close();

//Printing them into the console

for(int i=0; i<line; i++){

cout<<"Details of unit "<<i+1<<endl;
cout<<"Unit code is "<<units[i].code<<endl;
cout<<"Number of credit Hours of unit is "<<units[i].numberOfCreditHours<<endl;
cout<<"Number of Semester of unit is "<<units[i].semester<<endl;
cout<<"Number of preRequities of unit is "<<units[i].numberOfPreReq<<endl;
cout<<"Prerequesties are "<<endl;
for(int j=0; j<units[i].prerequisties.size(); i++)
cout<<units[i].prerequisties[j]<<endl;

cout<<"Number of postRequities of unit is "<<units[i].numberOfPostReq<<endl;
cout<<"Postrequesties are "<<endl;
for(int j=0; j<units[i].postrequisties.size(); i++)
cout<<units[i].postrequisties[j]<<endl;


}


return 0;
}

If you left with any doubts..feel free to ask.

Add a comment
Know the answer?
Add Answer to:
1. Define Structure UNIT as described in the list: ınit char codease complete the question int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours...

    Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours Unit semester of study List of prerequisites Number of prerequisites List of post requisites Number of post requisites Inside your main function declare an array of type Unit called units with size 20 elements. This array will contain the information related to all the units in our diploma program. Each of the elements of this array correspond with one our diploma units. II III...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

  • Hello! I'm posting this program that is partially completed if someone can help me out, I...

    Hello! I'm posting this program that is partially completed if someone can help me out, I will give you a good rating! Thanks, // You are given a partially completed program that creates a list of employees, like employees' record. // Each record has this information: employee's name, supervisors's name, department of the employee, room number. // The struct 'employeeRecord' holds information of one employee. Department is enum type. // An array of structs called 'list' is made to hold...

  • DO NOT use #include <cstdlib> , iterate, and list. USE pointers and arrays. 1.Define a class...

    DO NOT use #include <cstdlib> , iterate, and list. USE pointers and arrays. 1.Define a class named Family in a header file named: family.h (5 points) This file acts as a prototype. 2.Create the implementation file family.cpp (15 points total) a.The class will have the following members 1.Family name (5 points) 2. Parent (array of 2) (5 points) a.First Name b.Last Name c. Age (you must validate the age is the proper data type. You can use the code from...

  • C++ program that reads students' names followed by their test scores (5 scores) from the given...

    C++ program that reads students' names followed by their test scores (5 scores) from the given input file, students.txt. The program should output to a file, output.txt, each student's first name, last name, followed by the test scores and the relevant grade. All data should be separated by a single space. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, an array of testScores of type int...

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

  • Assignment 1 In this assignment you will be writing a tool to help you play the...

    Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...

  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT