鱼C论坛

 找回密码
 立即注册
查看: 1284|回复: 2

[已解决]类型的实参与 类型的形参不兼容

[复制链接]
发表于 2022-2-3 03:46:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这类问题怎么解决吖?

1.png

#include<stdio.h>
#include<string.h>
#define max 101

struct Box{
        int value;   // 总价格
        int weight;  // 重量
        double price;   // 单价
};

void QuickSort(struct Box *a, int left, int right) {
        int l = left;
        int r = right;
        double tmp;
        if (left < right) {
                tmp = a[left].price;
                while (l != r) {
                        while (r > l && strcmp(a[r].price, tmp) >= 0) {
                                r--;
                        }
                        a[l] = a[r];
                        while (l < r && strcmp(a[l].price, tmp) <= 0) {
                                l++;
                        }
                        a[r] = a[l];
                }
                a[l].price = tmp;
                QuickSort(a, left, l - 1);
                QuickSort(a, l + 1, right);
        }
}
最佳答案
2022-2-3 07:41:10
先说错误原因:strcmp函数是用来比较字符串的,所以它两个形参类型是char型,而你的tmp是double型,所以类型不匹配。
修正方法:你是想要比较a[r].price, tmp的话,直接用a[r].price<= tmp
代码修正:void QuickSort(struct Box *a, int left, int right) {
        int l = left;
        int r = right;
        double tmp;
        if (left < right) {
                tmp = a[left].price;
                while (l != r) {
                        while (r > l && a[r].price>= tmp) {
                                r--;
                        }
                        a[l] = a[r];
                        while (l < r && a[l].price <= tmp) {
                                l++;
                        }
                        a[r] = a[l];
                }
                a[l].price = tmp;
                QuickSort(a, left, l - 1);
                QuickSort(a, l + 1, right);
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-3 07:41:10 | 显示全部楼层    本楼为最佳答案   
先说错误原因:strcmp函数是用来比较字符串的,所以它两个形参类型是char型,而你的tmp是double型,所以类型不匹配。
修正方法:你是想要比较a[r].price, tmp的话,直接用a[r].price<= tmp
代码修正:void QuickSort(struct Box *a, int left, int right) {
        int l = left;
        int r = right;
        double tmp;
        if (left < right) {
                tmp = a[left].price;
                while (l != r) {
                        while (r > l && a[r].price>= tmp) {
                                r--;
                        }
                        a[l] = a[r];
                        while (l < r && a[l].price <= tmp) {
                                l++;
                        }
                        a[r] = a[l];
                }
                a[l].price = tmp;
                QuickSort(a, left, l - 1);
                QuickSort(a, l + 1, right);
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-3 14:30:20 | 显示全部楼层
Bailey_Kung 发表于 2022-2-3 07:41
先说错误原因:strcmp函数是用来比较字符串的,所以它两个形参类型是char型,而你的tmp是double型,所以类 ...

睡醒的时候懂了谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-6 14:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表