write code calculater app in ArkTS
- User
write code calculater app in ArkTS
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write code calculater app in ArkTS
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
ArkTS (ArkUI TypeScript) is a framework used for building applications on the OpenHarmony platform. Below is a simple code snippet for a basic calculator application written in ArkTS. This app will perform basic arithmetic operations such as addition, subtraction, multiplication, and division.
Make sure you have set up your development environment for ArkTS and OpenHarmony before proceeding with this code.
// app.ts
import Log from '@system.log';
interface CalculatorState {
currentInput: string;
previousInput: string;
operator: string | null;
}
export default {
data: {
currentInput: '0',
previousInput: '',
operator: null,
} as CalculatorState,
// Number input handler
onNumberClick(num: string): void {
if (this.currentInput === '0') {
this.currentInput = num;
} else {
this.currentInput += num;
}
},
// Operator input handler
onOperatorClick(op: string): void {
if (this.currentInput === '') return;
if (this.previousInput !== '') {
this.performCalculation();
}
this.operator = op;
this.previousInput = this.currentInput;
this.currentInput = '';
},
// Perform calculation
performCalculation(): void {
let result: number;
const prev = parseFloat(this.previousInput);
const current = parseFloat(this.currentInput);
if (isNaN(prev) || isNaN(current)) return;
switch (this.operator) {
case '+':
result = prev + current;
break;
case '-':
result = prev - current;
break;
case '*':
result = prev * current;
break;
case '/':
result = current !== 0 ? prev / current : NaN;
break;
default:
return;
}
this.currentInput = result.toString();
this.previousInput = '';
this.operator = null;
},
// Clear inputs
clear(): void {
this.currentInput = '0';
this.previousInput = '';
this.operator = null;
},
};
<!-- main.hml -->
<div class="container">
<text @click="clear" class="clear-button">C</text>
<text>{{ currentInput }}</text>
<div class="buttons">
<text class="button" @click="onNumberClick('1')">1</text>
<text class="button" @click="onNumberClick('2')">2</text>
<text class="button" @click="onNumberClick('3')">3</text>
<text class="button" @click="onOperatorClick('+')">+</text>
<text class="button" @click="onNumberClick('4')">4</text>
<text class="button" @click="onNumberClick('5')">5</text>
<text class="button" @click="onNumberClick('6')">6</text>
<text class="button" @click="onOperatorClick('-')">-</text>
<text class="button" @click="onNumberClick('7')">7</text>
<text class="button" @click="onNumberClick('8')">8</text>
<text class="button" @click="onNumberClick('9')">9</text>
<text class="button" @click="onOperatorClick('*')">*</text>
<text class="button" @click="onNumberClick('0')">0</text>
<text @click="performCalculation" class="equals-button">=</text>
<text class="button" @click="onOperatorClick('/')">/</text>
</div>
</div>
/* style.css */
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.button, .clear-button, .equals-button {
padding: 20px;
text-align: center;
font-size: 24px;
background-color: #ffcc00;
color: #fff;
border-radius: 5px;
cursor: pointer;
user-select: none;
}
.clear-button {
background-color: #f44336;
}
.equals-button {
grid-column: span 2;
background-color: #4caf50;
}
The above code is a straightforward implementation of a basic calculator, focusing primarily on functionality rather than complex UI design or error handling. Adjust styles and add additional functionality as needed to meet your requirements.