전기전자공학/프로젝트
#5-6,7 포인터 배열
LinZBe4hia
2017. 6. 29. 13:58
[예제 5-7]
readlines 프로그램을 고쳐서 alloc 함수를 사용하지 않고 main에서 사용 가능한 배열에 입력을 저장하도록 해보라.
프로그램 수행이 얼마나 빨라지는지 보라. (시간 함수를 아직 잘 못쓰겠다.) . --> 해결! : 터미널에 time ./main 하면 실행 시간을 알 수 있다.
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 | /* Reverse polish calculator */ / #include <stdio.h> #include <stdlib.h> /* for atof() */ #include <math.h> /* for fmod() */ #define MAXOP 100 /* max size of operand or operator */ #define NUMBER '0' /* signal that a number was found */ int getop(char[]); void push(double); double pop(void); void clear(void); int getline(char*, int); /* reverse Polish calculator */ // main.c main() { int type; double op1, op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '%': // 실수일 경우 정수로 만들어서 % 연산함 op2 = pop(); if (op2 != 0) push(fmod(pop(), op2)); else printf("error: zero divisor\n"); break; case 't': /* show top element of stack */ op2 = pop(); printf("\t%.8g\n",op2); push(op2); break; case 'c': /* clear the stack */ clear(); break; case 'd': /* duplicate top element */ op2 = pop(); push(op2); push(op2); break; case 's': /* swap the top two element */ op1 = pop(); op2 = pop(); push(op1); push(op2); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknow command %s\n", s); break; } } } // stack.c #define MAXVAL 100 /* maximum depth of val stack */ int sp = 0; /* next free stack position */ double val[MAXVAL]; /* val stack */ /* push: push f onto value stack */ void push(double f) { if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f); } /* pop: pop and return top value from stack */ double pop(void) { if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; } } /* clear: clear the stack */ void clear(void) { sp = 0; } // getop.c #include <ctype.h> int getch(void); void ungetch(int); #include <string.h> #include <time.h> #define MAXLINES 5000 char *lineptr[MAXLINES]; char linestor[MAXLINES]; void swap(char *v[], int i, int j); void qsort(char *v[], int left, int right); int readlines(char*lineptr[], char *linestor, int nlines); void writelines(char *lineptr[], int nlines); main() { int nlines; /* number of input lines read */ if ((nlines = readlines(lineptr, linestor, MAXLINES)) >= 0) { qsort(lineptr, 0, nlines - 1); writelines(lineptr, nlines); return 0; } else { printf("error: input too big to sort\n"); return 1; } } #define MAXLEN 1000 /* max length of any input line */ #define MAXSTOR 5000 int mgetline(char *, int); int readlines(char *lineptr[], char *linestor, int maxlines) { int len, nlines; char line[MAXLEN]; char *p = linestor; char *linestop = linestor + MAXSTOR; nlines = 0; while ((len = mgetline(line, MAXLEN)) > 0) if (nlines >= maxlines || p + len > linestop) return -1; else { line[len - 1] = '\0'; /* delete newlines */ strcpy(p, line); lineptr[nlines++] = p; p += len; } return nlines; } int mgetline(char *s, int lim) { int c; char *t = s; while (--lim > 0 && (c = getchar()) != EOF && c != '\n') *s++ = c; if (c == '\n') *s++ = c; *s = '\0'; return s - t; } /* qsort: sort v[left] ... v[right] into increasing order */ void qsort(char *v[], int left, int right) { int i, last; void swap(char *v[], int i, int j); if (left >= right) return; swap(v, left, (left + right) / 2); last = left; for (i = left + 1; i <= right; i++) if (strcmp(v[i], v[left])<0) swap(v, ++last, i); swap(v, left, last); qsort(v, left, last - 1); qsort(v, last + 1, right); } void writelines(char *lineptr[], int nlines) { int i; for (i = 0; i < nlines; i++) printf("%s\n", lineptr[i]); } /* swap: interchange v[i] and v[j] */ void swap(char *v[], int i, int j) { char *temp; temp = v[i]; v[i] = v[j]; v[j] = temp; } | cs |
[예졔 5-8]
day_of_year와 month_day 함수에 에러 검사하는 부분을 첨가해보라.
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 | #include <stdio.h> int day_of_year(int, int, int); void month_day(int yeat, int yearday); main() { int day; int pmon[] = { 0 }; int pday[] = { 0 }; day = day_of_year(1988, 2, 29); printf("%d\n", day); month_day(1988, 60, pmon, pday); printf("mon: %d\tday: %d\n", *pmon, *pday); return; } /* day_of_year: set day of year from month & day */ int day_of_year(int year, int month, int day) { int i, leap; leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; for (i = 1; i < month; i++) day += daytab[leap][i]; return day; } /* month_day: set month, day from day of year */ void month_day(int year, int yearday, int *pmonth, int *pday) { int i, leap; //check leap year leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; for (i = 1; yearday > daytab[leap][i]; i++) yearday -= daytab[leap][i]; *pmonth = i; *pday = yearday; } | cs |
[예제 5- 9]
day_of_year와 month_day 함수를 포인터를 사용하도록 고쳐라.
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 | #include <stdio.h> static char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; int day_of_year(int, int, int); void month_day(int yeat, int yearday); main() { int day; int pmon[] = { 0 }; int pday[] = { 0 }; day = day_of_year(1988, 2, 29); printf("%d\n", day); month_day(1988, 60, pmon, pday); printf("mon: %d\tday: %d\n", *pmon, *pday); return; } /* day_of_year: set day of year from month & day */ int day_of_year(int year, int month, int day) { int i, leap; char *p; leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; p = &daytab[leap][1]; // 처음에 1월을 가리키도록 초기화 for (i = 1; i < month; i++) day += *p++; return day; } /* month_day: set month, day from day of year */ void month_day(int year, int yearday, int *pmonth, int *pday) { int i, leap; char *p; //check leap year leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; p = &daytab[leap][1]; for (i = 1; yearday > daytab[leap][i]; i++) yearday -= *p++; *pmonth = i; *pday = yearday; } | cs |