Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 7 WEEK 1 – 구조체 (STRUCTURES)

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 7 WEEK 1 – 구조체 (STRUCTURES)"— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 7 WEEK 1 – 구조체 (STRUCTURES)

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 2 학습목표 1. 구조체의 개념 학습 2. 구조체 멤버 접근하기 3. 구조체 멤버 초기화 방법 학습 4. 구조체를 매개변수로 사용하기 5. 중첩 구조체 사용 방법 학습 6. 구조체를 이용한 프로그램 개발

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 3 LAB ASSIGNMENTS( 실습과제 ) Lesson 7A Lab 7.1 : 구조체 기초 Lab 7.2 : 구조체 초기화 Lab 7.3 : 중첩 구조체 Lesson 7B Lab 7.4: Student-Generated Code Assignments

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 4 Lab 7.1 구조체 기초 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “rect_struct.cpp” // This program uses a // structure to hold data about a rectangle Exercise 1: 주석 부분에 대한 프로그램을 완성하라. Rectangle 구조체 선언, box 구조체 변수 정의, 각 필드 값 입력 및 출력. http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 5 #include using namespace std; // Fill in the code to declare a structure named Rectangle which has // 4 double members: length, width, area, and perimeter. int main() { // Fill in the code to define a Rectangle variable named box. cout << "Enter the length of a rectangle: "; // Fill in the code to read in the length of box. cout << "Enter the width of a rectangle: "; // Fill in the code to read in the width of box. // Fill in the code to compute the area of box. // Fill in the code to compute the perimeter of box. cout << fixed << showpoint << setprecision(2); // Fill in the code to output the area with an appropriate message. // Fill in the code to output the perimeter with an appropriate message. return 0; } rect_struct.cpp

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 6 Exercise 2: 사용자가 입력한 직사각형이 정사각형인지를 결정하는 코드를 추가하여 다음과 같이 동작하게 수정하라. ( 구조체 멤버를 비교할 것 ) Enter the length of a rectangle: 7 Enter the width of a rectangle: 7 The area of the rectangle is 49.00 The perimeter of the rectangle is 28.00 The rectangle is a square.

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 7 Lab 7.2 구조체 초기화 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “ init_struct.cpp ” // This program demonstrates partially initialized // structure variables Exercise 1: 주석 부분에 대한 프로그램을 완성하라. Enter this year's income for Tim McGuiness : 30000 Name : Tim McGuiness Social Security Number : 255871234 Taxes due for this year: $10500.00 Enter this year's income for John Kane : 60000 Name : John Kane Social Security Number : 278990582 Taxes due for this year: $17400.00 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 8 #include using namespace std; struct taxPayer { char name[25]; // Uses a C-string, rather than a string object long int SSNum; // Social security number double taxRate; double income; double taxes; }; int main() { // Fill in the code to define and initialize a taxpayer structure variable // named citizen1 with the first 3 members initialized as follows: // name is Tim McGuiness, SSNum is 255871234, and taxRate is.35 // Fill in the code to define and initialize a taxpayer structure variable // named citizen2 with the first 3 members initialized as follows: // name is John Kane, SSNum is 278990582, and taxRate is.29 init_struct.cpp

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 9 cout.precision(2); // Works the same as cout << setprecision(2); cout << fixed << showpoint; // Fill in the code to prompt the user to enter this year's income // for citizen1. // Fill in the code to read this income into citizen1's appropriate // structure member. // Fill in the code to calculate taxes for citizen1 and store it // in his appropriate structure member. // Fill in the code to repeat the same steps for citizen2. cout << "Name: " << citizen1.name << endl; cout << "Social Security Number: " << citizen1.SSNum << endl; cout << "Taxes due for this year: " << citizen1.taxes << endl << endl; // Fill in the code to print out the same information for citizen2. return 0; } init_struct.cpp

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 10 http://ce.kyungil.ac.kr/~kykim/OOP2009/Labhttp://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “con_struct.cpp” // This program demonstrates using a // constructor to initialize structure members. Exercise 2: 주석 부분에 대한 프로그램을 완성하라. 구조체 멤버의 초기화를 위해 생성자 함수를 이용할 것. Default constructor 함수와 3 개의 매개 변수를 가지는 생성자 함수 사용.

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 11 #include using namespace std; struct taxPayer { string name; // Uses a string object, rather than a C-String long int SSNum; // Social security number double taxRate; double income; double taxes; taxPayer() // Constructor { name = ""; SSNum = 000000000; taxRate = 0; } // Fill in the code to create another constructor that has the parameters: n, id and // rate. name will be set to the value of n, SSNum will be set to the value of id and // taxRate will be set to the value of rate. }; con_struct.cpp

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 12 int main() { // Fill in the code to define and initialize (through the use of a constructor) a // taxPayer structure variable name citizen1 with the first 3 members initialized as // follows: name is Tim McGuiness, SSNum is 255871234, and taxRate is.35 // Fill in the code to define and initialize (to the default values) a taxPayer // structure variable named citizen2. // Fill in the code to set citizen2's name to John Brown. cout.precision(2); // Works the same as cout << setprecision(2); cout << fixed << showpoint; // Fill in the code to prompt for and input this year's income for citizen1, storing it // in citizen1's appropriate structure member. // Fill in the code to calculate taxes for citizen1 and store it in his appropriate // structure member. // Fill in the code to repeat the same steps for citizen2. cout << "Name: " << citizen1.name << endl; cout << "Social Security Number: " << citizen1.SSNum << endl; cout << "Taxes due for this year: " << citizen1.taxes << endl << endl; // Fill in the code to print out the same information for citizen2. return 0; }

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 13 Lab 7.3 중첩 구조체 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “ nestedRect_struct.cpp ” // This program uses a structure to hold data about a // rectangle.( 중첩 구조체 ) // Dimension structure inside a Rectangle structure Exercise 1: 주석 부분에 대한 프로그램을 완성하라. http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 14 #include using namespace std; // Fill in the code to declare a structure named Dimension that contains // 2 double members: length and width. // Fill in the code to declare a structure named Rectangle that contains 3 members: // area, perimeter, and size. The first two should each be a double but // size should be a Dimension structure variable. nestedRect_struct.cpp

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 15 int main() { // Fill in the code to define a Rectangle variable named box. cout << "Enter the length of a rectangle: "; // Fill in the code to read in the length and store it in the // appropriate box location. cout << "Enter the width of a rectangle: "; // Fill in the code to read in the width and store it in the // appropriate box location. // Fill in the code to compute the box area and store it in the // appropriate box location. // Fill in the code to compute the box perimeter and store it in // the appropriate box location. cout << fixed << showpoint << setprecision(2); cout << "The area of the rectangle is " << // Fill this in. cout << "The perimeter of the rectangle is " << // Fill this in. return 0; }

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 16 Exercise 2: area( 면적 ) 와 perimeter( 둘레 ) 을 멤버로 가지는 구조체 Results 를 정의하고, 직사각형 구조체인 Rectangle 을 다음과 같이 선언한 다음 Exercise 1 의 결과와 같이 되도록 수정하라. struct Rectangle { Dimension size; Results stat; };

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 17 Exercise 3: Exercise 2 에서 정의한 직사각형에 대한 중첩 구조체 Rectangle 을 이용하되, 면적과 둘레를 계산하기 위한 함수 findArea 와 findPerimeter 를 사용하도록 수정하라. 이 두 함수는 Dimension 자료형을 매개변수로 전달 받아 계산한 다음 그 결과를 double 로 반환하게 하고 이 값을 box 의 적절한 멤버에 저장하게 한 다음 출력하게 하라. /* findArea * * This function returns the area of a rectangle when passed * * a Dimension structure variable containing a length and a width */ /* findPerimeter * * This function returns the perimeter of a rectangle when passed * * a Dimension structure variable containing a length and a width */

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 18 Lab 7.4 독립적인 소형 프로그램 개발

19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 19 Thanks!!!!!!!!


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 7 WEEK 1 – 구조체 (STRUCTURES)"

Similar presentations


Ads by Google