diff --git a/areaCalculator.js b/areaCalculator.js new file mode 100644 index 0000000..39c8683 --- /dev/null +++ b/areaCalculator.js @@ -0,0 +1,55 @@ +const cal = require("./cal"); +const funcArr = []; +const areaArr = []; + +const getArea = function(shape, ...rest) { + let result = 0; + if (shape === "circle") { + if (rest.length === 1) { + result = cal.getCircleArea(rest[0]); + } else if (rest.length === 2) { + result = cal.getCircleSum(rest[0], rest[1]); + } + } else if (shape === "rect") { + result = cal.getSquareArea(rest[0], rest[1]); + } else if (shape === "trapezoid") { + result = cal.getTrapezoidArea(rest[0], rest[1], rest[2]); + } else if (shape === "cylinder") { + result = cal.getCircleArea(rest[0], rest[1]); + } else { + result = "잘못 입력하셨습니다"; + } + + if (result !== "잘못 입력하셨습니다") { + funcArr.push(shape); + areaArr.push(result); + } + return result; +} + +const printExecutionSequence = function() { + let result1 = "계산수행순서 : "; + for (shape of funcArr) { + result1 = result1 + shape + ", "; + } + console.log(result1); + console.log(); + + let result2 = ""; + for (let i = 0; i < funcArr.length; i++) { + console.log(funcArr[i] + " : " + areaArr[i]); + } +} + +function main() { + getArea('circle', 2); + getArea('circle', 1, 3); + getArea('rect', 2, 3); + getArea('trapezoid', 5, 8, 10); + getArea('cylinder', 5, 10); + getArea('triple', 4, 3); + + printExecutionSequence(); +} + +main(); diff --git a/cal.js b/cal.js new file mode 100644 index 0000000..c8170ab --- /dev/null +++ b/cal.js @@ -0,0 +1,51 @@ +const getCircleArea = radius => { + checkError(radius); + return Math.PI * radius * radius; +} + +const getSquareArea = (width, height) => { + checkError(width, height); + return width * height; +} + +const getTrapezoidArea = (top, bottom, height) => { + checkError(top, bottom, height); + return ((top + bottom) * height) / 2 +} + +const getCylinderArea = (radius, height) => { + checkError(radius, height); + return getCircleArea(radius) * height; +} + +const getCircleSum = (num1, num2) => { + let start, end; + if (num1 >= num2) { + start = num2; + end = num1; + } else { + start = num1; + end = num2; + } + + if (start === end) { + return Math.PI * end * end; + } else { + return Math.PI * end * end + getCircleSum(start, end - 1); + } +} + +const checkError = function(...args) { + for (let val of args) { + if (typeof val === "undefined") { + throw new Error("인자의 갯수가 부족합니다."); + } + if (typeof val !== "number") { + throw new Error("인자값이 숫자가 아닙니다."); + } + } +} + +module.exports = {getCircleArea, getSquareArea, + getTrapezoidArea, getCylinderArea, getCircleSum}; + \ No newline at end of file