Please read carefully. The output must match exactly as stated in the assignment
ID: 3755489 • Letter: P
Question
Please read carefully. The output must match exactly as stated in the assignment. Take your time if you have to, it's fine, as long as it's the CORRECT answer.
If the correct solution is provided with comments, you'll receive more than one thumbs up. This is a common problem in Computer Science course.
Thank you.
1. Introduction You will create a C++ program to manage a name list. Read a file that has a lot of items that stand for the information of a name list of certain class. Then sort the records by specific attributes to alphabetical ascending order, and output to a plain text file. 2. Input and Output a. Input file 1) The input file has multiple records (number> 1). Your program should read the records one by one from the beginning of the file to the end. Each record has uniform attributes (columns), the attributes appear in a fixed order, a record should contain all the keys, no empty values part will be given. But the input file may contain duplicated records. If two records have same id value, they can be recognized as duplicated records. You should always update record with the latter one. If somebody's name has more than one words, the separator would be underline _". Other than useful information in records, no other character or space will be given. 2) 3) Each record takes one line (ends in n'), and one line only contains oneExplanation / Answer
The requirement of this question is huge and todo the justice to such a problem in only 120 minutes is not possible. After reading all the requirements let me try to do my best. The problem is not complex in terms of logical understanding but it is more of parsing problem and the using sorting on the records.
The below is the solution for the problem. It is not complete as give time to solve it is not sufficient. However, i have added comments where ever required so that the student can easy complete the asssignment. Hope it helps :)
#include<iostream>
#include<string>
#include<vector>
#include <fstream>
struct record{
int id;
std::string first;
std::string last;
std::string dob;
float gpa;
};
#define THREE 3
// to split the arguments from commandline.
size_t split(const std::string &txt, std::vector<std::string> &strs, char ch)
{
size_t pos = txt.find( ch );
size_t initialPos = 0;
strs.clear();
// Decompose statement
while( pos != std::string::npos ) {
strs.push_back( txt.substr( initialPos, pos - initialPos ) );
initialPos = pos + 1;
pos = txt.find( ch, initialPos );
}
// Add the last one
strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) );
return strs.size();
}
// each line is a command in the file. just read a line and it will be a command
// AllCmd will have all the command at the end.
void readCommandsFromFile(std::string sortFile, std::vector<std::string> &AllCmd)
{
std::string line;
std::ifstream myfile;
myfile.open(sortFile);
if (myfile.is_open())
{
while (getline(myfile,line) )
{
AllCmd.push_back(line);
}
myfile.close();
}
}
struct record parseTheLineForRecordData(std::string lineData)
{
// file the logic here
}
// read line by line all the records from the file and parse each line in the record format.
void readRecordsFromFile(std::string fileName, std::vector<struct record> &allRecords)
{
printf(" readRecordsFromFile count ");
std::string line;
struct record tempRecord;
std::ifstream myfile;
myfile.open(fileName);
if (myfile.is_open())
{
while (getline(myfile,line) )
{
// todo
// tempRecord = parseTheLineForRecordData(); // need to write this function..
// once the record is created from the line
// push it on the list of records
allRecords.push_back(tempRecord);
// then clear the tempRecord
}
myfile.close();
}
}
void sortTheRecords(std::vector<struct record> records,std::vector<struct record> tempRecords, std::string sortingCmd)
{
}
int main(int argv, char **argvList)
{
std::string inputFileName, outputFileName, sortFileName;
std::vector<std::string> files;
struct record recordList[2000];
if(argv <= 1)
printf("Enter correct arguments!!! ");
else if(argv == 2)
{
std::string inputData = argvList[1];//.substr(argvList[1].find("""),argvList[1].length());
//inputFileName = inputData.substr(inputData.find(' '),);
size_t count = split(inputData,files,' ');
if(THREE!=count)
count = split(inputData,files,';');
if(THREE!=count)
{
printf("Error in input format!!");
exit(0);
}
printf("%s[%s] count [%s]", files[0].c_str(), files[1].c_str(), files[2].c_str());
inputFileName = files[0].substr(files[0].find('=')+1);
sortFileName = files[2].substr(files[2].find('=')+1);
outputFileName = files[1].substr(files[1].find('=')+1);
/*
* so far we have read all the input format
*/
std::vector<std::string> cmdList;
std::vector<struct record> recordList, tempRecordList;
// read the commands
readCommandsFromFile(sortFileName, cmdList);
readRecordsFromFile(inputFileName, recordList);
tempRecordList = recordList;
//for each command that we have read, perform the operation on the argvList
for(auto it = cmdList.begin(); it!= cmdList.end();it++)
{
sortTheRecords(recordList, tempRecordList, *it);
recordList = tempRecordList;
}
printf("%s[%s] count [%s]", inputFileName.c_str(), sortFileName.c_str(), outputFileName.c_str());
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.