编写程序实现对输入的字符串排序(仅对alpha字符排序)
一. 程序功能 int nlines; int c, rc = 0; while (--argc > 0 && (*++argv)[0] == -) { while (c = *++argv[0]) { switch(c) { case d: option |= DIR; case f: option |= FOLD; break; case n: option |= NUMERIC; break; case r: option |= DECR; break; default: printf("sort: illegal option %cn", c); argc = 1; rc = -1; break; } } } if (argc) printf("Usage: sort -fnrn"); else { if ((nlines = p_readlines(lineptr, MAXLINES)) > 0) { if (option & NUMERIC) p_qsort((void **)lineptr, 0, nlines-1,(int (*)(void *, void *))numcmp); else p_qsort((void **)lineptr, 0, nlines-1, (int (*)(void *, void *))charcmp); p_writelines(lineptr, nlines, option & DECR); } else { printf("Input too big to sort!n"); rc = -1; } } system("pause"); return rc; char a, b; int fold = (option & FOLD) ? 1 : 0; int dir = (option & DIR) ? 1 : 0; do { if (dir) { while (!isalnum(*s) && *s != && *s != ) s++; while (!isalnum(*t) && *t != && *t != ) t++; } a = fold ? tolower(*s): *s; s++; b = fold ? tolower(*t): *t; t++; if (a == b && a == ) { return 0; } } while (a == b); return a - b; int len, nlines; char *p = lines, line[MAXLEN]; nlines = 0; printf("Please input a string(# to end input): "); while ((len = getline(line, MAXLEN)) > 0) { if (strcmp(line, "#n") == 0) break; if (nlines >= maxlines || p + len >= lines + MAXSTORAGE) return -1; else { line[len - 1] = ; strcpy(p, line); lineptr[nlines++] = p; p += len; } printf("Please input a string(# to end input): "); } return nlines; int i; if (order) for (i = nlines -1; i >= 0; i--) printf("%sn", lineptr[i]); else for (i = 0; i < nlines; i++) printf("%sn", lineptr[i]); printf("n"); void *temp; temp = v[i]; v[i] = v[j]; v[j] = temp; int c, i; for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != n; ++i) s[i] = c; if (c == n) { s[i] = c; ++i; } s[i] = ; return i; double v1, v2; v1 = atof(s1); v2 = atof(s2); if (v1 < v2) return -1; else if (v1 > v2) return 1; else return 0; int i, last; if (left >= right) return; 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); p_qsort(v, left, last - 1, comp); p_qsort(v, last + 1, right, comp); int nlines; int c, rc = 0; while (--argc > 0 && (*++argv)[0] == -) { while (c = *++argv[0]) { switch(c) { case d: option |= DIR; case f: option |= FOLD; break; case n: option |= NUMERIC; break; case r: option |= DECR; break; default: printf("sort: illegal option %cn", c); argc = 1; rc = -1; break; } } } if (argc) printf("Usage: sort -fnrn"); else { if ((nlines = p_readlines(lineptr, MAXLINES)) > 0) { if (option & NUMERIC) p_qsort((void **)lineptr, 0, nlines-1,(int (*)(void *, void *))numcmp); else if ((option & (FOLD | DIR)) == 0x0c) { p_qsort((void **)lineptr, 0, nlines-1, (int (*)(void *, void *))folddircmp); } else if (option & FOLD) p_qsort((void **)lineptr, 0, nlines-1,(int (*)(void *, void *))foldcmp); else if (option & DIR) p_qsort((void **)lineptr, 0, nlines-1, (int (*)(void *, void *))dircmp); else p_qsort((void **)lineptr, 0, nlines-1, (int (*)(void *, void *))strcmp); p_writelines(lineptr, nlines, option & DECR); } else { printf("Input too big to sort!n"); rc = -1; } } system("pause"); return rc; char a, b; int fold = (option & FOLD) ? 1: 0; do { a = fold ? tolower(*s): *s; s++; b = fold ? tolower(*t): *t; t++; if (a == b && a == ) return 0; } while (a == b); return a - b; char a, b; int dir = (option & DIR) ? 1: 0; do { if (dir) { while (!isalnum(*s) && *s != && *s != ) s++; while (!isalnum(*t) && *t != && *t != ) t++; } if (a == b && a == ) return 0; } while (a == b); return a - b; char a, b; int fold = (option & FOLD) ? 1 : 0; int dir = (option & DIR) ? 1 : 0; do { if (dir) { while (!isalnum(*s) && *s != && *s != ) s++; while (!isalnum(*t) && *t != && *t != ) t++; } a = fold ? tolower(*s): *s; s++; b = fold ? tolower(*t): *t; t++; if (a == b && a == ) { return 0; } } while (a == b); return a - b; int len, nlines; char *p = lines, line[MAXLEN]; nlines = 0; printf("Please input a string(# to end input): "); while ((len = getline(line, MAXLEN)) > 0) { if (strcmp(line, "#n") == 0) break; if (nlines >= maxlines || p + len >= lines + MAXSTORAGE) return -1; else { line[len - 1] = ; strcpy(p, line); lineptr[nlines++] = p; p += len; } printf("Please input a string(# to end input): "); } return nlines; int i; if (order) for (i = nlines -1; i >= 0; i--) printf("%sn", lineptr[i]); else for (i = 0; i < nlines; i++) printf("%sn", lineptr[i]); printf("n"); void *temp; temp = v[i]; v[i] = v[j]; v[j] = temp; int c, i; for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != n; ++i) s[i] = c; if (c == n) { s[i] = c; ++i; } s[i] = ; return i; double v1, v2; v1 = atof(s1); v2 = atof(s2); if (v1 < v2) return -1; else if (v1 > v2) return 1; else return 0; int i, last; if (left >= right) return; 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); p_qsort(v, left, last - 1, comp); p_qsort(v, last + 1, right, comp);
编写程序实现对输入的字符串排序, 如果主函数传递了-n参数, 则按数字排序,否则按字符串排序.
同时,如果传递了-r参数,则降序, 否则升序.
同时,如果传递了-f参数,则不区分大小写
同时,如果传递了-d参数,则仅仅对alpha字符排序.
二. 程序源码
1) 法1
#include
#include
#include
#include
#define NUMERIC 1
#define DECR 2
#define FOLD 4
#define DIR 8
#define MAXLINES 5000
#define MAXLEN 1000
#define MAXSTORAGE 10000
char *lineptr[MAXLINES];
char lines[MAXSTORAGE];
int numcmp(char *, char *);
int charcmp(char *, char *);
int p_realines(char *lineptr[], int maxlines);
void p_qsort(void *v[], int left, int right, int(*comp)(void *, void *));
void p_writelines(char *lineptr[], int nlines, int order);
static char option = 0;
int main(int argc, char *argv[])
{
}
int charcmp(char *s, char *t)
{
}
int p_readlines(char *lineptr[], int maxlines)
{
}
void p_writelines(char *lineptr[], int nlines, int order)
{
}
void swap(void *v[], int i, int j)
{
}
int getline(char s[], int lim)
{
}
int numcmp(char *s1, char *s2)
{
}
void p_qsort(void *v[], int left, int right, int(*comp)(void *, void *))
{
}
2)法2
#include
#include
#include
#include
#define NUMERIC 1
#define DECR 2
#define FOLD 4
#define DIR 8
#define MAXLINES 5000
#define MAXLEN 1000
#define MAXSTORAGE 10000
char *lineptr[MAXLINES];
char lines[MAXSTORAGE];
int foldcmp(char *, char *);
int numcmp(char *, char *);
int dircmp(char *, char *);
int folddircmp(char *, char *);
int p_realines(char *lineptr[], int maxlines);
void p_qsort(void *v[], int left, int right, int(*comp)(void *, void *));
void p_writelines(char *lineptr[], int nlines, int order);
static char option = 0;
int main(int argc, char *argv[])
{
}
int foldcmp(char *s, char *t)
{
}
int dircmp(char *s, char *t)
{
}
int folddircmp(char *s, char *t)
{
}
int p_readlines(char *lineptr[], int maxlines)
{
}
void p_writelines(char *lineptr[], int nlines, int order)
{
}
void swap(void *v[], int i, int j)
{
}
int getline(char s[], int lim)
{
}
int numcmp(char *s1, char *s2)
{
}
void p_qsort(void *v[], int left, int right, int(*comp)(void *, void *))
{
}
三. 程序小结
1. 程序不能很好处理-d参数, 有待改进.
关键词: 编写程序字符串排序alph

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW
或用微信扫描左侧二维码