1 / 32

بین همه عشقای دنیا عشق است ابالفضل

بین همه عشقای دنیا عشق است ابالفضل. فروشگاه محصولات نرم افزاری و آموزشی سیب سرخ فیض آبادی09157711812. حالت اول توصيه ميشود. name. id. avg. struct student { char name[10] ; // char *name ; float avg ; int id ; } s , c [10] , *st , *my_class[10] ;

farrah
Download Presentation

بین همه عشقای دنیا عشق است ابالفضل

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. بین همه عشقای دنیا عشق است ابالفضل فروشگاه محصولات نرم افزاری و آموزشی سیب سرخ فیض آبادی09157711812 Shop.sibsorkh.ir

  2. حالت اول توصيه ميشود. name id avg struct student { char name[10] ; // char *name ; float avg ; int id ; } s , c [10] , *st , *my_class[10] ; student x ; // struct student x ; x.avg = 19.5 ; student *p ; // struct student *p ; (*p).avg = 18.75 ; // p->avg = 18.75 ; ساختار يا ساختمان يا ركورد ( Structure ) : براي دريافت فيلدها توصيه ميشود از gets استفاده نمائيم . char *s_avg; s_avg = (char *) malloc(10); gets (s_avg ); x.avg = atof (s_avg ); p->avg = atof (s_avg ); Shop.sibsorkh.ir

  3. مقدار دهي اوليّه struct : student st = {“ali”, 17.5, 123} ; student st = {“ali”} ; student st = {0} ; student st = {}; مجاز نيست student *s = {“ali”} ; مجاز نيست ساختار در ساختار : struct date { unsigned char m, d ; unsigned y; } ; struct employ { char name[20] ; date B_D; } e1 , e2 ; Shop.sibsorkh.ir

  4. s f union id { char s[10] ; long f ; } ; id x , *p ; يونيون يا اشتراك ( union ) : فضاي مورد نياز= فضاي مورد نياز براي بزرگترين فيلد ساختارهاي بيتي : struct date { unsigned int m : 4 , d : 5 ; unsigned int y : 11; } ; Shop.sibsorkh.ir

  5. ar n ; // int n[10]; student s ; // C در همه نسخه هاي typedef float real ; typedef int ar[10] ; تعريف نوع داده ( typedef ) : typedef char string[256] ; typedef struct { char name[10]; float avg ; } student ; typedef union { char s[10] ; long l ; } un ; Shop.sibsorkh.ir

  6. #include <stdio.h> #include <conio.h> void write_struct ( char n[] , float a ) { printf("%s %5.2f", n, a); } void main() { struct student { char name[10]; float avg; } s={"ali",12.5}; write_struct( s.name, s.avg ); } ارسال ساختار يا يونيون به توابع : الف ) ارسال فيلدها بطور مجزا Shop.sibsorkh.ir

  7. #include <stdio.h> #include <conio.h> struct student { char name[10]; float avg; }; void write_struct ( student st ) { printf("%s %5.2f", st.name, st.avg); } void main() { student s={"ali",12.5}; write_struct( s ); } ب ) ارسال مقدار ساختار Shop.sibsorkh.ir

  8. #include <stdio.h> #include <conio.h> #include <string.h> struct student { char name[10]; float avg; }; void rev_struct_str ( student *st ) { strrev(st->name); } void main() { student s={"ali",12.5}; rev_struct_str( &s ); printf("%s %5.2f", s.name, s.avg); } ج ) ارسال ساختار بصورت ارجاع Shop.sibsorkh.ir

  9. #include <stdio.h> #include <conio.h> #include <string.h> struct student { char name[10]; float avg; }; student rev_struct_str ( student st ) { strrev(st.name); return(st); } void main() { student s = {"ali",12.5}; s = rev_struct_str( s ); printf("%s %5.2f", s.name, s.avg); } مقدار بازگشتي تابع ميتواند ساختار باشد : Shop.sibsorkh.ir

  10. نوع داده شمارشي ( Enumerated ) : enum rgb_color { red , green , blue } ; rgb_color fg , bg ; مقادير بايد از قواعد نامگذاري شناسه ها پيروي كنند و ميتوانند ثابتهاي از قبل تعيين شده باشند . قابل خواندن از ورودي و نوشتن در خروجي نيستند. مگر در قالب عدد صحيح . برنامه را خواناتر مي كنند . enum weekday { sat , sun , mon , tue , wed , thu , fri }; Shop.sibsorkh.ir

  11. st next next next next struct student { char name[10] ; float avg ; student *next ; } *st , *head ; ساختارهاي خود ارجاع : struct node { long num ; node *next ; } ; node *list , *head ; list head Shop.sibsorkh.ir

  12. struct node { float avg; node *next ; } *list , *head ; const l = sizeof (node) ; void add_list(float n) { if (!head) head=list=(node *)malloc(l); else { list->next=(node *)malloc(l); list = list->next ; } list->avg = n ; list->next = NULL ; } ليست پيوندي ( Linked List ) : Shop.sibsorkh.ir

  13. void write_list() { while ( list != NULL ) { printf("%5.2f ",list->avg); list = list->next; } } void main() { head = NULL ; float n; do { scanf("%f",&n); if ( n != 0 ) add_list(n); } while ( n != 0 ) ; list = head ; write_list(); } Shop.sibsorkh.ir

  14. کلاس Class : Syntax : class classname [:baselist] { member list } <classname> can be any name unique within its scope. <baselist> lists the base class(es) that this class derives from. <baselist> is optional <member list> declares the class's data members and member functions. Shop.sibsorkh.ir

  15. #include <iostream.h> class circle { private : float radius; //Data Member public : double area( ); //Member Function void set_radius(float r); }; double circle::area( ) { return(3.14159*radius*radius); } void circle::set_radius(float r=10) { radius=r; } void main( ) { circle c; // circle *c=new circle( ); c.set_radius(10); // c  set_radius(10); // c  set_radius( ); cout<<c.area( ); // cout << c  area( ); } Shop.sibsorkh.ir

  16. #include <iostream.h> class circle { private : float radius; public : double area( ) { return(3.14159*radius*radius); } void set_radius ( float r=10 ) {radius = r; } }; void main( ) { circle c; // c is an object c.set_radius(10); cout << c.area( ); } Shop.sibsorkh.ir

  17. #include <iostream.h> class circle { private : float radius; public : double area( ) {return(3.14159*radius*radius);} circle(float); // Constructor }; circle::circle(float r=10) { radius=r; } void main( ) { circle c; // circle c(5) ; // circle *c=new circle(5); cout << c.area( ) ; // cout<<carea(); } سازنده (Constructor) : Shop.sibsorkh.ir

  18. #include <iostream.h> class circle { protected : float radius; public : double area(){ return(3.14159*radius*radius);} circle(float r=10){radius=r;}; ~circle() {cout << "\nDestroy object“ ; } //Destructor }; void main() { circle c(5); cout<<c.area(); } مخرب ها (Destructor) : 78.5397 Destroy object Shop.sibsorkh.ir

  19. #include <iostream.h> class circle { private : float radius; public : double area(){ return(3.14159*radius*radius);} circle(float r=10){ radius=r;} }; class cylinder : public circle { private : float height; public : double volume(){ return (area( )*height); } cylinder (float h=5) { height=h;} }; void main() { cylinder c; cout<<c.volume(); } وراثت و کلاسهای پایه (based) و کلاسهای مشتق شده (derived): // return(3.14159*radius*radius*height); این کد دارای خطاست ، زیرا radius ، private است. درصورتی که protected می بود ، عبارت درست بود. Shop.sibsorkh.ir

  20. #include <iostream.h> class circle { private : float radius; public : double area(){ return(3.14159*radius*radius);} circle(float r=10){ radius=r;} }; class cylinder : private circle { private : float height; public : double volume(){ return (area( )*height); } cylinder (float h=5) { height=h;} }; class test : public cylinder{ // radius , area is not accessible }; void main() { test c; cout<<c.volume(); cout<<c.area();// Error : ‘circle::area()’ is not accessible in function main() } وراثت از کلاس بصورت private : برای این کلاس، private تفاوتی با public ندارد. ولی برای کلاسی که از این کلاس مشتق می شود، همه اعضای کلاس پایه (circle) غیرقابل دسترس هستند. Shop.sibsorkh.ir

  21. #include <iostream.h> class circle { protected : float radius; public : double area(){ return(3.14159*radius*radius);} circle(float r=10){ radius=r;} }; class cylinder : protected circle { private : float height; public : double volume(){ return (area( )*height); } cylinder (float h=5) { height=h;} }; class test : public cylinder{ double func(){return radius*area();} // radius, area is protected and accessible }; void main() { test c; cout<<c.volume(); cout<<c.area();// Error : ‘circle::area()’ is not accessible in function main() } وراثت از کلاس بصورت protected: اگردرکلاس circle بصورت private تعریف شده بود،غیرقابل دسترس Shop.sibsorkh.ir

  22. #include <iostream.h> class circle { friend void main( ); protected : float radius; // private : float radius; public : double area() { return(3.14159*radius*radius);} circle (float r=10) { radius=r; } }; void main() { circle c; c.radius=5; // Error without friend void main( ) cout<<c.area(); } توابع دوست (friend) : Shop.sibsorkh.ir

  23. #include <iostream.h> class circle { friend class cylinder; protected : float radius; public : double area(){ return(3.14159*radius*radius);} circle(float r=10){radius=r;} }; class cylinder { private : float height; circle c; public : double volume(){ return (c.area( )*height); } cylinder (float h=5,float r=10) {c.radius=r; height=h;} }; void main() { cylinder c; cout<<c.volume(); } کلاسهای دوست (friend) : Shop.sibsorkh.ir

  24. FILE *f , *in , *out ; f = fopen (“نحوهبازكردن” , “نامخارجي” ) ; if (f==NULL) puts(“File Not Open “); if (!f) puts(“File Not Open “); فايلها ( Files ) : فايل Text : ا نتهاي سطر ( cr / lf ) - ا نتهاي فايل EOF) يا كاراكتر 26( - عدد 123 سه بايت فايل Binary : ا نتهاي سطر ( lf ) - ا ندازه فايل در FAT فايلهاي با دسترسي ترتيبي - فايلهاي با دسترسي تصادفي يا مستقيم نام خارجي :مثل “class.dat” و “c:\\a.txt” Shop.sibsorkh.ir

  25. FILE *in , *out ; in = fopen (“class.txt”,”r”) ; if (!in) puts (“File Not Open For Input...“); out = fopen (“myfile.dat”,”wb”); if (!out) puts (“File Not Open For Output...“); نحوه بازكردن فايلها : فايل BINARY : : “rb” خواندن “wb” : نوشتن “ab” : افزودن “r+b” و “w+b” و “a+b”:خوا ندن و نوشتن فايل : TEXT “r” يا : “rt” خواندن “w” يا : “wt” نوشتن “a” يا : “at” افزودن “r+” و “w+” و: “a+” خوا ندن و نوشتن Shop.sibsorkh.ir

  26. چند تابع روي فايلها : 1- feof( f ) : پايان فايل ( ابتدا بايد از فايل خوانده شود ) 2-fclose( f ) : بستن فايل 3- rewind( f ) : رفتن به ابتدا 4- ftell( f ) : موقعيت (بايت) 5- fcloseall( ) : بستن فايلها 6- fflush( f ) : انتقال بافر به فايل 7- flushall( ) : انتقال بافرها به فايلها 8- remove( External_File_Name) :حذف فايل 9- fseek( f, o, w ) : انتقال اشاره گر فايل–w=0 (آغازفايل) وw=1 ( موقعيت فعلي) و w=2 (پايان فايل) - o (فاصله به بايت) Shop.sibsorkh.ir

  27. void main( ) { FILE *f; char c; f=fopen("test.txt","w"); while((c=getchar())!='~') putc(c,f); fclose(f); } void main( ) { FILE *f; f=fopen("test.txt","r"); char c=getc(f); while (!feof(f)) { putchar(c); c=getc(f); } } چند دستور ورودي و خروجي فايلها : 1-int putc(int ch , FILE *f ) : عدم موفقيتEOF وگرنهch برميگردد 2-int getc( FILE *f ) : عدم موفقيتEOF برميگردد Shop.sibsorkh.ir

  28. void main( ) { FILE *f; char s[10]; f=fopen ("class.txt","w"); while (1) { gets(s); if (strcmp(s,"")==0) break; strcat(s,"\n"); fputs(s,f); } fclose(f); } void main() { FILE *f; char s[10]; f=fopen("class.txt","r"); fgets(s,10,f); while (!feof(f)) { puts(s); fgets(s,10,f); } } 3- : int fputs( char *s , FILE *f ) در هر صورت رشته ها پشت سر هم قرار ميگيرند . مگر: gets(s); strcat( s,”\n”); fputs(s, f); 4- : char *fgets( char *s , int l , FILE *f ) در s حداكثر L-1 كاراكتر Shop.sibsorkh.ir

  29. void main() { FILE *f; char *s; f=fopen("class.txt","w+"); for (int i=1;i<=3;++i) { gets(s); fprintf(f,"%s\n",s); } rewind(f); //fseek(f,0,0); fscanf(f,"%s",s); while (!feof(f)) { puts(s); fscanf(f,"%s",s); } } 5- : fprintf ( FILE *f ,”Format”, x ) بهتر است هرداده در يك سطر 6- fscanf ( FILE *f ,”Format”,&x ) : در مورد رشته & لازم نيست ahmad reza mohammad ahmad reza mohammad Shop.sibsorkh.ir

  30. void main() { FILE *f; float s; f=fopen("class.dat","wb"); for (int i=1;i<=3;++i) { scanf("%f",&s); fprintf(f,"%f ",s); } fclose(f); f=fopen("class.dat","rb"); fscanf(f,"%f",&s); while (!feof(f)) { printf("%5.2f\n",s); fscanf(f,"%f",&s); } } 19.5 17.75 14 19.50 17.75 14.00 Shop.sibsorkh.ir

  31. struct std { char name[10]; float avg; } st; void main() { FILE *f; char *avgs; if ((f=fopen("class.dat","w+b"))==NULL ) { printf("Error in Opening File..."); exit(0); } 7- fwrite (&x‚ Size_x, Num, f ) : براي كاراكترfwrite(&x, 1, n, f ) 8- fread ( &x‚ Size_x, Num, f ) : براي آرايه & لازم نيست Shop.sibsorkh.ir

  32. while(1) { printf("Name :"); gets(st.name); if (st.name[0]==NULL) break; printf("Avg :"); gets(avgs); st.avg=atof(avgs); fwrite(&st,sizeof(std),1 ,f); } fflush(f); rewind(f); float s=0; int t=0; fread(&st,sizeof(std),1,f); while( !feof(f)) { s+=st.avg; ++t; fread(&st,sizeof(std),1,f); } printf("Class Avg=%5.2f",s/t); fclose(f); } Shop.sibsorkh.ir

More Related