PDA

View Full Version : Need help w/ calculator



ShadowIce
08-26-2009, 01:38 PM
Hi all. I'm going back to college. I have decided to create a calculator for this purpose and this purpose alone.

I'm having a few problems..

No matter WHAT u click, as long as theres a # in there, the signs "*, /, +, or -" do NOT add more than ONE "*, /, +, or -", as long as theres a # before and / or after the sign. what i mean is u can do 3+3+3, but u cant do +3+3+3 or 3+++3 or +3+3+3+ or +++3..

this code ALMOST works. except its backwards.

instead of deleting any operators before the # and any double operators or 2 different operators both before or after #, it deletes the 1st 2 characters.. :S

I'm having a bit of a problem w/ code.




<html>
<head>
<script type="text/javascript">
function trim1(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function twoConsecutiveChars(str) {
if(document.forms.CALCULATOR.ans.value != ""){
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) == str.charAt(i+1)) {
alert('no');
document.forms.CALCULATOR.ans.value = trim1(str.charAt(-3));
}
}
}
}
function calc( btn )
{
var num = parseInt(btn.value,13);
if (isNaN(num) )
{
if(document.forms.CALCULATOR.ans.value != twoConsecutiveChars(document.forms.CALCULATOR.ans.value)){
document.forms.CALCULATOR.ans.value += btn.value;
} else if(btn.value == "=" && twoConsecutiveChars(document.forms.CALCULATOR.ans.value)){
document.forms.CALCULATOR.ans.value = eval(document.forms.CALCULATOR.ans.value);
}
} else {
document.forms.CALCULATOR.ans.value += num;
}
}
</script>
<style type="text/css">
form#CALCULATOR input { width: 30px; }
form#CALCULATOR .ans1 { width: 92px; }
</style>
</head>
<body>
<form id="CALCULATOR">
<input type="text" class="ans1" name="ans" size="20"><br>
<script>
for(var x=0;x<=12;x++){
if(x<=9){
document.write('<input type="button" class="btns" value="'+x+'" onclick="calc(this);">');
}else if(x==10){
var x1="*";
}else if(x==11){
var x1="/";
}else if(x==12){
var x1="=";
}
if(x>9){
document.write('<input type="button" class="btns" value="'+x1+'" onclick="calc(this);">');
}
if(x==2 || x==5 || x==8 || x==11){
document.write('<br>');
}
}
</script>
</form>
</body>
</html>


I also need to detect if u hit more than 1 sign, and make it stop it.

like this:

1/*2 or */1+2 or 2*/ or */1+2*/ or */1+-2*/

vwphillips
08-26-2009, 04:15 PM
<html>
<head>
<script type="text/javascript">
function trim1(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function twoConsecutiveChars(str) {
if(document.forms.CALCULATOR.ans.value != ""){
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) == str.charAt(i+1)) {
alert('no');
document.forms.CALCULATOR.ans.value = trim1(str.charAt(-3));
}
}
}
}
function calc( btn )
{
var frm=btn.form;
var num = btn.value;
if(btn.value == "="){
var result=eval(document.forms.CALCULATOR.ans.value);
if (isFinite(result)) frm.ans.value = result;
}
else {
frm.ans.value += num;
}
num=frm.ans.value;
if (/\D/.test(num.charAt(0))) frm.ans.value ='' ;
var lgth=num.length;
if (num.charAt(lgth-1)&&num.charAt(lgth-2)&&/\D/.test(num.charAt(lgth-1))&&/\D/.test(num.charAt(lgth-2))){
frm.ans.value=num.substring(0,lgth-1)
}
}
</script>
<style type="text/css">
form#CALCULATOR input { width: 30px; }
form#CALCULATOR .ans1 { width: 92px; }
</style>
</head>
<body>
<form id="CALCULATOR">
<input type="text" class="ans1" name="ans" size="20"><br>
<script>
for(var x=0;x<=12;x++){
if(x<=9){
document.write('<input type="button" class="btns" value="'+x+'" onclick="calc(this);">');
}else if(x==10){
var x1="*";
}else if(x==11){
var x1="/";
}else if(x==12){
var x1="=";
}
if(x>9){
document.write('<input type="button" class="btns" value="'+x1+'" onclick="calc(this);">');
}
if(x==2 || x==5 || x==8 || x==11){
document.write('<br>');
}
}
</script>
</form>
</body>
</html>

ShadowIce
08-26-2009, 06:30 PM
Ok. your code SORT of works. I found 4 flaws. 1 is that when u press 0, no matter how much u press it, it should only display 1 "0". also, when i press a button, the "0" should change into the # pressed. also, when u press a # then a sign then hit equals, it still calculates.

the 2nd thing is when u do this:

"1/1/"

it adds the "/" on to the end.

it needs to be "1/1/1"

also, im having a bit of a problem getting modulus to work right.



<html>
<head>
<script type="text/javascript">
var maxfunctions = 15;
function trim1(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function twoConsecutiveChars(str) {
if(document.forms.CALCULATOR.ans.value != ""){
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) == str.charAt(i+1)) {
alert('no');
}
}
}
}
function calc( btn )
{
var frm=btn.form;
var result;

var num = btn.value;
if(btn.value == "="){
if(num == trim1("Mod") && btn.onClick){
alert('mod');
result=eval(document.forms.CALCULATOR.ans.value % document.forms.CALCULATOR.ans.value);
}else{
result=eval(document.forms.CALCULATOR.ans.value);
}
if (isFinite(result)){ frm.ans.value = result;}
}
else {
frm.ans.value += num;
}
num=frm.ans.value;
if (/\D/.test(num.charAt(0))) frm.ans.value ='' ;
var lgth=num.length;
if (num.charAt(lgth-1)&&num.charAt(lgth-2)&&/\D/.test(num.charAt(lgth-1))&&/\D/.test(num.charAt(lgth-2))){
frm.ans.value=num.substring(0,lgth-1)
}
}
</script>
<style type="text/css">
form#CALCULATOR .mod { width: 32px; }
form#CALCULATOR input { width: 30px; }
form#CALCULATOR .ans1 { width: 92px; }
</style>
</head>
<body>
<form id="CALCULATOR">
<input type="text" value="0" class="ans1" name="ans" size="20"><br>
<script>
for(var x=0;x<=maxfunctions;x++){
if(x<=9){
document.write('<input type="button" class="btns" value="'+x+'" onclick="calc(this);">');
}else if(x==10){
var x1="*";
}else if(x==11){
var x1="/";
}else if(x==12){
var x1="=";
}else if(x==13){
var x1=".";
}else if(x==14){
var x1="+/-";
}else if(x==15){
var x1="Mod";
}
if(x>9 && x<=14){
document.write('<input type="button" class="btns" value="'+x1+'" onclick="calc(this);">');
}else if(x==15){
document.write('<input type="button" class="mod" value="'+x1+'" onclick="calc(this);">');
}
if(x==2 || x==5 || x==8 || x==11 || x==14){
document.write('<br>');
}
}
</script>
</form>
</body>
</html>


ANY help is GREATLY appreciated! :)