修改代码
#include <iostream>using namespace std;
#definePI3.1415926;
#defineAREA(r)PI * r * r
int main()
{
double radius, s;
cout << "Enter the radius of a circle: ";
cin >> radius;
s = AREA(radius);
cout << "radius=" << radius << "The area is " << s << "\n";
s = AREA(radius + 1);
cout << "radius=" << (radius + 1) << "The area is " << s << "\n";
return 0;
} #include <iostream>
using namespace std;
#define PI 3.1415926
#define AREA(r) PI * r * r
int main()
{
double radius, s;
cout << "Enter the radius of a circle: ";
cin >> radius;
s = AREA(radius);
cout << "radius=" << radius << "The area is " << s << "\n";
s = AREA(radius + 1);
cout << "radius=" << (radius + 1) << "The area is " << s << "\n";
return 0;
}
修改后的代码去掉了PI的分号,并且在宏定义AREA(r)中去掉了分号。分号会导致宏展开时出现意外的错误。
#include <iostream>
using namespace std;
#define PI 3.1415926
#define AREA(r) PI * r * r
int main()
{
double radius, s;
cout << "Enter the radius of a circle: ";
cin >> radius;
s = AREA(radius);
cout << "radius=" << radius << " The area is " << s << "\n";// 在输出中添加空格分隔符
s = AREA(radius + 1);
cout << "radius=" << (radius + 1) << " The area is " << s << "\n";// 在输出中添加空格分隔符
return 0;
}
在这段代码中,我帮您去掉了宏定义末尾多余的分号,并且修正了输出语句中的空格分隔符。现在定义的PI和AREA宏应该可以正确计算圆的面积了。如果有其他问题或需要进一步帮助,请随时提问。
页:
[1]