반응형
12. 다음 프로그램의 결과를 분석하라.
#include <stdio.h> int main() { int *ip1, *ip2; double *dp1, *dp2; int i1 = 1, i2 = 2; double d1 = 11.111, d2 = 22.222; ip1 = &i1; ip2 = ip1; dp1 = &d1; dp2 = dp1; printf("%8s %8s %8s %8s\n", " &ip1", " ip1", " &i1", " i1"); printf("%8u %8u %8u %8d\n\n", &ip1, ip1, &i1, i1); printf("%8s %8s %8s %8s\n", " &ip2", " ip2", " &i2", " i2"); printf("%8u %8u %8u %8d\n\n", &ip2, ip2, &i2, i2); printf("%8s %8s %8s %8s\n", " &dp1", " dp1", " &d1", " d1"); printf("%8u %8u %8u %8d\n\n", &dp1, dp1, &d1, d1); printf("%8s %8s %8s %8s\n", " &dp2", " dp2", " &d2", " d2"); printf("%8u %8u %8u %8.15f\n\n", &dp2, dp2, &d2, d2); }
13. 문자열을 입력받아서 특정 단어를 다른 단어로 변경하는 프로그램을 포인터를 이용하여 작성하라.
#include <stdio.h> #include <string.h> char* Converter(char* string, const char* source, const char* target); int main() { char str[100]; char source[50]; char target[50]; printf("input string :"); scanf_s("%s", str, 100); while (getchar() != '\n') {}; printf("source word : "); scanf_s("%s", source, 50); while (getchar() != '\n') {}; printf("target word : "); scanf_s("%s", target, 50); while (getchar() != '\n') {}; printf("output string : %s \n", Converter(str, source, target)); } char* Converter(char* string, const char* source, const char* target) { static char newStr[100]; const char* ps = source; const char* pt = target; int lenps = strlen(source); int lenpt = strlen(target); for(int i = 0; i < 100 && *string-1 != '\0'; i++){ if (*string == '\0') { newStr[i] = '\0'; } if (*string == *ps) { string += lenps; for (int k = i; k < i + lenpt; k++ ) { newStr[k] = *target++; } i += lenpt; } else { newStr[i-1] = *string++; } } return newStr; }
14. 임의의 스트링을 입력받아서 나오는 정수들을 전부 합한 결과를 출력하는 프로그램을 포인터를 이용해서 작성하라.
#include <stdio.h> #include <string.h> #include <stdlib.h> int SIIS (char* string); int main() { char sample[100]; printf("input string : "); scanf_s("%[^\n]s", sample, 100); printf("sum = %d \n", SIIS(sample)); } int SIIS(char* string) { int sum = 0; int Decimal = 0; char* ch = string; char temp[20] = { '\0' }; while (*ch) { int i = 0; while ('0' <= *ch && *ch <= '9') { temp[i] = *ch; i++; ch++; } temp[i] = '\0'; if (temp[0] != '\0') { sum += atoi(temp); temp[0] = '\0'; } ch++; } return sum; }
15. 주민번호를 문자열로 입력받아서 생년월일을 출력하고 남성인지 여성인지 판별하는 프로그램을 포인터를 이용해서 작성하라.
#include <stdio.h> void fIDtBS (char* string); int main() { char ID[15]; printf("Input \nID Number : "); scanf_s("%s", ID, 15); fIDtBS(ID); } void fIDtBS (char* string) { char ID[15] = { '\0' }; for (int i = 0; i < 14; i++) { ID[i] = *string; string++; } if (ID[8] == '1' || ID[8] == '2') { printf("Birth date : 19"); for (int i = 0; i < 6; i++) { printf("%c", ID[i]); if (i % 2 == 1) { printf("/"); } } if (ID[8] == '1') { printf(", male \n"); } else { printf(", female \n"); } } if (ID[8] == '3' || ID[8] == '4') { printf("Birth date : 20"); for (int i = 0; i < 6; i++) { printf("%c", ID[i]); if (i % 2 == 1) { printf("/"); } } if (ID[8] == '3') { printf(", male \n"); } else { printf(", female \n"); } } }
반응형