资源预览内容
第1页 / 共142页
第2页 / 共142页
第3页 / 共142页
第4页 / 共142页
第5页 / 共142页
第6页 / 共142页
第7页 / 共142页
第8页 / 共142页
第9页 / 共142页
第10页 / 共142页
亲,该文档总共142页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 programs 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 concepts 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 (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 you 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 character 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 purchasedCharacter 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 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.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 do 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. That 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 reserve 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 member 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.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 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 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号