Use Classes, strings, and VERY BASIC c++.....NO FANCEY SHORT CUTS We are suppose
ID: 3549142 • Letter: U
Question
Use Classes, strings, and VERY BASIC c++.....NO FANCEY SHORT CUTS
We are supposed to take a bar code converted to numbers like: 110100101000101011000010011. The two numbers on the outside are always ones so they are dropped thus leaving you with 25 digits separated into 5 groups of five digits each. Each group has exactly two ones; each digit stands for a certain number. From left to right the digits encode the values 7, 4, 2, 1, and 0. Multiply the corresponding digit value with the digit and compute the sum to get the final zip code. The table below shows the encoding for 10100.
Bar code digits 1 0 1 0 0
Value 7 4 2 1 0
Product 7 0 2 0 0
Zip code digit = 7+2= 9
Repeat for each group and combine all the sums to get the final zip code. If the sum equals 11 then the true value is 0 because a two digit number can't be present.
Write a zip code class that encodes and decodes 5-digit bar codes. The class should have two constructors. The first should input the zip code as an integer and the second should input the zip code as a bar code string consisting of one's and zero's. although you have two ways to input the zip code internally the class should store the zip code using only one format (you may store it as a bar code string or as a zip code number). The class should also have at least two public member functions, one to return the zip code as an integer, and the other to return the zip code in bar code format as a string. All helper functions should be declared private. Embed your class definition in a suitable test program. Your program should print an error message if an invalid bar code is passed to the constructor.
Explanation / Answer
Beginning with version 5.6, Perl uses logically-wide characters to represent strings internally. Starting in Perl 5.14, Perl-level operations work with characters rather than bytes within the scope of a use feature 'unicode_strings'|feature (or equivalently use 5.012 or higher). (This is not true if bytes have been explicitly requested by use bytes|bytes, nor necessarily true for interactions with the platform's operating system.) For earlier Perls, and when unicode_strings is not in effect, Perl provides a fairly safe environment that can handle both types of semantics in programs. For operations where Perl can unambiguously decide that the input data are characters, Perl switches to character semantics. For operations where this determination cannot be made without additional information from the user, Perl decides in favor of compatibility and chooses to use byte semantics. When use locale is in effect (which overrides use feature 'unicode_strings' in the same scope), Perl uses the semantics associated with the current locale. Otherwise, Perl uses the platform's native byte semantics for characters whose code points are less than 256, and Unicode semantics for those greater than 255. On EBCDIC platforms, this is almost seamless, as the EBCDIC code pages that Perl handles are equivalent to Unicode's first 256 code points. (The exception is that EBCDIC regular expression case-insensitive matching rules are not as as robust as Unicode's.) But on ASCII platforms, Perl uses US-ASCII (or Basic Latin in Unicode terminology) byte semantics, meaning that characters whose ordinal numbers are in the range 128 - 255 are undefined except for their ordinal numbers. This means that none have case (upper and lower), nor are any a member of character classes, like [:alpha:] or w. (But all do belong to the W class or the Perl regular expression extension [:^alpha:].) This behavior preserves compatibility with earlier versions of Perl, which allowed byte semantics in Perl operations only if none of the program's inputs were marked as being a source of Unicode character data. Such data may come from filehandles, from calls to external programs, from information provided by the system (such as %ENV), or from literals and constants in the source text. The utf8 pragma is primarily a compatibility device that enables recognition of UTF-(8|EBCDIC) in literals encountered by the parser. Note that this pragma is only required while Perl defaults to byte semantics; when character semantics become the default, this pragma may become a no-op. See the utf8 manpage. If strings operating under byte semantics and strings with Unicode character data are concatenated, the new string will have character semantics. This can cause surprises: See BUGS, below. You can choose to be warned when this happens. See the encoding::warnings manpage. Under character semantics, many operations that formerly operated on bytes now operate on characters. A character in Perl is logically just a number ranging from 0 to 2**31 or so. Larger characters may encode into longer sequences of bytes internally, but this internal detail is mostly hidden for Perl code(The only time that Perl considers a sequence of individual code points as a single logical character is in the X construct, already mentioned above. Therefore "character" in this discussion means a single Unicode code point.) Very nearly all Unicode character properties are accessible through regular expressions by using the p{} "matches property" construct and the P{} "doesn't match property" for its negation. For instance, p{Uppercase} matches any single character with the Unicode "Uppercase" property, while p{L} matches any character with a General_Category of "L" (letter) property. Brackets are not required for single letter property names, so p{L} is equivalent to pL. More formally, p{Uppercase} matches any single character whose Unicode Uppercase property value is True, and P{Uppercase} matches any character whose Uppercase property value is False, and they could have been written as p{Uppercase=True} and p{Uppercase=False}, respectively. This formality is needed when properties are not binary; that is, if they can take on more values than just True and False. For example, the Bidi_Class (see Bidirectional Character Types below), can take on several different values, such as Left, Right, Whitespace, and others. To match these, one needs to specify the property name (Bidi_Class), AND the value being matched against (Left, Right, etc.). This is done, as in the examples above, by having the two components separated by an equal sign (or interchangeably, a colon), like p{Bidi_Class: Left}. All Unicode-defined character properties may be written in these compound forms of p{property=value} or p{property:value}, but Perl provides some additional properties that are written only in the single form, as well as single-form short-cuts for all binary properties and certain others described below, in which you may omit the property name and the equals or colon separator. Most Unicode character properties have at least two synonyms (or aliases if you prefer): a short one that is easier to type and a longer one that is more descriptive and hence easier to understand. Thus the "L" and "Letter" properties above are equivalent and can be used interchangeably. Likewise, "Upper" is a synonym for "Uppercase", and we could have written p{Uppercase} equivalently as p{Upper}. Also, there are typically various synonyms for the values the property can be. For binary properties, "True" has 3 synonyms: "T", "Yes", and "Y"; and "False has correspondingly "F", "No", and "N". But be careful. A short form of a value for one property may not mean the same thing as the same short form for another. Thus, for the General_Category property, "L" means "Letter", but for the Bidi_Class property, "L" means "Left". A complete list of properties and synonyms is in the perluniprops manpage. Upper/lower case differences in property names and values are irrelevant; thus p{Upper} means the same thing as p{upper} or even p{UpPeR}. Similarly, you can add or subtract underscores anywhere in the middle of a word, so that these are also equivalent to p{U_p_p_e_r}. And white space is irrelevant adjacent to non-word characters, such as the braces and the equals or colon separators, so p{ Upper } and p{ Upper_case : Y } are equivalent to these as well. In fact, white space and even hyphens can usually be added or deleted anywhere. So even p{ Up-per case = Yes} is equivalent. All this is called "loose-matching" by Unicode. The few places where stricter matching is used is in the middle of numbers, and in the Perl extension properties that begin or end with an underscore. Stricter matching cares about white space (except adjacent to non-word characters), hyphens, and non-interior underscores. You can also use negation in both p{} and P{} by introducing a caret (^) between the first brace and the property name: p{^Tamil} is equal to P{Tamil}. Almost all properties are immune to case-insensitive matching. That is, adding a /i regular expression modifier does not change what they match. There are two sets that are affected. The first set is Uppercase_Letter, Lowercase_Letter, and Titlecase_Letter, all of which match Cased_Letter under /i matching. And the second set is Uppercase, Lowercase, and Titlecase, all of which match Cased under /i matching. This set also includes its subsets PosixUpper and PosixLower both of which under /i matching match PosixAlpha. (The difference between these sets is that some things, such as Roman numerals, come in both upper and lower case so they are Cased, but aren't considered letters, so they aren't Cased_Letters.) General_Category
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.