nodejs输出图形
2016-08-08 本文已影响1290人
一直玩编程
function Shape() {
this.printStr = "";
return new Shape.prototype.init();
}
Shape.prototype = {
//换行
init: function() {
return this;
},
n: function() {
this.printStr += "\n";
},
//空格
space: function() {
this.printStr += " ";
},
/*
第行输出
type 类型 实心 solid 空心 hollow
sp 开始的空格数
mp 字符之间的空格数
str 字符
num 次数
*/
printChar: function(type, sp, mp, str, num) {
this.clean();
switch (type) {
case 'solid':
for (var i = 0; i < sp; i++) {
this.space();
};
for (var i = 0; i < num; i++) {
this.printStr += str;
for (var j = 0; j < mp; j++) {
this.space();
}
}
break;
case 'hollow':
for (var i = 0; i < sp; i++) {
this.space();
};
this.printStr += str;
for (var i = 0; i < num - 1; i++) {
this.space();
for (var j = 0; j < mp; j++) {
this.space();
}
};
this.printStr = this.deleteLastChar(this.printStr)
this.printStr += str;
break;
default:
}
console.log(this.printStr);
// this.clean();
// switch (type) {
// case 'solid':
// for (var i = 0; i < num; i++) {
// this.printStr += str;
// }
// break;
// case 'hollow':
// this.printStr += str;
// num === 2 ? num = 2 : num;
// for (var i = 0; i < num - 2; i++) {
// this.space();
// }
// this.printStr += str;
// break;
// default:
// }
// console.log(this.printStr);
},
//清空当前printStr;
clean: function() {
this.printStr = "";
},
deleteLastChar: function(str) {
return str.substring(0, (str.length - 1));
},
/*
矩形
type 类型 实心 solid 空心 hollow
sp 开始的空格数
mp 字符之间的空格数
str 字符
num 次数
line 行数
*/
rectangle: function(type, sp, mp, str, num, line) {
switch (type) {
case 'solid':
for (var i = 0; i < line; i++) {
this.printChar(type, sp, mp, str, num);
this.n();
};
break;
case 'hollow':
this.printChar('solid', sp, mp, str, num);
this.n();
for (var i = 0; i < line - 2; i++) {
this.printChar(type, sp, mp, str, num);
this.n();
};
this.printChar('solid', sp, mp, str, num);
this.n();
break;
default:
}
},
/*
三角形
type 类型 实心 solid 空心 hollow
sp 开始的空格数
mp 字符之间的空格数
str 字符
num 次数
line 行数
tolerance 公差 必须是大于0的偶数
*/
triangle: function(type, sp, mp, str, line, tolerance) {
if (tolerance % 2) {
console.log("公差必须是大于0的偶数");
return;
}
switch (type) {
case 'solid':
for (var i = 1, j = sp, k = line; k > 0; i += tolerance, j -= (tolerance / 2), k--) {
this.printChar("solid", j, mp, str, i);
}
break;
case 'hollow':
for (var i = 1, j = sp, k = line; k > 0; i += tolerance, j -= (tolerance / 2), k--) {
if (k === 1) {
this.printChar("solid", j, mp, str, i);
} else {
this.printChar("hollow", j, mp, str, i);
}
}
break;
default:
}
},
/*
梯形
type 类型 实心 solid 空心 hollow
sp 开始的空格数
mp 字符之间的空格数
str 字符
num 次数
line 行数
tolerance 公差 必须是大于0的偶数
*/
trapezoid: function(type, sn, sp, mp, str, line, tolerance) {
if (tolerance % 2) {
console.log("公差必须是大于0的偶数");
return;
}
switch (type) {
case 'solid':
for (var i = sn, j = sp, k = line; k > 0; i += tolerance, j -= (tolerance / 2), k--) {
this.printChar("solid", j, mp, str, i);
}
break;
case 'hollow':
for (var i = sn, j = sp, k = line; k > 0; i += tolerance, j -= (tolerance / 2), k--) {
if (k === 1 || k === line) {
this.printChar("solid", j, mp, str, i);
} else {
this.printChar("hollow", j, mp, str, i);
}
}
break;
default:
}
}
}
Shape.prototype.init.prototype = Shape.prototype;
global.wShape = Shape();
//矩形
// var shape = new Shape();
// shape.rectangle('solid', 0, 5, '*', 5, 5);
// console.log("\n");
// var shapee = new Shape();
// shapee.rectangle('hollow', 0, 5, '#', 5, 5);
//三角形
// var shape = new Shape();
// shape.triangle('solid', 30, 0, "*", 10, 2);
// console.log("\n");
// var shapee = new Shape();
// shapee.triangle('hollow', 30, 0, "#", 10, 2);
//梯形
// var shape = new Shape();
// shape.trapezoid('solid', 5, 30, 0, "*", 10, 2);
// console.log("\n");
// var shapee = new Shape();
// shapee.trapezoid('hollow',5 ,30, 0, "#", 10, 2);
//矩形
wShape.rectangle('solid', 30, 5, '*', 5, 5);
console.log("\n");
wShape.rectangle('hollow', 30, 5, '#', 5, 5);
console.log("\n");
//三角形
wShape.triangle('solid', 30, 0, "*", 10, 2);
wShape.triangle('hollow', 30, 0, "#", 10, 2);
console.log("\n");
//梯形
wShape.trapezoid('solid', 5, 30, 0, "*", 10, 2);
wShape.trapezoid('hollow',5 ,30, 0, "#", 10, 2);
效果

