c - 结构类型的排序数组

标签 c arrays sorting pointers struct

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NAMELEN 32



 struct Currency
{
    char name[NAMELEN];
    double exchange_rate;
    int action;
};



 void printCurrency(struct Currency *cur);

void readCurrency(struct Currency *cur);

int mystrcmp(const char* s1, const char *s2);

int compareCurrency(struct Currency *c1, struct Currency *c2);

void sortCurrency(struct Currency *carr[],int n); 

int main(void) {

    int i = -1, size = 0;

    struct Currency  a, b, list[] = { { "Polish Zloty", 1.f, 0 },{ "United States dollar", 4.2f, 1 },{ "Euro", 4.4f, 0 },
    { "Turkish Lira", 2.f, 1 },{ "Japanese Yen",2.3f ,0 },{ "Swedish krona",2.0f,1 },
    { "Swiss frank",2.4f,0 },{ "Singapore dollar",2.2f,0 },{ "Euro", 4.f, 1 },
    { "Swedish krona",2.5f,0 },{ "Swiss frank",2.2f,1 },{ "Singapore dollar",2.8f,1 },
    { "Singapore dollar",2.4f,1 },{ "Polish Zloty", 1.1f, 1 } };

    i++;
    size = sizeof(list) / sizeof(struct Currency);
    readCurrency(&a);
    printCurrency(&a);
    puts("");


    {
        char* s1 = "aliot";
        char* s2 = "ali";
        puts("\nTest mystrcmp():");
        printf("%s %s : %d\n", s1, s1, mystrcmp(s1, s1));
        printf("%s %s : %d\n", s2, s1, mystrcmp(s2, s1));
        printf("%s %s : %d\n\n", s1, s2, mystrcmp(s1, s2));
    }


    readCurrency(&b);
    puts("Test comparing currencies:");
    printCurrency(&b);
    puts("\nand");
    printCurrency(&a);
    printf("\nResult (a,b)%d (b,a)%d (b,b)%d\n", compareCurrency(&a, &b), compareCurrency(&b, &a), compareCurrency(&b, &b));

    puts("\nUnsorted list");
    for (i = 0; i < size; i++) {
        printCurrency(&list[i]);
        puts("");
    }

    sortCurrency(list, size);

    puts("\nSorted list");
    for (i = 0; i < size; i++) {
        printCurrency(&list[i]);
        puts("");
    }
/**/
    return 0;
}

void readCurrency(struct Currency *cur) {
    scanf("%s %lf %d", cur->name, &cur->exchange_rate, &cur->action);
}

void printCurrency(struct Currency* cur) {
    printf("currency name :%s \ncurrency rate : %f\n", cur->name, cur->exchange_rate);
    if (cur->action == 0)
        printf("action : buy");
    else
        printf("action : sell");
}

int mystrcmp(const char* s1, const char *s2) {
    int i = 0;
    while(s1[i]==s2[i]){
        if(s1[i] == '\0')
            return 0;
        i++;
    }
    return (int)s1[i] - (int)s2[i];
}

int compareCurrency(struct Currency *c1, struct Currency *c2) {
    if(mystrcmp(c1->name,c2->name) == 0 ){
        if(c1->exchange_rate == c2->exchange_rate){
            return 0;
        }
        else
            return c1->exchange_rate - c2->exchange_rate;
    }   
}

void sortCurrency(struct Currency *carr[],int n){
    int i,j;
    struct Currency tmp;
    for(i = 1 ; i < n  ; i++){
        tmp = carr[i];
        for(j = i - 1 ; j < n - 1 ; j++){
         if(compareCurrency(carr[j],carr[j+1]) > 0){
             tmp = carr[j];
             carr[j] = carr[j + 1];
             carr[j + 1] = tmp;
            }    
        }
    }
}


你好朋友,
 我有一些由struct和函数sortCurrency定义的类型的预定义货币列表,我需要根据它们的exchange_rate对所有这些货币进行排序,但是我怎么会遇到一些错误。

错误:

 inlab.c: In function ‘main’:
inlab.c:67:15: warning: passing argument 1 of ‘sortCurrency’ from incompatible pointer type [-Wincompatible-pointer-types]
  sortCurrency(list, size);
               ^~~~
inlab.c:25:6: note: expected ‘struct Currency **’ but argument is of type ‘struct Currency *’
 void sortCurrency(struct Currency *carr[],int n);
      ^~~~~~~~~~~~
inlab.c: In function ‘sortCurrency’:
inlab.c:116:9: error: incompatible types when assigning to type ‘struct Currency’ from type ‘struct Currency *’
     tmp = carr[j];
         ^
inlab.c:118:17: error: incompatible types when assigning to type ‘struct Currency *’ from type ‘struct Currency’
     carr[j + 1] = tmp;


我想要的输出

Compare your program's output with the following (user input highlighted in bold):
Give: name exchange_rate buy or sell (0 or 1)
Bitcoin 52000 1 // user input
Bitcoin 52000.000000 sell

Test mystrcmp():
Ala Ala : 0
Alan Ala : 110
Ala Alan : -110

Give: name exchange_rate buy or sell (0 or 1)
Ethereum 1500 0 // user input
Test comparing currencies:
Ethereum 1500.000000 buy
and
Bitcoin 52000.000000 sell
Result (a,b)-3 (b,a)3 (b,b)0

Unsorted list
Polish Zloty 1.000000 buy
United States dollar 4.200000 sell
Euro 4.400000 buy
Turkish Lira 2.000000 sell
Japanese Yen 2.300000 buy
Swedish krona 2.000000 sell
Swiss frank 2.400000 buy
Singapore dollar 2.200000 buy
Euro 4.000000 sell
Swedish krona 2.500000 buy
Swiss frank 2.200000 sell
Singapore dollar 2.800000 sell
Singapore dollar 2.400000 sell
Polish Zloty 1.100000 sell

Sorted list
Euro 4.400000 buy
Euro 4.000000 sell
Japanese Yen 2.300000 buy
Polish Zloty 1.000000 buy
Polish Zloty 1.100000 sell
Singapore dollar 2.200000 buy
Singapore dollar 2.400000 sell
Singapore dollar 2.800000 sell
Swedish krona 2.500000 buy
Swedish krona 2.000000 sell
Swiss frank 2.400000 buy
Swiss frank 2.200000 sell
Turkish Lira 2.000000 sell
United States dollar 4.200000 sell

最佳答案

您必须听听编译器的抱怨。

我修复了基本类型不匹配的问题,该问题使您可以编译并进行进一步的测试:

void sortCurrency(struct Currency *carr[],int n); 


已更改为:

void sortCurrency(struct Currency carr[], int n); 


并且对compareCurrency的调用已调整。

     if(compareCurrency(&carr[j],&carr[j+1]) > 0){


您需要调试排序,因为它不起作用。
这是修改后的程序及其当前输出:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NAMELEN 32

struct Currency
{
    char name[NAMELEN];
    double exchange_rate;
    int action;
};

void printCurrency(struct Currency *cur);

void readCurrency(struct Currency *cur);

int mystrcmp(const char* s1, const char *s2);

int compareCurrency(struct Currency *c1, struct Currency *c2);

void sortCurrency(struct Currency carr[], int n); 


int mystrcmp(const char* s1, const char *s2) {
    int i = 0;
    while(s1[i]==s2[i]){
        if(s1[i] == '\0')
            return 0;
        i++;
    }
    return (int)s1[i] - (int)s2[i];
}

int compareCurrency(struct Currency *c1, struct Currency *c2) {
    if(mystrcmp(c1->name,c2->name) == 0 ){
        if(c1->exchange_rate == c2->exchange_rate){
            return 0;
        }
        else
            return c1->exchange_rate - c2->exchange_rate;
    }   
}

void sortCurrency(struct Currency carr[],int n){
    int i,j;
    struct Currency tmp;
    for(i = 1 ; i < n  ; i++){
        tmp = carr[i];
        for(j = i - 1 ; j < n - 1 ; j++){
         if(compareCurrency(&carr[j],&carr[j+1]) > 0){
             tmp = carr[j];
             carr[j] = carr[j + 1];
             carr[j + 1] = tmp;
            }    
        }
    }
}

void readCurrency(struct Currency *cur) {
    scanf("%s %lf %d", cur->name, &cur->exchange_rate, &cur->action);
}

void printCurrency(struct Currency* cur) {
    printf("currency name :%s \ncurrency rate : %f\n", cur->name, cur->exchange_rate);

    if (cur->action == 0)
        printf("action : buy");
    else
        printf("action : sell");
}

int main(void) {

    int i = -1, size = 0;

    struct Currency  a, b;
    struct Currency list[] = 
    {
    { "Polish Zloty", 1.f, 0 },{ "United States dollar", 4.2f, 1 },{ "Euro", 4.4f, 0 },
    { "Turkish Lira", 2.f, 1 },{ "Japanese Yen",2.3f ,0 },{ "Swedish krona",2.0f,1 },
    { "Swiss frank",2.4f,0 },{ "Singapore dollar",2.2f,0 },{ "Euro", 4.f, 1 },
    { "Swedish krona",2.5f,0 },{ "Swiss frank",2.2f,1 },{ "Singapore dollar",2.8f,1 },
    { "Singapore dollar",2.4f,1 },{ "Polish Zloty", 1.1f, 1 } 
    };

    i++;

    size = sizeof(list) / sizeof(struct Currency);

    readCurrency(&a);
    printCurrency(&a);

    puts("");

    {
        char* s1 = "aliot";
        char* s2 = "ali";
        puts("\nTest mystrcmp():");
        printf("%s %s : %d\n", s1, s1, mystrcmp(s1, s1));
        printf("%s %s : %d\n", s2, s1, mystrcmp(s2, s1));
        printf("%s %s : %d\n\n", s1, s2, mystrcmp(s1, s2));
    }


    readCurrency(&b);
    puts("Test comparing currencies:");
    printCurrency(&b);
    puts("\nand");
    printCurrency(&a);
    printf("\nResult (a,b)%d (b,a)%d (b,b)%d\n", compareCurrency(&a, &b), compareCurrency(&b, &a), compareCurrency(&b, &b));

    puts("\nUnsorted list");
    for (i = 0; i < size; i++) {
        printCurrency(&list[i]);
        puts("");
    }

    sortCurrency(list, size);

    puts("\nSorted list");
    for (i = 0; i < size; i++) {
        printCurrency(&list[i]);
        puts("");
    }
/**/
    return 0;
}


输出:

zloty 4.3 1
dollar 2.2 0
currency name :zloty 
currency rate : 4.300000
action : sell

Test mystrcmp():
aliot aliot : 0
ali aliot : -111
aliot ali : 111

Test comparing currencies:
currency name :dollar 
currency rate : 2.200000
action : buy
and
currency name :zloty 
currency rate : 4.300000
action : sell
Result (a,b)22 (b,a)-22 (b,b)0

Unsorted list
currency name :Polish Zloty 
currency rate : 1.000000
action : buy
currency name :United States dollar 
currency rate : 4.200000
action : sell
currency name :Euro 
currency rate : 4.400000
action : buy
currency name :Turkish Lira 
currency rate : 2.000000
action : sell
currency name :Japanese Yen 
currency rate : 2.300000
action : buy
currency name :Swedish krona 
currency rate : 2.000000
action : sell
currency name :Swiss frank 
currency rate : 2.400000
action : buy
currency name :Singapore dollar 
currency rate : 2.200000
action : buy
currency name :Euro 
currency rate : 4.000000
action : sell
currency name :Swedish krona 
currency rate : 2.500000
action : buy
currency name :Swiss frank 
currency rate : 2.200000
action : sell
currency name :Singapore dollar 
currency rate : 2.800000
action : sell
currency name :Singapore dollar 
currency rate : 2.400000
action : sell
currency name :Polish Zloty 
currency rate : 1.100000
action : sell

Sorted list
currency name :Polish Zloty 
currency rate : 1.000000
action : buy
currency name :Euro 
currency rate : 4.400000
action : buy
currency name :Japanese Yen 
currency rate : 2.300000
action : buy
currency name :Singapore dollar 
currency rate : 2.200000
action : buy
currency name :Euro 
currency rate : 4.000000
action : sell
currency name :Singapore dollar 
currency rate : 2.800000
action : sell
currency name :Polish Zloty 
currency rate : 1.100000
action : sell
currency name :Singapore dollar 
currency rate : 2.400000
action : sell
currency name :Swedish krona 
currency rate : 2.000000
action : sell
currency name :Swedish krona 
currency rate : 2.500000
action : buy
currency name :Swiss frank 
currency rate : 2.400000
action : buy
currency name :Swiss frank 
currency rate : 2.200000
action : sell
currency name :Turkish Lira 
currency rate : 2.000000
action : sell
currency name :United States dollar 
currency rate : 4.200000
action : sell

关于c - 结构类型的排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47738533/

相关文章:

c - ANSI C : Pointers to strings literals

javascript - 如何将 Angular ui-grid 与字符串数组一起使用

c++ - 用 2 维数组初始化 3 维数组

arrays - 我可以在 Perl 中命名一个匿名数组吗?

database - 复制稳定 sortKey 的概念

c++ - 将多重集排序为升序子序列,每个可用元素出现一次

c - 如何在 C 代码中转到上一行

c++ - 我的代码不适用于大数字

c - 确定进程是否已死 - 通过 PID

java - 对字符串数组进行排序会出现 NullPointerException