[C and pointer] ch16. Standard function library

Chapter 16: Standard Function Library

GithubLink: ch16. Standard function library

The library function can greatly simplify the usual business processing, and there is no need to re-create the wheels, and the quality of the wheels made by myself is not good...but I personally think that some library functions that are not commonly used can be understood and know what they do. That’s fine, and it’s really only used in certain business processes. Just check the documentation when you use it.

Summary of this chapter and points to note

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here


Answers to some after-school exercises

16.12 Question

  1. This is the UBbehavior of the base basein the range of 0,2 to 36, in the book P329can be seen definitions. This may be able to output a string of results, but it is meaningless and portability will be greatly reduced.

  2. Yes, all that is needed are numbers that have no obvious relationship to each other. Just enough, it can be used to create data and write a match!

  3. Obviously, if 100 random numbers are generated and the range is limited between 0 and 1, then the result depends on the "quality" of the random number.

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-fPhZwF0J-1610330412009)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210108085441.png)]

    Haha, my random numbers are not so regular yet, see you demo01.c.

  4. Did not understand what the question means. The answer to the very good use of sleep function sleep()statistical whether this time will be able to be counted separately out. See the answer: It is definitely not easy to do. Some implementations provide a sleep function to pause the program for some time; if aduring the sleep clock value continues to increase, and then measuring the elapsed time. If you don't have this, you can try to read from standard input without input for ten seconds. If the clock continues to increase at this time, it is a measure of the running time on the machine or operating system is not very good at managing their I/Oequipment. Another option is to start another project; if the clock is in the first while the second program is running, it is measuring the running time.

  5. Why do I only realize that the month needs to be incremented by 1? You really need to pay attention to several functions passing parameters! See P332and P333.

    [External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-OqJ3OnPM-1610330412009)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210108090548.png)]

  6. The main problem is the call to setjmpinvoke the function returns longjmp. This means that the state information stored in the jump buffer is no longer valid, so the result is unpredictable. In contrast to this, the fact is that the main function does not check whether the number of command line parameters given is small. What happens when this is executed? It depends on the specific machine. Some will abort the return address illegal activities returned from the number set_bufferfunction. Others will enter an infinite loop somewhere. In particular RISCmachine, it does not have the function parameters are stored in the return address on the stack, it will enter an infinite loop. See P337Warnings section.

  7. Most OJdirectly SE, but in my native compiler has only warningestimated open warning level is too low, the programmer should avoid this situation, the implementation of the two results are random.

  8. Sort in descending order.

16.13 Programming Exercises

  1. Very good one subject, simple realization of a pband, though by means of library functions. Using the main()function parameter passing command line arguments to the reception is very good, very standard for the code! See demo03.c.

  2. It is actually a very common type of questions, related to the probability, if you think you simply need to %6be mapped to a simple, but apparently still too young ah. See answer:

    [External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-CNMZlCCJ-1610330412010)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210108101734.png)]

  3. A simple mock question, see demo04.c.

  4. There is a formula for calculating the week by year, which seems to be called Kim Larson's formula. tm_wdayDo not need to manually assign, after the conversion tmstructure will be formatted, you can easily determine the day of the week! ! ! See P335. See demo05.c.

  5. Note △ton the line. See demo06.c.

  6. Common mistakes: the paymentnumber of parameters passed as an integer, the number of years and failed to interest converted to monthly equivalent, failed to convert decimals interest, not rounded to the nearest penny.

  7. I'm too lazy to do this experiment, after referring to the answer book.

  8. sscanfWill no longer work because there may be any number of ages. Instead, we will use strtol. strtolIn this leading spaces can be ignored, and the conversion in the decimal sense, is really too convenient, directly to the second argument to char *buffetch address is &bufpassed as a secondary pointer into very spiritual. See demo08.c. Contrast can look ch15 README.mdprogramming 7.

  9. I did this experiment when I talked about probability in a high school math class. The teacher also helped select a pair of classmates in the same year and the same month. The answer is surprising: in a group of 30 people, there is a 70% chance that at least two of their birthdays will be on the same day. Only a group of around 23 members can win the tie. My computer is not being introduced <malloc.h>... should <stdlib.h>include space allocation function, and can also be normal use, do not go toss. See demo09.c.

  10. I have summarized before: [Sorting Algorithm] 0. Column introduction and links to articles. Insertion is better than bubbling. When the number of reverse order pairs is equal, the number of insertion exchanges will be less!

Essay

  1. Random function rand()and srand()random seeds. Although I have written a lot of algorithmic questions, I haven't used the data to test the program myself. This has to be put on the agenda in the near future.

  2. String conversion functions are also very important. Just read it when you use it. But LeetCodein allows you to achieve atoi()and atof()is really quite disgusting.

  3. As abs()and fabs()do not know now abs()support floating-point numbers yet, but still in accordance with the criteria on the line.

  4. I haven't used trigonometric functions and hyperbolic functions very much... I have proved the bottom-changing formula easily, and I'm born.

  5. Floating-point number representation, this is not very understandable. Floating-point number standards may vary from machine to machine. Can these functions be used to transfer floating-point numbers?

  6. floor(), ceil()The need to pay attention to a negative number, then C++in \a simple rounding to 0, and -3 / 2is simply -1, but floor(-3/2)is -2, ceil(-3/2)-1. Please pay attention C++to this, take the value towards zero, and ignore the decimal places. If you want to round up, add 0.5 after it. And in some cases, it needs to be rounded down, and the rounding down formula has a corresponding identity transformation, I forgot...

  7. Just look at the date function.

  8. This non-local jump is a bit showy, I haven't seen it before. Combined with P337examples to help. But I feel I still use less...

  9. The suggestion of signal is to look at the operating system directly, it will explain it more clearly. Stereotyped essays such as inter-process communication must be memorized. Signal processing functions, volatileetc. are all basic knowledge.

  10. Just print the variable parameters to understand the purpose.

  11. assert()Macro is still quite common, it is a macro, not a function! As for sorting qsort()and binary search bsearch()function, also found in the P345Notes program beginning in bsearchspelling errors. But I really have not used these two functions in this, write a custom cmp()function, pass the function name equal to the transfer function pointers can be called.

  12. locale Just look at it briefly, you can format numbers, currency and other representations, which is more convenient and can improve portability.

doubt

  1. Distinction floor()and ceil()difference function, and when dealing with two different roles in the negative.

  2. Non-local jumps are a bit silly.

  3. Signals and localeare simply looked at.

  4. 4 weeks to determine the programming problem quite the show, it is convenient, mktime()after organizing internal formatted directly, call the structure members tm_wdaycan get a few weeks.

  5. 8 using the programming problem strtol()to be converted to digital space-numeric string line spacing, and it is very beautiful. The feature of using its second parameter and ignoring leading spaces to solve this problem is that it can choose the base. strtol()Is designed to handle integers, there basemay be provided, and strtod()is designed to handle floating-point character string, it is no set baseup

Guess you like

Origin blog.csdn.net/yl_puyu/article/details/112462232