From 99ee21ceb6186e456992660d5eed3d457fb29c77 Mon Sep 17 00:00:00 2001 From: Kim Tae Sung Date: Tue, 22 Oct 2024 00:03:14 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B3=BC=EC=A0=9C1=EC=A3=BC=EC=B0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 35 +++++++++++++++++- src/Sum_stringNum.js | 10 ++++++ src/check_custom_marks.js | 20 +++++++++++ src/custom_markIs.js | 9 +++++ src/input.js | 21 +++++++++++ src/using_mark_and_search_num.js | 62 ++++++++++++++++++++++++++++++++ 6 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 src/Sum_stringNum.js create mode 100644 src/check_custom_marks.js create mode 100644 src/custom_markIs.js create mode 100644 src/input.js create mode 100644 src/using_mark_and_search_num.js diff --git a/src/App.js b/src/App.js index 091aa0a5d..b24759411 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,38 @@ +import { Console, MissionUtils } from "@woowacourse/mission-utils"; +import { check_custom_mark } from "/Users/kimttang/javascript-calculator-7/src/check_custom_marks.js" +import { custom_mark } from "./custom_markIs.js"; +import { search_num } from "./using_mark_and_search_num.js"; +import { Sum_string_num } from "./Sum_stringNum.js"; + class App { - async run() {} + async run() { + // input 받기 끝 + let userInput = '4;5;6'; + let customMk = ''; + let last_indexOfCusMark; + let Str_num_array = []; + let sum = 0; + + if(check_custom_mark(userInput) !== false){ // 문자안에 커스텀 마크가 있는 경우 + customMk = custom_mark(userInput, check_custom_mark(userInput)); + Str_num_array = search_num(userInput, check_custom_mark(userInput), customMk); + sum = Sum_string_num(Str_num_array); + Console.print(sum); + + } + else{ + Str_num_array = search_num(userInput); + sum = Sum_string_num(Str_num_array); + Console.print(sum); + } + + } } + + + + + + export default App; diff --git a/src/Sum_stringNum.js b/src/Sum_stringNum.js new file mode 100644 index 000000000..0c5bac50a --- /dev/null +++ b/src/Sum_stringNum.js @@ -0,0 +1,10 @@ +export function Sum_string_num(Str_num_array){ + + let sum = 0; + + for(let value of Str_num_array){ + sum += parseInt(value); + } + + return sum; +} \ No newline at end of file diff --git a/src/check_custom_marks.js b/src/check_custom_marks.js new file mode 100644 index 000000000..bc0cce927 --- /dev/null +++ b/src/check_custom_marks.js @@ -0,0 +1,20 @@ +export function check_custom_mark(user_input){ + const length_userInput = user_input.length; + let index_oflastmark; + + if(user_input[0] === '/' && user_input[1] === '/'){ + for(let i = 2; i < length_userInput; i++){ + if(user_input[2] === '\n'){ + throw new Error("Error") + + } + if(user_input[i] === '\n'){ + index_oflastmark = i; + } + } + return index_oflastmark; // 첫번째 두번쨰 문자가 //면 \n만 몇번째에 있는지 알면 그사이가 전부 커스텀 구분자인지 알수있음. + } + else{ + return false; + } +} \ No newline at end of file diff --git a/src/custom_markIs.js b/src/custom_markIs.js new file mode 100644 index 000000000..0f7b412bf --- /dev/null +++ b/src/custom_markIs.js @@ -0,0 +1,9 @@ +export function custom_mark (user_input, index_oflastmark){ + let customMark = ''; + + for(let i = 2; i < index_oflastmark; i++){ + customMark += user_input[i]; + } + + return customMark; +} \ No newline at end of file diff --git a/src/input.js b/src/input.js new file mode 100644 index 000000000..5feda31df --- /dev/null +++ b/src/input.js @@ -0,0 +1,21 @@ +import { Console, MissionUtils } from "@woowacourse/mission-utils"; + +let savedUsername; // 전역 변수 선언 + +export async function getUsername() { + try{ + const input = await Console.readLineAsync('숫자만 입력해주세요:'); + if (isNaN(input) || input.trim() === '') { + throw new Error('유효한 숫자가 아닙니다.'); + } + savedUsername = input; // 전역 변수에 저장 + console.log('입력된 숫자:', savedUsername); + return savedUsername; + + } catch (error) { + console.error('오류 발생:', error.message); + } +} + + + diff --git a/src/using_mark_and_search_num.js b/src/using_mark_and_search_num.js new file mode 100644 index 000000000..1783ae54b --- /dev/null +++ b/src/using_mark_and_search_num.js @@ -0,0 +1,62 @@ +export function search_num(user_input, customMark_lastIndex = 0, custom_mark = 0){ + + let j = 0; + let generalMk = ':,'; + let num_Oneline = ''; + let search_num_array = []; + + // ///{}\n 3{}555{}65{}22 i = 5부터 시작 7 { 9 16 17 3번째 {} + if(user_input[0] === '/' && user_input[1] === '/'){ + + for(let i = customMark_lastIndex + 1; i < user_input.length; i++){ + + if(user_input[i] === custom_mark[0]){ + let count_custom_mark = 0; + for(j = i ; j < i + custom_mark.length ; j++){ + if(user_input[j] !== custom_mark[count_custom_mark]){ + throw new Error("중간에 구분자를 잘못 입력하셨습니다."); + } + else{ + count_custom_mark += 1; // 2 + } + + } + i = j; // i = 9 + search_num_array.push((num_Oneline).toString()); + num_Oneline = ''; + } + if(user_input[i] !== custom_mark[0]){ + if(isNaN(user_input[i])) + throw new Error("중간에 구분자를 잘못 입력하셨습니다."); + } + + num_Oneline += user_input[i]; + if(i == user_input.length-1 ) + search_num_array.push((num_Oneline).toString()); + + + } + } + + else{ + + for(let i = 0 ; i < user_input.length; i++){ + + if(user_input[i] === generalMk[0] || user_input[i] === generalMk[1]){ + search_num_array.push((num_Oneline).toString()); + num_Oneline = ''; + + } + + + + num_Oneline += user_input[i]; + if(i == (user_input.length - 1)) + search_num_array.push((num_Oneline).toString()); + + } + } + + + return search_num_array; +} \ No newline at end of file