明总推荐的入门书籍,但是发现课后习题不是那么容易,网上很难找到完整的答案,现在特整理出来
#include <iostream>
//#include <cstdlib>
//#include <string>
#include <cmath>
using namespace std;
int main(void)
{
double max(double,double);
double min(double,double);
double a,b;
while(1)
{
cin >> a;
if(cin.fail())
{
char c;
cin.clear();
c = cin.get();
if (c == '|')
{
cout << "game over ^-^!" << endl;
break;
}
else
{
cout << "Input wrong information !" << endl;
break;
}
}
cin >> b;
if(cin.fail())
{
char c;
cin.clear();
c = cin.get();
if (c == '|')
{
cout << "game over ^-^!" << endl;
break;
}
else
{
cout << "Input wrong information !" << endl;
break;
}
}
cout << "a = " << a << " , b = " << b << endl;
cout <<"The larger number is : " << max(a,b)<<" "<<"The smaller number is : "<<min(a,b)<<endl;
if (abs(a-b) < 1.0/100)
cout <<"the numbers are almost equal"<<endl;
}
system("pause");
return 0;
}
double max(double a,double b)
{
if (a>b)
return a;
else return b;
}
double min(double a,double b)
{
if (a<=b)
return a;
else return b;
}
1. 关于cin的用法参考点击打开链接(主要是cin.fail() / cin.clear / cin.get())
2. double、int类型等的绝对值相关 abs,fabs