<< Chapter < Page Chapter >> Page >

printf("%s | %s | %d-%d-%d | %s\n",s.MSSV,s.HoTen,

s.NgaySinh.Ngay,s.NgaySinh.Thang,s.NgaySinh.Nam,s.DiaChi);

}

int main()

{

SinhVien SV, s;

printf("Nhap MSSV: ");gets(SV.MSSV);

printf("Nhap Ho va ten: ");gets(SV.HoTen);

printf("Sinh ngay: ");scanf("%d",&SV.NgaySinh.Ngay);

printf("Thang: ");scanf("%d",&SV.NgaySinh.Thang);

printf("Nam: ");scanf("%d",&SV.NgaySinh.Nam);

printf("Gioi tinh (0: Nu), (1: Nam): ");scanf("%d",&SV.Phai);

flushall();

printf("Dia chi: ");gets(SV.DiaChi);

InSV(SV);

s=SV;/* Gán trị cho mẩu tin s*/

InSV(s);

getch();

return 0;

}

Lưu ý:

- Các biến cấu trúc có thể gán cho nhau. Thực chất đây là thao tác trên toàn bộ cấu trúc không phải trên một trường riêng rẽ nào. Chương trình trên dòng s=SV là một ví dụ.

- Với các biến kiểu cấu trúc ta không thể thực hiện được các thao tác sau đây:

  • Sử dụng các hàm xuất nhập trên biến cấu trúc.
  • Các phép toán quan hệ, các phép toán số học và logic.

Ví dụ: Nhập vào hai số phức và tính tổng của chúng. Ta biết rằng số phức là một cặp (a,b) trong đó a, b là các số thực, a gọi là phần thực, b là phần ảo. (Đôi khi người ta cũng viết số phức dưới dạng a + ib trong đó i là một đơn vị ảo có tính chất i2=-1). Gọi số phức c1=(a1, b1) và c2=(a2,b2) khi đó tổng của hai số phức c1 và c2 là một số phức c3 mà c3=(a1+a2, b1+b2). Với hiểu biết như vậy ta có thể xem mỗi số phức là một cấu trúc có hai trường, một trường biểu diễn cho phần thực, một trường biểu diễn cho phần ảo. Việc tính tổng của hai số phức được tính bằng cách lấy phần thực cộng với phần thực và phần ảo cộng với phần ảo.

#include<conio.h>

#include<stdio.h>

#include<string.h>

typedef struct

{

float Thuc;

float Ao;

} SoPhuc;

/* Hàm in số phức lên màn hình*/

void InSoPhuc(SoPhuc p)

{

printf("%.2f + i%.2f\n",p.Thuc,p.Ao);

}

int main()

{

SoPhuc p1,p2,p;

clrscr();

printf("Nhap so phuc thu nhat:\n");

printf("Phan thuc: ");scanf("%f",&p1.Thuc);

printf("Phan ao: ");scanf("%f",&p1.Ao);

printf("Nhap so phuc thu hai:\n");

printf("Phan thuc: ");scanf("%f",&p2.Thuc);

printf("Phan ao: ");scanf("%f",&p2.Ao);

printf("So phuc thu nhat: ");

InSoPhuc(p1);

printf("So phuc thu hai: ");

InSoPhuc(p2);

p.Thuc = p1.Thuc+p2.Thuc;

p.Ao = p1.Ao + p2.Ao;

printf("Tong 2 so phuc: ");

InSoPhuc(p);

getch();

return 0;

}

Kết quả thực hiện chương trình:

Khởi tạo cấu trúc

Việc khởi tạo cấu trúc có thể được thực hiện trong lúc khai báo biến cấu trúc. Các trường của cấu trúc được khởi tạo được đạt giữa 2 dấu { và }, chúng được phân cách nhau bởi dấu phẩy (,).

Ví dụ: Khởi tạo biến cấu trúc NgaySinh:

struct NgayThang NgaySinh ={29, 8, 1986};

Con trỏ cấu trúc

Khai báo

Việc khai báo một biến con trỏ kiểu cấu trúc cũng tương tự như khi khai báo một biến con trỏ khác, nghĩa là đặt thêm dấu * vào phía trước tên biến.

Cú pháp: struct<Tên cấu trúc>*<Tên biến con trỏ>;

Ví dụ: Ta có thể khai báo một con trỏ cấu trúc kiểu NgayThang như sau:

struct NgayThang *p;

/* NgayThang *p; // Nếu có định nghĩa kiểu */

Sử dụng các con trỏ kiểu cấu trúc

Khi khai báo biến con trỏ cấu trúc, biến con trỏ chưa có địa chỉ cụ thể. Lúc này nó chỉ mới được cấp phát 2 byte để lưu giữ địa chỉ và được ghi nhận là con trỏ chỉ đến 1 cấu trúc, nhưng chưa chỉ đến 1 đối tượng cụ thể. Muốn thao tác trên con trỏ cấu trúc hợp lệ, cũng tương tự như các con trỏ khác, ta phải:

- Cấp phát một vùng nhớ cho nó (sử dụng hàm malloc() hay calloc)

- Hoặc, cho nó quản lý địa chỉ của một biến cấu trúc nào đó.

Ví dụ: Sau khi khởi tạo giá trị của cấu trúc:

struct NgayThang Ngay = {29,8,1986};

p =&Ngay;

lúc này biến con trỏ p đã chứa địa chỉ của Ngay.

Truy cập các thành phần của cấu trúc đang được quản lý bởi con trỏ

Để truy cập đến từng trường của 1 cấu trúc thông qua con trỏ của nó, ta sử dụng toán tử dấu mũi tên (->: dấu - và dấu>).

Ngoài ra, ta vẫn có thể sử dụng đến phép toán * để truy cập vùng dữ liệu đang được quản lý bởi con trỏ cấu trúc để lấy thông tin cần thiết.

Ví dụ: Sử dụng con trỏ cấu trúc.

#include<conio.h>

#include<stdio.h>

typedef struct

{

unsigned char Ngay;

unsigned char Thang;

unsigned int Nam;

} NgayThang;

int main()

{

NgayThang Ng={29,8,1986};

NgayThang *p;

clrscr();

p=&Ng;

printf("Truy cap binh thuong %d-%d-%d\n",

Ng.Ngay,Ng.Thang,Ng.Nam);

printf("Truy cap qua con tro %d-%d-%d\n",

p->Ngay,p->Thang,p->Nam);

printf("Truy cap qua vung nho con tro %d-%d-%d\n",

(*p).Ngay,(*p).Thang,(*p).Nam);

getch();

return 0;

}

Kết quả:

Bài tập

Mục đích yêu cầu

Làm quen và biết cách sử dụng kiểu dữ liệu cấu trúc kết hợp với các kiểu dữ liệu đã học. Phân biệt kiểu dữ liệu mảng và kiểu cấu trúc. Thực hiện các bài tập trong phần nội dung.

Nội dung

1. Hãy định nghĩa kiểu:

struct Hoso{

char HoTen[40];

float Diem;

char Loai[10];

};

Viết chương trình nhập vào họ tên, điểm của n học sinh. Xếp loại văn hóa theo cách sau:

ĐiểmXếp loại

9, 10 Giỏi

7, 8 Khá

5, 6 Trung bình

dưới 5 Không đạt

In danh sách lên màn hình theo dạng sau:

XEP LOAI VAN HOA

HO VA TEN DIEM XEPLOAI

Nguyen Van A 7 Kha

Ho Thi B 5 Trung binh

Dang Kim C 4 Khong dat

........................................................................................................

2. Xem một phân số là một cấu trúc có hai trường là tử số và mẫu số. Hãy viết chương trình thực hiện các phép toán cộng, trừ, nhân, chia hai phân số. (Các kết quả phải tối giản ).

3. Tạo một danh sách cán bộ công nhân viên, mỗi người người xem như một cấu trúc bao gồm các trường Ho, Ten, Luong, Tuoi, Dchi. Nhập một số người vào danh sách, sắp xếp tên theo thứ tự từ điển, in danh sách đã sắp xếp theo mẫu sau:

DANH SACH CAN BO CONG NHAN VIEN

| STT | HO VA TEN | LUONG | TUOI | DIACHI |

| 1 | Nguyen Van A | 333.00 | 26 | Can Tho |

| 2| Dang Kim B | 290.00 | 23 | Vinh Long |

Questions & Answers

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Cấu trúc dữ liệu. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10766/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Cấu trúc dữ liệu' conversation and receive update notifications?

Ask