dayglow 2021-08-26 10:26 采纳率: 33.3%
浏览 119
已结题

c++中用compare函数比较两个数大小

img
如题,是老师给的作业,但写出来总有错。帮忙看看
#include
using namespace std;

// 请在此编写compare()函数

//~

void PrintLine(const char* s) {
cout<< s << endl;
}

void main() {
int x, y;
PrintLine("Input X:");
cin>>x;
PrintLine("Input Y:");
cin>>y;

int result = 0;
//result = compare(x, y);
if (result == 0) {
    PrintLine("X is equal to Y.");
} else if (result < 0) {
    PrintLine("X is less than Y.");
} else {
    PrintLine("X is greater than Y.");
}

}

  • 写回答

3条回答 默认 最新

  • 诺er~ 2021-08-26 10:28
    关注
    
    int compare(int a,int b)
    {
        if(a<b) return -1;
        if(a>b) return 1;
        else return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月26日
  • 修改了问题 8月26日
  • 创建了问题 8月26日