Top Banner
1 دا ام خ ن ب ی س ی و ن ه م ا رن ب ان ن زC (21814 ( Lecture 14 Structures
30

بنام خدا زبان برنامه نویسی C (21814 ( Lecture 14 Structures

Jan 25, 2016

Download

Documents

shayla

بنام خدا زبان برنامه نویسی C (21814 ( Lecture 14 Structures. زبان برنامه نویسی C (21814 (. Structures یکی از پرکاربرد ترین روشهای ترکیب کردن داده ها در زبان C استفاده از Structure ها می باشد. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

1

خدا بنام

نویسی برنامه )C (21814زبان

Lecture 14

Structures

Page 2: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

2

نویسی برنامه )C (21814زبان

Structures

ترکیبکردن • روشهای ترین پرکاربرد از یکیزبان در ها از Cداده می Structureاستفاده ها

باشد.

یک • از استفاده روشبرخالفحالت این درها داده توان می ) )آرایه، انواع با ها متغیر

. نمود استفاده جا یک در مختلفرا

یک • بخشهای از یک هر جا این Structureدر. است جداگانه نام دارای ها آرایه برخالف

Page 3: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

3

نویسی برنامه C (21814 ( - Structureزبان

آن • از روشاستفاده این مهم های کاربرد از. باشد اطالعاتیمی بانکهای ایجاد برای

جلسه • این درنمایش – Structureنحوه

آنها – روی به اجرا قابل های عملیاتآنها – با ها آرایه ترکیب نحوه

می قرار بررسی گیرند. مورد

Page 4: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

4

نویسی برنامه C (21814 ( - Structureزبان

از • دهد Structureاستفاده می را امکان این ها / / مورد فرد مختلفیکشی خاصیتهای که. شوند ارایه هم با مرتبط صورت به مطالعه

• : بانک در ورزشکار مختلفیک اطالعات مثال. ورزشی های فدراسیون اطالعاتی

متغیر • متعددی Structureیک تعداد شاملتعریفشده اعضای )slots)فضای که است

Structure میشوند .)members or fields)نامیده

Page 5: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

5

نویسی برنامه C (21814 ( - Structureزبان

اعضای • از یک از Structureهر یکی بیانگر . باشند می نظر مورد خصوصیاتشی

بانکهای • در رفته کار به های زبان دریک یک Structureاطالعاتی، نام نامیده Recordبه

. میشود

Page 6: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

6

نویسی برنامه C (21814 ( - Structureزبان

Structure Declaration

struct tag {

type variable;

type variable;

} variable, variable, …;

optional (but need at least one).

called members

Page 7: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

7

نویسی برنامه C (21814 ( - Structureزبان

شامل Structure typeیک •

–Structure type specification کلمه . structباکه – اختیاری نام می tag nameیک نامیده

شود.داخل } {.– در اعضا تعریف–.) اختیاری ) ها متغیر نام

Page 8: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

8

نویسی برنامه C (21814 ( - Structureزبان

روشتعریف • همانند دقیقا اعضا تعریف نحوه. است متغیر یک

یک • عضو دارای structureدو توانند typeمی. باشند مشابه

یک • اعضای از یک توانند structureهر می. شوند زده صدا مجزا بصورت

به • منحصر نام دارای بایستی اعضا از یک هر. باشند فردی

Page 9: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

9

نویسی برنامه C (21814 ( - Structureزبان

:مثالstruct course }

char * dept; /* e.g. “IE" */char * title; /* e.g. "Intro to C Programming"*/int number; /* e.g. 21814 */

int section; /* e.g. 1 */ {;

• This defines a type only )struct course(, no memory used or variables defined. This is not a declaration!!

Page 10: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

10

نویسی برنامه C (21814 ( - Structureزبان

Variables of type 'course‘

struct course }char * dept; /* e.g. IE */

char * title; /* e.g. Intro to C Programming*/

int number; /* e.g. 21814 */

int section; /* e.g. 1 */

{ c1, c2, c3;

• Defines and Declares three 'course-type' variables.

Page 11: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

11

نویسی برنامه C (21814 ( - Structureزبان

اعضای به دهی مقدار Structure:نحوهc1.dept = "IE"; /* called 'dot' notation */

c1.title = "Intro to C Programming";

c1.number = 21814;

c1.section = 1;

printf)“%s %s %d %d\n”, c1.dept, c1.title, c1.number, c1.section(;

IE Intro to C Programming 21814 1

Page 12: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

12

نویسی برنامه C (21814 ( - Structureزبان

نوع میان آنها Structureتفاوت تعریف نحوه و

struct course } /* this defines the new 'struct course' type */

char * dept; /* e.g. IE */

char * title; /* e.g. Intro to C Programming*/

int number; /* e.g. 21814 */

int section; /* e.g. 1 */

{;

struct course courses[100]; /* an array )collection( of 100 courses */

courses[0].dept = “English”; /* note syntax */

courses[1].dept = “Psychology”; /* etc. */

Page 13: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

13

نویسی برنامه C (21814 ( - Structureزبان

c:10

struct card { char *suit; int val;} c;

c.suit = "clubs";c.val = 10;

&c.suit

&c.val

clubs\0

123456:

123456

Page 14: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

14

نویسی برنامه C (21814 ( - Structureزبان

struct card }

char *suit;

int val;

{ c;

: باشند می هم روشمانند دو اینstruct card }

char *suit;

int val;

{;

struct card c;

Page 15: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

15

نویسی برنامه C (21814 ( - Structureزبان

تعریفیک • در Structureهنگام توان مینام زیر مانند هایی تعریف Structureحالت را

نکرد.struct }

char * name;

char *tel_no;

{ tel_list[100]; /* list of 100 names & telnos */

این • به متغیر نوع یک فقط حالتی چنین در. است تعریفشده صورت

Page 16: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

16

نویسی برنامه C (21814 ( - Structureزبان

تعریفیک • حداقل Structureهنگام نوشتن ،. است ضروری دو این از یکی

struct }

char * name;

char *tel_no;

{;

• This is useless -- no variables of this type and no tag to use to declare variables.

Page 17: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

17

نویسی برنامه C (21814 ( - Structureزبان

typedef declaration

گذاری • نام برای مناسب روشهای از typeیکیاز استفاده موجود .typedefهای باشد می

typedef old_type new_type_name;

• Can be used to rename all current types.• char, int, float, double, struct, and pointers to

these.• Mostly used with struct type

Page 18: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

18

نویسی برنامه C (21814 ( - Structureزبان

typedef struct }

char * dept;

char * title;

int number;

int section;

{ course_t;

/* course_t is new type and can be used in declarations */

int main)void( }

course_t IE21814, cs161, cs162; /* declares 3 courses */

IE21814.title = "Introduction to C Programming";

etc.

Page 19: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

19

نویسی برنامه C (21814 ( - Structureزبان

اعضای به دهی مقدار Structure:نحوه

:مثالtypedef struct Lumber}

char type[11]; //type of wood

int height, width, length;

float price;

int quantity;

{ lumber_t;

lumber_t sale;

lumber_t plank = }“white oak”, 1, 6, 8, 5.82, 158{;

Page 20: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

20

نویسی برنامه C (21814 ( - StructureزبانTypedef or Tag?

typedef struct {char * dept;char * title;int number;int section;

} course_t;

/* 'course_t' is type*/course_t c1, c2, c3;

struct course {char * dept;char * title;int number;int section;

};

/* 'struct course' is type*/struct course c1, c2, c3;

Page 21: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

21

نویسی برنامه C (21814 ( - Structureزبان

توجه:

نویسی برنامه زبان برای Cدر دستوریدو . Structure مقایسه صورت در ندارد وجود

دو مقایسه به برنامه structureنیاز بایستی. داد انجام نویسی

Page 22: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

22

نویسی برنامه C (21814 ( - Structureزبان

typedef struct }

char * dept; char * title;

int number; int section;

{ course_t;

int main)void ( }

course_t c, *p;

p = &c; /* dept member in struct pointed to by p */

p->dept = "cs";

p->number = 151;

printf)"%s %d", c.dept, c.number(;

return 0;

{

prints:cs 151

Page 23: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

23

نویسی برنامه C (21814 ( - Structureزبان

:مثالtypedef struct Lumber}

char type[11]; int height, width, length;

float price; int quantity;

{ lumber_t;

lumber_t plank = }“white oak”, 1, 6, 8, 5.82, 158{;

float cost;

Lumber_t *p;

P = &plank;

Cost = p->price;

Page 24: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

24

نویسی برنامه C (21814 ( - Structureزبان

یک اعضای به دستیابی :structureروشهایمستقیم – دسترسی

printf)“ %i \n “,plank.height(;

از – از : pointerاستفاده روش این arrowدر

operator اعضای به دسترسی structureبرای. شود می استفاده

printf) “ %i \n”, p-> height(;

Page 25: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

25

نویسی برنامه C (21814 ( - Structureزبان

یک مقادیر انتقال :structureروش دیگری به

typedef struct Lumber}

char type[11]; int height, width, length;

float price; int quantity;

{ lumber_t;

lumber_t sale;

lumber_t plank = }“white oak”, 1, 6, 8, 5.82, 158{;

sale = plank;

Page 26: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

26

نویسی برنامه C (21814 ( - Structureزبان

انتقال به structureنحوه :functionها ها توان • و structureمی به pointerها که هایی

عنوان structureیک به را کنند می اشارهargument یک . functionهای برد کار به

یک • که آنجا متعددی structureاز اجزای دارایکه است بهتر باشد، تواند آن pointerمی

. شود استفاده

Page 27: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

27

نویسی برنامه C (21814 ( - Structureزبان

typedef struct } /* note: comes before print_card prototype */char * suit;int val;

{ card_t; /* function prototype uses 'card' type */

void print_card)card_t c(;

int main)void( }card_t c = }"clubs", 10{; /* note: initialization! */print_card)c(; /* call by value */return 0;

{

void print_card)card_t c( } /* this c is a copy of the c in main */printf)"%d of %s\n", c.val, c.suit(;

{

Page 28: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

28

نویسی برنامه C (21814 ( - Structureزبان

typedef struct } char * suit; int val; { card_t;

void print_card)card_t c(;void init_card)card_t *p, char * suit, int val(;

int main)void( }card_t c;init_card)&c, "clubs", 10(; /* call by reference */print_card)c(; /* call by value */return 0;

{void print_card)card_t c( }

printf)"%d of %s\n", c.val, c.suit(;{void init_card)card_t *p, char * suit, int val(} /* p points to c in

main */p->suit = suit;p->val = val;

{

Page 29: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

29

نویسی برنامه C (21814 ( - Structureزبان

:تمرینبه دانشجویان، لیست که بنویسید ای برنامه

ورودی فایل یک از را مربوطه اطالعات همراهمیان های واریانسامتحان و میانگین گرفته،

. کاربر سپساز کند محاسبه را ترم پایان و ترمنام ) نام، برحسب را لیست کردن مرتب نحوه

ترم، میان نمره دانشجویی، شماره خانوادگی، . ) برنامه این در نماید مرتب ترم پایان نمره یا و

یک در را دانشجویان ذخیره structureاطالعاتکنید.

Page 30: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 14 Structures

30

نویسی برنامه )C (21814زبان

:تمرینبصورت تابع یک زدن روشصدا ترین مناسب

prototype void myFun( student_t x);

است ؟کدام )myFun )Ali_Jamshidi الف-

.bmyFun )&Ali_Jamshidi(

.emyFun )student_t Ali_Jamshidi(

)myFun )*Ali_Jamshidi د-