一、 打印三角形
要求:
打印如下类似的三角形
*
***
*****
*******
效果:
思路:
(1) 空格每往下一层少一个。
(2) *是奇数数列,(i – 1)* 2 + 1
核心代码:
for(int i = 1; i <= layer; i++) {
for(int j = 1;j <= (layer - i); j++) {
cout<<" ";
}
for(int k = 1; k <= (i - 1) * 2 + 1; k++) {
cout<<"*";
}
cout<<"\n";
}
for(int j = 1;j <= (layer - i); j++) {
cout<<" ";
}
for(int k = 1; k <= (i - 1) * 2 + 1; k++) {
cout<<"*";
}
cout<<"\n";
}
二、 打印菱形
要求:
打印如下类似的菱形
*
***
*****
***
*
效果:
思路:
(1) 空格是对称的,中间层(例如要求中的菱形,中间层为3)的空格为0,而两边的空格数则为当前层数减去中间层的绝对值。
(2) *也是对称的,中间层数减去空格数,就等于如上奇数数列的i.
核心代码:
for(int i = 1; i <= layer; i++) {
int j;
for(j = 1;j <= abs(i - mid); j++) {
cout<<" ";
}
for(int k = 1; k <= (mid - j) * 2 + 1; k++) {
cout<<"*";
}
cout<<"\n";
}
int j;
for(j = 1;j <= abs(i - mid); j++) {
cout<<" ";
}
for(int k = 1; k <= (mid - j) * 2 + 1; k++) {
cout<<"*";
}
cout<<"\n";
}
三、所有代码:
代码
//GeometryTest.cpp
//powered by Alan
//2010.09.18
#include<iostream>
using namespace std;
//画三角形
void DrawTri();
//画菱形
void DrawDia();
int main() {
int type = 0;
cout<<"选择您需要的图形:\n";
cout<<" 1.三角形\n";
cout<<" 2.菱形\n";
cin>>type;
while(!(cin.good() && type == 1 || type == 2)) {
cin.clear();
cin.ignore(1024,'\n');
cout<<"输入的图形类型不正确重新输入:\n";
cin>>type;
}
switch(type) {
case 1:
DrawTri();
break;
case 2:
DrawDia();
break;
default:
cout<<"输入的图形类型不正确重新输入:\n";
break;
}
return 0;
}
void DrawTri() {
//记录层数
int layer = 0;
cout<<"输入层数:\n";
cin>>layer;
while(!(cin.good() && layer > 1)) {
cin.clear();
cin.ignore(1024,'\n');
cout<<"输入的层数类型不正确或层数小于2,请重新输入:\n";
cin>>layer;
}
cout<<layer<<"层三角形:\n";
for(int i = 1; i <= layer; i++) {
for(int j = 1;j <= (layer - i); j++) {
cout<<" ";
}
for(int k = 1; k <= (i - 1) * 2 + 1; k++) {
cout<<"*";
}
cout<<"\n";
}
}
void DrawDia() {
//记录层数
int layer = 0;
//记录中间层
int mid;
cout<<"输入层数:\n";
cin>>layer;
while(!(cin.good() && layer > 3 && (layer%2 != 0))) {
cin.clear();
cin.ignore(1024,'\n');
cout<<"输入的层数类型不正确或层数小于3或层数不为基数,请重新输入:\n";
cin>>layer;
}
mid = (layer + 1)/2;
cout<<layer<<"层菱形:\n";
for(int i = 1; i <= layer; i++) {
int j;
for(j = 1;j <= abs(i - mid); j++) {
cout<<" ";
}
for(int k = 1; k <= (mid - j) * 2 + 1; k++) {
cout<<"*";
}
cout<<"\n";
}
}
//powered by Alan
//2010.09.18
#include<iostream>
using namespace std;
//画三角形
void DrawTri();
//画菱形
void DrawDia();
int main() {
int type = 0;
cout<<"选择您需要的图形:\n";
cout<<" 1.三角形\n";
cout<<" 2.菱形\n";
cin>>type;
while(!(cin.good() && type == 1 || type == 2)) {
cin.clear();
cin.ignore(1024,'\n');
cout<<"输入的图形类型不正确重新输入:\n";
cin>>type;
}
switch(type) {
case 1:
DrawTri();
break;
case 2:
DrawDia();
break;
default:
cout<<"输入的图形类型不正确重新输入:\n";
break;
}
return 0;
}
void DrawTri() {
//记录层数
int layer = 0;
cout<<"输入层数:\n";
cin>>layer;
while(!(cin.good() && layer > 1)) {
cin.clear();
cin.ignore(1024,'\n');
cout<<"输入的层数类型不正确或层数小于2,请重新输入:\n";
cin>>layer;
}
cout<<layer<<"层三角形:\n";
for(int i = 1; i <= layer; i++) {
for(int j = 1;j <= (layer - i); j++) {
cout<<" ";
}
for(int k = 1; k <= (i - 1) * 2 + 1; k++) {
cout<<"*";
}
cout<<"\n";
}
}
void DrawDia() {
//记录层数
int layer = 0;
//记录中间层
int mid;
cout<<"输入层数:\n";
cin>>layer;
while(!(cin.good() && layer > 3 && (layer%2 != 0))) {
cin.clear();
cin.ignore(1024,'\n');
cout<<"输入的层数类型不正确或层数小于3或层数不为基数,请重新输入:\n";
cin>>layer;
}
mid = (layer + 1)/2;
cout<<layer<<"层菱形:\n";
for(int i = 1; i <= layer; i++) {
int j;
for(j = 1;j <= abs(i - mid); j++) {
cout<<" ";
}
for(int k = 1; k <= (mid - j) * 2 + 1; k++) {
cout<<"*";
}
cout<<"\n";
}
}