сряда, 27 май 2015 г.

C++ Student Project

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define N 8

using namespace std;
struct library
{
    char title[30];
    int year;
    char press[30];
    char firstName[30];
    char lastName[30];
};
typedef struct library library_r;

 static int comp1(const void *pArg1, const void *pArg2)
{
  int rezult;

  rezult = strcmp(((library_r*)pArg1)->title, ((library_r*)pArg2)->title);
  return rezult;
}
void Input(library_r books[N])
{
    strcpy(books[0].title,"The Mad Dog");
    strcpy(books[0].firstName,"Antoan");
    strcpy(books[0].lastName,"Dimitrov");
    strcpy(books[0].press,"Levski Vendetta");
    books[0].year=1996;
    strcpy(books[1].title,"Hogfather");
    strcpy(books[1].firstName,"Terry");
    strcpy(books[1].lastName,"Pratchett");
    strcpy(books[1].press,"Vuzev");
    books[1].year=1996;
    strcpy(books[2].title,"Mort");
    strcpy(books[2].firstName,"Terry");
    strcpy(books[2].lastName,"Pratchett");
    strcpy(books[2].press,"Vuzev");
    books[2].year=1987;
    strcpy(books[3].title,"Equal Rites");
    strcpy(books[3].firstName,"Terry");
    strcpy(books[3].lastName,"Pratchett");
    strcpy(books[3].press,"Vuzev");
    books[3].year=1987;
    strcpy(books[4].title,"I heard that song before");
    strcpy(books[4].firstName,"Mary");
    strcpy(books[4].lastName,"Clark");
    strcpy(books[4].press,"Bard");
    books[4].year=2001;
    strcpy(books[5].title,"Weep no more, my lady");
    strcpy(books[5].firstName,"Mary");
    strcpy(books[5].lastName,"Clark");
    strcpy(books[5].press,"Bard");
    books[5].year=1987;
    strcpy(books[6].title,"The shadow of your smile");
    strcpy(books[6].firstName,"Mary");
    strcpy(books[6].lastName,"Clark");
    strcpy(books[6].press,"Bard");
    books[6].year=2004;
    strcpy(books[7].title,"Crazy White Nigga");
    strcpy(books[7].firstName,"Koleca");
    strcpy(books[7].lastName,"Nedkov");
    strcpy(books[7].press,"Levski Ghetto");
    books[7].year=1996;  
    return;
}

void GivenYear(library_r books[N])
{
    int year2;
    int counter=1;
    cout<<"Enter Year : ";
    cin>>year2;
    cout<<"Books of that year : "<<endl;
    for(int i=0;i<N;i++)
    {
        if(year2==books[i].year)
        {
            cout<<counter<<"."<<books[i].title<<endl;
            counter++;
        }
    }
    return;
}

void GivenAuthor(library_r books[N])
{  
    char choiceFirstName[30];
    char choiceLastName[30];
 int counter=1;
    cout<<"Enter First Name : "<<endl;
    cin>>choiceFirstName;
    cout<<"Enter Last Name : "<<endl;
    cin>>choiceLastName;
    cout<<"Books from that author :"<<endl;
    for(int i=0;i<N;i++)
    {
        if((!strcmp(books[i].firstName,choiceFirstName))&&(!strcmp(books[i].lastName,choiceLastName)))
        {
            cout<<counter<<"."<<books[i].title<<endl;
 counter++;
        }
    }
    return;
}
void GivenPress(library_r books[N])
{
 char choicePress[30];
 int counter=1;
 cout<<"Enter press : "<<endl;
 cin>>choicePress;
 cout<<"Books from this press : "<<endl;
 for(int i=0;i<N;i++)
 {
 if(!strcmp(choicePress,books[i].press))
 {
 cout<<counter<<"."<<books[i].title<<endl;
 counter++;
 }
 }
}
void OutputingBooks(library_r books[N])
{
 for(int i=0;i<N;i++)
 {
    cout<<"Name of Book : "<<books[i].title<<endl;
    cout<<"First Name of Author : "<<books[i].firstName<<endl;
    cout<<"Last Name of Author : "<<books[i].lastName<<endl;
    cout<<"Press of book : "<<books[i].press<<endl;
    cout<<"----------------------------------------------------------------------------"<<endl;
   getchar();
 }

}


int main()
{
cout<<ded;
    char choice;
    struct library books[N];  
    struct library *b1;
    Input(books);
    OutputingBooks(books);
    do
    {

     do
        {
    cout<<"Choose Function : "<<endl;
    cout<<"1.Books from given year."<<endl;
    cout<<"2.Books from given author."<<endl;
    cout<<"3.Number of books of a given press."<<endl;
    cout<<"4.Sorting books alphabetical."<<endl;
    cout<<"5.Outputing books."<<endl;
    cout<<"6.End of the program."<<endl;
    cin>>choice;
}while(choice<'1'||choice>'6');
      b1=books;
    switch(choice)
    {
        case '1':GivenYear(b1);
        break;
        case '2':GivenAuthor(b1);
        break;
        case '3':GivenPress(b1);
        break;
        case '4':qsort(b1, N, sizeof(library_r), comp1);
        break;
        case '5':OutputingBooks(b1);
  break;


    }
    }while(choice !=6);
     system("pause");
     return 0;
}