-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMidtermExamPrac.c
869 lines (588 loc) · 12.5 KB
/
MidtermExamPrac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
// 2021 -2 중간고사 연습문제 코드
//============== 1번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// =============================================
// 구조체와 열거형을 정의
enum is_correct { WRONGANSWER, ACCEPTED };
typedef struct {
enum is_correct result;
int score;
}Quiz;
// ==============================================
int main()
{
Quiz midterm[5] = {
{WRONGANSWER,10},
{ACCEPTED,20},
{ACCEPTED,10},
{WRONGANSWER,15},
{ACCEPTED,45} };
int sum = 0; //점수 합계저장하는 변수
// =========================================
// 시험점수 합계를 구하는 코드 작성
for (int i = 0; i < 5; i++)
{
if (midterm[i].result == ACCEPTED)
{
sum += midterm[i].score;
}
}
// ========================================
printf("%d", sum);
}
#endif
// =========== 2번 ===============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int a[10], b[10];
//==========================================
// 이 부분에 소스코드를 작성해 주세요.
// 포인터 변수 선언, 값을 복사하는 프로그램 등등
for (int i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
}
char* p = (char*)&a;
char* pp = (char*)&b;
for (int i = 0; i < sizeof(a); i++)
{
*(pp+i) = *(p + i);
}
//==========================================
//배열 b를 출력하는 부분
for (int i = 0; i < 10; i++)
{
printf("b[%d] ---> %d\n", i, b[i]);
}
return;
}
#endif
// ================== 3번 ===================
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int myStrlen(char* string);
int main() {
char arr[100];
scanf("%s", arr);
printf("문자열의 길이 : %d\n", myStrlen(arr));
return 0;
}
// ======================================
// myStrlen 함수 정의를 여기에 작성
int myStrlen(char* string) {
int cnt = 0;
while (string[cnt] != NULL) {
cnt++;
}
return cnt;
}
// =====================================
#endif
//============== 4번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct Customer {
char name[10];
int visit;
};
void showInfo(int _index, struct Customer* _parr);
int main()
{
struct Customer list[10];
int index = 0;
char name[10];
while (1) {
scanf("%s", name);
if (strcmp(name, "end") == 0) {
break;
}
strcpy(list[index].name, name);
scanf("%d", &(list[index].visit));
index++;
}
showInfo(index, list);
}
// ================================================
// showInfo 함수의 정의를 구현
void showInfo(int _index, struct Customer* _parr) {
for (int i = 0; i < _index; i++)
{
printf("%s ---> %d\n", (_parr + i)->name, (_parr + i)->visit);
}
}
//=================================================
#endif
//============== 5번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
char phone[15];
scanf("%s", phone);
// ================================================
// 이 부분에 코드를 작성하세요.
// 포인터를 사용해 '-'를 공백으로 바꾸기
char* p = phone;
while (*p != NULL)
{
if (*p == '-')
{
*p = ' ';
}
p++;
}
//=================================================
printf("phone: %s",phone);
}
#endif
//============== 6번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
char text[50];
scanf("%s", text);
// ================================================
// 이 부분에 코드를 작성하세요.
char* p = text;
int cnt = 0;
while (*p != '!')
{
if (*p >= 'A' && *p <= 'Z')
{
cnt++;
}
p++;
if (*p == NULL)
{
printf("%d", cnt);
return 0;
}
}
printf("%d", cnt);
return 0;
//=================================================
}
#endif
//==============7번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void SWAP(int* text, int a, int b);
int main()
{
int text[8] = { 1,2,3,4,5,6,7,8 };
for (int i = 0; i < 4; i++)
{ // SWAP함수에 적당한 인자를 넣어 호출하라
//SWAP(, , );
SWAP(text, i, 7 - i);
}
for (int i = 0; i < 8; i++)
{
printf("%d \n", text[i]);
}
}
// =============================================
// SWAP함수를 구현하라
void SWAP(int* text, int a, int b) {
int temp = *(text + a);
*(text + a) = *(text + b);
*(text + b) = temp;
}
// =============================================
#endif
//============== 8번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void Pick(char** p, int part);
char person1[10] = "kim";
char person2[10] = "lee";
char person3[10] = "park";
int main()
{
char* p = NULL;
int part;
scanf("%d", &part);
Pick(&p, part);
printf("%s", p);
}
// =============================================
// PICK 함수를 구현하라
void Pick(char** p, int part) {
switch (part % 3) {
case 0:
*p = person1;
break;
case 1:
*p = person2;
break;
case 2:
*p = person3;
break;
}
}
// =============================================
#endif
//============== 9번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int sub1(int _a, int _b)
{
return _a - _b;
}
int sub2(int _a, int _b)
{
return _b - _a;
}
int main()
{
int result = 0;
int a, b;
scanf("%d %d", &a, &b);
// =============================================
// 이 부분에 코드를 작성하여 넣으시오.
int (*pf)(int, int) = NULL;
if (a > b)
{
pf = sub1;
}
else
{
pf = sub2;
}
result = pf(a, b);
// ==============================================
printf("result: %d", result);
return 0;
}
#endif
//============== 10번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void SWAP(char* p, int size);
int main()
{
char text[50];
scanf("%s", text);
SWAP(text, strlen(text));
printf("%s", text);
return 0;
}
// =============================================
// SWAP함수 정의
void SWAP(char* p, int size)
{
int cur = 0;
while (*(p + cur) != NULL)
{
if (*(p+cur) == 'a')
{
if (*(p + cur + 1) == 'n')
{
if (*(p + cur + 2) == 'd')
{
*(p+cur) = '&';
for (int i = cur+1; i < size-1; i++)
{
*(p + i) = *(p + i + 2);
}
size -= 2;
}
}
}
cur++;
}
}
// ===============================================
#endif
//============== 11번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
typedef enum {SAMSUNG, KAKAO, LG} COMPANY;
struct Stock {
COMPANY company;
int num;
int price;
};
int main()
{
struct Stock account[3] = {
{SAMSUNG, 10, 90000},
{KAKAO, 5, 100000},
{LG, 3, 110000}
};
int cur_price[3];
cur_price[SAMSUNG] = 69400;
cur_price[KAKAO] = 121500;
cur_price[LG] = 125000;
int revenue = 0;
// ====================================================
// 이 부분에 코드를 작성하세요.
for (int i = 0; i < 3; i++)
{
revenue += account[i].num * (cur_price[i] - account[i].price);
}
// ===================================================
printf("%d", revenue);
}
#endif
//============== 12번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include < stdlib.h >
int area(int _x1, int _x2, int _y1, int _y2);
int main()
{
int N;
int x1, x2, y1, y2;
int sum = 0; //면적의 합계
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
sum += area(x1, x2, y1, y2);
}
printf("%d", sum);
}
int area(int _x1, int _x2, int _y1, int _y2)
{
return abs(_x2 - _x1)* abs(_y2 - _y1);
}
#endif
//============== 13번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
unsigned int a =0X12345678; //예시
int N = 0;
scanf("%d", &N); // 1~4의 숫자
// ==========================================
// 이 부분에 코드를 작성하세요.
char* p = (char*)&a;
for (int i = 0; i < N; i++)
{
*(p + i) = 0;
}
printf("%X", a);
// ==========================================
}
#endif
//============== 14번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int score_list[10] = { 67, 49, 30, 15, 20, 59, 51, 12, 0, 78 };
void selectScore(int** k, int N);
int main()
{
int N; //0~9
scanf("%d", &N);
int* p = NULL;
selectScore(&p, N);
printf("%d", *p);
}
// =========================================
// selectScore 함수를 정의
void selectScore(int** k, int N)
{
*k = score_list + N;
}
//===========================================
#endif
//============== 15번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int Count(char** _names, char key);
int main()
{
char* names[4] = {
"karina",
"winter",
"giselle",
"ningning"
};
char key;
int cnt = 0;
scanf("%c", &key);
cnt = Count(names, key);
printf("%d", cnt);
}
// =====================================
// 이 부분에 Count함수를 정의하세요.
int Count(char** _names, char key)
{
int cnt = 0;
for (int i = 0; i < 4; i++)
{
char* p = _names[i];
while (*p != NULL) {
if (*p == key)
{
cnt++;
}
p++;
}
}
return cnt;
}
// =======================================
#endif
//============== 16번 =============
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int funcNumber; // 함수 번호
int num1, num2;
printf("함수 번호와 계산할 값을 입력하세요: ");
scanf("%d %d %d", &funcNumber, &num1, &num2); // 함수 번호와 계산할 값을 입력받음
// ====================================================
// 여기에 함수포인터 배열을 선언하고 호출하는 코드를 작성하세요.
int (*fp[4])(int, int) = { add, sub, mul, div };
printf("%d\n", fp[funcNumber](num1, num2)); // 함수 포인터 배열을 인덱스로 접근하여 함수 호출
// ===================================================
return 0;
}
#endif
// =========== 17 번 ==================
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct node
{
int data;
struct node* next;
};
int main() {
int a = 0, b = 0, c = 0;
scanf("%d %d %d", &a, &b, &c);
struct node x = { a, NULL };
struct node y = { b, NULL };
struct node z = { c, NULL };
// ====================================
// 이 부분에 코드를 작성해주세요.
x.next = &y;
y.next = &z;
printf("%d", x.next->next->data);
// ====================================
return 0;
}
#endif
// =========== 18 번 ==================
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
typedef struct node
{
int data;
struct node* next;
struct node* pre;
} NODE;
void LinkNew(NODE* _x, NODE* _new);
int main() {
int n1, n2;
scanf("%d %d", &n1, &n2);
NODE x = { 10, NULL , NULL };
NODE new1 = { n1, NULL , NULL };
NODE new2 = { n2, NULL , NULL };
LinkNew(&x, &new1);
LinkNew(&new1, &new2);
// =====여기에 코드를 작성하세요..=========
// new2를 이용해 x의 data멤버 값 출력
printf("%d\n", new2.pre->pre->data);
// ====================================
return 0;
}
// ==============================================
// LinkNew 함수를 정의하세요.
void LinkNew(NODE* _x, NODE* _new) {
_x->next = _new;
_new->pre = _x;
}
// ==============================================
#endif
// =========== 19번 ==================
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int a = 0x12345678;
int b = 0x0a0b0c0d;
int c = 0xabcd1234;
int d = 0x87654321;
char* p[4] = { (char*)&a, (char*)&b, (char*)&c, (char*)&d };
// ===========================================================
// 여기에 코드를 작성하세요.
for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 3 - j; k++)
{
if (*(p[k]) > *(p[k + 1])) //앞이 사전순으로 더 크면
{
char temp = *(p[k]);
*(p[k]) = *(p[k+1]);
*(p[k + 1]) = temp;
}
}
}
// ===========================================================
printf("%x\n%x\n%x\n%x\n", a, b, c, d);
return 0;
}
#endif
// =========== 20번 ==================
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main() {
char arr[100], record[26] = { 0 };
int cnt;
scanf("%s", arr);
// ==========================================================
// 이 부분에 코드를 작성하세요. (단, 반복문은 하나만 쓸 것)
for (cnt = 0; cnt < strlen(arr); cnt++) {
record[arr[cnt] - 'a']++;
}
// ==========================================================
for (cnt = 0; cnt < 26; cnt++) {
printf("%c : %d times\n", cnt + 'a', record[cnt]);
}
}
#endif