Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[문자열덧셈계산기] 김태성 미션 제출합니다. #600

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions src/Sum_stringNum.js
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions src/check_custom_marks.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions src/custom_markIs.js
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions src/input.js
Original file line number Diff line number Diff line change
@@ -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);
}
}



62 changes: 62 additions & 0 deletions src/using_mark_and_search_num.js
Original file line number Diff line number Diff line change
@@ -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;
}