/***************************************
* PrintStars.cpp | Copyright by SirX
* 2006-01-07
****************************************/
#include <iostream>
#include <string>
#include<conio.h> //getch()
using namespace std;
class Star {
char star; //you can change it
char blank; //you can change it
string leftSpace; //you can change it
void prtMsg(const string &msg) /*print message*/ {
cout<<"*Error : "<<msg<<endl;
}
void prtLineStars(int num) /*print a line of stars with number of num*/ {
for(int i=0;i<num-1;i++) cout<<star<<blank;
cout<<star;
}
void prtLine(int blankNum,int length); //blankNum==the number of blanks
public:
Star(const string &leftSpac="",char blan=' ',char sta='*') : star(sta),blank(blan),leftSpace(leftSpac) {}
//print an up triangle of an definite length
bool prtUpTriagl(int length,int rightShift=0);
//print a down triangle of an definite length
bool prtDownTriagl(int length,int rightShift=0);
//print a rhombus of an definite length
void prtStars(int length,int rightShift=0);
};
void Star::prtLine(int blankNum,int length) {
cout<<leftSpace; //left space
for(int i=0;i<blankNum;i++) cout<<blank;
prtLineStars(length-blankNum);
for(int j=0;j<blankNum;j++) cout<<blank;
}
bool Star::prtUpTriagl(int length /*triangle's length*/,int rightShift) {
if(length<1) { prtMsg("Not positive length."); return false;}
if(rightShift<=0) rightShift=0;
else length+=rightShift;
for(int i=length-1;i>rightShift;i--) {
prtLine(i,length);
cout<<endl;
}
prtLine(rightShift,length);
return true;
}
bool Star::prtDownTriagl(int length /*triangle's length*/,int rightShift) {
if(length<1) { prtMsg("Not positive length."); return false;}
if(rightShift<=0) rightShift=0;
else length+=rightShift;
for(int i=rightShift;i<length-1;i++) {
prtLine(i,length);
cout<<endl;
}
prtLine(length-1,length);
return true;
}
void Star::prtStars(int length,int rightShift) {
if(!prtUpTriagl(length,rightShift))
prtMsg("The rhombus's length must be > 0.");
else {
if(1==length) return;
cout<<endl;
prtDownTriagl(length-1,rightShift+1);
}
}
int main()
{
char star='*';
int len=1;
cout<<"请输入组成菱形的元素 : ";
cin>>star;
cout<<"请输入菱形的边长 : ";
cin>>len;
Star a(" ",' ',star);
a.prtStars(len);
cout<<endl;
system("pause"); //pause
return 0;
}