今天学习了宏的内容
//预处理器在执行宏指令时将程序中出现的“宏”用“字符串”代替,这一过程称为宏展开
//例题8.1求圆周长和面积
#include
using namespace std;
#define PI 3.14
int main()
{
float r;
float c, s;
cout << “请输入圆的半径(cm):”;
cin >> r;
c = 2 * PI * r;
s = PI * r * r;
cout << “圆周长为” << c << “厘米” << endl;
cout << “圆面积为” << s << “平方厘米” << endl;
}
//使用宏定义的好处就在于便于代码的维护和理解
//定义带参数的宏,求一个数的平方
#include
using namespace std;
#define square®®*®;
int main()
{
int a = 2, b = 3, c;
c = square(a);
cout << a << “的平方为” << c << endl;
c = square(a + b);
cout << a + b << “的平方为” << c << endl;
}
//利用条件编译,输入一行字母字符,根据需要设定条件编译,使之需要设定条件编译,使之能将字母全改成大写输出,否则小写输出
#include
using namespace std;
#define Mark 1
int main()
{
char c;
do
{
cin.get©;
#ifdef Mark
if ( c >= ‘a’ && c <= ‘z’ )
c = c + 32;
#endif
cout << c;
} while (c!=’\n’);
}