Skip to content

Commit 5952cc2

Browse files
committed
+ boost.asio
1 parent 772c5ba commit 5952cc2

File tree

4,333 files changed

+938591
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,333 files changed

+938591
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Program 8.1 Scoping out scope
2+
#include <stdio.h>
3+
4+
int main(void)
5+
{
6+
int count1 = 1; // Declared in outer block
7+
8+
do {
9+
int count2 = 0; // Declared in inner block
10+
++count2;
11+
printf("count1 = %d count2 = %d\n", count1, count2);
12+
} while (++count1 <= 5);
13+
14+
// count2 no longer exists
15+
printf("count1 = %d\n", count1);
16+
return 0;
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Program 8.2 More scope in this example
2+
#include <stdio.h>
3+
4+
int main(void)
5+
{
6+
int count = 0; // Declared in outer block
7+
8+
do {
9+
int count = 0; // This is another variable called count
10+
++count; // this applies to inner count
11+
printf("count = %d\n", count);
12+
} while (++count <= 5); // This works with outer count
13+
14+
printf("count = %d\n", count); // Inner count is dead, this is outer count
15+
return 0;
16+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Program 8.3 Calculating an average using functions
2+
#define __STDC_WANT_LIB_EXT1__ 1
3+
#include <stdio.h>
4+
#define MAX_COUNT 50
5+
6+
// Function to calculate the sum of array elements
7+
// n is the number of elements in array x
8+
double Sum(double x[], size_t n)
9+
{
10+
double sum = 0.0;
11+
12+
for (size_t i = 0 ; i < n ; ++i)
13+
sum += x[i];
14+
15+
return sum;
16+
}
17+
18+
// Function to calculate the average of array elements
19+
double Average(double x[], size_t n)
20+
{
21+
return Sum(x, n) / n;
22+
}
23+
24+
// Function to read in data items and store in data array
25+
// The function returns the number of items stored
26+
size_t GetData(double *data)
27+
{
28+
size_t nValues = 0;
29+
printf("How many values do you want to enter (Maximum %d)? ", MAX_COUNT);
30+
scanf_s("%zd", &nValues);
31+
32+
if (nValues > MAX_COUNT) {
33+
printf("Maximum count exceeded. %d items will be read.", MAX_COUNT);
34+
nValues = MAX_COUNT;
35+
}
36+
37+
for (size_t i = 0 ; i < nValues ; ++i)
38+
scanf_s("%lf", &data[i]);
39+
40+
return nValues;
41+
}
42+
// main program - execution always starts here
43+
int main(void)
44+
{
45+
double samples[MAX_COUNT] = {0.0};
46+
size_t sampleCount = GetData(samples);
47+
double average = Average(samples, sampleCount);
48+
printf("The average of the values you entered is: %.2lf\n", average);
49+
return 0;
50+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Program 8.4 The functional approach to string sorting
2+
#define __STDC_WANT_LIB_EXT1__ 1
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <stdbool.h>
6+
#include <string.h>
7+
#define BUF_LEN 256 // Input buffer length
8+
#define INIT_NSTR 2 // Initial number of strings
9+
#define NSTR_INCR 2 // Increment to number of strings
10+
11+
char* str_in(); // Reads a string
12+
void str_sort(const char**, size_t); // Sorts an array of strings
13+
void swap(const char**, const char**); // Swaps two pointers
14+
void str_out(const char* const*, size_t); // Outputs the strings
15+
void free_memory(char**, size_t); // Free all heap memory
16+
17+
// Function main - execution starts here
18+
int main(void)
19+
{
20+
size_t pS_size = INIT_NSTR; // count of pS elements
21+
char **pS = calloc(pS_size, sizeof(char*)); // Array of string pointers
22+
char **pTemp = NULL; // Temporary pointer
23+
size_t str_count = 0; // Number of strings read
24+
char *pStr = NULL; // String pointer
25+
printf("Enter one string per line. Press Enter to end:\n");
26+
27+
while ((pStr = str_in()) != NULL) {
28+
if (str_count == pS_size) {
29+
pS_size += NSTR_INCR;
30+
31+
if (!(pTemp = realloc(pS, pS_size * sizeof(char*)))) {
32+
printf("Memory allocation for array of strings failed.\n");
33+
return 2;
34+
}
35+
36+
pS = pTemp;
37+
}
38+
39+
pS[str_count++] = pStr;
40+
}
41+
42+
str_sort(pS, str_count); // Sort strings
43+
str_out(pS, str_count); // Output strings
44+
free_memory(pS, str_count); // Free all heap memory
45+
return 0;
46+
}
47+
48+
/******************************************************************
49+
* String input routine *
50+
* Returns the address of the string or NULL for an empty string.*
51+
* address of the string otherwise. *
52+
* Also returns NULL if no memory is obtained or if there is an *
53+
* error reading from the keyboard. *
54+
******************************************************************/
55+
char* str_in(void)
56+
{
57+
char buf[BUF_LEN]; // Space to store input string
58+
59+
if (!gets_s(buf, BUF_LEN)) { // If NULL returned...
60+
// ...end the operation
61+
printf("\nError reading string.\n");
62+
return NULL;
63+
}
64+
65+
if (buf[0] == '\0') // If empty string read...
66+
return NULL; // ...end the operation
67+
68+
size_t str_len = strnlen_s(buf, BUF_LEN) + 1;
69+
char *pString = malloc(str_len);
70+
71+
if (!pString) { // If no memory allocated...
72+
printf("Memory allocation failure.\n");
73+
return NULL; // ...end the operation
74+
}
75+
76+
strcpy_s(pString, str_len, buf); // Copy string read to new memory
77+
return pString;
78+
}
79+
80+
/****************************************************
81+
* String sort routine *
82+
* First argument is array of pointers to constant *
83+
* strings which is of type const char*[]. *
84+
* Second argument is the number of elements in the *
85+
* pointer array – i.e. the number of strings *
86+
***************************************************/
87+
void str_sort(const char **p, size_t n)
88+
{
89+
bool sorted = false; // Strings sorted indicator
90+
91+
while (!sorted) { // Loop until there are no swaps
92+
sorted = true; // Initialize to indicate no swaps
93+
94+
for (int i = 0 ; i < n - 1 ; ++i) {
95+
if (strcmp(p[i], p[i + 1]) > 0) {
96+
sorted = false; // indicate we are out of order
97+
swap(&p[i], &p[i + 1]); // Swap the string addresses
98+
}
99+
}
100+
}
101+
}
102+
103+
/******************************************************
104+
* Swap two pointers *
105+
* The arguments are type 'pointer to pointer to char'*
106+
* This is necessary to interchange the two pointers. *
107+
******************************************************/
108+
109+
void swap(const char** p1, const char** p2)
110+
{
111+
const char *pT = *p1;
112+
*p1 = *p2;
113+
*p2 = pT;
114+
}
115+
116+
/******************************************************************
117+
* String output routine *
118+
* First argument is an array of const pointers to const strings. *
119+
* The second argument is the number of the number of strings. *
120+
******************************************************************/
121+
void str_out(const char* const* pStr, size_t n)
122+
{
123+
printf("The sorted strings are:\n");
124+
125+
for (size_t i = 0 ; i < n ; ++i)
126+
printf("%s\n", pStr[i]); // Display a string
127+
}
128+
129+
/********************************************
130+
* Free heap memory *
131+
* First argument is the array of pointers. *
132+
* Second argument is the number of strings *
133+
* in the array. *
134+
*******************************************/
135+
void free_memory(char **pS, size_t n)
136+
{
137+
for (size_t i = 0 ; i < n ; ++i) {
138+
free(pS[i]);
139+
pS[i] = NULL;
140+
}
141+
142+
free(pS);
143+
pS = NULL;
144+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Program 8.5 A function to increase your pay
2+
#include <stdio.h>
3+
4+
long *IncomePlus(long* pPay); // Prototype for increased pay function
5+
6+
int main(void)
7+
{
8+
long your_pay = 30000L; // Starting salary
9+
long *pold_pay = &your_pay; // Pointer to pay value
10+
long *pnew_pay = NULL; // Pointer to hold return value
11+
pnew_pay = IncomePlus(pold_pay);
12+
printf("Old pay = $%ld\n", *pold_pay);
13+
printf(" New pay = $%ld\n", *pnew_pay);
14+
return 0;
15+
}
16+
17+
// Definition of function to increment pay
18+
long* IncomePlus(long *pPay)
19+
{
20+
*pPay += 10000L; // Increment the value for pay
21+
return pPay; // Return the address
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Program 8.6 A function to increase your pay that doesn’t
2+
#include <stdio.h>
3+
4+
long *IncomePlus(long* pPay); // Prototype for increased pay function
5+
6+
int main(void)
7+
{
8+
long your_pay = 30000L; // Starting salary
9+
long *pold_pay = &your_pay; // Pointer to pay value
10+
long *pnew_pay = NULL; // Pointer to hold return value
11+
pnew_pay = IncomePlus(pold_pay);
12+
// printf("Old pay = $%ld\n", *pold_pay);
13+
// printf(" New pay = $%ld\n", *pnew_pay);
14+
printf("\nOld pay = $%ld New pay = $%ld\n", *pold_pay, *pnew_pay);
15+
return 0;
16+
}
17+
18+
// Definition of function to increment pay
19+
long *IncomePlus(long *pPay)
20+
{
21+
long pay = 0; // Local variable for the result
22+
pay = *pPay + 10000; // Increment the value for pay
23+
return &pay; // Return the address of the new pay
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Program 9.1 Pointing to functions
2+
#include <stdio.h>
3+
4+
// Function prototypes
5+
int sum(int, int);
6+
int product(int, int);
7+
int difference(int, int);
8+
9+
int main(void)
10+
{
11+
int a = 10; // Initial value for a
12+
int b = 5; // Initial value for b
13+
int result = 0; // Storage for results
14+
int (*pfun)(int, int); // Function pointer declaration
15+
pfun = sum; // Points to function sum()
16+
result = pfun(a, b); // Call sum() through pointer
17+
printf("pfun = sum result = %2d\n", result);
18+
pfun = product; // Points to function product()
19+
result = pfun(a, b); // Call product() through pointer
20+
printf("pfun = product result = %2d\n", result);
21+
pfun = difference; // Points to function difference()
22+
result = pfun(a, b); // Call difference() through pointer
23+
printf("pfun = difference result = %2d\n", result);
24+
return 0;
25+
}
26+
27+
int sum(int x, int y)
28+
{
29+
return x + y;
30+
}
31+
32+
int product(int x, int y)
33+
{
34+
return x * y;
35+
}
36+
37+
int difference(int x, int y)
38+
{
39+
return x - y;
40+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Program 9.2 Arrays of Pointers to functions
2+
#include <stdio.h>
3+
4+
// Function prototypes
5+
int sum(int, int);
6+
int product(int, int);
7+
int difference(int, int);
8+
9+
int main(void)
10+
{
11+
int a = 10; // Initial value for a
12+
int b = 5; // Initial value for b
13+
int result = 0; // Storage for results
14+
int (*pfun[3])(int, int); // Function pointer array declaration
15+
// Initialize pointers
16+
pfun[0] = sum;
17+
pfun[1] = product;
18+
pfun[2] = difference;
19+
20+
// Execute each function pointed to
21+
for (int i = 0 ; i < 3 ; ++i) {
22+
result = pfun[i](a, b); // Call the function through a pointer
23+
printf("result = %2d\n", result); // Display the result
24+
}
25+
26+
// Call all three functions through pointers in an expression
27+
result = pfun[1](pfun[0](a, b), pfun[2](a, b));
28+
printf("The product of the sum and the difference = %2d\n", result);
29+
return 0;
30+
}
31+
32+
int sum(int x, int y)
33+
{
34+
return x + y;
35+
}
36+
37+
int product(int x, int y)
38+
{
39+
return x * y;
40+
}
41+
42+
int difference(int x, int y)
43+
{
44+
return x - y;
45+
}

0 commit comments

Comments
 (0)