代码实例

#include <iostream>
using namespace std;
int main()
{
double radius;
double area;
// Step 1: Read in radius
radius = 20;
// Step 2: Compute area
area = radius * radius * 3.14159;
// Step 3: Display the area
cout << "The area is " << area << endl;
return 0;
}
  1. #include <iostream>: 这行引入了输入输出流库,允许程序使用cin和cout进行输入输出操作。
  2. using namespace std;: 这行声明使用标准命名空间,这样就不需要在每个标准库函数前加上std::。
  3. int main(): 这是程序的主函数,程序从这里开始执行。
  4. double radius;double area;: 这两行声明了两个双精度浮点数变量,用于存储圆的半径和面积。
  5. radius = 20;: 这行给radius变量赋值20。在实际应用中,这个值可能会从用户输入中获取。
  6. area = radius * radius * 3.14159;: 这行计算圆的面积。公式是πr²,其中π近似为3.14159。
  7. cout << "The area is " << area << endl;: 这行输出计算结果。它会在控制台显示"The area is ",然后是计算出的面积值。
  8. return 0;: 这行表示程序正常结束,返回0给操作系统。

声明变量

image.png


#include <iostream>
using namespace std;
int main()
{
// Step 1: Read in radius
double radius;
cout << "Enter a radius: ";
cin >> radius;
// Step 2: Compute area
double area = radius * radius * 3.14159;
// Step 3: Display the area
cout << "The area is " << area << endl;
return 0;
}

cout发音为“see-out”。用于输出,并使用插入运算符 ( <<)

cin发音为“see-in”。用于输入,并使用提取运算符(>>