Witam, jestem dość początkujący w programowaniu i po całkiem udanym okresie programowania strukturalnego przyszedł moment na obietkowe.. Mam za zadanie stworzyć listę studentów korzystając z struktury typu lista. Mam stworzyć menu tekstowe umożliwiające dopisywanie studentów do listy, usuwanie z listy, wypisywanie listy, znajdowanie studentów na
liście, sortowanie listy, odczyt liczby studentów na liście. No i to by było na tyle..
Teraz "pochwale" się kodem jaki udało mi się napisać (manu jest stworzone tylko po to żeby było.. nie mam tam na razie żadnych funkcji bo za bardzo nie wiem jak je zrobić.. :( )
#pragma once
#include <string>
using namespace std;
class CPerson
{
public:
CPerson(void);
CPerson(string name, string last_name, int age);
~CPerson(void);
string getName(void);
string getLastName(void);
int getAge(void);
void setName(string name);
void setLastName(string last_name);
void setAge(int age);
void printPerson(void);
friend ostream& CPerson:: operator<<(ostream& stream, CPerson &person);
private:
string Name;
string LastName;
int Age;
};
#include "Person.h"
#include <iostream>
using namespace std;
CPerson::CPerson(void)
{
Age = -1; //nadanie wartości domyślnej
}
CPerson::CPerson(string name, string last_name, int age)
{
Name = name;
LastName = last_name;
Age = age;
}
CPerson::~CPerson(void)
{
}
string CPerson::getName(void)
{
return Name;
}
string CPerson::getLastName(void)
{
return LastName;
}
int CPerson::getAge(void)
{
return Age;
}
void CPerson::setName(string name)
{
Name = name;
}
void CPerson::setLastName(string last_name)
{
LastName = last_name;
}
void CPerson::setAge(int age)
{
Age = age;
}
void CPerson::printPerson(void) //bardzo nieelegancka forma wypisywania na ekran ale na początek bardzo prosta
{
cout << "Imie: " << Name << " Nazwisko: " << LastName << " Wiek: " << Age << endl;
}
ostream& operator<<(ostream& stream, CPerson &person) //tak powinno być to zrealizowane jako przeładownie operatora <<
{
stream << "Imie: " << person.Name << " Nazwisko: " << person.LastName << " Wiek: " << person.Age;
return stream;
}
#include <iostream>
#include "Person.h"
#include <list>
using namespace std;
#define DEFAULT_NAME "DefaultName"
#define DEFAULT_LAST_NAME "DefaultLastName"
#define DEFAULT_AGE 10
int main(){
int ile;
cout << " Witaj w MENU ! "<<endl;
cout<<"Wybierz 1- dopisz studenta do listy"<<endl
<<"Wybierz 2- usun studenta z listy"<<endl
<<"Wybierz 3-Wypisz liste"<<endl
<<"Wybierz 4-Znajdz studenta na liscie"<<endl
<<"Wybierz 5-Posortuj liste"<<endl
<<"Wybierz 6-Odczytaj liste studentow na liscie"<<endl;
cin>>ile;
switch(ile){
case 1:
cout<< "opcja 1";
break;
case 2:
cout<<"opcja 2 ";
break;
case 3:
cout<<"opcja 3";
break;
case 4:
cout<<"opcja 4";
break;
case 5:
cout<<"opcja 5";
break;
case 6:
cout<<"opcja 6";
break;
int liczba;
}
CPerson person1("Jan", "Kowalski", 22);
//iterator do odpowiedniej klasy
list <CPerson> lst;
for(int i=0; i<10; i++)
{
lst.push_back(person1);
}
//it=lst.begin();
//wyswietlenie wszystkich elementow za pomoca iteratorow
for ( list<CPerson>::iterator it=lst.begin(); it!=lst.end();it++){
cout<<*it;
}
lst.push_back(person1);
//it=lst.begin();
person1.printPerson();
CPerson person2;
person2.printPerson();
person2.setName("Adam");
person2.setLastName("Michalski");
person2.setAge(30);
person2.printPerson();
CPerson person3("Stanislaw", "Nowakowski", 30);
person3.printPerson();
CPerson person4 = person3; //wykożystywany jest operator przypiania
const int n = 6;
CPerson person_array[n] = {person1, person2, person3, person4}; //podczas inicjalizacji tablicy uruchamiany jest konstruktor kopiujący
cout << "--------- Lista osob ---------" << endl;
for(int i = 0; i < n; i++)
{
if(person_array[i].getName() == "")
person_array[i].setName(DEFAULT_NAME);
if(person_array[i].getLastName() == "")
person_array[i].setLastName(DEFAULT_LAST_NAME);
if(person_array[i].getAge() < 0)
person_array[i].setAge(DEFAULT_AGE);
cout << person_array[i] << endl; //wypisywanie na ekran możliwe dzięki przeładowaniu operatora <<
}
system("pause");
return 0;
}
Pomógłby mi ktoś ogarnąć ten chaos.. ?