如题,是老师给的作业,但写出来总有错。帮忙看看
#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.");
}
}
int compare(int a,int b)
{
if(a<b) return -1;
if(a>b) return 1;
else return 0;
}
int compare(int x,int y)
{
if(x>y)
return 1;
else if(x < y)
return -1;
return 0;
}
1.compare是用来比较字符串的,不是比较数值的。
2.compare的返回值是传入的参数x或y,不是0,1,-1。