使用C++,定义多个简单函数完成直角三角形,等腰三角形,矩形,平行四边形,菱形的输出
时间: 2024-11-18 17:19:15 浏览: 45
在C++中,你可以定义一系列函数来分别处理这五种图形的输出。这里是一个简单的示例,每个函数接收相应的属性作为输入并打印出图形:
```cpp
#include <iostream>
// 直角三角形
void print_right_triangle(int side) {
for (int i = 0; i <= side; ++i) {
std::cout << "* ";
for (int j = 0; j < side - i; ++j)
std::cout << " ";
std::cout << "\n";
}
}
// 等腰三角形
void print_isosceles_triangle(int base, int height) {
for (int i = 0; i < base; ++i) {
if (i == 0 || i == base / 2) {
std::cout << "#";
} else {
std::cout << " ";
}
for (int j = 0; j < height - 1; ++j) {
std::cout << " ";
}
std::cout << "#\n";
}
}
// 矩形
void print_rectangle(int width, int height) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j)
std::cout << "* ";
std::cout << "\n";
}
}
// 平行四边形
void print_parallelogram(int base, int side) {
for (int i = 0; i < base; ++i) {
for (int j = 0; j < side; ++j)
std::cout << "* ";
if (i % 2 == 0)
std::cout << "\n";
else
std::cout << "\t";
}
}
// 菱形
void print_kite(int length, int width) {
for (int i = 0; i < length; ++i) {
if (i == 0 || i == length / 2)
std::cout << "* ";
else if (i % 2 == 0)
std::cout << " ";
for (int j = 0; j < width; ++j) {
if (j == 0 || j == width / 2)
std::cout << "* ";
else
std::cout << " ";
}
std::cout << "\n";
}
}
// 打印帮助信息
void print_usage() {
std::cout << "Usage: Call a function with appropriate parameters like print_right_triangle(side)\n";
}
int main() {
// 示例调用
print_right_triangle(5);
print_isosceles_triangle(6, 4);
print_rectangle(4, 3);
print_parallelogram(5, 3);
print_kite(7, 4);
return 0;
}
```
阅读全文