《CByExample(四)》由会员分享,可在线阅读,更多相关《CByExample(四)(142页珍藏版)》请在金锄头文库上搜索。
1、Part IVStructures and File Input/OutputStructuresUsing structures, you have the ability to group data and work with the grouped data as a whole. Business data processing uses the concept of structures in almost every program. Being able to manipulate several variables as a single group makes your pr
2、ograms easier to manage.This chapter introduces the following concepts: Structure definitions Initializing structures The dot operator (.) Structure assignment Nested structuresThis chapter is one of the last in the book to present new concepts. The remainder of the book builds on the structure conc
3、epts you learn in this chapter.Introduction to StructuresStructures can have members of different data types.A structure is a collection of one or more variable types. As you know, each element in an array must be the same data type, and you must refer to the entire array by its name. Each element (
4、called a member) in a structure can be a different data type.Suppose you wanted to keep track of your CD music collection. You might want to track the following pieces of information about each CD:TitleArtistNumber of songsCostDate purchasedThere would be five members in this CD structure.TIP: If yo
5、u have programmed in other computer languages, or if you have ever used a database program, C+ structures are analogous to file records, and members are analogous to fields in those records.After deciding on the members, you must decide what data type each member is. The title and artist are charact
6、er arrays, the number of songs is an integer, the cost is floating-point, and the date is another character array. This information is represented like this:Member NameData TypeTitleCharacter array of 25 charactersArtistCharacter array of 20 charactersNumber of songsIntegerCostFloating-pointDate pur
7、chasedCharacter array of eight charactersEach structure you define can have an associated structure name called a structure tag. Structure tags are not required in most cases, but it is generally best to define one for each structure in your program. The structure tag is not a variable name. Unlike
8、array names, which reference the array as variables, a structure tag is simply a label for the structures format.You name structure tags yourself, using the same naming rules for variables. If you give the CD structure a structure tag named cd.coilection, you are informing C+ that the tag called cd.
9、coiiection looks like two character arrays, followed by an integer, a floatingpoint value, and a final character array.A structure tag is a label for the structures fonnat.A structure tag is actually a newly defined data type that you, the programmer, define. When you want to store an integer, you d
10、o not have to define to C+ what an integer is. C+ already recognizes an integer. When you want to store a CD collections data, however, C+ is not capable of recognizing what format your CD collection takes. You have to tell C+(using the example being described here) that you need a new data type. Th
11、at data type will be your structure tag, called cd.coiiection in this example, and it looks like the structure previously described (two character arrays, integer, floatingpoint, and character array).NOTE: No memory is reserved for structure tags. A structure tag is your own data type. C+ does not r
12、eserve memory for the integer data type until you declare an integer variable. C+ does not reserve memory for a structure until you declare a structure variable.Figure 28.1 shows the CD structure, graphically representing the data types in the structure. Notice that there are five members and each m
13、ember is a different data type. The entire structure is called cd.coiiection because that is the structure tag.Figure 28.1. The layout of the cd.coiiection structure.NOTE: The mailing-list application in Appendix F uses a structure to hold peoples names, addresses, cities, states, and61381ZIP codes.
14、Examples1. Suppose you were asked to write a program for a companys inventory system. The company had been using a card-file inventory system to track the following items:Item nameQuantity in stockQuantity on orderRetail priceWholesale priceThis would be a perfect use for a structure containing five
15、 members. Before defining the structure, you have to determine the data types of each member. After asking questions about the range of data (you must know the largest item name, and the highest possible quantity that would appear on order to ensure your data types can hold the data), you decide to
16、use the following structure tag and data types:MemberData TypeItem nameCharacter array of 20 charactersQuantity in stocklong intQuantity on orderlong intRetail pricedoubleWholesale pricedouble2. Suppose the same company also wanted you to write a program to keep track of their monthly and annual salaries and to print a report at the end