对于这个任务我必须创建我自己的字符串类。我最初编写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