16th c Pre-processor and C library
Develop a header file that contains the preprocessor definitions you need to use
Max.h #ifndef _max_h_ #define _MAX_H_ #define MAX (x, y) ((x) > (y)? ( X):(Y)) #endif
2. The harmonic mean of two numbers can be obtained by first averaging the reciprocal of two numbers and then counting the reciprocal. Define a macro "function" to perform this operation using the # define directive. Write a simple program to test the macro.
#include <stdio.h> #define HARMONIC_AVERAGEH (x, y) (1/((1/(x) + 1/(Y))/2)//The answer to the book, two times the product of the number divided by twice number and equal to this two number of harmonic averages// Reading less, a lot of math problems do not understand, alas!!! #define Hmean (x, y) (2.0 * (×) * (y)/((x) + (y))) int main (void) {Double X, Y, z;x = 3;y = 9;z = Harmonic_averageh (x, Y);p RI NTF (The harmonic averages of the "%.2lf%.2lf are:%.2lf\n", X, Y, z);p rintf ("The harmonic mean of%.2lf%.2LF is:%.2lf\n", X, Y, Hmean (x, y)); return 0;}
3. Polar coordinates describe the vector with the length of the vector and the counter-clockwise corner of the vector relative to the x-axis. The Cartesian coordinate uses the x and y coordinates of the vector to describe the vector (see figure 16.3). Writes a program that reads the length and angle of a vector (in degrees) and then displays the x and Y coordinates. The relevant equations are as follows:
x = r cos a y = r sin a
To complete this conversion, you need to use a function that takes a struct with a polar coordinate as an argument, returns a structure that contains a Cartesian coordinate (or a pointer to a struct).
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/5E/wKiom1V6SRDz-uQPAAA204c-lWc804.jpg "title=" Qq20150612104149.jpg "alt=" wkiom1v6srdz-uqpaaa204c-lwc804.jpg "/>
It's a math problem, I don't know whether to do it right #include <stdio.h> #include <math.h> #define PI 3.141592654struct polar{double len;double angle;}; struct tect{double x;double y;}; Struct tect polartotect (const struct polar * p); Int main (void) {struct polar input;struct tect result;printf ("Enter vector length and angle: "); scanf ("%lf%lf ", &input.len, &input.angle); Result = polartotect (&input); printf ("polar: %.2lf %.2lf\n", input.len, input.angle);p rintf ("result: %.2lf %. 2lf\n ", result.x, result.y); return 0;} Struct tect polartotect (const struct polar * p) {struct tect temp;// The answer to the book Why does it require a degree instead of a right angle with the degree of input? Read less do not understand, you should be rectangular coordinates need it. double ang = pi / 180 * p->angle;temp.x = p->len * acos (ANG); temp.y = p->lEn * asin (ANG); return temp;}
The 4.ANSI library describes the clock () function in this way:
#include <time.h>
clock_t clock (void);
clock_t is the type defined in Time.h. The clock () function returns the processor time, whose units depend on the implementation (if it is not available or cannot represent the processor clock, the function returns-1). The same clocks_per_sec defined in Time.h is the number of processor time units per second. Therefore, the return value of two call function clock () is calculated, then the difference is removed with clocks_per_sec, and the result is the time interval between two calls in seconds. Before division, the type of the value is assigned as a double type, and the time can be exactly after the decimal point. Write a function, accept a time delay number as an argument, and then run a loop until the time is over. Write a simple program to test the function.
#include <stdio.h> #include <time.h>void delay (int.), int main (void) {int t;printf ("Enter the time to delay in seconds, Enter the letter Q to exit: "), while (scanf ("%d ", &t) = = 1) {delay (t);p rintf (" Time to. \ n ");p rintf (" Please enter the time to delay in seconds, enter the letter Q to exit: ");} return 0;} The value of void delay (int time) {Double i;//i is the number of seconds that the program runs when it executes to this statement I = (double) clock ()/Clocks_per_sec;while ((double) clock ()/Clocks_ Per_sec-i < time);}
5. Write a function. The visit function accepts the following parameters: the name of an int array, the size of an array, and a value that represents the number of picks. The function then randomly selects the specified number of elements from the array and prints them. Each element is selected at most once (draw or select a jury member). Also, if your implementation supports time () (described in the 12th chapter) or other similar functions, the output of this function can be used in Srand () to initialize the random number generator rand (). Write a simple program to test the function.
#include <stdio.h> #include <stdlib.h> #include <time.h> #define NUM 36void Fun (int * PA, int n, int b); int Main (void) {int a[num];int I, num;for (i = 0; i < NUM; i++) A[i] = i + 1;srand ((unsigned) time (NULL));p rintf ("Please enter Number:"); wh Ile (scanf ("%d", &num) = = 1 && num > 0 && num <= num) {fun (A, num, num);p rintf ("Please enter Number:");} return 0;} void Fun (int * PA, int n, int b) {int len, Temp;do{len = rand ()% n;printf ("%3d", Pa[len]); if (len = = n + 1) n--;else{temp = Pa[n];p a[n] = Pa[len];p a[n] = temp;n--;}} while (--b);p rintf ("\ n");}
6. Modify the program listing 16.15 to use an array of struct names elements (defined after the program manifest) instead of using a double array, using fewer elements, and explicitly initializing the array as an array of the appropriate names.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define num 6struct names{char first[12];char last[12];}; Void showarray (struct names ar[], int n); Int mycomp (CONST&NBSP;VOID&NBSP;*P1, &NBSP;CONST&NBSP;VOID&NBSP;*P2); Int main (void) {struct names name[num] ={{ { " Dehua " }, { " Liu " } },{ { " Xueiyou " }, { " Zhang "&NBSP;} },{ { "Fucheng" }, { "Guo" } },{ { "Jahun" }, { "Zhang" } },{ { "Jahun" }, { "Liang" } },{ { " Chaowei " }, { " Liang " } }};//not only English do not understand, pinyin is bad puts (" random list: "); Showarray ( Name, num); Qsort (Name, num, sizeof (struct names), mycomp);p UTS ("\nsorted list: "); Showarray (name, num); return 0;} Void showarray (STRUCT&NBSp;names * pname, int n) {int index;for (index = 0; index < n; index++) {printf ("%s.%s", pname[index].last, pname[index].first);p Utchar (' \ n ');}} Int mycomp (CONST&NBSP;VOID&NBSP;*P1,&NBSP;CONST&NBSP;VOID&NBSP;*P2) {const struct names * a1 = (const struct names *) p1;const struct names * a2 = (const struct names *) p2;int i = strcmp (a1->last, a2->last); (i != 0) return i;elsereturn strcmp (a1->last, a2->last);}
7. The following is a program fragment that uses a mutable function:
#include <stdio.h> #include <stdlib.h> #include <stdarg.h>void show_array (const double ar[], int n); Double * New_d_array (int n, ...); int main () {double * p1;double * p2;p1 = New_d_array (5, 1.2, 2.3, 3.4, 4.5, 5.6);p 2 = New_d_array (4, 100.0, 20.00, 8.08, 1 890.0); Show_array (P1, 5); Show_array (P2, 4); free (p1); free (p2); return 0;}
The New_d_array () function accepts an int parameter and a variable number of double arguments. This function returns a pointer to a block of memory allocated by malloc (). The int parameter specifies the number of elements in the dynamic array: A double value is used to initialize the element (the first value is given the first element, and so on). Provide Show_array () and New_d_array () codes to make the program complete.
#include <stdio.h> #include <stdlib.h> #include <stdarg.h>// The key of this problem is the application of stdarg.h and function variable parameters Void show_array (const double ar[], int n);d ouble * New_d_array (int n, ..); Int main () {Double * p1;double * p2;p1 = new_d_array (5, 1.2, 2.3, 3.4, 4.5, 5.6);p 2 = new_d_array (4, 100.0, 20.00, 8.08, -1890.0); Show_array (p1, 5); Show_array (p2, 4); free (p1), free (p2); return 0;} Double * new_d_array (int lim, ...) {Va_list va;va_start (Va, lim);d ouble * p;p = (double *) malloc (lim * sizeof (double));for (int i = 0; i < lim; i++) p[i] = va_arg (va, double); Va_end (VA); return p;} Void show_array (const double ar[], int n) {int i;for (i = 0; i < n; i++) Printf ("%.2lf ", ar[i]);p rintf ("\ n");}
This article is from the "30 years old school Programming" blog, please be sure to keep this source http://xiongyi85.blog.51cto.com/8756903/1661356
C Primer Plus (fifth edition) 16th Chapter C Preprocessor and C library programming exercises