C++ C++ please and thank you. I already have: __________________________________
ID: 3917055 • Letter: C
Question
C++ C++ please and thank you.
I already have:
________________________________________________
#ifndef Cd_hpp
#define Cd_hpp
#include <stdio.h>
#include <iostream>
#include "complex.h"
#include <string>
#include "string.h"
#define MAX_STR_LEN 1000
using namespace std;
class Cd
{
public:
string artist;
string title;
int year;
string genre;
string fan;
string imageUrl;
//Methods
Cd();
void print();
void set(string artist_in, string title_in, int year_in, string genre_in, string fan_in);
int getCommmaIndex( string str, int index[]);
//void printWidthLsabel(string label);
int parseCSV(string csvStr, string values[]);
void setFromCSV(string csv);
};
#endif /* Cd_hpp */
__________________________________________________________________
#include "Cd.hpp"
Cd::Cd()
{
}
void Cd::print()
{
cout <<" aretist:" << artist;
cout <<" title:" << title;
cout <<" year:" << year;
cout <<" genre:" << genre;
cout <<" fan:" << fan;
}
void Cd::set(string artist_in, string title_in, int year_in, string genre_in, string fan_in)
{
artist = artist_in;
title = title_in;
year = year_in;
genre = genre_in;
fan = fan_in;
}
int Cd::getCommmaIndex( string str, int index[])
{
int i, count;
count=0;
for (i=0;i<str.length();i++)
{
if (str[i] == ',')
{
index[count] = i;
count= count +1;
}
}
return (count);
}
int Cd::parseCSV(string csvStr, string values[])
{
int i, i1,i2,len;
int index[MAX_STR_LEN];
int numCommas;
numCommas=getCommmaIndex(csvStr, index);
for(i=0;i<=numCommas;i++)
{
if(i==0) i1 = 0;
else i1 = index[i-1] + 1;
if (i==numCommas) i2 = csvStr.length() - 1;
else i2 = index[i]-1;
len = i2-i1 +1;
values[i] = csvStr.substr(i1, len);
}
return (numCommas + 1);
}
void Cd::setFromCSV(string csv)
{
string values[100];
int numValues;
numValues = parseCSV(csv, values);
artist = values[0];
title = values[1];
int yearAsInt = atoi(values[2].c_str());
year =yearAsInt;
genre = values[3];
fan = values [4];
imageUrl = values[5];
}
_________________________________________
main part
____________________________________
int main()
{
Cd myCd;
string str, values[100];
int numValues;
myCd.set("David jk", "Ziggy sed", 1999, " Classic Rock ", "Zack Zikko");
myCd.print();
str = "one,two,three,four,five";
numValues= myCd.parseCSV(str, values);
for (int i=0; i<numValues; i++)
{
printf(" valus[%d]: %s" ,i, values[i].c_str());
}
for (int i=0; i<numValues; i++)
{
cout<<" values["<<i<<"]:"<< values[i];
}
return 0;
}
Explanation / Answer
here is your files : ------------>>>>>>>>
CdLib.hpp : -------->>>>>>
#ifndef CDLIB_H
#define CDLIB_H
#include "Cd.cpp"
#include<iostream>
#include<fstream>
#define MAX 200
using namespace std;
class CdLib{
public:
int n;
Cd *cds;
CdLib();
void setFromFile(string file);
void print(string label);
void printFromIndexOrder(int index[],string label);
string* getTitles();
string* getArtists();
int* getYears();
string* getGenres();
string* getFans();
string* getImageURLs();
private:
void swapInts(int *a,int *b){
int t = *a;
*a = *b;
*b = t;
}
void swapStrings(string *s1,string *s2){
string s = *s1;
*s1 = *s2;
*s2 = s;
}
void sortInts(int dim,int x[],int index[]){
//please give detail specification
}
void sortStrings(int dim,string str[],int index[]){
//please give detail specification
}
};
#endif
CdLib.cpp : ---------->>>>>>>
#include "CdLib.hpp"
CdLib::CdLib(){
n = 0;
cds = new Cd[MAX];
}
string* CdLib::getArtists(){
string *str = new string[n];
for(int i = 0;i<n;i++){
str[i] = cds[i].artist;
}
return str;
}
string* CdLib::getFans(){
string *str = new string[n];
for(int i = 0;i<n;i++){
str[i] = cds[i].fan;
}
}
string* CdLib::getGenres(){
string *str = new string[n];
for(int i = 0;i<n;i++){
str[i] = cds[i].genre;
}
}
string* CdLib::getImageURLs(){
string *str = new string[n];
for(int i = 0;i<n;i++){
str[i] = cds[i].imageUrl;
}
}
string* CdLib::getTitles(){
string *str = new string[n];
for(int i = 0;i<n;i++){
str[i] = cds[i].title;
}
}
int* CdLib::getYears(){
int *str = new string[n];
for(int i = 0;i<n;i++){
str[i] = cds[i].year;
}
}
void CdLib::setFromFile(string file){
ifstream ifs;
ifs.open(file);
string line;
int n = 0;
while(!ifs.eof() && n < MAX){
getline(ifs,line);
cds[n].setFromCSV(line);
n++;
}
ifs.close();
}
void CdLib::print(string label){
for(int i = 0;i<n;i++){
cout<<endl;
cout<<label;
cds[i].print();
}
}
void CdLib::printFromIndexOrder(int index[],string label){
int s = sizeof(index)/sizeof(index[0]);
for(int i = 0;i<s;i++){
if(index[i] >= 0 && index[i] < n){
cout<<endl;
cout<<label;
cds[index[i]].print();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.