Συζήτηση
Γεια χαρά, Επισκέπτης
Όνομα χρήστη: Κωδικός: Να με θυμάσαι

ΘΕΜΑ: C++ Take data from file & Sorting to another file

C++ Take data from file & Sorting to another file 5 Χρόνια 9 Μήνες πριν #5154

  • nickpsal
  • Το Άβαταρ του/της nickpsal
  • Αποσυνδεμένος
  • py____
  • Δημοσιεύσεις: 22
  • Ληφθείσες Ευχαριστίες 3
Καλησπέρα, έχω μια απορία.
Έχω δημιουργήσει 3 αρχεία για ένα πρόγραμμα στην C++, BibElement.h, BibElement.cpp, main.cpp. Θέλω να φτιάξω ένα πρόγραμμα όπου θα ορίζω μια κλάση BibElement με τα πεδία type, id, author, title, journal, volume, booktitle, publisher, pages, year (όλα string). Το πρόγραμμά αυτό θα διαβάζει αρχεία κειμένου, τα οποία θα έχει τα στοιχεία των βιβλίων. Έχω φτιάξει ήδη αυτό το αρχείο (input.txt). Μετά θα ζητά από το χρήστη το όνομα του αρχείου εισόδου και μετά δύο κριτήρια ταξινόμησης πρωτεύον και δευτερεύον (που θα είναι οποιοδήποτε από τα ακόλουθα πεδία: type, id, author και year) και θα αποθηκεύει στο output.txt.

Η απορία μου είναι πως μπορώ να κάνω Sort του input.txt μέσα από το BibElement.cpp & να παίρνει τα 2 κλειδιά από το χρήστη. Σας παρακαλώ βοηθήστε με όποιος ξέρει!
Παρακάτω σας στέλνω τα αρχεία αλλά δεν ξέρω αν είναι και σωστά όλα!

Αυτό είναι το main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "BibElement.h"
using namespace std;

string text, temp, sortkey;
int i = 1, n = 0;
int main(void)
{
fstream fileRead, fileWrite;
BibElement obj1();
cout << "Which 2 keys to sort your books in file:" << endl;
cout << "1. Type" << endl;
cout << "2. Id" << endl;
cout << "3. Author" << endl;
cout << "4. Year" << endl;
cin >> sortkey;
obj1::print();

fileRead.open("input.txt", ios::in);
if(!fileRead)
{
cerr << "File not found!" << endl;
exit(1);
}
getline(fileRead,text);
do {
getline(fileRead,temp);
n++;
} while (!fileRead.eof());
fileRead.close();
fileRead.open("input.txt", ios::in);
for(i=0; i<n; i++)
{
if(i>0)
text += '\n';
getline(fileRead,temp);
text = text + temp;
}
fileRead.close();
fileWrite.open("output.txt", ios::out);
if(!fileWrite)
{
cerr << "File is busy...!" << endl;
exit(1);
}
n = text.length();
fileWrite << text;
fileWrite.close();
return 0; }

Αυτό είναι το BibElement.cpp:
#include <iostream>
#include <vector>
#include <algorithm>
#include "BibElement.h"
using namespace std;

BibElement::BibElement(string name, string name2, string name3, string name4, string name5, string name6, string name7, string name8, string name9, string name10)
{
setType(name);
setId(name2);
setAuthor(name3);
setTitle(name4);
setJournal(name5);
setVolume(name6);
setBooktitle(name7);
setPublisher(name8);
setPages(name9);
setYear(name10);
}
void BibElement::setType(string name)
{
if(name!="@article" && name!="@Book" && name!="@inproceedings")
type = ' ';
else
type = name;
}
void BibElement::setId(string name2)
{
id = name2;
}
void BibElement::setAuthor(string name3)
{
author = name3;
}
void BibElement::setTitle(string name4)
{
title = name4;
}
void BibElement::setJournal(string name5)
{
journal = name5;
}
void BibElement::setVolume(string name6)
{
volume = name6;
}
void BibElement::setBooktitle(string name7)
{
booktitle = name7;
}
void BibElement::setPublisher(string name8)
{
publisher = name8;
}
void BibElement::setPages(string name9)
{
pages = name9;
}
void BibElement::setYear(string name10)
{
year = name10;
}

string BibElement::getType(void)
{
return type;
}
string BibElement::getId(void)
{
return id;
}
string BibElement::getAuthor(void)
{
return author;
}
string BibElement::getTitle(void)
{
return title;
}
string BibElement::getJournal(void)
{
return journal;
}
string BibElement::getVolume(void)
{
return volume;
}
string BibElement::getBooktitle(void)
{
return booktitle;
}
string BibElement::getPublisher(void)
{
return publisher;
}
string BibElement::getPages(void)
{
return pages;
}
string BibElement::getYear(void)
{
return year;
}
void BibElement::print()
{
if(sortkey=="type" || sortkey=="Type")
{
vector<BibElement> books;
sort(books.begin(), books.end(), [](Book a, Book b)
{
return a.getType();
});
}
else if(sortkey=="id" || sortkey=="Id")
{
vector<BibElement> books;
sort(books.begin(), books.end(), [](Book a, Book b)
{
return a.getId();
});
}
else if(sortkey=="author" || sortkey=="Author")
{
vector<BibElement> books;
sort(books.begin(), books.end(), [](Book a, Book b)
{
return a.getAuthor();
});
}
else
{
vector<BibElement> books;
sort(books.begin(), books.end(), [](Book a, Book b)
{
return a.getYear();
});
}
}
Πρέπει να είστε εγγεγραμμένο μέλος του Φόρουμ για να κάνετε μια δημοσίευση.

C++ Take data from file & Sorting to another file 5 Χρόνια 9 Μήνες πριν #5156

  • babaliaris1
  • Το Άβαταρ του/της babaliaris1
  • Αποσυνδεμένος
  • python
  • Δημοσιεύσεις: 445
  • Ληφθείσες Ευχαριστίες 75
Θα φτιάξεις μιά μέθοδο μέσα στο BibElement.cpp την οποία θα καλείς μέσα από την main. Αν θέλεις να ταξινομήσεις το αρχείο,τότε σαν παράμετρο θα περνάς το filestream και τα δύο κλειδιά του χρήστη με βάση τα οποία κάνεις ταξινόμησει.
Πρέπει να είστε εγγεγραμμένο μέλος του Φόρουμ για να κάνετε μια δημοσίευση.
Συντονιστές: pmav99
Χρόνος δημιουργίας σελίδας: 0.674 δευτερόλεπτα

Μοιράσου το!

Powered by CoalaWeb

Λίστα Ταχυδρομείου