對於這個任務我必須創建我自己的字符串類。我最初編寫compareto方法來比較兩個字符串,但返回的是較大者。我想要做的是比較並返回哪個字母大,即比較兩個字符串,例如:smith和htims。用我設計compareto方法的方式是,結果是它們是平等的。我想要做的是告訴我哪個字母是按字母順序排列的,因此對於我的例子來說,受害者會先來。我知道如何在Java或甚至C語言中使用<string.h>
庫來實現這一點,我只是對自己如何做到這一點感到困惑。比較字符串和用戶創建的字符串類
編輯:我只是想指出,我不是在尋找代碼的答案,而是在我應該如何寫代碼微調。
int compareto(void * S1, void * S2){
String s1 = (String S1);
String s2 = (String S2);
int i, cs1 = 0, cs2 = 0; //cs1 is count of s1, cs2 is count of s2
while(s1->c[i] != '\0'){ //basically, while there is a word
if(s1->c[i] < s2->c[i]) // if string 1 char is less than string 2 char
cs2++; //add to string 2 count
else (s1->c[i] > s2->c[i]) //vice versa
cs1++;
i++;
}
//for my return I basically have
if(cs1>cs2){
return 1;
}
else if(cs2 > cs1){
return 2;
}
return 0;
這裏是mystring.h
typedef struct mystring {
char * c;
int length;
int (*sLength)(void * s);
char (*charAt)(void * s, int i);
int (*compareTo)(void * s1, void * s2);
struct mystring * (*concat)(void * s1, void * s2);
struct mystring * (*subString)(void * s, int begin, int end);
void (*printS)(void * s);
} string_t;
typedef string_t * String;
任何建議,我所有的谷歌搜索涉及使用<string.h>
庫,所以我有沒有運氣。
我使用它來遍歷鏈表,並刪除姓的名字與用戶試圖刪除的人相匹配的人。
這裏是我的測試代碼,以幫助澄清我的問題(注意的compareTo是在刪除功能):
int main() {
Node startnode, currentnode, newnode;
int ans, success;
String who;
who = newString2();
startnode = (Node) malloc(sizeof(pq_t));
startnode->next = NULL;
currentnode = startnode;
ans = menu();
while (ans != 0) {
switch (ans) {
case add:
newnode = getStudent();
startnode = insert(newnode, startnode);
break;
case remove:
printf("Enter the last name of the person you want to delete : \n");
scanf("%s", &who->c);
startnode = removeStudent(startnode, who, &success);
if (success == 0)
printf("UNFOUND\n");
else
printf("permanently DELETED\n");
break;
case view:
printf("Now displaying the list : \n");
displaylist(startnode);
break;
}
ans = menu();
}
}
Node removeStudent(Node head, String who, int * success) {
Node p, l; //p = pointer node, l = previous node
Student current; //Im using generics, so I have to case the current node->obj as a student.
String ln, cln; //the last name of the person I want to remove, and the last name of the current node
p = head;
l = p;
//there can be three cases, p->first node, p->last node, p->some node in between
if (head->obj == NULL) {
printf("The list is empty\n"); //when the list is empty
*success = 0;
return NULL;
}
while (p != NULL) {
current = (Student) p->obj;
cln = current->ln;
if (ln->compareTo(who, cln) == 0) {
if (head == p) { //when there is only one node
head = head->next;
free(p);
*success = 1;
return head;
} else if (p->next == NULL) { //last node
l->next = NULL;
free(p);
*success = 1;
return head;
} else {
l->next = p->next; //middle
free(p);
*success = 1;
return head;
}
}
l = p;
p = p->next;
}
*success = 0;
return head;//couldnt find the node
}
在搜索列表時,您不想要什麼順序。你想要按字母順序排列的ASCII-betical(http://catb.org/jargon/html/A/ASCIIbetical-order.html)還是其他的東西?你想改變你的'compareto'函數,還是定義另一個?另外,爲什麼你將'void *'傳遞給函數,而不是'String *',這將使它們類型安全?你把函數指針放在'string_t'中來實現繼承還是封裝?你的海明距離比較算法的推理是什麼? – outis 2010-03-29 00:08:07
我的目標是按字母順序排序。我使用這個功能的目的是通過姓氏從鏈表中刪除一個人。 void *的原因是因爲我們必須使用泛型來完成這項任務,以瞭解它們的工作原理。 – meepz 2010-03-29 00:24:30
通過幾個例子,你的問題將會大大地解釋**。如果你以單元測試的形式編寫例子,你甚至可以看到你的代碼是否適合這些例子。 – 2010-03-29 01:46:27