Skip to content

test commit for adding squareroot, sin #46

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

Open
wants to merge 1 commit into
base: master
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
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ <h1>
</td>
<td><button value="%">%</button></td>
</tr>
<tr>
<td><button value="√">√</button></td>
<td><button value="sin">sin</button></td>
<td><button value="cos">cos</button></td>
<td><button value="tan">tan</button></td>
</tr>
<tr>
<td><button value="7">7</button></td>
<td><button value="8">8</button></td>
Expand Down
66 changes: 48 additions & 18 deletions scripts/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,38 @@ let isSign = true;
for (item of buttons) {
item.addEventListener("click", (e) => {
buttonText = e.target.innerText;
if (buttonText == "X" && !isSign) {

// 根号功能
if (buttonText == "√" && !isSign) {
if (flag == 1) {
flag = 0;
}
screenValue = Math.sqrt(eval(screen.value));
screen.value = screenValue;
}

// sin, cos, tan 功能
else if (buttonText == "sin" || buttonText == "cos" || buttonText == "tan") {
if (flag == 1) {
flag = 0;
}
let angle = eval(screen.value);
let result;

if (buttonText == "sin") {
result = Math.sin(toRadians(angle));
} else if (buttonText == "cos") {
result = Math.cos(toRadians(angle));
} else if (buttonText == "tan") {
result = Math.tan(toRadians(angle));
}

screenValue = result;
screen.value = screenValue;
}

// 其他按钮处理
else if (buttonText == "X" && !isSign) {
if (flag == 1) {
flag = 0;
}
Expand All @@ -42,14 +73,13 @@ for (item of buttons) {
} else {
screen.classList.remove("negative");
}
} else if(buttonText=="(" || buttonText==")") {
if(flag==1){
flag =0;
} else if (buttonText == "(" || buttonText == ")") {
if (flag == 1) {
flag = 0;
}
screenValue+=buttonText;
screen.value=screenValue;
}
else if (isNumber(buttonText)) {
screenValue += buttonText;
screen.value = screenValue;
} else if (isNumber(buttonText)) {
if (flag == 1) {
screenValue = buttonText;
flag = 0;
Expand Down Expand Up @@ -85,27 +115,27 @@ window.onerror = function () {
console.clear();
};

// ... (same code as before)

function checkForBracketMulti() {
// ... (same code as before)

// Check for potential multiple operations after brackets and eval expression.
if (eval(screenValue) !== undefined) {
if (!Number.isInteger(eval(screenValue))){
screen.value = eval(screenValue).toFixed(2);
}
else {
if (!Number.isInteger(eval(screenValue))) {
screen.value = eval(screenValue).toFixed(2);
} else {
screen.value = eval(screenValue);
}

lastScreenValue = screenValue;
screenValue = screen.value;
if (parseFloat(screen.value) < 0) {
screen.classList.add("negative");
} else {
screen.classList.remove("negative");
}
// ... (same code as before)
}
flag = 1;
}

// 将角度转换为弧度
function toRadians(degrees) {
return degrees * (Math.PI / 180);
}