課程-集合與結構_第1頁
課程-集合與結構_第2頁
課程-集合與結構_第3頁
課程-集合與結構_第4頁
課程-集合與結構_第5頁
免費預覽已結束,剩余25頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

結構由數目固定的成員構成各成員可以具有不同的數據類型一個結構變量在內存占有一片連續的 空間5.3

結構struct

標識符{類型成員1

;類型成員2

;…類型成員n

;}

;5.3.1

定義結構結構類型定義形式為:例:struct

employee{

char name

[

10

]

;long

code

;double salary

;r

address

[

50

]

;char phone

[

20

]

;}

;類型名5.3.1定義結構可以用不同方法定義一個結構變量(1)

類型之后 變量例:struct

employee{

char

name

[

10

]

;long

code

;double

salary;char

address

[

50

]

;char

phone

[

20

]

;}

;employee

worker1,

worker2,

*Emp

;5.3.1定義結構例:struct

employee{

char name

[

10

]

;long

code

;double salary

;char

address

[

50

]

;char phone

[

20

]

;}worker1,

worker2,

*Emp

;可以用不同方法定義一個結構變量類型之后

變量類型的同時 變量5.3.1定義結構}worker1,

worker2,

*Emp

;可以用不同方法定義一個結構變量類型之后

變量類型的同時 變量直接

結構類型變量例:struct

employee{

char name

[

10

]

;long

code

;double salary

;char

address

[

50

]

;char phone

[

20

]

;5.3.1定義結構例:struct

employee{

char name

[

10

]

;long

code

;double salary

;char

address

[

50

]

;char phone

[

20

]

;}

;employee

worker1,

worker2,*Emp

=

&worker1

;Wang

Li9910834561200.5guang

zhou87111111說明(1)結構變量占有一片連續內存空間,具有結構類型的特征Emp

worker15.3.1定義結構struct

date{

int month

;int

day

;int

year

;}

;struct

employee{

char name

[

10

]

;date

birthday

;long code

;double salary

;char

address

[

50

]

;char phone

[

11

]

;} worker1,

worker2

;說明(2)一個結構類型的成員可以是另一個已定義的結構類型例如:為職工結構添加出生日期信息類型和變量 為:son

;5.3.1定義結構說明(2)一個結構類型的成員可以是另一個已定義的結構類型struct{

char name

[

10

]

;long code

;double salary

;char

address

[

50

]

;char phone

[

11

]

;} worker1,

worker2

;錯誤不能實現的無窮遞歸結構5.3.1定義結構說明(3)

結構類型變量可以同時初始化struct

employee{

char

name

[

10

]

;long code

;double salary

;char

address

[

50

];char

phone

[

11

];}

worker

=

{"Wang

Li

"

,

991083456,

1200.5,

"guang

zhou

"

,"

87111111

"

}

;結構變量.成員點運算符(1)

結構變量的成員//例5-8#

include

<iostream>using

namespace

std

;struct

weather//結構類型double

wind; }

;{

double

temp;int

main

(){

weather

today

;//結構類型變量today

.

temp

=

10.5

;//對結構變量成員賦值today

.

wind

=

3.1

;cout

<<

“Temp

=

<<

today

.

temp

<<

endl

;//按成員輸出cout

<<

“Wind

=

<<

today

.

wind

<<

endl

;}結構變量成員5.3.2結構結構指針->成員(*結構指針).成員(2)用指針//例5-9double

salary;

}

;#

include

<iostream>using

namespace

std

;#

include

<cstring>struct{

char

name[20]

; unsigned

long

id;int

main(

){//定義結構指針//取結構變量地址//對結構成員賦值cout

<<

pp

->

name

<<

‘\t’

<<

pp

->

id

<<‘\t’

<<

pp

->

salary

<<

endl

;}pr1

;**

pppp

;;pp

=

&

pr1

;strcpy

(

pp

->

nnaame

,

“David

Marat”

)

;pp

->

id

=

987654321

;pp->

salary

=

335.0

;5.3.2結構結構變量的成員結構指針->成員(*結構指針).成員(2)用指針//例5-9double

salary;

}

;#

include

<iostream>using

namespace

std

;#

include

<cstring>struct{

char

name[20]

; unsigned

long

id;int

main(

){pr1

;*

pp

;pp

=

&

pr1

;//定義結構指針//取結構變量地址//對結構成員賦值cout

<<

pp

->

name

<<

‘\t’

<<

pp

->

id

<<‘\t’

<<

pp

->

salary

<<

endl

;}strcpy(

p(*pp-p>).nnaame

,

“David

Marat”

)

;(p*pp>).

id

=

987654321

;(p*pp->).salary

=

335.0

;5.3.2結構結構變量的成員today

=

yesterday

;//結構變量整體賦值cout

<<

“Temp

=

<<

today

.

temp<<

endl

;cout

<<

“Wind

=

<<

today

.

wind

<<

endl

;}(3)類型相同的結構變量可以整體賦值//例5-10#

include

<iostream>using

namespace

std

;struct

weather{

double

temp;

double

wind; }

yesterday

;int

main

(){

weather

today

;yesterday

.

temp

=

10.5

;yesterday

.

wind

=

3.1

;5.3.2

結構double

wind; }

yesterday

;例如:struct

weather1{

double

temp;struct

weather2{

double

temp;double

wind; }

today

;(3)類型相同的結構變量可以整體賦值“類型相同的變量”是指用同一類型標識符說明的變量yesterday

和today盡管成員相同,但不是同類型變量不可以整體賦值5.3.2結構(4)結構類型參數函數的結構類型參數可以傳值、指針及參數5.3.2結構#

include

<iostream>using

namespace

std

;struct

weather{

double

temp;

double

wind; }

;void

funstu(weather

w){

w.temp=15;w.wind=2.3;cout

<<

"

fun:\nTemp

=

"

<<

w.temp

<<

ecout

<<

"Wind

=

"

<<

w.wind

<<

endl

;}int

main

(

){

weather

day

;day

.

temp

=

10.5

;day

.

wind

=3.1

;cout

<<

"1:\nTemp

=

"

<<

day.temp

<<

ecout

<<

"Wind

=

"

<<

day.wind

<<

endl

;funstu(day);cout

<<

"

2:\nTemp

=

"

<<

day.temp<<

ecout

<<

"Wind=

"

<<

day.wind

<<

endl

;}傳值#

include

<iostream>using

namespace

std

;struct

weather{

double

temp;

double

wind; }

;void

funstu(weather

&w){

w.temp=15;w.wind=2.3;cout

<<

"

fun:\nTemp

=

"

<<

w.temp

<<encout

<<

"Wind

=

"

<<

w.wind

<<

endl

;}int

main

(

){

weather

day

;day

.

temp

=

10.5

;day

.

wind

=3.1

;cout

<<

"1:\nTemp

=

"

<<

day.temp

<<encout

<<

"Wind

=

"

<<

day.wind

<<

endl

;funstu(day);cout

<<

"

2:\nTemp

=

"

<<

day.temp<<

encout

<<

"Wind=

"

<<

day.wind

<<

endl

;}參數#

include

<iostream>using

namespace

std

;struct

weather{

double

temp;

double

wind; }

;void

funstu(weather

*w){

w->temp=15;w->wind=2.3;cout

<<

"

fun:\nTemp

=

"

<<

w->temp

<<cout

<<

"Wind=

"

<<

w->wind

<<

endl

;}int

main

(

){

weather

day

;day

.

temp

=

10.5

;day

.

wind

=3.1

;nncout

<<

"1:\nTemp

=

"

<<

day.temp

<<

ecout

<<

"Wind

=

"

<<

day.wind

<<

endl

;funstu(&day);cout

<<

"

2:\nTemp

=

"

<<

day.temp<<

ecout

<<

"Wind=

"

<<

day.wind

<<

endl

;}傳地址S_ary[0].xS_ary[1].xS_ary[0].aS_ary[1].a……S_ary[9].aS_ary[9].x5.4

結構數組數組的元素類型為結構類型時,稱為結構數組。例如struct

S_type{

int

a;

double

x;};S_type

S_ary[10];S_ary是一個有10個元素的數組,元素類型是S_type。數組的每一個元素包含兩個數據成員。#

include

<iostream>using

namespace

std

;struct//結構定義{

char

name[10]

;

unsigned

int

id;

double

salary

;

}

;allone[6];

//結構數組int

main

(

){

int

i

; temp

;for

(

i

=

0

;

i

<

6

;

i

++

)//結構變量//輸入數據{

cout<<

i

<<

":name:

"

;

cgets

(

allone[i].name

)

;cout

<<

"id:

"

; cin

>>

allone[i].id

;cout

<<

"salary:

"

; cin>>

allone[i].salary

;cout

<<

endl

;}

;cout

<<

"Sort:\n"

;for(i=1;i<6;i++)

//以成員salary作關鍵字排序{

for

(

int

j=

0;

j<=

5-i

;

j++

){if(allone[j].salary>allone[j+1].salary

)

//結構變量的整體交換{

temp

=

allone[j]

; allone[j]

=

allone[j+1]

; allone[j+1]

=

temp

;

}}}for(i=0;i<6;i++)

//輸出排序后數據cout

<<

allone[i].name

<<

'\t'

<<

allone[i].id

<<

'\t'

<<

allone[i].salary

<<

endl

;}例5-11

對結構數組以某一成員作關鍵字排序{

int

i

;for

(

i

=

0

;

i

<

6

;

i

++

){

cout<<

i

<<

":name:

"

;

cgets

(

allone[i].name

)

;cout

<<

"id:

"

; cin

>>

allone[i].id

;cout

<<

"salary:

"

; cin>>

allone[i].salary

;cout

<<

endl

;}

;cout

<<

"Sort:\n"

;for(i=1;i<6;i++)

//以成員salary作關鍵字排序{

for

(

int

j=

0;

j<=

5-i

;

j++

){if(allone[j].salary>allone[j+1].salary

)

//結構變量的整體交換{

temp

=

allone[j]

; allone[j]

=

allone[j+1]

; allone[j+1]

=

temp

;

}}}for(i=0;i<6;i++)

//輸出排序后數據cout

<<

allone[i].name

<<

'\t'

<<

allone[i].id

<<

'\t'

<<

allone[i].salary

<<

endl

;}例5-11

對結構數組以某一成員作關鍵字排序#

include

<iostream>using

namespace

std

;struct//結構定義{

char

name[10]

;

unsigned

int

id;

double

salary;

};allone[6]

;temp

;//結構數組//結構變量//輸入數據#

include

<iostream>using

namespace

std

;struct//結構定義{

char

name[10]

;

unsigned

int

id;

double

salary

;

}

;//結構數組{

if

(

allone[j].salary

>

allone[j+1].salary

)//結構變量的整體交換{

temp

=

allone[j]

; allone[j]

=

allone[j+1]

; allone[j+1]

=

temp

;

}}}for(i=0;i<6;i++)

//輸出排序后數據cout

<<

allone[i].name

<<

'\t'

<<

allone[i].id

<<

'\t'

<<

allone[i].salary

<<

endl

;}例5-11

對結構數組以某一成員作關鍵字排序allone[6]

;int

main

(

){

int

i

;temp

;for

(

i

=

0;

i

<

6

;

i

++

)//結構變量//輸入數據{

cout

<<

i

<<

":

name:

"

;

cgets

(

allone[i].name

)

;cout

<<

"id:

"; cin

>>

allone[i].id

;cin

>>

allone[i].salary

;cout

<<

"salary:

"

;cout

<<

endl

;}

;接受空格輸入#

include

<iostream>using

namespace

std

;struct//結構定義{

char

name[10]

;

unsigned

int

id;

double

salary

;

}

;allone[6];

//結構數組int

main

(

){int

i; temp;//結構變量for(i=0;i<6;i++)

//輸入數據例5-11

對結構數組以某一成員作關鍵字排序{

cout<<

i

<<

":name:

"

;

cgets

(

allone[i].name

)

;cout

<<

"id:

"

; cin

>>

allone[i].id

;cout

<<

"salary:

"

; cin

>>

allone[i]

salary

;cout

<<

“Sort:\n"

;//以成員salary作關鍵字排序for

(

i=1

;

i

<

6

;

i

++

){

for

(

int

j

=

0

;

j

<=

5-i

;

j

++

){

if

(

allone[j].salary

>

allone[j+1].salary

)//結構變量的整體交換{

temp

=

allone[j]

; allone[j]

=

allone[j+1]

; allone[j+1]

=

temp

;

}}}for(i=0;i<6;i++)

//輸出排序后數據cout

<<

allone[i].name

<<

'\t'

<<

allone[i].id

<<

'\t'

<<

allone[i].salary

<<

endl

;}冒泡排序#

include

<iostream>using

namespace

std

;struct//結構定義{

char

name[10]

;

unsigned

int

id;

double

salary

;

}

;allone[6];

//結構數組int

main

(

){int

i; temp;//結構變量for(i=0;i<6;i++)

//輸入數據{

cout<<

i

<<

":name:

"

;

cgets

(

allone[i].name

)

;cout

<<

"id:

"

; cin

>>

allone[i].id

;cout

<<

"salary:

"

; cin>>

allone[i].salary

;cout

<<

endl

;}

;cout

<<

"Sort:\n"

;for(i=1;i<6;i++)

//以成員salary作關鍵字排序{

for

(

int

j=

0;

j<=

5-i

;

j++

){if(allone[j].salary>allone[j+1].salary

)

//結構變量的整體交換{

temp

=

allone[j]

; allone[j]

=

allone[j+1]

; allone[j+1]

=

temp

;

}

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論