
ïŒçæ¡æåºïŒ Cèšèªæå®€ 第12å(ç¶) - 課é¡ãšæŒç¿ - 2023.02.08æŽæ°
kznããäž»å¬ã®ãCèšèªæå®€ã®ç¬¬12åã
ä»åã¯ãäžé詊éšã§ãã
ããããçã
é£æåºŠãäžãããæã«è² ããªããªããããªã®ã§ã
ç§ãšããŠã¯ãããã§ç¹ã皌ãã§ãããªããšãããŸããã
ããã°ããã°ã
ã§ã¯ãïŒãïŒãïŒãã¹ã¿ãŒãïŒ
åäœç¢ºèªç°å¢ã¯ãã¡ã
課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡1
é·æ¹åœ¢ã®ç蟺ãšé·èŸºã®é·ããåŒãæ°ãšããŠãé·æ¹åœ¢ã®é¢ç©ãæ±ãã颿°ãæžããªããããªãé·ãã¯æŽæ°ã§ããã
#include <stdio.h>
#define S_SIDE 4
#define L_SIDE 5
int test01(int x, int y) {
return x * y;
}
void main() {
printf("--- Test01 results ---\n");
printf("short_side_length = %d\n", S_SIDE);
printf("long_side_length = %d\n", L_SIDE);
printf("area of rectangle = %d\n", test01(S_SIDE, L_SIDE));
}--- Test01 results ---
short_side_length = 4
long_side_length = 5
area of rectangle = 20課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡2
課é¡1ã§æžãã颿°ãåŒãæ°ä»ããã¯ãã䜿ã£ãŠæžãçŽããªããã
#include <stdio.h>
#define TEST02(x,y) (x * y)
#define S_SIDE 6
#define L_SIDE 7
void main() {
printf("--- Test02 results ---\n");
printf("short_side_length = %d\n", S_SIDE);
printf("long_side_length = %d\n", L_SIDE);
printf("area of rectangle = %d\n", TEST02(S_SIDE, L_SIDE));
}--- Test02 results ---
short_side_length = 6
long_side_length = 7
area of rectangle = 42課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡3
åã®ååŸãåŒãæ°ãšããŠãåã®é¢ç©ãæ±ãã颿°ãæžããªããããªãååŸã¯æŽæ°ã§è¯ããé¢ç©ã¯å°æ°ãšãããååšçã¯ãã¯ãã䜿ã£ãŠå®çŸ©ããªããã
#include <stdio.h>
#define PI 3.14
#define RADIUS 4
double test03(int x) {
return x * x * PI;
}
void main() {
printf("--- Test03 results ---\n");
printf("radius = %d\n", RADIUS);
printf("area of circle = %f\n", test03(RADIUS));
}--- Test03 results ---
radius = 4
area of circle = 50.240000âââ ãããCèšèªã«èªä¹ã®æŒç®åã£ãŠãç¡ãã£ããã§ããã£ãïŒ
課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡4
æŽæ°ã®é åãšèŠçŽ ã®æ°ãåŒãæ°ã§æž¡ããé åã®ããããã®èŠçŽ ã«ãŒããä»£å ¥ãã颿°ãæžããªããã
#include <stdio.h>
#include <stdlib.h>
#define SAMPLE 10
void test04(int* x, int y) {
for (int i = 0; i < y; i++) {
*x++ = 0;
}
}
void main() {
int s = SAMPLE;
int *a;
a = (int*)malloc(sizeof(int) * s);
if (a == NULL) exit(0);
test04(a, s); //é
åã®èŠçŽ ãå
šãŠãŒãã«ããŸã
printf("--- Test04 results ---\n");
for (int i = 0; i < s; i++) {
printf("%d\n", *a++ );
}
free(a);
}--- Test04 results ---
0
0
0
0
0
0
0
0
0
0â2023.02.03ä¿®æ£ïŒãã€ã³ã¿å€æ° *x 㯠intåãªã®ã§ãNULLãã»ããããã®ã¯ãã¡ãšã®ãæå°ãããã ããŸãããæé£ãããããŸãã以äžãtext04*()颿°ãæµçšããŠãã課é¡5ã8ã«ã€ããŠãä¿®æ£ããŸããã
課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡5
æŽæ°ã®é åãšèŠçŽ ã®æ°ãåŒãæ°ã§æž¡ããé åã®ããããã®èŠçŽ ã«ã©ã³ãã ãªå€ãä»£å ¥ãã颿°ãæžããªãããã©ã³ãã ãªå€ãåŸãã«ã¯ stdlib.h ã§å®çŸ©ãããŠãã rand() ã䜿ãããšã
#include <stdio.h>
#include <stdlib.h>
#define SAMPLE 10
#define RANGE 10
void test04(int* x, int y) {
for (int i = 0; i < y; i++) {
*x++ = 0;
}
}
void test05(int *x, int y) {
for (int i = 0; i < y; i++) {
*x++ = rand() % RANGE;
}
}
void main() {
int s = SAMPLE;
int* a;
a = (int*)malloc(sizeof(int) * s);
if (a == NULL) exit(0);
test04(a, s); //é
åã®èŠçŽ ãå
šãŠãŒãã«ããŸã
test05(a, s); //é
åã«ä¹±æ°ãå²ãä»ããŸã
printf("--- Test05 results ---\n");
for (int i = 0; i < s; i++) {
printf("%d\n", *a++);
}
free(a);
}--- Test05 results ---
5
8
0
0
5
9
1
7
9
4âââãäžè¬çã«ã¯æéãä¹±æ°ã®ã·ãŒããšããŠäžãããšæãã®ã§ããããã®ããã«time.hãincludeããããšãããšãWebçã ãšå®è¡æã«ã¹ã¯ãªãããšã©ãŒã«ãªããŸããã®ã§ããã®ã³ãŒãã«ã¯å«ããŠããŸããã
⻠課é¡ã«æç€ºã¯ãããŸããã§ããããä¹±æ°ã®ç¯å²ã¯0-9ã«éå®ããŸããã
課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡6
課é¡5ã§äœã£ãé åãåŒãæ°ã§æž¡ããŠããã®é åã«å«ãŸããå€ã®æå°å€ãè¿ã颿°ãæžããªããã
#include <stdio.h>
#include <stdlib.h>
#define SAMPLE 10
#define RANGE 10
void test04(int* x, int y) {
for (int i = 0; i < y; i++) {
*x++ = 0;
}
}
void test05(int *x, int y) {
for (int i = 0; i < y; i++) {
*x++ = rand() % RANGE;
}
}
int test06(int *x, int y) {
int min = INT_MAX;
for (int i = 0; i < y; i++) {
min = (x[i] < min) ? x[i] : min;
}
return min;
}
void main() {
int s = SAMPLE;
int *a;
int min;
a = (int*)malloc(sizeof(int) * s);
if (a == NULL) exit(0);
test04(a, s); //é
åã®èŠçŽ ãå
šãŠãŒãã«ããŸã
test05(a, s); //é
åã«ä¹±æ°ãå²ãä»ããŸã
min = test06(a, s); //é
åã®æå°å€ãæ±ããŸã
printf("--- Test06 results ---\n");
for (int i = 0; i < s; i++) {
printf("%d\n", *a++);
}
printf("minimum = %d\n", min);
free(a);
}--- Test06 results ---
5
6
9
5
5
6
4
4
8
9
minimum = 4â»ãµã³ãã«æ°ã倧ãããšãæå°å€ãã»ãŒïŒã«ãªãã®ã§ããµã³ãã«æ°ã10ã«ããŸããã
課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡7
課é¡6ã§äœã£ã颿°ãåèã«ããŠãé åã«å«ãŸããå€ã®æå°å€ãšãæå°å€ã§ãã£ãèŠçŽ ãããã€ãã£ãã®ããè¿ã颿°ãæžããªããã
#include <stdio.h>
#include <stdlib.h>
#define SAMPLE 10
#define RANGE 10
void test04(int* x, int y) {
for (int i = 0; i < y; i++) {
*x++ = 0;
}
}
void test05(int *x, int y) {
for (int i = 0; i < y; i++) {
*x++ = rand() % RANGE;
}
}
int test06(int *x, int y) {
int min = INT_MAX;
for (int i = 0; i < y; i++) {
min = (x[i] < min) ? x[i] : min;
}
return min;
}
void test07(int *x, int *y, int z) {
for (int i = 0; i < z; i++) {
y[ x[i] ]++;
}
}
void main() {
int s = SAMPLE;
int *a;
int min;
int count[RANGE];
a = (int*)malloc(sizeof(int) * s);
if (a == NULL) exit(0);
test04(a, s); //é
åã®èŠçŽ ãå
šãŠãŒãã«ããŸã
test04(count, RANGE); //é
åã®èŠçŽ ãå
šãŠãŒãã«ããŸã
test05(a, s); //é
åã«ä¹±æ°ãå²ãä»ããŸã
min = test06(a, s); //é
åã®æå°å€ãæ±ããŸã
test07(a, count, s); // åèŠçŽ æ¯ã®åºçŸåæ°ãã«ãŠã³ãããé
åcountã«æ ŒçŽããŸã
printf("--- Test07 results ---\n");
for (int i = 0; i < s; i++) {
printf("%d\n", *a++);
}
printf("minimum = %d\n", min);
printf("count = %d\n", count[min]);
free(a);
}--- Test07 results ---
5
9
7
7
2
8
2
3
5
9
minimum = 2
count = 2
â2023.01.28ä¿®æ£ïŒä¹±æ°ã®åºçŸåæ°ãæ ŒçŽããé åcountã®å®çŸ©ãmain()ã«ç§»åããæŽã«é åã®äžèº«ãåæåããã«ã¯test04()ãäœ¿ãæ§ã«ãããžãã¯ãèŠçŽããŸããã
課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡8
åŒãæ°ã§æž¡ãããæååã®äžã«ããã£ãšãå€ãå«ãŸããæåãšããã®æ°ãè¿ã颿°ãæžããªãããå«ãŸããæåã®æ°ãåãå Žåã«ã¯ãã©ã®æåãè¿ããŠãè¯ãã
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define INT_MIN -2147483648
#define SAMPLE_TEXT "The Quick Brown Fox Jumps Over The Lazy Dog"
#define LETTERS 255
void test04(int* x, int y) {
for (int i = 0; i < y; i++) {
*x++ = 0;
}
}
void test08_01(char *x, int *y, int z) {
for (int i = 0 ; i < z; i++) {
y [ (unsigned char) x[i] ]++;
}
}
char test08_02(int *x) {
int max = INT_MIN;
int index = 0;
for (int i = 0; i < 0xff; i++) {
if ( x[i] > max) {
max = x[i];
index = i;
}
}
return (char)index;
}
/* 衚瀺æå以å€ãâ\xâã§å§ãŸã16é²ã³ãŒãã®æååã«å€æãã - 第10å課é¡ã§äœæãã颿°ãæµçš*/
void char_to_str(char *s, char c)
{
if (isgraph(c))
{
sprintf(s, "%c\0", (unsigned char)c);
}
else
{
sprintf(s, "\\x%02x\0", (unsigned char)c);
}
}
void ctrl_to_str(char *dst, char *src)
{
int len = strlen(src);
int i = 0;
char s[5] = {0};
for (i = 0; i < len; i++)
{
if (src[i] == '\\')
{
s = "\\\\";
}
else
{
char_to_str(s, src[i]);
}
strcat(dst, s);
}
}
void main() {
int count[LETTERS];
char txt[] = SAMPLE_TEXT;
char letter, *out1, out2[];
out1 = (char*)realloc(NULL, (strlen(txt) * 4 + 1));
if (out1 == NULL) exit(-1);
test04(count, LETTERS); //é
åã®èŠçŽ ãå
šãŠãŒãã«ããŸã
ctrl_to_str(out1, txt);
test08_01(txt, count, strlen(txt)); //çºçé »åºŠãã«ãŠã³ãããŸã
letter = test08_02(count); //æå€§äœ¿çšé »åºŠã®æåãæ€çŽ¢ããŸã
char_to_str(out2, letter); //é衚瀺æåãç·šéããŸã
printf("--- Test08 results ---\n");
printf("source text = \"%s\"\n", out1);
printf("most used letter = \"%s\"\n", out2);
printf("frequency of use = %d\n", count [(unsigned char)letter] );
free(out1);
}â2023.01.29ä¿®æ£ïŒ
1. æåã®çºçé »åºŠãæ ŒçŽããé
åcountãåæåããã®ã¯test04()ã«çµ±äžã
2. æåã®çºçé »åºŠãã«ãŠã³ãããããžãã¯ãtest08_01()ãšããŠåé¢ã
3. æå€§äœ¿çšé »åºŠã®æåãæ€çŽ¢ããããžãã¯ãtest08_02()ãšããã
4. 倿°å®çŸ©ã®èŠçŽãã
â2023.02.08ä¿®æ£ïŒkznããã®ãææã«åºã¥ããŠä¿®æ£ããŸããã
1. SAMPLE_TEXT ããchar[]ã§å®çŸ©ããã
2. 笊å·ãªãåãžã®å倿ãããïŒïŒç®æïŒ
void test08_01(char *x, int *y, int z) {
for (int i = 0 ; i < z; i++) {
yã[ (unsigned char) x[i] ]++;
}
}printf("frequency of use = %d\n", count [(unsigned char)letter] );3. 衚瀺å¯èœãªæå以å€ã¯ã"\x99"ã®åœ¢åŒã§è¡šç€ºããã
--- Test08 results ---
source text = "The\x20Quick\x20Brown\x20Fox\x20Jumps\x20Over\x20The\x20Lazy\x20Dog"
most used letter = "\x20"
frequency of use = 8課é¡ã«ãããåŒãæ°ã§æž¡ãããæååããã衚瀺å¯èœãªæåã ããšã¯éããªãã®ã§ãå
šæåçš®ãã«ãŠã³ãããŸããã
ãµã³ãã«ã®è±æã ãšãæãé »åºŠãå€ãã®ã¯ã¹ããŒã¹ã ã£ããããŠé¢çœããããŸãããããã§ããµã³ãã«ã®æååãã匷å¶çã«ã¹ããŒã¹ãæé€ããŠãå床å®è¡ããŸããã
--- Test08 results ---
source text = "TheQuickBrownFoxJumpsOverTheLazyDog"
most used letter = "e"
frequency of use = 3ãã¡ãã®æ¹ããæ©èœããŠããã£ãœãã
課é¡ïŒã®çæ¡ãšå®è¡çµæ
課é¡9
åŒãæ°ã§æž¡ãããïŒã€ã®æååãåçã«ç¢ºä¿ããé åã«é£çµããŠè¿ã颿°ãæžããªãããïŒã€ã®æååãé£çµãã颿°ãæžããŠäœ¿ã£ãŠãè¯ããstdlib.h ã«ãã realloc() ãåèã«ããããšã
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEXT1 "Ich "
#define TEXT2 "liebe "
#define TEXT3 "dich."
char *test09(char *x, char *y, char *z) {
int length = 0;
char *a, *b, *c;
length = strlen(x) + 1;
a = (char*)realloc(NULL, length);
if (a == NULL) exit(-1);
strncpy(a, x, length);
length += strlen(y);
b = (char*)realloc(a, length);
if (b == NULL) exit(-1);
strncat(b, y, length);
length += strlen(z);
c = (char*)realloc(b, length);
if (c == NULL) exit(-1);
strncat(c, z, length);
return c;
}
void main() {
char t1[] = TEXT1;
char t2[] = TEXT2;
char t3[] = TEXT3;
char *dup;
printf("--- Test09 results ---\n");
printf("1st text = \"%s\"\n", t1);
printf("2nd text = \"%s\"\n", t2);
printf("3rd text = \"%s\"\n", t3);
dup = test09(t1, t2, t3);
printf("concatinated text \"%s\"\n", dup);
free(dup);
}â2023.02.08ä¿®æ£ïŒkznããã®ãææã«åºã¥ããŠãrealloc()ã§ã¡ã¢ãªã確ä¿ã§ããªãå Žåã®æ»ãå€ãâ1ãšããŸããã
--- Test09 results ---
1st text = "Ich "
2nd text = "liebe "
3rd text = "dich."
concatinated text "Ich liebe dich."æŒç¿ã®çæ¡ãšå®è¡çµæ
æŒç¿
main颿°ã¯å®ã¯ int main(int argc, char** argv) ãšãã圢ã§ã³ãã³ãã©ã€ã³ã®åŒãæ°ãåãåãããšãåºæ¥ãŸããæåã®åŒãæ°ã«ã³ãã³ãã©ã€ã³åŒãæ°ã®æ°ã次ã®åŒãæ°ã«ã³ãã³ãã©ã€ã³ã§æž¡ãããæååãžã®ãã€ã³ã¿ãå ¥ã£ãŠããé åãæž¡ãããŸãããããè§£éããŠã³ãã³ãã©ã€ã³ã§äžããããæååã衚瀺ããã³ãŒããæžããªããã
exercises.c
#include <stdio.h>
int main(int argc, char *argv[]) {
for(int i = 1; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}gccã§ã³ã³ãã€ã«ããŸã
Last login: Sun Jan 29 09:00:17 on ttys000
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
jm3nrhMBP-EN:~ akio$ cd \Documents
jm3nrhMBP-EN:Documents akio$ cd \c
jm3nrhMBP-EN:c akio$ gcc exercises.cå®è¡çµæïŒãã®1ïŒ
jm3nrhMBP-EN:c akio$ ./a.out Hello world
Hello
world"Hello"ãš"world"ã®ïŒã€ã®åŒæ°ãæž¡ããŸããã®ã§ãæ³å®éããããããé£ç¶ããŠè¡šç€ºãããŸãã
å®è¡çµæïŒãã®2ïŒ
jm3nrhMBP-EN:c akio$ ./a.out âHello worldâ
âHello
worldââHello worldâãšããããšã§äžã€ã®åŒæ°ãšããŠæ±ã£ãŠãããããšãæåŸ ããã®ã§ãããããã«ã¯ãªããŒã·ã§ã³ãå«ããŠåŒæ°ãšããŠæ±ãããŸããã
è§£çïŒ2023.02.03ïŒ ããã 課é¡ïŒãïŒ
ãªããªãæºç¹ãåãã®ã¯é£ããã§ãã
課é¡4ã§äœæããtest04()颿°ãã課é¡5ã8ã§ã䜿ããŸãããŠããã®ã§ãããtest04()颿°ã®äžã§intåã®ãã€ã³ã¿å€æ°ã«NULLãã»ããããã®ã¯å¥œãŸãããªããšã®ãæå°ããããäžæ°ã«è»äžŠã¿æžç¹ãã€ããŠããŸããŸãããããããã
è§£çïŒ2023.02.03ïŒ ããã 課é¡ïŒãïŒ + æŒç¿
課é¡ïŒãïŒã«ãããŠããæå°ãããã ããŸãããæé£ãããããŸãã
ãšããããåãããŠã¿ãŠåäœç¢ºèªã¯ããŠããã®ã§ãããç§ã®æ§ãªçŽ äººãæãã€ãçšåºŠã®ãã¹ãã£ãŠçµéšå€ãæµ
ãããŠãæ¡ä»¶ãé§ã
æŒãã§ããã
ãšãŠãæãããã£ãŠãçµã¿èŸŒã¿ã¢ããªãšãæãã€ãããããã«ãããŸããã
ã§ããçããã®ã³ã¡ã³ãã®ãããã§ãã©ã€ã¢ã³ããšã©ãŒãç¹°ãè¿ãããšã§ãå°ãã¥ã€é²åããŠããæ§ãªæ°ãããŸããããã£ãŠãã¢ãžã£ã€ã«éçºã£ãŠåŒãã§ããã®ããªïŒïŒç¬
ãããããã°ããã®éãèªäž»äŒåŠããŸãã
ç¶ãã¯ãç§ãæ¥æ¬ã«åž°åœããŠäžæ®µèœããåŸã§åéããããšæã£ãŠãŸãã
ãããŸã§èªãã§ããã ããæé£ãããããŸããã