上周四 Programming 课程布置了一个作业,要求如下:
Write a C module for sorting string pointer array. And write a test program for string sorting using the module.
大致意思就是对一个字符数组进行排序;比较的方法有两种,一种是基于 ASCII 码的大小,一个是基于整数值的大小;最后用冒泡排序来测试这两种比较方法。
真的是,时隔很久又重新捡起 C 语言啊。
charstrcmp()
该函数是基于 ASCII 码来比较字符串的。一般的字符串比较都是基于这种方法,先比较首字母的 ASCII 码的大小,如果首字母相同再比较第二个字母,以此类推。举例来说:
“april” and “may” -> “may” > “april”
“standard” and “stantalone” -> “standard” > “tandalone” (‘r’>’l’)
因此,这个函数比较好实现:
/*
function charstrcmp(): compare two strings based on ASCII
*/
int charstrcmp(char *s, char *t)
{
int i=0;
while(s[i]!='\0' || t[i]!='\0')
{
if(s[i] > t[i])
{
return 1;
}
else if(s[i] < t[i])
{
return -1;
}
i++;
}
return 0;
}
initstrcmp()
该函数是基于整数值来比较字符串的。这里第一想法是把字符串数字转换成数字,然后进行比较,但是试过之后感觉好麻烦,而且很多报错。后面就按照下面的步骤来进行了,比较简单:
- 基于正负数的符号比较
- 如果符合相同,基于长度比较
- 如果长度相同,基于字符串大小比较
实现如下:
int initstrcmp(char *s, char *t)
{
int i = 0, sign = 1;
// if s or t is negative
if (s[i] != '-' && t[i] == '-') { return 1; }
if (s[i] == '-' && t[i] != '-') { return -1; }
// if s and t are all negative
if (s[i] == '-' && t[i] == '-' ) { sign = -1; }
// compare based on the length of strings
if(strlen(s) > strlen(t)) { return sign; }
if(strlen(s) < strlen(t)) { return -sign; }
// compare based on the value of strings
while(s[i] != '\0' || t[i] != '\0')
{
if(s[i] > t[i]) { return sign; }
else if(s[i] < t[i]) { return -sign; }
i++;
}
return 0;
}
bubble_sort()
该函数是实现基于前两个比较方法的冒泡排序。关于冒泡排序就不多说了,直接看下面代码:
/*
funciton swap(): swap two pointers
*/
void swap(char **s, char **t)
{
char *tmp;
tmp = *s;
*s = *t;
*t = tmp;
tmp = NULL;
}
/*
function bubble_sort(): bubble sort for strings
*/
void bubble_sort(void *str[], int num, int(*compare)(char *, char*))
{
int i, j;
for(i=0; i<num-1; i++)
{
for(j=0;j<num-1-i;j++)
{
// if str[j] > str[j+1], should swap them
if(compare((char *)str[j], (char *)str[j+1]) > 0)
{
// swap
swap(&str[j], &str[j+1]);
}
}
}
}
实现结果
完整代码如下:
#include <stdio.h>
#include <string.h>
/*
function ptint_out(): print out strings
*/
void print_out(char *str[], int num)
{
printf("{ ");
int i;
for(i=0; i<num; i++)
{
printf("%s ", str[i]);
}
printf("}\n");
}
/*
function charstrcmp(): compare two strings based on ASCII
*/
int charstrcmp(char *s, char *t)
{
int i=0;
while(s[i]!='\0' || t[i]!='\0')
{
if(s[i] > t[i])
{
return 1;
}
else if(s[i] < t[i])
{
return -1;
}
i++;
}
return 0;
}
/*
function initstrcmp(): compare two strings based on integer value
*/
int initstrcmp(char *s, char *t)
{
int i = 0, sign = 1;
// if s or t is negative
if (s[i] != '-' && t[i] == '-') { return 1; }
if (s[i] == '-' && t[i] != '-') { return -1; }
// if s and t are all negative, change the sign
if (s[i] == '-' && t[i] == '-' ) { sign = -1; }
// compare based on the length of strings
if(strlen(s) > strlen(t)) { return sign; }
if(strlen(s) < strlen(t)) { return -sign; }
// compare based on the value of strings
while(s[i] != '\0' || t[i] != '\0')
{
if(s[i] > t[i]) { return sign; }
else if(s[i] < t[i]) { return -sign; }
i++;
}
return 0;
}
/*
funciton swap(): swap two pointers
*/
void swap(char **s, char **t)
{
char *tmp;
tmp = *s;
*s = *t;
*t = tmp;
tmp = NULL;
}
/*
function bubble_sort(): bubble sort for strings
*/
void bubble_sort(void *str[], int num, int(*compare)(char *, char*))
{
int i, j;
for(i=0; i<num-1; i++)
{
for(j=0;j<num-1-i;j++)
{
// if str[j] > str[j+1], should swap them
if(compare((char *)str[j], (char *)str[j+1]) > 0)
{
// swap
swap(&str[j], &str[j+1]);
}
}
}
}
/*
test program for the charstrcmp() and initstrcmp()
*/
int main()
{
// test strings
int num=6;
char *str[] = {"3", "27", "123", "5", "9", "1"};
char *str_1[] = {"3", "27", "123", "5", "9", "1"};
printf("\nOriginal strings: \n");
print_out(str, num);
printf("\n============ Sorted Result ============\n");
// compare based on ASCII
bubble_sort(str,num,charstrcmp);
// compare based on Integer Value
bubble_sort(str_1,num,initstrcmp);
printf("\nResult of %s method:\n", "ASCII");
print_out(str,num);
printf("\nResult of %s method:\n", "Integer Value");
print_out(str_1,num);
return 1;
}
运行结果如下:
网友评论