-
#5-6,7 포인터 배열전기전자공학/프로젝트 2017. 6. 29. 13:58
[예제 5-7]
readlines 프로그램을 고쳐서 alloc 함수를 사용하지 않고 main에서 사용 가능한 배열에 입력을 저장하도록 해보라.
프로그램 수행이 얼마나 빨라지는지 보라. (시간 함수를 아직 잘 못쓰겠다.) . --> 해결! : 터미널에 time ./main 하면 실행 시간을 알 수 있다.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223/* 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.cmain() {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);elseprintf("error: zero divisor\n");break;case '%': // 실수일 경우 정수로 만들어서 % 연산함op2 = pop();if (op2 != 0)push(fmod(pop(), op2));elseprintf("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;elseprintf("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 5000char *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 5000int 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 함수에 에러 검사하는 부분을 첨가해보라.
123456789101112131415161718192021222324252627282930313233343536373839#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 yearleap = 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 함수를 포인터를 사용하도록 고쳐라.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#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 yearleap = 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 '전기전자공학 > 프로젝트' 카테고리의 다른 글
[ACES] 메모 (assert, (0) 2017.07.11 [ACES - C ] 데이터 형과 변수의 종류(지역변수, 전역변수, 정적변수) (0) 2017.07.11 [Kernighan - C] #5 - 5 (0) 2017.06.29 [Kernighan - C] #4-1,2 함수의 기초 (0) 2017.06.26 [Kernighan - C] #2-11 조건문 (0) 2017.06.25