1. Trang chủ
  2. » Công Nghệ Thông Tin

Tin học cơ sở 3 pot

26 1,6K 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 26
Dung lượng 367 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

• 2 nd aggregate data type: struct • Recall: aggregate meaning "grouping" – Recall array: collection of values of same type – Structure: collection of values of different types • Treated

Trang 1

Tin h c c s 3 ọ ơ ở

Structure

Trang 2

Outline

Trang 3

2 nd aggregate data type: struct

Recall: aggregate meaning "grouping"

– Recall array: collection of values of same type – Structure: collection of values of different types

Treated as a single item, like arrays

Major difference: Must first "define"

struct

– Prior to declaring any variables

Trang 4

– Heterogeneous (components are different types) – Fixed-size (well-defined set of components)

Trang 5

Structured Data Types

variables, perhaps of different

types, grouped together under a

Trang 6

Structured Data Types

– Just a “placeholder” for what struct will “look like”

– Name of struct

– Name of each field

– Type of each field (could be another

record/struct)

Trang 7

Defining Struct

Defining a new data type using struct

struct date  name of new "type"

struct date yesterday;

Once defined, this new data type could be used as other data types:

–Variable of this type could be assigned to another variable of the same type

–Variable of this type could be passed as a parameter to a function

Trang 8

Declare Structure Variable

variables of this new type:

struct date today;

– Just like declaring simple types

– Variable today now of type struct date

– It contains "member values"

• Each of the struct "parts"

Trang 9

Accessing Structure Members

Dot Operator to access members

today.day

today.month

today.year

Called "member variables"

– The "parts" of the structure variable

– Different structs can have same name member

variables

• No conflicts

Trang 10

Defining New Type Struct

struct

typedef struct date Date;

basic type without the need of

Trang 11

Structure Example

void printData(Date today)

{

printf("The day is: %i", today.day);

printf("The month is: %i", today.month); printf("The year is: %i", today.year); }

void getData(Date *today);

Trang 13

Structure Assignments

Given structure named Fruit

Declare two structure variables:

Fruit apples, oranges;

– Both are variables of type "Fruit"

– Simple assignments are legal:

apples = oranges;

• Simply copies each member variable from apples

into member variables from oranges

Trang 14

Structure Other Operations

Operations which are NOT defined for struct type:

– compare for equality/inequality

(i.e apples == oranges is not a legal expression)

– compare based on ordering (<, >, )

(i.e apples < oranges is not a legal expression)

– arithmetic operations

(i.e apples + oranges is not a legal expression)

– reading from or writing to text files

(i.e no printf(apples) no scanf(&oranges))

If you need such operations, write your own functions.

Trang 15

Structure and Pointer

– (struct x)* is a pointer to struct x

– & gives the address of a structure

binds more tightly than *

field of the struct:

– *p.a means *(p.a) which is ILLEGAL.

– You must use (*p).a

– For convenience , the operator -> combines indirection and

component selection.

– p->a is equivalent to (*p).a

Trang 16

Structure and Pointer

ap->x = 0; /* right */

Trang 17

Structure and Pointer

void getData(Date *today)

Trang 18

Structure as Function Argument

– Pass-by-value – Pass-by-reference – Or combination

– Return-type is structure type – Return statement in function definition sends structure variable back to caller

Trang 19

Other Type Definition

We can use the keyword typedef to

make our own definitions:

typedef float Floating;

This means variables can be declared

as Floating but they will actually be

of type float.

If we later decide we need more

precision, we can change to:

typedef double Floating;

without changing the codes

Trang 20

Combining struct and typedef

struct employee { char name[30];

Note: we use the convention that the name of the

defined type is the same as the struct modifier,

but with the first letter capitalized.

Trang 21

Self-referential types

A very powerful programming technique

is to create struct with fields which contain a reference to an object in the same struct For example:

Trang 22

Linked Lists

Consider the following data

structure:

– A list that grow over time

– Items are added one by one

– Each item is a number

It might grow like this:

Trang 23

Linked Lists

How do you implement such a list in C++?

You can use an array but you must check the array doesn't overflow and allocate memory for a new larger array if the array fills.

Can be implemented using a self-referential type.

Each element in the linked list need:

– some information (the data)

– a connection to the next item

The individual elements in data structures like this are often called nodes.

Trang 24

struct list_node *list_start;

The next field of the struct will be NULL.

Trang 25

Linked Lists

Ngày đăng: 08/03/2014, 00:20

TỪ KHÓA LIÊN QUAN

w