Struct Data Type




Yüklə 71.33 Kb.
tarix28.02.2016
ölçüsü71.33 Kb.
Struct Data Type
Struct (record) is a structure data type. Like an array, a struct is a correction of related data items. Unlike an array, however, the individual components of a struct can contain data of different types. We can use a struct to store a variety of information about a person, such as name, marital status, age and date of birth.

Each data item is stored in a separate field. We can reference each data item stored in a struct through its field name.



Field



StudID

StudName

BirthDate

Dept

Faculty

46051163

Jittas

12 Jul 1986

Bio

Sciences

46052780

Panita

20 Oct 1986

CPE

Engineer

46053240

Mollapak

18 Dec 1986

EE

Engineer


Examples of struct Declaration (hold only one record)
namespace Cons_Struct1

{

class Class1



{

public struct person

{

public string ID;

public string Name;

public double salary;

public char gender;

public bool status;

}

static void Main(string[] args)

{

person a;
a.ID = Console.ReadLine();

a.Name = Console.ReadLine();

a.salary = double.Parse(Console.ReadLine());

a.gender = char.Parse(Console.ReadLine());

a.status = bool.Parse(Console.ReadLine());

Console.WriteLine(a.ID);

Console.WriteLine(a.Name);

Console.WriteLine(a.salary);

Console.WriteLine(a.status);
}

}


E9002

Nonthavat

48000.00

M

True

a.status


a.gender

a.Salary

a.Name

a.ID

The field names describe the nature of information represented. The content of each field determine the appropriate data type. For example, the employee’s name should be stored in a string field.



How to reference a struct variable?

namespace Cons_Struct1

{

class Class1



{

public struct person

{

public string ID;

public string Name;

public int salary;

public char gender;

public bool status;

public double insurance;

}
static void Main(string[] args)

{
person a;


a.ID = Console.ReadLine();

a.Name = Console.ReadLine();

a.salary = int.Parse(Console.ReadLine());

a.gender = char.Parse(Console.ReadLine());

a.status = bool.Parse(Console.ReadLine());

if ((a.salary <= 10000) && (a.gender == 'F'))

a.insurance = a.salary * 0.5;

else

a.insurance = a.salary * 0.2;
Console.WriteLine(a.ID);

Console.WriteLine(a.Name);

Console.WriteLine(a.salary);

Console.WriteLine(a.status);

Console.WriteLine(a.insurance);

}

}



}

*** Struct variable can be directly assigned to the other. ***

static void Main(string[] args)

{
person a, b;

……………………………………….;

……………………………………….; // อ่านค่า หรือ assign ค่าให้แก่ struct a แล้ว

……………………………………….;

b = a; // assign ตัวแปร struct a ให้แก่ struct b

}

การ assign ตัวแปร struct a ให้กับ struct b มีผลเหมือนกับ การ assign ค่าแต่ละฟิลด์ของ a ให้กับ b ดังนี้



b.ID = a.ID;

b.Name = a.Name;

b.salary = a.salary;

b.gender = a.gender;

b.status = a.status;

b.insurance = a.insurance;


Array of Struct
namespace Cons_Struct1

{

class Class1



{

const int n = 5;
struct person

{

public string ID;



public string Name;

public int salary;

public char gender;

public bool status;

}
static void Main(string[] args)

{
person [] a = new person[n];

for (int k = 0; k < n; k++)

{

a[k].ID = Console.ReadLine();



a[k].Name = Console.ReadLine();

a[k].salary = int.Parse(Console.ReadLine());

a[k].gender = char.Parse(Console.ReadLine());

a[k].status = bool.Parse(Console.ReadLine());


}

}

}



}





ID

Name

salary

gender

status

เรคอร์ด 1

E9001

Vallapa

4500

F

False

เรคอร์ด 2

E9002

Pongrit

4800

M

True

เรคอร์ด 3

E9003

Mollapak

3480

F

False

เรคอร์ด 4

E9004

Veekit

4780

M

True

เรคอร์ด 5

E9005

Panita

4320

F

False


How to reference each element of array of struct?

namespace Cons_Struct1

{

class Class1



{

const int n = 5;

struct person

{

public string ID;



public string Name;

public int salary;

public char gender;

public bool status;

public double insurance;

}
static void Main(string[] args)

{
person [] a = new person[n];

for (int k = 0; k < n; k++)

{

a[k].ID = Console.ReadLine();



a[k].Name = Console.ReadLine();

a[k].salary = int.Parse(Console.ReadLine());

a[k].gender = char.Parse(Console.ReadLine());

a[k].status = bool.Parse(Console.ReadLine());


}

for (int k = 0; k < n; k++)

{

if ((a[k].gender == 'F') && (a[k].status))

a[k].insurance = a[k].salary * 0.2;

else a[k].insurance = 0.0;

}

for (int k = 0; k < n; k++)



{

Console.Write("{0} {1} {2} ", a[k].ID, a[k].Name, a[k].salary);

Console.Write("{0} {1} {2}",a[k].gender, a[k].status, a[k].insurance);

Console.WriteLine();

}

}

}



}


Program Example:
Assume that we have 5 lines of the population data of a community during the period 1950 to 1954. Each line of the data contains a year and the corresponding population. The data lines are in ascending order by year.

Write the program to read the data and determine the two consecutive years in which the percentage increase in population was the greatest. (The output is the two consecutive years with the largest percentage increase in population.


Input/Output Description
Year Population


[(56 – 82)/82 * 100 = -32%

1950 82


[(71 – 56)/56 * 100 = 27%

1951 56


[(86 – 71)/71 * 100 = 21%

1952 71
1
[(102 – 86)/86 * 100 = 18%
953 86
1954 102

namespace Cons_Struct3

{

class Class1



{

struct CensusData

{

public int year;



public int pop;

}

static void Main(string[] args)



{
Console.Write("How many years :");

int n = int.Parse(Console.ReadLine());

double [] percent = new double[n-1];

double bestPercent = 0.0;

int year1 = 0, year2 = 0, bestYear = 0;

CensusData [] census = new CensusData [n];


for (int k = 0; k < n; k++)

{

census[k].year = int.Parse(Console.ReadLine());



census[k].pop = int.Parse(Console.ReadLine());

}

for (int k = 0; k < (n-1); k++)

percent[k] = 0.0;

for (int k = 0; k < (n-1); k++)

{

percent[k] = (census[k+1].pop - census[k].pop)*100/census[k].pop;



if ((percent[k] > bestPercent) || (k == 0))

{

bestPercent = percent[k];



bestYear = census[k+1].year;

}

}



for (int k = 0; k<(n-1); k++)

{

Console.Write("{0} ",percent[k]);



}

Console.WriteLine();

year1 = bestYear - 1;

year2 = bestYear;

Console.WriteLine("Greatest between {0} and {1}",year1, year2);

}

}



}

การผ่านพารามิเตอร์
namespace Cons_Struct4

{

class Class1



{

public struct bookType

{

public string ID;



public int price;

}
static bookType ChangePrice(bookType book)

{

book.price = book.price * 2;



Console.WriteLine("Display at ChangePrice() ");

Console.WriteLine("{0} {1}", book.ID, book.price);

return book;
}
static void Main(string[] args)

{

bookType book, result;


book.ID = Console.ReadLine();

book.price = int.Parse(Console.ReadLine());

result = ChangePrice(book);

Console.WriteLine("Display at Main()");

Console.WriteLine("{0} {1}", result.ID, result.price);

}

}



}

namespace Cons_Struct4

{

class ClassStruct



{

public struct bookType

{

public string ID;



public int price;

}

static void ChangePrice(bookType [] book, int n)

{

for (int k = 0; k < n; k++)

book[k].price = book[k].price * 2;

Console.WriteLine("Display at ChangePrice() ");

for (int k = 0; k < n; k++)

Console.WriteLine("{0} {1}", book[k].ID, book[k].price);

}
static void Main(string[] args)

{

int n;

Console.WriteLine("Enter number of records : ");

n = int.Parse(Console.ReadLine());



bookType [] book = new bookType[n];
for (int k = 0; k{

book[k].ID = Console.ReadLine();



book[k].price = int.Parse(Console.ReadLine());

}

ChangePrice(book, n);



Console.WriteLine("Display at Main()");

for (int k = 0; k < n; k++)

Console.WriteLine("{0} {1}", book[k].ID, book[k].price);



}

}

}


การผ่าน อาร์เรย์ของ struct เป็นการผ่านค่า memory address ของตำแหน่งที่เป็นอาร์เรย์ ของ struct นั้นไปให้กับเมธอดที่ถูกเรียก ดังนั้นไม่ว่าจะ Pass by value หรือ Pass by ref ก็ให้ผลลัพธ์เหมือนกัน


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azrefs.org 2016
rəhbərliyinə müraciət

    Ana səhifə