반응형
아래 코드를 unit_converter.html 로 저장하여 브라우저에서 불러 사용하면됨
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>공학용 단위 변환기</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #2c3e50, #3498db);
color: white;
padding: 30px;
text-align: center;
}
.header h1 {
font-size: 2.5em;
margin-bottom: 10px;
}
.header p {
font-size: 1.1em;
opacity: 0.9;
}
.tabs {
display: flex;
background: #f8f9fa;
border-bottom: 1px solid #dee2e6;
}
.tab {
flex: 1;
padding: 15px;
text-align: center;
cursor: pointer;
background: #f8f9fa;
border: none;
font-size: 1em;
transition: all 0.3s ease;
}
.tab:hover {
background: #e9ecef;
}
.tab.active {
background: #007bff;
color: white;
}
.tab-content {
display: none;
padding: 30px;
}
.tab-content.active {
display: block;
}
.converter-group {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
.input-group {
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 8px;
font-weight: 600;
color: #495057;
}
.input-group input, .input-group select {
padding: 12px;
border: 2px solid #dee2e6;
border-radius: 8px;
font-size: 1em;
transition: border-color 0.3s ease;
}
.input-group input:focus, .input-group select:focus {
outline: none;
border-color: #007bff;
}
.result {
background: #f8f9fa;
padding: 20px;
border-radius: 8px;
margin-top: 20px;
text-align: center;
}
.result h3 {
color: #495057;
margin-bottom: 10px;
}
.result-value {
font-size: 1.5em;
font-weight: bold;
color: #007bff;
}
.formula {
background: #e3f2fd;
padding: 15px;
border-radius: 8px;
margin-top: 15px;
font-family: 'Courier New', monospace;
font-size: 0.9em;
color: #1976d2;
}
@media (max-width: 768px) {
.converter-group {
grid-template-columns: 1fr;
}
.tabs {
flex-direction: column;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>공학용 단위 변환기</h1>
<p>길이, 무게, 온도, 면적, 부피 단위를 쉽게 변환하세요</p>
</div>
<div class="tabs">
<button class="tab active" onclick="showTab('length')">길이</button>
<button class="tab" onclick="showTab('weight')">무게</button>
<button class="tab" onclick="showTab('temperature')">온도</button>
<button class="tab" onclick="showTab('area')">면적</button>
<button class="tab" onclick="showTab('volume')">부피</button>
</div>
<!-- 길이 변환 -->
<div id="length" class="tab-content active">
<div class="converter-group">
<div class="input-group">
<label for="length-from">변환할 값</label>
<input type="number" id="length-from" placeholder="값을 입력하세요" oninput="convertLength()">
<select id="length-from-unit" onchange="convertLength()">
<option value="meter">미터 (m)</option>
<option value="kilometer">킬로미터 (km)</option>
<option value="centimeter">센티미터 (cm)</option>
<option value="millimeter">밀리미터 (mm)</option>
<option value="mile">마일 (mi)</option>
<option value="yard">야드 (yd)</option>
<option value="foot">피트 (ft)</option>
<option value="inch">인치 (in)</option>
</select>
</div>
<div class="input-group">
<label for="length-to">변환 결과</label>
<input type="number" id="length-to" readonly>
<select id="length-to-unit" onchange="convertLength()">
<option value="meter">미터 (m)</option>
<option value="kilometer">킬로미터 (km)</option>
<option value="centimeter">센티미터 (cm)</option>
<option value="millimeter">밀리미터 (mm)</option>
<option value="mile">마일 (mi)</option>
<option value="yard">야드 (yd)</option>
<option value="foot">피트 (ft)</option>
<option value="inch">인치 (in)</option>
</select>
</div>
</div>
<div class="result">
<h3>변환 결과</h3>
<div class="result-value" id="length-result">0</div>
<div class="formula" id="length-formula">변환 공식이 여기에 표시됩니다</div>
</div>
</div>
<!-- 무게 변환 -->
<div id="weight" class="tab-content">
<div class="converter-group">
<div class="input-group">
<label for="weight-from">변환할 값</label>
<input type="number" id="weight-from" placeholder="값을 입력하세요" oninput="convertWeight()">
<select id="weight-from-unit" onchange="convertWeight()">
<option value="kilogram">킬로그램 (kg)</option>
<option value="gram">그램 (g)</option>
<option value="milligram">밀리그램 (mg)</option>
<option value="ton">톤 (t)</option>
<option value="pound">파운드 (lb)</option>
<option value="ounce">온스 (oz)</option>
</select>
</div>
<div class="input-group">
<label for="weight-to">변환 결과</label>
<input type="number" id="weight-to" readonly>
<select id="weight-to-unit" onchange="convertWeight()">
<option value="kilogram">킬로그램 (kg)</option>
<option value="gram">그램 (g)</option>
<option value="milligram">밀리그램 (mg)</option>
<option value="ton">톤 (t)</option>
<option value="pound">파운드 (lb)</option>
<option value="ounce">온스 (oz)</option>
</select>
</div>
</div>
<div class="result">
<h3>변환 결과</h3>
<div class="result-value" id="weight-result">0</div>
<div class="formula" id="weight-formula">변환 공식이 여기에 표시됩니다</div>
</div>
</div>
<!-- 온도 변환 -->
<div id="temperature" class="tab-content">
<div class="converter-group">
<div class="input-group">
<label for="temp-from">변환할 값</label>
<input type="number" id="temp-from" placeholder="값을 입력하세요" oninput="convertTemperature()">
<select id="temp-from-unit" onchange="convertTemperature()">
<option value="celsius">섭씨 (°C)</option>
<option value="fahrenheit">화씨 (°F)</option>
<option value="kelvin">켈빈 (K)</option>
</select>
</div>
<div class="input-group">
<label for="temp-to">변환 결과</label>
<input type="number" id="temp-to" readonly>
<select id="temp-to-unit" onchange="convertTemperature()">
<option value="celsius">섭씨 (°C)</option>
<option value="fahrenheit">화씨 (°F)</option>
<option value="kelvin">켈빈 (K)</option>
</select>
</div>
</div>
<div class="result">
<h3>변환 결과</h3>
<div class="result-value" id="temp-result">0</div>
<div class="formula" id="temp-formula">변환 공식이 여기에 표시됩니다</div>
</div>
</div>
<!-- 면적 변환 -->
<div id="area" class="tab-content">
<div class="converter-group">
<div class="input-group">
<label for="area-from">변환할 값</label>
<input type="number" id="area-from" placeholder="값을 입력하세요" oninput="convertArea()">
<select id="area-from-unit" onchange="convertArea()">
<option value="squaremeter">제곱미터 (m²)</option>
<option value="squarekilometer">제곱킬로미터 (km²)</option>
<option value="squarecentimeter">제곱센티미터 (cm²)</option>
<option value="hectare">헥타르 (ha)</option>
<option value="acre">에이커 (ac)</option>
<option value="squarefoot">제곱피트 (ft²)</option>
<option value="squareyard">제곱야드 (yd²)</option>
<option value="pyeong">평 (坪)</option>
</select>
</div>
<div class="input-group">
<label for="area-to">변환 결과</label>
<input type="number" id="area-to" readonly>
<select id="area-to-unit" onchange="convertArea()">
<option value="squaremeter">제곱미터 (m²)</option>
<option value="squarekilometer">제곱킬로미터 (km²)</option>
<option value="squarecentimeter">제곱센티미터 (cm²)</option>
<option value="hectare">헥타르 (ha)</option>
<option value="acre">에이커 (ac)</option>
<option value="squarefoot">제곱피트 (ft²)</option>
<option value="squareyard">제곱야드 (yd²)</option>
<option value="pyeong">평 (坪)</option>
</select>
</div>
</div>
<div class="result">
<h3>변환 결과</h3>
<div class="result-value" id="area-result">0</div>
<div class="formula" id="area-formula">변환 공식이 여기에 표시됩니다</div>
</div>
</div>
<!-- 부피 변환 -->
<div id="volume" class="tab-content">
<div class="converter-group">
<div class="input-group">
<label for="volume-from">변환할 값</label>
<input type="number" id="volume-from" placeholder="값을 입력하세요" oninput="convertVolume()">
<select id="volume-from-unit" onchange="convertVolume()">
<option value="cubicmeter">세제곱미터 (m³)</option>
<option value="liter">리터 (L)</option>
<option value="milliliter">밀리리터 (mL)</option>
<option value="gallon">갤런 (gal)</option>
<option value="quart">쿼트 (qt)</option>
<option value="pint">파인트 (pt)</option>
<option value="cup">컵 (c)</option>
<option value="fluidounce">액량 온스 (fl oz)</option>
</select>
</div>
<div class="input-group">
<label for="volume-to">변환 결과</label>
<input type="number" id="volume-to" readonly>
<select id="volume-to-unit" onchange="convertVolume()">
<option value="cubicmeter">세제곱미터 (m³)</option>
<option value="liter">리터 (L)</option>
<option value="milliliter">밀리리터 (mL)</option>
<option value="gallon">갤런 (gal)</option>
<option value="quart">쿼트 (qt)</option>
<option value="pint">파인트 (pt)</option>
<option value="cup">컵 (c)</option>
<option value="fluidounce">액량 온스 (fl oz)</option>
</select>
</div>
</div>
<div class="result">
<h3>변환 결과</h3>
<div class="result-value" id="volume-result">0</div>
<div class="formula" id="volume-formula">변환 공식이 여기에 표시됩니다</div>
</div>
</div>
</div>
<script>
// 단위 변환 상수 (기준: 미터법 단위)
const lengthUnits = {
meter: 1,
kilometer: 0.001,
centimeter: 100,
millimeter: 1000,
mile: 0.000621371,
yard: 1.09361,
foot: 3.28084,
inch: 39.3701
};
const weightUnits = {
kilogram: 1,
gram: 1000,
milligram: 1000000,
ton: 0.001,
pound: 2.20462,
ounce: 35.274
};
const areaUnits = {
squaremeter: 1,
squarekilometer: 0.000001,
squarecentimeter: 10000,
hectare: 0.0001,
acre: 0.000247105,
squarefoot: 10.7639,
squareyard: 1.19599,
pyeong: 0.3025
};
const volumeUnits = {
cubicmeter: 1,
liter: 1000,
milliliter: 1000000,
gallon: 264.172,
quart: 1056.69,
pint: 2113.38,
cup: 4226.75,
fluidounce: 33814
};
// 탭 전환 함수
function showTab(tabName) {
// 모든 탭 내용 숨기기
const tabContents = document.querySelectorAll('.tab-content');
tabContents.forEach(tab => tab.classList.remove('active'));
// 모든 탭 버튼 비활성화
const tabs = document.querySelectorAll('.tab');
tabs.forEach(tab => tab.classList.remove('active'));
// 선택한 탭 내용 보이기
document.getElementById(tabName).classList.add('active');
// 선택한 탭 버튼 활성화
event.target.classList.add('active');
}
// 길이 변환 함수
function convertLength() {
const fromValue = parseFloat(document.getElementById('length-from').value) || 0;
const fromUnit = document.getElementById('length-from-unit').value;
const toUnit = document.getElementById('length-to-unit').value;
// 미터로 변환 후 목표 단위로 변환
const meters = fromValue / lengthUnits[fromUnit];
const result = meters * lengthUnits[toUnit];
document.getElementById('length-to').value = result.toFixed(6);
document.getElementById('length-result').textContent = `${fromValue} ${getUnitName(fromUnit)} = ${result.toFixed(6)} ${getUnitName(toUnit)}`;
document.getElementById('length-formula').textContent = `${fromValue} × (${lengthUnits[toUnit]} / ${lengthUnits[fromUnit]}) = ${result.toFixed(6)}`;
}
// 무게 변환 함수
function convertWeight() {
const fromValue = parseFloat(document.getElementById('weight-from').value) || 0;
const fromUnit = document.getElementById('weight-from-unit').value;
const toUnit = document.getElementById('weight-to-unit').value;
// 킬로그램으로 변환 후 목표 단위로 변환
const kilograms = fromValue / weightUnits[fromUnit];
const result = kilograms * weightUnits[toUnit];
document.getElementById('weight-to').value = result.toFixed(6);
document.getElementById('weight-result').textContent = `${fromValue} ${getUnitName(fromUnit)} = ${result.toFixed(6)} ${getUnitName(toUnit)}`;
document.getElementById('weight-formula').textContent = `${fromValue} × (${weightUnits[toUnit]} / ${weightUnits[fromUnit]}) = ${result.toFixed(6)}`;
}
// 온도 변환 함수
function convertTemperature() {
const fromValue = parseFloat(document.getElementById('temp-from').value) || 0;
const fromUnit = document.getElementById('temp-from-unit').value;
const toUnit = document.getElementById('temp-to-unit').value;
let celsius, result;
let formula = '';
// 섭씨로 변환
switch(fromUnit) {
case 'celsius':
celsius = fromValue;
break;
case 'fahrenheit':
celsius = (fromValue - 32) * 5/9;
break;
case 'kelvin':
celsius = fromValue - 273.15;
break;
}
// 섭씨에서 목표 단위로 변환
switch(toUnit) {
case 'celsius':
result = celsius;
formula = `${fromValue}°${fromUnit.charAt(0).toUpperCase()} = ${result.toFixed(2)}°C`;
break;
case 'fahrenheit':
result = celsius * 9/5 + 32;
formula = `${fromValue}°${fromUnit.charAt(0).toUpperCase()} × (9/5) + 32 = ${result.toFixed(2)}°F`;
break;
case 'kelvin':
result = celsius + 273.15;
formula = `${fromValue}°${fromUnit.charAt(0).toUpperCase()} + 273.15 = ${result.toFixed(2)}K`;
break;
}
document.getElementById('temp-to').value = result.toFixed(2);
document.getElementById('temp-result').textContent = `${fromValue}°${fromUnit.charAt(0).toUpperCase()} = ${result.toFixed(2)}°${toUnit.charAt(0).toUpperCase()}`;
document.getElementById('temp-formula').textContent = formula;
}
// 면적 변환 함수
function convertArea() {
const fromValue = parseFloat(document.getElementById('area-from').value) || 0;
const fromUnit = document.getElementById('area-from-unit').value;
const toUnit = document.getElementById('area-to-unit').value;
// 제곱미터로 변환 후 목표 단위로 변환
const squareMeters = fromValue / areaUnits[fromUnit];
const result = squareMeters * areaUnits[toUnit];
document.getElementById('area-to').value = result.toFixed(6);
document.getElementById('area-result').textContent = `${fromValue} ${getUnitName(fromUnit)} = ${result.toFixed(6)} ${getUnitName(toUnit)}`;
document.getElementById('area-formula').textContent = `${fromValue} × (${areaUnits[toUnit]} / ${areaUnits[fromUnit]}) = ${result.toFixed(6)}`;
}
// 부피 변환 함수
function convertVolume() {
const fromValue = parseFloat(document.getElementById('volume-from').value) || 0;
const fromUnit = document.getElementById('volume-from-unit').value;
const toUnit = document.getElementById('volume-to-unit').value;
// 세제곱미터로 변환 후 목표 단위로 변환
const cubicMeters = fromValue / volumeUnits[fromUnit];
const result = cubicMeters * volumeUnits[toUnit];
document.getElementById('volume-to').value = result.toFixed(6);
document.getElementById('volume-result').textContent = `${fromValue} ${getUnitName(fromUnit)} = ${result.toFixed(6)} ${getUnitName(toUnit)}`;
document.getElementById('volume-formula').textContent = `${fromValue} × (${volumeUnits[toUnit]} / ${volumeUnits[fromUnit]}) = ${result.toFixed(6)}`;
}
// 단위 이름 가져오기 함수
function getUnitName(unitKey) {
const unitNames = {
meter: 'm',
kilometer: 'km',
centimeter: 'cm',
millimeter: 'mm',
mile: 'mi',
yard: 'yd',
foot: 'ft',
inch: 'in',
kilogram: 'kg',
gram: 'g',
milligram: 'mg',
ton: 't',
pound: 'lb',
ounce: 'oz',
celsius: '°C',
fahrenheit: '°F',
kelvin: 'K',
squaremeter: 'm²',
squarekilometer: 'km²',
squarecentimeter: 'cm²',
hectare: 'ha',
acre: 'ac',
squarefoot: 'ft²',
squareyard: 'yd²',
pyeong: '평',
cubicmeter: 'm³',
liter: 'L',
milliliter: 'mL',
gallon: 'gal',
quart: 'qt',
pint: 'pt',
cup: 'c',
fluidounce: 'fl oz'
};
return unitNames[unitKey] || unitKey;
}
</script>
</body>
</html>반응형
'IT, 프로그램, SW개발 > 알고리즘' 카테고리의 다른 글
| 다각형 근사화 Douglas-Peucker 알고리즘 원리 (더글러스 피커 알고리 (0) | 2024.05.07 |
|---|---|
| DFS, BFS 스터디용 (0) | 2021.02.05 |
| 트라이(Trie)알고리즘 (0) | 2020.04.07 |