2010-03-28 96 views
1

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

在搜索列表时,您不想要什么顺序。你想要按字母顺序排列的ASCII-betical(http://catb.org/jargon/html/A/ASCIIbetical-order.html)还是其他的东西?你想改变你的'compareto'函数,还是定义另一个?另外,为什么你将'void *'传递给函数,而不是'String *',这将使它们类型安全?你把函数指针放在'string_t'中来实现继承还是封装?你的海明距离比较算法的推理是什么? – outis 2010-03-29 00:08:07

+0

我的目标是按字母顺序排序。我使用这个功能的目的是通过姓氏从链表中删除一个人。 void *的原因是因为我们必须使用泛型来完成这项任务,以了解它们的工作原理。 – meepz 2010-03-29 00:24:30

+0

通过几个例子,你的问题将会大大地解释**。如果你以单元测试的形式编写例子,你甚至可以看到你的代码是否适合这些例子。 – 2010-03-29 01:46:27

回答

1

尝试比较以下字符串对:

“ABC”与“DEF”

“ADF” 与 “BBB”

“ABC” 与 “CBA”

你会得到什么样的结果?更重要的是,为什么?这些结果与你想要得到的结果相比如何?

(你应该先解决它在你的头上。锻炼身体的C1和C2的值进行比较循环的每个步骤。)

+0

我得到的结果是:2,1,2. 这些结果只是告诉我哪个字符串累积地比我想要得到的字符串更大(或按顺序)。我知道如何在java中编写这个代码,并且使用,我只是困惑于如何自己编写代码。我会继续尝试去思考它 – meepz 2010-03-29 01:30:56

+0

问问你自己,你知道哪一个字符串是第一个:“ABC”或“ACB”。然后,“AABC”与“AACC”。你这样做就像你想要电脑一样,所以慢一点,弄清楚你在做什么。将其转化为代码应该相当简单。 – CWF 2010-03-29 02:46:52

0

首先,ln没有正确的样品removeStudent()在初始化,因此调用ln->compareTo可能会导致段错误。希望ln已在您的实际代码中正确初始化。

要定义字符串排序,您可以首先定义数据库圈中已知的“排序规则”:字符排序。您可以将排序规则实现为函数(称为chrcmp),也可以内联在字符串比较函数中。重要的是要定义它。

一般而言,某种类型的排序会在该类型的序列上产生lexicographic order:比较两个序列,找到它们不同的第一个地方;较小的序列是在该位置具有较小元素的序列。

更正式地,假设序列索引从0开始。让一个b基类型长度米分别Ñ,的是序列。词典顺序一个≤b为:

  • 一个< b其中a R B 并用于所有0≤Ĵ<我
  • 一个< B A Ĵ =米Ĵ如果A是b的前缀
  • A = b如果m = n和一个 = b 对于所有0≤我<米

其中, “A是B的前缀” 是指米< n和一个 = B 对于所有0≤我<米。

这种方法的优点是你可以编写一个比较函数,它可以与任何homogeneous序列类型一起工作:字符串,字符串列表,整数数组,什么是你。如果您专门针对以空字符结尾的字符串比较函数,则不需要担心前缀的情况;只需将'\ 0'作为整理中的最小字符。

从一般比较功能(称为比如说,lexiCompare),你可以定义

lexicCompareString (a, b): 
    return lexicCompare(a, b, chrcmp) 

和字符串的compareTo成员设置为lexicCompareString