将字符串与用户创建的字符串类进行比较

时间:2010-03-28 23:23:09

标签: c

对于这项任务,我必须创建自己的字符串类。我最初写了compareto方法来比较两个字符串,但返回总体较大的那个。我想要做的是比较并返回哪一个字母大,即比较两个字符串,例如:史密斯和受害者。我设计compareto方法的方法是结果是它们是相同的。我想要做的是告诉我哪一个按字母顺序排在第一位,所以对于我的例子来说,受害者会先来。我理解如何使用<string.h>库在Java中甚至在C中执行此操作,我只是对如何自己这样做感到困惑。

编辑:我只想注意,我不是在寻找代码答案,而是在编写代码时轻推。

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
}

2 个答案:

答案 0 :(得分:1)

尝试比较以下字符串对:

“ABC”与“DEF”

“ADF”vs“BBB”

“ABC”vs“CBA”

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

(你应该首先解决这个问题。在比较循环的每一步中计算出c1和c2的值。)

答案 1 :(得分:0)

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

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

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

更正式地说,假设序列从0开始编号。让 a b 是长度 m 和<的基本类型的序列强> n ,分别。字典顺序a≤b是:

  • a&lt; b,其中a i R b i 并且 j = m j 对于所有0≤j<1。我
  • a&lt; b如果a是b
  • 的前缀
  • a = b如果m = n并且a i = b i 对于所有0≤i<1。米

其中“a是b的前缀”表示m i = b i 对于所有0≤i<1。米。

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

从一般比较函数(称为lexiCompare),您可以定义

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

并将String的compareTo成员设置为lexicCompareString