Strukturalar
Structura – bul bir neshe structuralardi, maydanlardi ha’m har qiyli tiplerdi biriktiriwshi tip).
qasiyeti:
- avtor (qatar)
- ati (qatar)
- jili (putin san)
- Betler sani (putin san)
masele: usilardin barligin bir putin etip birlestiriw
struct Book {
char author[40];
char title[80];
int year;
int pages;
};
Stucturaga berilgen magliwmatlardi kiritiw?
Yad ajiratilmaydi!
!
structura
ati
maydan
Struktura sitaksisi struct { ; ; … ; }; Bul jerde - struktura ko'rinisinde jarati'li'p ati'rg'an jan'a tu’rdin’ ati', i> i> - strukturani'n' i-maydani'ni'n' (ati') dag'azasi'.
Programmaliq ulgiler
Ma’sele: books.dat fayilinda kitap haqqindag’ berilgenler keltirilgen bolip, ol Book tipindegi ( 100 den kishi) structura korinisinde berilgen. Olardin’ barliginin’ shiqqan jilina 2008 di ornatip qaytadan usi faylg’a jazin.
#include
struct Book { … };
void main()
{
Book B[100];
int i, n;
FILE *f;
f = fopen ( "books.dat", "rb" );
n = fread ( B, sizeof(Book), 100, f );
fclose(f);
for ( i = 0; i < n; i ++ ) B[i].year = 2008;
fp = fopen("books.dat", "wb" );
fwrite ( B, sizeof(Book), n, f );
fclose ( f );
}
struct Book { … };
f = fopen ( "books.dat", "rb" );
n = fread ( B, sizeof(Book), 100, f );
fclose ( f );
fp = fopen("books.dat", "wb" );
fwrite ( B, sizeof(Book), n, f );
fclose ( f );
structuranin’ toliq jaziliwi
Massivti oqiw (≤ 100 struktura), n o’zgeriwshige massiv uzinlig’i jaziladi
Massivke (n strukturani) jaziw
#include
#include
using namespace std;
double Kvadrat(double x){
double y;
y=pow(x,2);
cout<<"kvadrat(R) ishladi"<
return y;
}
unsigned int Kvadrat(int x){
cout<<"unsigned ishladi"<
return pow(x,2);
}
int main(){
int x1;
unsigned int y1;
cin>>x1;
y1=Kvadrat(x1);
double x2,y2;
cin>>x2;
y2=Kvadrat(x2);
cout<<"y1="<
cout<<"y2="<
return 0;
}
Struktura misal
#include
#include
using namespace std;
int fact(int n){
double y;
if(!n || n==1)
return 1;
else{cout<
return fact(n-1)+n;}
}
int main(){
int n;
cin>>n;
cout<}
Struktura misal
#include
using namespace std;
struct Nuqta {
float x;
float y;
};
int main()
{
int i,n,k=0;
cout<<"n="; cin>>n;
Nuqta a[n];
for(i=0;i
{
cin>>a[i].x;
cin>>a[i].y;
}
for(i=0;i
if(a[i].x>0 && a[i].y>0)
{
cout<<"("<
k++;
}
cout<<"1-chorakdagi nuqtalar soni "<
return 0;
}
Struktura misal
#include
#include
using namespace std;
struct Talaba {
char ism[20];
char guruh[3];
int yil; };
int main(){
int i,n;
cout<<"n="; cin>>n;
Talaba t[n];
for(i=0;i
cout<<"Ism:";
cin>>t[i].ism;
cout<<"guruh";
cin>>t[i].guruh;
cout<<"yil";
cin>>t[i].yil; }
cout<<"1997 yil tug'ilganlar"<
for(i=0;i
if(t[i].yil==1997 && strcmp(t[i].guruh, "1-g")==0) {
cout<<"ism"<
} return 0;
}
Struktura misal
Do'stlaringiz bilan baham: |