-
Notifications
You must be signed in to change notification settings - Fork 3
[tracy] react-calculator 구현 완료입니다. #5
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
base: tracy
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| { | ||
| "name": "react_calculator", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@testing-library/jest-dom": "^4.2.4", | ||
| "@testing-library/react": "^9.4.0", | ||
| "@testing-library/user-event": "^7.2.1", | ||
| "react": "^16.12.0", | ||
| "react-dom": "^16.12.0", | ||
| "react-scripts": "3.3.0" | ||
| }, | ||
| "scripts": { | ||
| "start": "react-scripts start", | ||
| "build": "react-scripts build", | ||
| "test": "react-scripts test", | ||
| "eject": "react-scripts eject" | ||
| }, | ||
| "eslintConfig": { | ||
| "extends": "react-app" | ||
| }, | ||
| "browserslist": { | ||
| "production": [ | ||
| ">0.2%", | ||
| "not dead", | ||
| "not op_mini all" | ||
| ], | ||
| "development": [ | ||
| "last 1 chrome version", | ||
| "last 1 firefox version", | ||
| "last 1 safari version" | ||
| ] | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <meta name="theme-color" content="#000000" /> | ||
| <meta | ||
| name="description" | ||
| content="Web site created using create-react-app" | ||
| /> | ||
| <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> | ||
| <!-- | ||
| manifest.json provides metadata used when your web app is installed on a | ||
| user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ | ||
| --> | ||
| <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> | ||
| <!-- | ||
| Notice the use of %PUBLIC_URL% in the tags above. | ||
| It will be replaced with the URL of the `public` folder during the build. | ||
| Only files inside the `public` folder can be referenced from the HTML. | ||
|
|
||
| Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will | ||
| work correctly both with client-side routing and a non-root public URL. | ||
| Learn how to configure a non-root public URL by running `npm run build`. | ||
| --> | ||
| <title>React App</title> | ||
| </head> | ||
| <body> | ||
| <noscript>You need to enable JavaScript to run this app.</noscript> | ||
| <div id="root"></div> | ||
| <!-- | ||
| This HTML file is a template. | ||
| If you open it directly in the browser, you will see an empty page. | ||
|
|
||
| You can add webfonts, meta tags, or analytics to this file. | ||
| The build step will place the bundled scripts into the <body> tag. | ||
|
|
||
| To begin the development, run `npm start` or `yarn start`. | ||
| To create a production bundle, use `npm run build` or `yarn build`. | ||
| --> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "short_name": "React App", | ||
| "name": "Create React App Sample", | ||
| "icons": [ | ||
| { | ||
| "src": "favicon.ico", | ||
| "sizes": "64x64 32x32 24x24 16x16", | ||
| "type": "image/x-icon" | ||
| }, | ||
| { | ||
| "src": "logo192.png", | ||
| "type": "image/png", | ||
| "sizes": "192x192" | ||
| }, | ||
| { | ||
| "src": "logo512.png", | ||
| "type": "image/png", | ||
| "sizes": "512x512" | ||
| } | ||
| ], | ||
| "start_url": ".", | ||
| "display": "standalone", | ||
| "theme_color": "#000000", | ||
| "background_color": "#ffffff" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # https://www.robotstxt.org/robotstxt.html | ||
| User-agent: * |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import clearAll from './clearAll' | ||
| import inputNumber from './inputNumber' | ||
| import decimalPoint from './decimalPoint' | ||
| import inputResult from './inputResult' | ||
| import inputContinuity from './inputContinuity' | ||
|
|
||
| function calculate(state, button) { | ||
| if (button === "C") { | ||
| return clearAll() | ||
| } | ||
|
|
||
| if ("0123456789".indexOf(button) !== -1) { | ||
| return inputNumber(state,button) | ||
| } | ||
|
|
||
| if (button === ".") { | ||
| return decimalPoint(state.nextValue) | ||
| } | ||
|
|
||
| if (button === "=") { | ||
| return inputResult(state) | ||
| } | ||
|
|
||
| if (state.operatorValue) { | ||
| return inputContinuity(state,button) | ||
| } | ||
|
|
||
| if (!state.nextValue) { | ||
| return { operatorValue: button }; | ||
| } | ||
|
|
||
| return { | ||
| displayValue: state.nextValue, | ||
| nextValue: null, | ||
| operatorValue: button, | ||
| }; | ||
| } | ||
|
|
||
| export default calculate; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| function clearAll(){ | ||
| return { | ||
| displayValue: "0", | ||
| nextValue: null, | ||
| operatorValue: null, | ||
| }; | ||
| } | ||
|
|
||
| export default clearAll |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| function decimalPoint(next){ | ||
| if (next) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if(next)가 의미하는게 명확하지 않습니다. |
||
| if (next.includes(".")) { | ||
| return {}; | ||
| } | ||
| return { nextValue: next + "." }; | ||
| } | ||
| return { nextValue: "0." }; | ||
|
Comment on lines
+2
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 들여쓰기 간격 맞춰주세요! |
||
| } | ||
|
|
||
| export default decimalPoint | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| function doOperation(state) { | ||
| const operator = state.operatorValue | ||
| const preOperand = parseFloat(state.displayValue || "0") | ||
| const backOperand = parseFloat(state.nextValue || (operator === "/" || operator === '*' ? "1": "0")) | ||
|
Comment on lines
+3
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (operator === "+") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여유가 된다면 string연산자와 대응하는 함수를 객체로 묶어서 개발해 보는걸 추천합니다. |
||
| return (preOperand+backOperand).toString(); | ||
| } | ||
| if (operator === "-") { | ||
| return (preOperand-backOperand).toString(); | ||
| } | ||
| if (operator === "*") { | ||
| return (preOperand*backOperand).toString(); | ||
| } | ||
| if (operator === "/") { | ||
| if (backOperand == "0") { | ||
| return "NaN"; | ||
| } | ||
| if (backOperand != "0"){ | ||
| return (preOperand/backOperand).toString(); | ||
| } | ||
| } | ||
| console.log('error'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이번 요구사항에 console 사용은 없습니다! 지워주세요. |
||
| } | ||
|
|
||
| export default doOperation; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import doOperation from './doOperation' | ||
|
|
||
| function inputContinuity(state,button){ | ||
| return { | ||
| displayValue: doOperation(state), | ||
| nextValue: null, | ||
| operatorValue: button, | ||
| }; | ||
| } | ||
| export default inputContinuity |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| function inputNumber(state,button){ | ||
| const next = state.nextValue | ||
| const operator = state.operatorValue | ||
|
|
||
| if (button === "0" && next === "0") { | ||
| return {}; | ||
| } | ||
|
|
||
| if (operator) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indent가 맞지 않네요 |
||
| if (next) { | ||
| return { nextValue: next + button }; | ||
| } | ||
| return { nextValue: button }; | ||
| } | ||
|
|
||
| if (next) { | ||
| const nextValue = next === "0" ? button : next + button; | ||
| return { | ||
| nextValue: nextValue, | ||
| displayValue: null, | ||
| }; | ||
| } | ||
| return { | ||
| nextValue: button, | ||
| displayValue: null, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| export default inputNumber | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import doOperation from './doOperation' | ||
|
|
||
| function inputResult(state){ | ||
| if (state.nextValue && state.operatorValue) { | ||
| return { | ||
| displayValue: doOperation(state), | ||
| nextValue: null, | ||
| operatorValue: null, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export default inputResult |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import Calculator from './layout/reactCalculator'; | ||
|
|
||
| ReactDOM.render(<Calculator />, document.getElementById('root')); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import React, {Component, Fragment } from 'react'; | ||
|
|
||
| class buttonLayout extends Component{ | ||
| render(){ | ||
| return( | ||
| <Fragment> | ||
| <button name="C" onClick={event => this.props.click(event.target.name)}>C</button> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 버튼도 모두 React Component로 만들어보는 걸 추천합니다. |
||
| <div> | ||
| <button name="7" onClick={event => this.props.click(event.target.name)}>7</button> | ||
| <button name="8" onClick={event => this.props.click(event.target.name)}>8</button> | ||
| <button name="9" onClick={event => this.props.click(event.target.name)}>9</button> | ||
| <button name="/" onClick={event => this.props.click(event.target.name)}>/</button> | ||
| </div> | ||
| <div> | ||
| <button name="4" onClick={event => this.props.click(event.target.name)}>4</button> | ||
| <button name="5" onClick={event => this.props.click(event.target.name)}>5</button> | ||
| <button name="6" onClick={event => this.props.click(event.target.name)}>6</button> | ||
| <button name="*" onClick={event => this.props.click(event.target.name)}>*</button> | ||
| </div> | ||
| <div> | ||
| <button name="1" onClick={event => this.props.click(event.target.name)}>1</button> | ||
| <button name="2" onClick={event => this.props.click(event.target.name)}>2</button> | ||
| <button name="3" onClick={event => this.props.click(event.target.name)}>3</button> | ||
| <button name="-" onClick={event => this.props.click(event.target.name)}>-</button> | ||
| </div> | ||
| <div> | ||
| <button name="0" onClick={event => this.props.click(event.target.name)}>0</button> | ||
| <button name="." onClick={event => this.props.click(event.target.name)}>.</button> | ||
| <button name="=" onClick={event => this.props.click(event.target.name)}>=</button> | ||
| <button name="+" onClick={event => this.props.click(event.target.name)}>+</button> | ||
| </div> | ||
| </Fragment> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default buttonLayout; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import React, {Component } from 'react'; | ||
|
|
||
| class displayLayout extends Component{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 클래스명은 대문자로 시작합니다. |
||
| render(){ | ||
| const displayStyle = {textAlign:"right"} | ||
| return( | ||
| <input type="text" readOnly style={displayStyle} value={this.props.value}></input> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default displayLayout; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import React, {Component } from 'react'; | ||
| import calculate from "../function/calculate" | ||
| import DisplayLayout from "./displayLayout" | ||
| import ButtonLayout from "./buttonLayout" | ||
|
|
||
| class reactCalculator extends Component{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 클래스명은 대문자로 시작합니다 |
||
| constructor(){ | ||
| super() | ||
| this.state = { | ||
| nextValue: null, | ||
| operatorValue: null, | ||
| displayValue: null | ||
| } | ||
| } | ||
|
|
||
| buttonClick = (buttonName) => { | ||
| this.setState(calculate(this.state, buttonName)) | ||
| } | ||
|
|
||
| inputKeyboard = (event) => { | ||
| let {key} = event | ||
| const operations = ['/','*','+','-','.','='] | ||
|
|
||
| if (key === 'Enter') | ||
| key = '=' | ||
|
|
||
| if ((/\d/).test(key)) { | ||
| event.preventDefault() | ||
| this.buttonClick(key) | ||
| } | ||
| if (operations.indexOf(key)!= -1) { | ||
| event.preventDefault() | ||
| this.buttonClick(key) | ||
| } | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| document.addEventListener('keydown', this.inputKeyboard) | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| document.removeEventListener('keydown', this.inputKeyboard) | ||
| } | ||
|
|
||
| render(){ | ||
| return( | ||
| <div> | ||
| <DisplayLayout value = {this.state.nextValue || this.state.displayValue}></DisplayLayout> | ||
| <ButtonLayout click = {this.buttonClick}></ButtonLayout> | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default reactCalculator; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
함수명은 Naming Convention에 의거하여 동사를 사용해야 합니다.