You are on page 1of 6

Декларирање на променливи од типот структура:

Прв начин:
struct Books
{
   string  title;
   string  author;
   string  subject;
   int   book_id;
};
int main( )
{
  //Declare Book1 of type Book
   struct Books Book1;
   //Declare Book2 of type Book   
    struct Books Book2;       
 
Втор начин:
struct Books
{
   string  title;
   string  author;
   string  subject;
   int   book_id;
}Book1,Book2;

Пример-програма-1 (прв начин)

//p1 Books

#include <iostream>

#include <cstring>

using namespace std; 

struct Books

      string  title;

      string  author;

      string  subject;

      int   book_id;

};
int main( )

   struct Books Book1;        // Declare Book1 of type Book

   struct Books Book2;        // Declare Book2 of type Book

   // book 1 specification

   Book1.title="Learn C++ Programming";

   Book1.author="Chand Miyan";

   Book1.subject="C++ Programming";

   Book1.book_id = 6495407;

   // book 2 specification

   Book2.title= "Telecom Billing";

   Book2.author="Yakit Singha";

   Book2.subject="Telecom";

   Book2.book_id = 6495700;

   // Print Book1 info

   cout << "Book 1 title : " << Book1.title <<endl;

   cout << "Book 1 author : " << Book1.author <<endl;

   cout << "Book 1 subject : " << Book1.subject <<endl;

   cout << "Book 1 id : " << Book1.book_id <<endl;

   cout<<endl;

   // Print Book2 info

   cout << "Book 2 title : " << Book2.title <<endl;


   cout << "Book 2 author : " << Book2.author <<endl;

   cout << "Book 2 subject : " << Book2.subject <<endl;

   cout << "Book 2 id : " << Book2.book_id <<endl;

   cout<<endl;

system("pause");

return 0;

}
 

Пример-програма-2 (втор начин)

//p2 Books

#include <iostream>

#include <cstring>

using namespace std;

struct Books

      string  title;

      string  author;

      string  subject;

      int   book_id;

}Book1,Book2;

int main( )

   // book 1 specification

   Book1.title="Learn C++ Programming";

   Book1.author="Chand Miyan";
   Book1.subject="C++ Programming";

   Book1.book_id = 6495407;

   // book 2 specification

   Book2.title= "Telecom Billing";

   Book2.author="Yakit Singha";

   Book2.subject="Telecom";

   Book2.book_id = 6495700;

   // Print Book1 info

   cout << "Book 1 title : " << Book1.title <<endl;

   cout << "Book 1 author : " << Book1.author <<endl;

   cout << "Book 1 subject : " << Book1.subject <<endl;

   cout << "Book 1 id : " << Book1.book_id <<endl;

   cout<<endl;

   // Print Book2 info

   cout << "Book 2 title : " << Book2.title <<endl;

   cout << "Book 2 author : " << Book2.author <<endl;

   cout << "Book 2 subject : " << Book2.subject <<endl;

   cout << "Book 2 id : " << Book2.book_id <<endl;

   cout<<endl;

system("pause");

return 0;

}
 
 
Да се состави програма со која ќе се прикаже името и презимето на студентот со
поголем просек, од двајца внесени студенти.
Насоки:
Да се креира структура Student која ќе содржи податоци за името (char[15]),
презимето(char[20]), број во дневник (int) и просек на студентот (float).
Податоците за двајца студенти да се внесат преку тастатура.
Да се напише потпрограма (функција) која ќе прима податок од типот структура и ќе
ги прикаже податоците на структурата student.
 
#include<iostream>
#include<cstring>
using namespace std;
struct Student
{
   char ime[15];
   char prezime[20];
   int broj_vo_dnevnik;
   float prosek;
}Student1,Student2;
 
void print_student(struct Student S)
{
     cout<<"Podatoci za student:"<<endl;
     cout<<"Ime:"<<S.ime<<endl;
     cout<<"Prezime:"<<S.prezime<<endl;
     cout<<"Broj vo dnevnik:"<<S.broj_vo_dnevnik<<endl;
     cout<<"Prosek na studentot:"<<S.prosek<<endl;
}
    
main()
{
    char ime[15],prezime[20];
    int broj;
    float prosek;
    cout<<"Vnesete podatoci za prviot student:"<<endl;
    cout<<"Ime:";cin>>ime;
    strcpy(Student1.ime,ime);
    cout<<"Prezime:";cin>>prezime;
    strcpy(Student1.prezime,prezime);
    cout<<"Broj vo dnevnik:";cin>>broj;
    Student1.broj_vo_dnevnik=broj;
    cout<<"Prosek na ucenikot:";cin>>prosek;
    Student1.prosek=prosek;
    cout<<endl;
   
    cout<<"Vnesete podatoci za vtoriot student:"<<endl;
    cout<<"Ime:";cin>>ime;
    strcpy(Student2.ime,ime);
    cout<<"Prezime:";cin>>prezime;
    strcpy(Student2.prezime,prezime);
    cout<<"Broj vo dnevnik:";cin>>broj;
    Student2.broj_vo_dnevnik=broj;
    cout<<"Prosek na ucenikot:";cin>>prosek;
    Student2.prosek=prosek;
   
    cout<<endl;
    print_student(Student1);
    cout<<endl;
    print_student(Student2);
    cout<<endl;
    if(Student1.prosek==Student2.prosek)
       cout<<"Dvajcata studenti imaat ist prosek"<<endl;
    else if(Student1.prosek>Student2.prosek)
            cout<<"Studentot koj ima pogolem prosek e:"<<Student1.ime<<"
"<<Student1.prezime<<endl;
         else
            cout<<"Studentot koj ima pogolem prosek e:"<<Student2.ime<<"
"<<Student2.prezime<<endl;  
system("pause");
return 0;
}

You might also like