閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Advertisements

1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Basic Elements of C++ Chapter 2.
Review of C++ Programming Part II Sheng-Fang Huang.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 7 Defining Your Own Data Types. 2 What Is a struct ?  A structure is a user-defined type You define it using the keyword struct so it is often.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 Chapter 8 Destructor & Operator Overloading. 2 Destructor  A destructor is a function that is called when an object is no longer required. A constructor.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Review 1 List Data Structure List operations List Implementation Array Linked List.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
尋隱者不遇 松下問童子, 言師採藥去。 只在此山中, 雲深不知處。 ~ 賈島 1. C Time Library ctime  Types clock_t - Clock type clock_t size_t - Unsigned integral type size_t time_t - Time.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Week 9 - Wednesday.  What did we talk about last time?  structs.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Chapter 1.2 Introduction to C++ Programming
Pointers and Dynamic Arrays
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Popping Items Off a Stack Using a Function Lesson xx
Chapter 7 Pointers and C-Strings
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Week 9 - Wednesday CS222.
Arrays Arrays exist in almost every computer language.
Basic Elements of C++.
Student Book An Introduction
CSCE 210 Data Structures and Algorithms
8 Pointers.
Basic Elements of C++ Chapter 2.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 2 Elementary Programming
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
One-Dimensional Array Introduction Lesson xx
7 Arrays.
2.1 Parts of a C++ Program.
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
Arrays Kingdom of Saudi Arabia
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Functions Pass By Value Pass by Reference
Chapter 2: Introduction to C++.
7 Arrays.
Chapter 9: Data Structures: Arrays
Arrays Arrays A few types Structures of related data items
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Chapter 9: Pointers and String
Lecture 2 Arrays & Pointers September 7, 2004
各題答對人數.
閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.
Pointers, Dynamic Data, and Reference Types
Video: The Sound of Raining
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。

Chapter 7 Defining Your Own Data Types (P.267)

What Is a struct? (P.268) A structure is a user-defined type You define it using the keyword struct so it is often referred as a struct. Compared to the data types we have seen, some real world objects must be described by several items: Time - hh:mm:ss Point - (x,y) Circle - (x,y,r) Rational number - q/p

Defining a struct (P.268) struct POINT { float x; float y; }; Note: This doesn’t define any variables. It only creates a new type. Each line defining an element in the struct is terminated by a semicolon A semicolon also appears after the closing brace.

Creating Variables of Type POINT POINT p1, p2; If you also want to initialize a struct: POINT p1 = { 1.0, 2.0 }; The syntax is similar to the one to initialize an array.

Accessing the Members of a struct P.269 Member selection operator (.) p1.x = 3.0; p2.y += 2.0; You may manipulate p2.y just as any variable of type float.

Figure 7-1 on P.270

Ex7_01.cpp (P.270) Putting the definition of the struct at global scope allows you to declare a variable of type Rectangle anywhere in the .cpp file. Hut2 = Hut1; Hut2.left = Hut1.left; Hut2.top = Hut1.top; Hut2.right = Hut1.right; Hut2.bottom = Hut1.bottom;

Pass by Reference (P.271) long area(const Rectangle& aRect) { return (aRect.right - aRect.left) * (aRect.bottom - aRect.top); } By passing a reference , the code runs a little faster because the argument is not copied. The parameter is const so that the function cannot change the argument that is passed to it.

Pass by Reference (2) void moveRect(Rectangle& aRect, const int x, const int y) { const int length = aRect.right - aRect.left; // Get length of rectangle const int width = aRect.bottom - aRect.top); // Get width of rectangle aRect.left = x; // Set top-left point aRect.top = y; // to new position aRect.right = x + length; // Get bottom-right point as aRect.bottom = y + width; // increment from new position return; } Because aRect is passed as a reference, the function is able to modify the members of the Rectangle directly.

Exercise Based on Ex7_01.cpp, write a function bool equalAreaRect(const Rectangle& a, const Rectangle& b) which compares the area of two rectangles. In your main(), use at least two test cases to demonstrate that your function is working fine.

Addition of Rational Numbers (1) Write a program to calculate the addition of rational numbers. You need to develop a function add() to perform the addition, and a function print() to display a rational number.

EX: Addition of Rational Numbers (2) Modify the previous exercise and define a struct Rational. int main() { const unsigned short N = 5; Rational a[N] = { {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5} }; Rational b[N] = { {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1} }; Rational c[N]; int i; for (i=0; i<N; i++) add(a[i], b[i], c[i]); print(a[i]); cout << " + "; print(b[i]); cout << " = "; print(c[i]); cout << endl; } return 0;

Ex: Addition of Rational Numbers (3) Modify the previous exercise for the following main(). int main() { const unsigned short N = 5; Rational a[N] = { {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5} }; Rational b[N] = { {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1} }; Rational c[N]; int i; for (i=0; i<N; i++) c[i] = add(a[i], b[i]); print(a[i]); cout << " + "; print(b[i]); cout << " = "; print(c[i]); cout << endl; } return 0;

EX: Sort an array of rational numbers Modify your own exchange sort function to sort an array of rational numbers. Suppose you defined a structure struct Q { int q; int p; }; a function to display the array void print_array(Q a[], int n) { for (int i=0; i<n; i++) cout << a[i].q << '/' << a[i].p << ' '; cout << endl; }

and a main program to test it. int main() { Q a[] = { {7,3}, {1,5}, {6, 5}, {4, 3} }; int size = sizeof(a) / sizeof(a[0]); print_array(a, size); exchange_sort(a, size, cmp); return 0; } Now all you need to supply is a cmp() function and a revised exchange_sort() function. The output should be 7/3 1/5 6/5 4/3 1/5 6/5 4/3 7/3

Using Pointers with a struct (P.274) RECT* pRect = NULL; Define a pointer to RECT RECT aRect; pRect = &aRect; Set pointer to the address of aRect

Accessing Structure Members through a Pointer (P.275) RECT aRect = { 0, 0, 100, 100}; RECT* pRect = &aRect; (*pRect).Top += 10; The parenthesis to de-reference the pointer are necessary (P.61) pRect->Top += 10; Indirect member selection operator

C Time Library <ctime> Types clock_t - Clock type size_t - Unsigned integral type time_t - Time type struct tm - Time structure (See Chapter 7) Time manipulation clock - Ticks since the program was launched time - Get current time mktime - Convert tm structure to time_t Macro CLOCKS_PER_SEC - Clock ticks per second Conversion asctime - Convert tm structure to string ctime - Convert time_t value to string gmtime - Convert time_t to tm as UTC time localtime - Convert time_t to tm as local time strftime - Format time as string

time() #include <iostream> #include <ctime> using std::cout; using std::cin; int main() { int i; time_t t1, t2; t1 = time(NULL); cin >> i; time(&t2); cout << t2 - t1 << " seconds.\n"; return 0; }

struct tm struct tm gmtime() - Convert time_t to tm as UTC time localtime() - Convert time_t to tm as local time mktime() - Convert tm structure to time_t asctime() - Convert tm structure to string strftime() - Format time as string

struct tm The structure contains 9 members of type int: struct tm { int tm_sec; /* seconds after the minute - [0,59] */ int tm_min; /* minutes after the hour - [0,59] */ int tm_hour; /* hours since midnight - [0,23] */ int tm_mday; /* day of the month - [1,31] */ int tm_mon; /* months since January - [0,11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday - [0,6] */ int tm_yday; /* days since January 1 - [0,365] */ int tm_isdst; /* daylight savings time flag */ };

localtime() and asctime() example #include <ctime> struct tm * localtime ( const time_t * timer ); #include <iostream> #include <ctime> int main () { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); std::cout << "It is Year " << timeinfo->tm_year + 1900 << std::endl; // years since 1900 std::cout <<"Current local time and date: " << asctime (timeinfo) << std::endl; return 0; } #include <ctime> char * asctime ( const struct tm * timeptr );

strftime() #include <ctime> size_t strftime ( char * buffer, size_t maxsize, const char * format, const struct tm * timeptr ); strftime() #include <iostream> #include <ctime> int main () { time_t rawtime; struct tm * timeinfo; char buffer [80]; time ( &rawtime ); timeinfo = localtime ( &rawtime ); strftime (buffer,80,"Now it's %I:%M%p.",timeinfo); std::cout << buffer << std::endl; return 0; } In addition to the default format like Thu Dec 27 21:31:04 2012 you may define your own format to display the time.

HW: struct tm Define a variable timeinfo of the data type struct tm. Initialize it with your birthday. You only need to supply the first 7 members. You may skip tm_yday and tm_isdst if you are not sure of that. Convert that time to an ASCII string by asctime(). I was born at Tue May 15 16:30:59 2014.

A struct can contain a pointer (P.274) struct ListElement { RECT aRect; // RECT member of structure ListElement* pNext; // Pointer to a list element }; Linked List

Create a Linked List struct ListElement { int value; // value of an element ListElement* pNext; // Pointer to a list element }; int main() { ListElement LE5 = { 5, NULL }; ListElement LE4 = { 4, &LE5 }; ListElement LE3 = { 3, &LE4 }; ListElement LE2 = { 2, &LE3 }; ListElement LE1 = { 1, &LE2 }; PrintList(&LE1); return 0; } 5 4 3 2 1

Print a Linked List void PrintList(ListElement* p) {     while (p != NULL)     {         std::cout << p->value;         p = p->pNext;     } } 1 2 3 4 5

Dynamic Memory Allocation (P.163) Sometimes depending on the input data, you may allocate different amount of space for storing different types of variables at execution time int n = 0; cout << "Input the size of the vector - "; cin >> n; int vector[n]; error C2057: expected constant expression

Why Use Pointers? (P.148) Use pointer notation to operate on data stored in an array Enable access within a function to arrays, that are defined outside the function Allocate space for variables dynamically.

Free Store (Heap) To hold a string entered by the user, there is no way you can know in advance how large this string could be. Free Store - When your program is executed, there is unused memory in your computer. You can dynamically allocate space within the free store for a new variable.

The new Operator Request memory for a double variable, and return the address of the space double* pvalue = NULL; pvalue = new double; Initialize a variable created by new pvalue = new double(9999.0); Use this pointer to reference the variable (indirection operator) *pvalue = 1234.0;

The delete Operator When you no longer need the (dynamically allocated) variable, you can free up the memory space. delete pvalue; Release memory pointed to by pvalue pvalue = NULL; Reset the pointer to NULL After you release the space, the memory can be used to store a different variable later.

Allocating Memory Dynamically for Arrays Allocate a string of twenty characters char* pstr; pstr = new char[20]; delete [] pstr; Note the use of square brackets to indicate that you are deleting an array. pstr = 0; Set pointer to null

Exercise: Sorting Unknown Number of Integers Write a program to read a series of positive integers from the user. The total number of input is unknown. Stop when the user supplies 0 or a negative number. Then output the series of numbers in reserve order. For example, the input is 1 3 5 7 2 4 6 0, the output will be 6 4 2 7 5 3 1. Hint: Store the input numbers in a linked list.

Adding a New Element 4 3 2 1 head Allocate a new element to store the input value. Update LE4.pnext to point to LE3. Update head pointing to LE4.

Adding a New Element 4 3 2 1 head Allocate a new element to store the input value. Update LE4.pnext to point to LE3. Update head pointing to LE4.

HW: Linked-List Sorting & Binary Tree