main에 전달 된 인수에 따라 알파벳순/숫자 식으로 입력 선을 정렬하는 프로그램을 만들고 있습니다. 그리고 이것은 후속 운동은 다음과 같습니다함수에 대한 C 포인터
그래서, 라인 내에서 필드에 옵션의 독립적 인 설정에 따라 정렬 각 필드를 수행 할 수 있습니다 정렬, 필드 처리 기능을 추가합니다. (이 책의 인덱스 페이지 번호에 대한 인덱스 범주 및 -n에 대한 -df으로 분류되었다.) 임 조금들이 필드 뜻 것에 의아해
.
field_of 기능이 정확히 무엇입니까? 필드가 빈 문자가 아닌 문자열 인 원래 포인터 필드 시간을 증가 시키나요? num_fields를 입력하는 경우
또한, 다음 기능은 최초의 비 제로의 비교에 도달 할 때 리턴 비교, 맞나요? 결과가 0 (등가 문자열)이면 문자열을 바꿀 필요가 없으므로 결과를 반환하지 않습니다. 그렇지 않으면 숫자를 반환합니다.
#include <stdio.h>
#include <string.h>
#define MAX_FIELDS 10
#define FLAG_DIRECTORY (0x01 << 0)
#define FLAG_FOLD (0x01 << 1)
#define FLAG_NUMERIC (0x01 << 2)
#define FLAG_REVERSE (0x01 << 3)
int fieldarray[MAX_FIELDS];
unsigned char flagarray[MAX_FIELDS];
int num_fields = 0;
#define MAXLINES 5000 /* max #lines to be sorted */
char *lineptr[MAXLINES]; /* pointers to text lines */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
static char *field_of(char *s, int n);
static isdir(int c);
int compare(char *, char *);
int stringcmp(char *, char *);
int stringcmpi(char *, char *);
int dircmp(char *, char *);
int numcmp(char *, char *);
int main()
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort((void **) lineptr, 0, nlines-1, (int (*)(void *, void *)) compare);
writelines(lineptr, nlines);
return 0;
} else {
printf("input too big to sort\n");
return 1;
}
}
#define MAXLEN 1000 /* max length of any input line */
int getline(char *, int);
char *alloc(int);
/* qsort: sort v[left]...v[right] into increasing order */
void qsort(void *v[], int left, int right,
int (*comp)(void *, void *))
{
int i, last;
void swap(void *v[], int, int);
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; i <= right; i++)
if ((*comp)(v[i], v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1, comp);
qsort(v, last+1, right, comp);
}
void swap(void *v[], int i, int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
static char *field_of(char *s, int n)
{
while (isspace(*s))
s++;
while (--n > 0) {
while (!isspace(*s)) {
if (*s == '\0')
return NULL;
s++;
}
}
return s;
}
static isdir(int c)
{
return isalpha(c) || isdigit(c) || isspace(c);
}
int compare_field(char *s1, char *s2, unsigned int flags)
{
int d;
if (flags & FLAG_NUMERIC) {
d = numcmp(s1, s2);
} else if (flags & FLAG_DIRECTORY) {
do {
while (!isdir(*s1) && !isspace(*s1) && *s1 != '\0')
s1++;
while (!isdir(*s2) && !isspace(*s2) && *s2 != '\0')
s2++;
if (flags & FLAG_FOLD)
d = toupper(*s1) - toupper(*s2);
else
d = *s1 - *s2;
} while (d == 0 && !isspace(*s1) && !isspace(*s2)
&& *s1++ != '\0' && *s2++ != '\0');
} else {
do {
if (flags & FLAG_FOLD)
d = toupper(*s1) - toupper(*s2);
else
d = *s1 - *s2;
} while (d == 0 && !isspace(*s1) && !isspace(*s2)
&& *s1++ != '\0' && *s2++ != '\0');
}
if (flags & FLAG_REVERSE)
return -d;
else
return d;
}
/* compare: compare s1 and s2 according to the values of the
external variables numeric, reverse, fold, and directory. */
int compare(char *s1, char *s2)
{
int i, d;
char *f1, *f2;
for (i = 0; i < num_fields; i++) {
f1 = field_of(s1, fieldarray[i]);
f2 = field_of(s2, fieldarray[i]);
d = compare_field(f1, f2, flagarray[i]);
if (d != 0)
return d;
}
if (numeric)
d = numcmp(s1, s2);
else if (directory)
d = dircmp(s1, s2);
else
d = stringcmp(s1, s2);
if (reverse)
return -d;
else
return d;
}
/* stringcmp: compare s1 and s2 as strings */
int stringcmp(char *s1, char *s2)
{
if (fold)
return stringcmpi(s1, s2);
else
return strcmp(s1, s2);
}
/* stringcmpi: compare s1 and s2 case-insensitively */
int stringcmpi(char *s1, char *s2)
{
while (toupper(*s1) == toupper(*s2)) {
if (*s1 == '\0')
return 0;
s1++;
s2++;
}
return toupper(*s1) - toupper(*s2);
}
/* dircmp: compare s1 and s2 in "directory order" */
int dircmp(char *s1, char *s2)
{
int d;
do {
while (!isdir(*s1) && *s1 != '\0')
s1++;
while (!isdir(*s2) && *s2 != '\0')
s2++;
if (fold)
d = toupper(*s1) - toupper(*s2);
else
d = *s1 - *s2;
} while (d == 0 && *s1++ != '\0' && *s2++ != '\0');
return d;
}
/* numcmp: compare s1 and s2 numerically */
int numcmp(char *s1, char *s2)
{
extern double atof(const char *);
double v1, v2;
v1 = atof(s1);
v2 = atof(s2);
if (v1 < v2)
return -1;
else if (v1 > v2)
return 1;
else
return 0;
}
코드 예제를 줄이는 것이 좋겠습니까? 당신이 묻는 부분을 말하기는 어렵습니다. –
은 K & R의이 코드입니까? –
야, 너는이 질문을하는 방식에 너 자신을 돕지 않을거야. 1) 질문이 아주 모호합니다. 잘 정의 된 질문이 없습니다. 2) 코드가 너무 많습니다. 문제가있는 짧은 스 니펫이나 설명하고 싶은 짧은 스 니펫으로 당깁니다. 10 또는 15 줄 이상의 코드를 가진 질문은 거의 좋은 대답을 얻지 못합니다. –