본문 바로가기

CS Study/C

(10)
10. 포인터 - 자료구조 1. Stack에 정수 저장하기 #include #include struct stackNode { int data; struct stackNode *nextPtr; }; typedef struct stackNode StackNode; typedef StackNode* StackNodePtr; void push(StackNodePtr *topPtr, int info); int pop(StackNodePtr *topPtr); int isEmpty(StackNodePtr topPtr); void printStack(StackNodePtr currentPtr); void instructions(); void instructions() { printf("Enter choice : \n"); printf("\t 1..
9. file read/write 1. file control - read/write #include int main() { char readfile[20]; char writefile[20]; char text[100]; printf("Enter the name of file to read : "); scanf("%s", readfile); printf("Enter the name of file to write : "); scanf("%s", writefile); FILE *fp1 = fopen(readfile, "r"); FILE *fp2 = fopen(writefile, "w"); while (fgets(text, sizeof(text), fp1) != NULL) { if (*text != '\n') fputs(text, fp2);..
8. char 자료형 / 포인터 심화 1. 3개의 수 덧셈 - 문자열 정수로 변환 #include #include #include int main(void) { int sum = 0; char str[6]; int i; for (i = 0; i < 3; i++) { printf("Enter an integer string : "); scanf("%s", str); sum += atol(str); } printf("The total is %d", sum); return 0; } 2. 문장에 있는 알파벳 개수 세기 #include #include #include const int NALPHA = 'z' - 'a' + 1; int alpha_count[NALPHA] = { 0 }; int total_count = 0; int main() { ch..
7. 포인터 / 포인터 함수 배열 1. 포인터로 숫자 지정하기 #include int main(void) { int a, b, c,temp; int * pa = &a; int * pb = &b; int * pc = &c; int * ptemp = &temp; printf("Enter three numbers : "); scanf_s("%d %d %d",&a,&b,&c); printf("before : %d %d %d\n", a, b, c); (*pa) += 1; (*pb) *= (*pc); (*ptemp) = (*pb); (*pb) = (*pc); (*pc) = (*ptemp); printf("after : %d %d %d", *pa, *pb, *pc); } 2. 문자열 비교 #include void det(char *a, char *b..
6. Array / MultiArray / String 1. 반복되지 않은 수 찾기 #include #include #include #define MAX 20 #define NUMBER 15 void main() { int i, tmp; int save[NUMBER]; int count = 0; int isSame = 0; srand(time(0)); while (count < NUMBER) { isSame = 0; tmp = rand() % MAX + 1; for (int i = 0; i < count; i++) { if (tmp == save[i]) { isSame = 1; break; } } if (isSame == 0) { save[count] = tmp; count++; } } printf("Nonrepititive array values are:\..
5. function - recursive 1. 최대공약수 구하기 #include int gcd(int x, int y); int main(void) { int a, b; int c=0, d=0; printf("Enter two positive integers : "); scanf_s("%d %d", &a, &b); while (1) { if (a > 0 && b > 0) { break; } else { printf("Enter two positive integers : "); scanf_s("%d %d", &a, &b); } } printf("%d", gcd(a, b)); } int gcd(int x, int y) { int z; if (x >= y) { if (x%y == 0) { return y; } else if (x%y != 0) { y..
4. math.h / time.h 1. 제곱수 출력 #include #include int main(void) { double a, b, c; int i; for (i = 0; i = 0 && b >= 0) { c = sqrt(pow(a, 2) + pow(b, 2)); printf("%1f", c); } if(a
3. do-while / switch / for 1. 최대 정수 구하기 #include int main(void) { int a, b, c, d, e, temp; printf("Enter the number : "); scanf_s("%d", &a); printf("Enter the number : "); scanf_s("%d", &b); printf("Enter the number : "); scanf_s("%d", &c); printf("Enter the number : "); scanf_s("%d", &d); printf("Enter the number : "); scanf_s("%d", &e); do { if (a < b) { temp = a; a = b; b = temp; } if (a < c) { temp = a; a = c; c = tem..