- Prof. amr Goneid, AUC
1
CSCE 110 PROGRAMMING FUNDAMENTALS
WITH C++
- Prof. Amr Goneid
WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type Prof. - - PowerPoint PPT Presentation
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type Prof. amr Goneid, AUC 1 The Struct Data Type Prof. amr Goneid, AUC 2 The Struct Data Type What are Structs ? Definition & Declaration
1
2
3
4
5
(a) Define a struct type: struct <struct type> { <type 1> <member 1>; <type 2> <member 2>; …… <type n> <member n>; }; (b) Declare variables of that type: <struct type> <var1> <var2> .. ;
6
// Definition of struct employee struct employee { string id; string name; char gender; int numDepend; money rate; money totWages; }; employee engineer , assistant;
7
8
9
10
11
12
13
14
15
// A member of a struct may itself be a struct,e.g. struct nameType { cin >> person.address; string first; cin >> person.phone; string middle; cin >> person.name.first; string last; cin >> person.name.last; }; struct personInfo { nameType name; string address; string phone; }; personInfo person;
16
struct point{ double x, y; }; point P; struct line{ point p1, p2; }; line L; struct triangle{ point p1, p2, p3; }; triangle T;
p.x,p.y L.p1.x,L.p1.y L.p2.x,L.p2.y T.p1.x,T.p1.y T.p2.x,T.p2.y T.p3.x,T.p3.y
17
18
19
20
// FILE: StudentStat.h struct studentStat { string name; int scores[3]; float average; char grade; };
21
// File: printStats.cpp // Prints the exam statistics // Pre: The members of the struct variable // student are assigned values. // Post: Each member of student is displayed. void printStats(const studentStat student) { cout << "Exam scores for " << student. name << ": "
22
cout << student.scores[0] << ' ' << student.scores[1]<< ' ' << student.scores[2] << endl; cout << "Average score: " << student.average << endl; cout << "Letter grade : " << student.grade << endl; }
23
// File: ReadEmp.cpp // Reads one employee record into oneemployee #include <string> #include <iostream> // Pre: None // Post: Data are read into struct oneEmployee void readEmployee(employee& oneEmployee) { cout << "Enter a name terminated by # : ";
24
getline(cin, oneEmployee.name, '#'); cout << "Enter an id number: "; cin >> oneEmployee.id; cout << "Enter gender (F or M): "; cin >> oneEmployee.gender; cout << "Enter number of dependents: "; cin >> oneEmployee.numDepend; cout << "Enter hourly rate: "; cin >> oneEmployee.rate; }
25
26
27
28
29
Allocates storage for struct of type electric and places
Copy contents of p struct to q struct
30
31
We can build a sequence of nodes linked by
First node pointed to by head contains the first digit
Last node’s next is NULL. A cursor points to the current node. It can advance
head NULL
. next First Last
32
next
33
// Create first node p = new node; p->d = Long_pi[0]; p-> next = NULL; head = cursor = p; //Create rest of nodes for (i = 1; i < Long_pi.length(); i++){ p = new node; p->d = Long_pi[i]; p->next = NULL; cursor->next = p; cursor = cursor->next; }