您的位置 首页 IOT

编写程序完成对输入的字符串排序(能够规则比较的字符)

一程序功能编写程序实现对输入的字符串排序,如果主函数传递了-n参数,则按数字排序,否则按字符串排序同时,如果传递了-r参数,则降序,

一. 程序功用

编写程序完成对输入的字符串排序, 假如主函数传递了-n参数, 则按数字排序,不然按字符串排序.
一起,假如传递了-r参数,则降序, 不然升序.
一起,假如传递了-f参数,则不区别大小写
一起,假如传递了-d参数,则只是对alpha字符排序.
一起,假如参数中有+2 -10,则只是比较第2个字符到第10个字符
二. 程序源码
//main.c
#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 charcmp(char *, char *);
void error(char *);
int numcmp(char *, char *);
void readargs(int argc, char *argv[]);
int p_readlines(char *lineptr[], int maxlines);
void p_qsort(void *v[], int left, int right, int (*comp)(void *, void *));
void writelines(char *lineptr[], int nlines, int order);
char option = 0;
int pos1 = 0;
int pos2 = 0;
int main(int argc, char *argv[])
{
int nlines;
int rc = 0;
readargs(argc, argv);
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);
writelines(lineptr, nlines, option & DECR);
} else {
printf(“input too big to sort!\n”);
rc = -1;
}
system(“pause”);
return rc;
}
void readargs(int argc, char *argv[])
{
int c;
while (–argc > 0 && (c = (*++argv)[0]) == – || c == +) {
if (c == – && !isdigit(*(argv[0]+1)))
{
while (c = *++argv[0])
{
switch(c) {
case d:
option |= DIR;
break;
case f:
option |= FOLD;
break;
case n:
option |= NUMERIC;
break;
case r:
option |= DECR;
break;
default:
printf(“sort: illegal option %c\n”, c);
error(“Usage: sort -dfnr [+pos1] [-pos2]”);
break;
}
}
} else if (c == -) {
pos2 = atoi(argv[0]+1);
} else if ((pos1 = atoi(argv[0]+1)) < 0)
error(“Usage: sort -dfnr [+pos1] [-pos2]”);
}
if (argc || pos1 > pos2)
error(“Usage: sort -dfnr [+pos1] [-pos2]”);
}
int p_readlines(char *lineptr[], int maxlines)
{
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] = \0;
strcpy(p, line);
lineptr[nlines++] = p;
p += len;
}
printf(“Please input a string(# to end input): “);
}
return nlines;
}
void writelines(char *lineptr[], int nlines, int order)
{
int i;
if (order)
for (i = nlines -1; i >= 0; i–)
printf(“%s\n”, lineptr[i]);
else
for (i = 0; i < nlines; i++)
printf(“%s\n”, lineptr[i]);
printf(“\n”);
}
void swap(void *v[], int i, int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
int getline(char s[], int lim)
{
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] = \0;
return i;
}
void p_qsort(void *v[], int left, int right, int(*comp)(void *, void *))
{
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);
}
void error(char *s)
{
printf(“%s\n”, s);
exit(1);
}
//numcmp.c
#include
#include
#include
#define MAXSTR 100
void substr(char *s, char *t, int maxstr);
int numcmp(char *s1, char *s2)
{
double v1, v2;
char str[MAXSTR];
substr(s1, str, MAXSTR);
v1 = atof(str);
substr(s2, str, MAXSTR);
v2 = atof(str);
if (v1 < v2)
return -1;
else if (v1 > v2)
return 1;
else
return 0;
}
#define FOLD 4
#define DIR 8
int charcmp(char *s, char *t)
{
char a, b;
int i, j, endpos;
extern char option;
extern int pos1, pos2;
int fold = (option & FOLD) ? 1: 0;
int dir = (option & DIR) ? 1: 0;
i = j = pos1;
if (pos2 > 0)
endpos = pos2;
else if ((endpos = strlen(s)) > strlen(t))
endpos = strlen(t);
do {
if (dir) {
while (i < endpos && !isalpha(s[i]) && s[i] != && s[i] != \0)
i++;
while (j < endpos && !isalpha(t[j]) && t[j] != && t[j] != \0)
j++;
}
if (i < endpos && j < endpos) {
a = fold ? tolower(s[i]): s[i];
i++;
b = fold ? tolower(t[j]): t[j];
j++;
if (a == b && a == \0)
return 0;
}
} while (a == b && i < endpos && j < endpos);
return a – b;
}
//substr.c
#include
void error(char *);
void substr(char *s, char *str)
{
int i, j, len;
extern int pos1, pos2;
len = strlen(s);
if (pos2 > 0 && len > pos2)
len = pos2;
else if (pos2 > 0 && len < pos2)
error(“substr: string too short”);
for (j = 0, i = pos1; i < len; i++, j++)
str[j] = s[i];
str[j] = \0;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/iot/262288.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部