c语言 输入若干字符串 用指针和一位数组 冒泡排序,C 语言作业 - 1 - 指针使用与冒泡排序...
上周四 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
{
for(j=0;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
#include
/*
function ptint_out(): print out strings
*/
void print_out(char *str[], int num)
{
printf("{ ");
int i;
for(i=0; 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
{
for(j=0;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;
}
運行結果如下:
總結
以上是生活随笔為你收集整理的c语言 输入若干字符串 用指针和一位数组 冒泡排序,C 语言作业 - 1 - 指针使用与冒泡排序...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 6600k内存条选择:性能需求与预算的平
- 下一篇: 大根堆的删除c语言,小根堆大根堆的动态创