C++控制台打印正三角形、倒三角形、平行四边形

新手练习,若有不标准或者错误以及方法较笨的地方欢迎大家指出。
多帮我积累积累经验,感谢!o(╥﹏╥)o

正文
标题与思路:
①正三角
在这里插入图片描述

 //正三角
void regularTriangle(int a)
{
    
    
	cout << "※正三角\n";
	for (int i = 0; i < a; i++)
	{
    
    
		for (int j = a; j > i+1; j--) 
			cout << " ";
		for (int j = 0; j < i+1; j++)
			cout << "* ";
		cout << "\n";
	}
}

②倒三角
在这里插入图片描述

 //倒三角
void invertedTriangle(int a) {
    
    
	cout << "\n※倒三角\n";
	for (int i = 0; i < a; i++)
	{
    
    
		for (int j = 0; j < i; j++)
			cout << " ";
		for (int j = a; j > i; j--)
			cout << "* ";
		cout << "\n";
	}
}

③平行四边形
有上面两问可知,我们只需要在要输入行数的一半

void parallelogram(int a) {
    
    
	if (a % 2 == 0) {
    
    
		cout << "\n平行四边形无法生成,因为不是奇数!";
	}
	else {
    
    
		cout << "\n※平行四边形\n";
		for (int i = 0; i < a; i++)
		{
    
    
			if (i < a / 2 + 1) {
    
    
				for (int j = a; j > i + 1; j--)
					cout << " ";
				for (int j = 0; j < i + 1; j++)
					cout << "* ";
				cout << "\n";
			}
			else {
    
    
				for (int j = 0; j < i; j++)
					cout << " ";
				for (int j = a; j > i; j--)
					cout << "* ";
				cout << "\n";
			}
		}
	}
}

执行Main函数并输出打印结果:

int main()
{
    
    
	cout << "請輸入要輸入的長度:";
	int a;
	cin >> a;
	regularTriangle(a);
	invertedTriangle(a);
	parallelogram(a);
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43492821/article/details/121600073