#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
ifstream fin("datein.txt");
ofstream fout("dateout.txt");
struct nod {
char nume[100];
int a;
char d[100];
nod* adrd, * adrs;
};
nod* prim, * ultim, * r;
void creare(nod*& prim, nod*& ultim)
{
int n, i;
nod* p, * q1, * q2;
fin >> n;
prim = new nod;
fin.get();
fin >> prim->nume;
fin >> prim->a >> prim->d;
fin.get();
prim->adrs = NULL;
prim->adrd = NULL;
ultim = prim;
for (i = 2; i <= n; i++){
` p = new nod;
fin >> p->nume;
fin >> p->a >> p->d;
fin.get();
p->adrd = NULL;
p->adrs = NULL;
q1 = ultim;
q2 = ultim->adrs;
if (p->a >= ultim->a) {
p->adrs = ultim;
ultim = p;}
else{
int ok = 0;
while (ok == 0 && q2 != NULL){
if (p->a < q1->a){
if (p->a >= q2->a) ok = 1;
else{
q1 = q2;
q2 = q2->adrs;
}
}
}
if (q2 == NULL) q1->adrs = p;
else {
q1->adrs = p;
p->adrs = q2;
}
}
q1 = prim;
q2 = prim->adrd;
if (strcmp(p->nume, prim->nume) <= 0) {
p->adrd = prim;
prim = p;
}
else {
int ok = 0;
while (ok == 0 && q2 != NULL){
if (strcmp(p->nume, q1->nume) > 0){
if (strcmp(p->nume, q2->nume) <= 0) ok = 1;
else {
q1 = q2;
q2 = q2->adrd;
}
}
}
if (q2 == NULL) q1->adrd = p;
else {
q1->adrd = p;
p->adrd = q2;
}
}
}
}
void total_stele(nod* prim)
{
nod* p = prim;
int nr = 0;
cout << "Numarul total de stele este:";
while (p != NULL){
nr = nr + p->a;
p = p->adrd;
}
cout << nr<<endl;
}
void total_pt(nod* prim)
{
nod* p = prim;
int nr = 0;
cout << "Ptolemeu a denumit ";
while (p != NULL) {
if(strcmp(p->d,"PG")==0)
nr = nr ++;
p = p->adrd;
}
cout << nr << " stele"<<endl;
}
void tiparire(nod* prim) {
nod* p = prim;
cout << "Lista de elemenete este:" << endl;
while (p != NULL)
{
cout << p->nume << " " << p->a << " " << p->d << endl;
p = p->adrd;
}
cout << endl;
}
void tiparireDS(nod* ultim)
{
nod* p = ultim;
cout << "Lista de elemenete este:" << endl;
while (p != NULL)
{
cout << p->nume << " " << p->a << " " << p->d << endl;
p = p->adrs;
}
cout << endl;
}
void inserare(nod* &prim, nod* &utlim) {
int n;
fin >> n;
for (int i = 1; i <= n; i++)
{
nod* p, * q1, * q2;
p = new nod;
fin.get();
fin >> p->nume;
fin >> p->a >> p->d;
p->adrd = NULL;
p->adrs = NULL;
if (strcmp(p->nume, prim->nume) < 0) {
p->adrd = prim;
prim = p;}
else
{
q1 = prim;
q2 = prim->adrd;
int ok = 0;
while (ok == 0 && q2 != NULL)
{
if (strcmp(p->nume, q1->nume) > 0) {
if (strcmp(p->nume, q2->nume) <= 0) ok = 1;
else {
q1 = q2;
q2 = q2->adrd;
}
}
}
if (q2 == NULL) q1->adrd = p;
else {
q1->adrd = p;
p->adrd = q2;
}
}
if (p->a >= ultim->a) {
p->adrs = ultim;
ultim = p;
}
else {
q1 = ultim;
q2 = ultim->adrs;
int ok = 0;
while (ok == 0 && q2 != NULL)
{
if (p->a < q1->a)
{
if (p->a >= q2->a) ok = 1;
else {
q1 = q2;
q2 = q2->adrs;
}
}
}
if (q2 == NULL) q1->adrs = p;
else {
q1->adrs = p;
p->adrs = q2;
}
}
}
tiparire(prim);
}
void stergere(nod* prim, nod* ultim)
{
nod * q1;
char p[50];
cin >> p;
int ok = 0;
q1 = prim;
while (q1 != NULL && ok ==0)
{
if (strcmp(p, q1->adrd->nume) == 0)
{
q1->adrd = q1->adrd->adrd;
ok = 1;
}
else q1 = q1->adrd;
}
q1 = ultim;
ok = 0;
while (q1 != NULL && ok == 0) {
if (strcmp(p, q1->adrs->nume) == 0)
{
q1->adrs = q1->adrs->adrs;
ok = 1;
}
else q1 = q1->adrs;
}
tiparire(prim);
}
void alegere(nod* prim, nod* ultim)
{
int n;
do{
if (system("CLS")) system("clear");
cout << "____________________________" << endl;
cout << " MENIU" << endl;
cout << "0. Afisarea listei " << endl;
cout << "1. Lista revizuita" << endl;
cout << "2. Lista completata" << endl;
cout << "3. Concurs gimnaziu" << endl;
cout << "4. Concurs clase primare (sectiunea stele totale)" << endl;
cout << "5. Concurs clase primare (sectiunea Ptolemeu)" << endl;
cout << "6. EXIT" << endl;
cout << "____________________________" << endl;
cout << "Ce optiune doriti sa alegeti?" << endl;
cin >> n;
if (n == 0) tiparire(prim);
if (n == 1){
cout << "Sa se stearga elementul:";
stergere(prim,ultim);
}
if (n == 2) inserare(prim, ultim);
if (n == 3) tiparireDS(ultim);
if (n == 4) total_stele(prim);
if (n == 5) total_pt(prim);
}while(n<6);
}
int main()
{
creare(prim, ultim);
alegere(prim, ultim);
return 0;
}