Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c7a088

Browse files
committed
Exercise 27.13; Exercise 27.14
1 parent cde623b commit 9c7a088

File tree

5 files changed

+340
-0
lines changed

5 files changed

+340
-0
lines changed

‎Chapter_27/C27_Exercise_27.13.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/* Exercise 27.13 */
2+
3+
#include<stdio.h>
4+
#include<stdbool.h>
5+
#include<stdlib.h>
6+
#include<string.h>
7+
#include<ctype.h>
8+
#include"C27_Exercise_27.13.h"
9+
10+
11+
int main()
12+
{
13+
enum Action {
14+
EXIT = -1, PRINTACTIONLIST,
15+
CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, CASE9, CASE10
16+
};
17+
const char* actionList = "\tList of actions:\n"
18+
" (1) Read string\n"
19+
" (-1) Exit, (0) Print the list of actions\n\n";
20+
printf("%s", actionList);
21+
int action;
22+
bool cond = true;
23+
while (cond) {
24+
printf("\nPlease enter the action: ");
25+
int ret = scanf("%d", &action);
26+
char ch;
27+
while ((ch = getchar()) != '\n');
28+
if (ret <= 0) {
29+
printf("\n\n\t\tError. Incorrect input\n");
30+
continue;
31+
}
32+
switch (action) {
33+
case CASE1: {
34+
putchar('\n');
35+
FILE* pOut;
36+
pOut = fopen("C27_Exercise_27.13.txt", "w");
37+
if (pOut == NULL) break;
38+
String s;
39+
Initialize(&s, 5);
40+
printf("Enter a string (press ENTER -> CONTROL+Z -> ENTER to end input):\n");
41+
while (GetString(stdin, &s)) {
42+
fprintf(pOut, "%s\n", s.str);
43+
}
44+
Destroy(&s);
45+
fclose(pOut);
46+
putchar('\n');
47+
break;
48+
}
49+
case PRINTACTIONLIST:
50+
printf("%s", actionList);
51+
break;
52+
case EXIT:
53+
cond = false;
54+
break;
55+
default:
56+
printf("\t\tError. Incorrect action number\n");
57+
break;
58+
}
59+
}
60+
return SUCCESS;
61+
}
62+
63+
void PrintError(const char* message)
64+
{
65+
fprintf(stderr, "\t\tError -> ");
66+
fprintf(stderr, message);
67+
fprintf(stderr, "\n");
68+
}
69+
70+
bool Initialize(String* string, size_t size)
71+
{
72+
if (string == NULL) {
73+
PrintError("Initialize(): arg1 == NULL");
74+
return false;
75+
}
76+
if (size == 0) {
77+
PrintError("Initialize(): arg2 == 0");
78+
return false;
79+
}
80+
string->str = (char*)malloc(sizeof(char) * size);
81+
if (string->str == NULL) {
82+
PrintError("Initialize(): no memory allocated");
83+
return false;
84+
}
85+
string->sz = 0;
86+
string->maxsz = size;
87+
return true;
88+
}
89+
90+
bool Extend(String* string)
91+
{
92+
if (string == NULL) {
93+
PrintError("Extend(): arg1 == NULL");
94+
return false;
95+
}
96+
char* newBlock = (char*)realloc(string->str, sizeof(char) * string->maxsz * EXTENDMULTIPLIER);
97+
if (newBlock == NULL) {
98+
PrintError("Extend(): no memory reallocated");
99+
return false;
100+
}
101+
string->str = newBlock;
102+
string->maxsz *= EXTENDMULTIPLIER;
103+
return true;
104+
}
105+
106+
bool Destroy(String* string)
107+
{
108+
if (string == NULL) {
109+
PrintError("Destroy(): arg1 == NULL");
110+
return false;
111+
}
112+
free(string->str);
113+
string->str = NULL;
114+
string->sz = string->maxsz = 0;
115+
return true;
116+
}
117+
118+
bool GetString(FILE* stream, String* string)
119+
{
120+
if (string == NULL) {
121+
PrintError("GetString(): arg1 == NULL");
122+
return false;
123+
}
124+
if (feof(stream)) return false;
125+
int c;
126+
while (true) {
127+
c = getc(stream);
128+
if (c == EOF) return false;
129+
if (isspace(c) == false) {
130+
ungetc(c, stream);
131+
break;
132+
}
133+
}
134+
string->sz = 0;
135+
string->str[string->sz] = '0円';
136+
while (true) {
137+
c = getc(stream);
138+
if (string->sz == string->maxsz) {
139+
if (Extend(string) == false) {
140+
ungetc(c, stream);
141+
ungetc(string->str[--string->sz], stream);
142+
string->str[string->sz] = '0円';
143+
return false;
144+
}
145+
}
146+
if (isspace(c)) {
147+
string->str[string->sz++] = '0円';
148+
return true;
149+
}
150+
string->str[string->sz++] = c;
151+
}
152+
}

‎Chapter_27/C27_Exercise_27.13.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Exercise 27.13 */
2+
3+
#ifndef C27_Exercise_27_13_H
4+
#define C27_Exercise_27_13_H
5+
6+
#define EXTENDMULTIPLIER 2
7+
8+
enum ReturnTypeCommon { FAILURE = -1, SUCCESS };
9+
10+
const char* sp_2 = " ";
11+
const char* sp_4 = " ";
12+
const char* sp_6 = " ";
13+
const char* sp_8 = " ";
14+
const char* vsp_2 = "\n\n";
15+
const char* vsp_3 = "\n\n\n";
16+
const char* vsp_4 = "\n\n\n\n";
17+
18+
typedef struct StructString {
19+
char* str;
20+
int sz;
21+
int maxsz;
22+
} String;
23+
24+
void PrintError(const char* message);
25+
bool Initialize(String* string, size_t size);
26+
bool Extend(String* string);
27+
bool Destroy(String* string);
28+
bool GetString(FILE* stream, String* string);
29+
30+
#endif

‎Chapter_27/C27_Exercise_27.14.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* Exercise 27.14 */
2+
3+
#include<stdio.h>
4+
#include<stdbool.h>
5+
#include<stdlib.h>
6+
#include<string.h>
7+
#include<ctype.h>
8+
#include"C27_Exercise_27.14.h"
9+
10+
11+
int main()
12+
{
13+
enum Action {
14+
EXIT = -1, PRINTACTIONLIST,
15+
CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, CASE9, CASE10
16+
};
17+
const char* actionList = "\tList of actions:\n"
18+
" (1) LargestSmallestMedianMean()\n"
19+
" (-1) Exit, (0) Print the list of actions\n\n";
20+
printf("%s", actionList);
21+
int action;
22+
bool cond = true;
23+
while (cond) {
24+
printf("\nPlease enter the action: ");
25+
int ret = scanf("%d", &action);
26+
char ch;
27+
while ((ch = getchar()) != '\n');
28+
if (ret <= 0) {
29+
printf("\n\n\t\tError. Incorrect input\n");
30+
continue;
31+
}
32+
switch (action) {
33+
case CASE1: {
34+
putchar('\n');
35+
printf("\nPlease enter size of array: ");
36+
int size;
37+
if (scanf("%d", &size) <= 0 || size <= 0) {
38+
PrintError("Incorrect input");
39+
break;
40+
}
41+
int* arr = (int*)malloc(sizeof(int) * size);
42+
if (arr == NULL) {
43+
PrintError("No memory reallocated");
44+
break;
45+
}
46+
bool proceed = true;
47+
for (int i = 0; i < size; ++i) {
48+
printf("Please enter a number #%d: ", i + 1);
49+
int number;
50+
if (scanf("%d", &number) <= 0) {
51+
PrintError("Incorrect input");
52+
proceed = false;
53+
break;
54+
}
55+
arr[i] = number;
56+
}
57+
Value* pval = LargestSmallestMedianMean(arr, size);
58+
if (pval == NULL) break;
59+
printf("Largest: %d\n", pval->largest);
60+
printf("Smallest: %d\n", pval->smallest);
61+
printf("Median: %f\n", pval->median);
62+
printf("Mean: %f\n", pval->mean);
63+
64+
putchar('\n');
65+
break;
66+
}
67+
case PRINTACTIONLIST:
68+
printf("%s", actionList);
69+
break;
70+
case EXIT:
71+
cond = false;
72+
break;
73+
default:
74+
printf("\t\tError. Incorrect action number\n");
75+
break;
76+
}
77+
}
78+
return SUCCESS;
79+
}
80+
81+
void PrintError(const char* message)
82+
{
83+
fprintf(stderr, "\t\tError -> ");
84+
fprintf(stderr, message);
85+
fprintf(stderr, "\n");
86+
}
87+
88+
Value* LargestSmallestMedianMean(int* arr, size_t size)
89+
{
90+
if (arr == NULL) {
91+
PrintError("LargestSmallestMedianMean(): arg1 == NULL");
92+
return NULL;
93+
}
94+
Value* pval = (Value*)malloc(sizeof(Value));
95+
if (pval == NULL) {
96+
PrintError("LargestSmallestMedianMean(): no memory reallocated");
97+
return NULL;
98+
}
99+
qsort(arr, size, sizeof(int), CompareInt);
100+
pval->largest = arr[size - 1];
101+
pval->smallest = arr[0];
102+
if (size % 2) {
103+
pval->median = arr[size / 2];
104+
}
105+
else {
106+
pval->median = (arr[size / 2 - 1] + arr[size / 2]) / 2;
107+
}
108+
pval->mean = 0.0;
109+
for (int i = 0; i < size; ++i) {
110+
pval->mean += arr[i];
111+
}
112+
pval->mean /= size;
113+
return pval;
114+
}
115+
116+
int CompareInt(const void* a, const void* b)
117+
{
118+
return (*(int*)a - *(int*)b);
119+
}

‎Chapter_27/C27_Exercise_27.14.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Exercise 27.14 */
2+
3+
#ifndef C27_Exercise_27_14_H
4+
#define C27_Exercise_27_14_H
5+
6+
enum ReturnTypeCommon { FAILURE = -1, SUCCESS };
7+
8+
const char* sp_2 = " ";
9+
const char* sp_4 = " ";
10+
const char* sp_6 = " ";
11+
const char* sp_8 = " ";
12+
const char* vsp_2 = "\n\n";
13+
const char* vsp_3 = "\n\n\n";
14+
const char* vsp_4 = "\n\n\n\n";
15+
16+
typedef struct StructValue {
17+
int largest;
18+
int smallest;
19+
double median;
20+
double mean;
21+
} Value;
22+
23+
void PrintError(const char* message);
24+
Value* LargestSmallestMedianMean(int* arr, size_t size);
25+
int CompareInt(const void* a, const void* b);
26+
27+
#endif

‎Chapter_27/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ set (FILE_NAME7 C27_Exercise_27.9)
2929
set (SOURCE_CXX_LIST7 ${FILE_NAME7}.cpp)
3030
set (FILE_NAME8 C27_Exercise_27.12)
3131
set (SOURCE_CXX_LIST8 ${FILE_NAME8}.cpp)
32+
set (FILE_NAME9 C27_Exercise_27.13)
33+
set (SOURCE_CXX_LIST9 ${FILE_NAME9}.cpp)
34+
set (FILE_NAME10 C27_Exercise_27.14)
35+
set (SOURCE_CXX_LIST10 ${FILE_NAME10}.cpp)
3236

3337

3438
# Add source to this project's executable.
@@ -40,6 +44,8 @@ add_executable (${FILE_NAME5} ${SOURCE_CXX_LIST5})
4044
add_executable (${FILE_NAME6} ${SOURCE_CXX_LIST6})
4145
add_executable (${FILE_NAME7} ${SOURCE_CXX_LIST7})
4246
add_executable (${FILE_NAME8} ${SOURCE_CXX_LIST8})
47+
add_executable (${FILE_NAME9} ${SOURCE_CXX_LIST9})
48+
add_executable (${FILE_NAME10} ${SOURCE_CXX_LIST10})
4349

4450
if (${COMPILE_AS_C})
4551
target_compile_options(${FILE_NAME} PRIVATE "/TC")
@@ -49,6 +55,8 @@ target_compile_options(${FILE_NAME5} PRIVATE "/TC")
4955
target_compile_options(${FILE_NAME6} PRIVATE "/TC")
5056
target_compile_options(${FILE_NAME7} PRIVATE "/TC")
5157
target_compile_options(${FILE_NAME8} PRIVATE "/TC")
58+
target_compile_options(${FILE_NAME9} PRIVATE "/TC")
59+
target_compile_options(${FILE_NAME10} PRIVATE "/TC")
5260
endif()
5361

5462
# TODO: Add tests and install targets if needed.
@@ -68,3 +76,7 @@ install (TARGETS ${FILE_NAME7} CONFIGURATIONS Debug DESTINATION Build/Debug)
6876
install (TARGETS ${FILE_NAME7} CONFIGURATIONS Release DESTINATION Build)
6977
install (TARGETS ${FILE_NAME8} CONFIGURATIONS Debug DESTINATION Build/Debug)
7078
install (TARGETS ${FILE_NAME8} CONFIGURATIONS Release DESTINATION Build)
79+
install (TARGETS ${FILE_NAME9} CONFIGURATIONS Debug DESTINATION Build/Debug)
80+
install (TARGETS ${FILE_NAME9} CONFIGURATIONS Release DESTINATION Build)
81+
install (TARGETS ${FILE_NAME10} CONFIGURATIONS Debug DESTINATION Build/Debug)
82+
install (TARGETS ${FILE_NAME10} CONFIGURATIONS Release DESTINATION Build)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /