Fraction calculation in Perl

This Perl script is able to add,subtract,divide and multiply fractional numbers:

use strict;
#test data:
my @B1 = _read();
my @B2 = _read();
#test addition
my @BC = _add(@B1,@B2);
print "+:",_print(@BC);
# test substract
   @BC = _sub(@BC,@B2);
print "-:",_print(@BC);
#simplify the result
   @BC = simplify(@BC);

#test multiplication
   @BC = _mul(@B1,@B2);
print "*:",_print(@BC);
#test division
   @BC = _div(@BC,@B2);
print "/:",_print(@BC);
#simplify the result
   @BC = simplify(@BC);

sub _add {
    my $gn = $_[1]*$_[3];
    return(($_[0]*($gn/$_[1]))+($_[2]*($gn/$_[3])),$gn);
}

sub _sub {
    my $gn = $_[1]*$_[3];
    return(($_[0]*($gn/$_[1]))-($_[2]*($gn/$_[3])),$gn);
}

sub _mul {
    return($_[0]*$_[2],$_[1]*$_[3]);
}

sub _div {
    return($_[0]*$_[3],$_[1]*$_[2]);
}

sub _print {
    my @D = &simplify;
    my $c = 0;
    while(1){
        if($D[0]>=$D[1]){
            $D[0] -= $D[1];
            ++$c;
        }else{
            last;    
        }
    }
    if($c){
        return "$c+$D[0]/$D[1](".&fractial2number.")\n"; 
    }else{
        return "$D[0]/$D[1](".&fractial2number.")\n"; 
    }
}

sub _read {
    print "fractal number:";
    my $in = <STDIN>;
    if($in =~ /\//o){
       $in =~ /^(.+)\/(.+)$/o;
        return($1,$2);
    }else{
       return(number2fractial($in)); 
    }
}

sub number2fractial {
    $_[0] =~ /^(.+)\.(.+)$/o;
    my $d = $1;
    my $c = $2;
    my $u = $2;
       $u =~ s/./0/g;
       $u = "1".$u;
    return($c+($d*$u),"$u");
}

sub fractial2number {
    return($_[0]/$_[1]);
}

sub simplify {
    for($_ = $_[1];$_>=2;--$_){
        return($_[0]/$_,$_[1]/$_) if($_[0]%$_==0 && $_[1]%$_==0);
    }
    return($_[0],$_[1]);
}

This is the output from 1/2 and 1.25, at first add both then subtract from the result, multiply and then divide from the result:

fractal number:1/2  
fractal number:1.25 
+:1+3/4(1.75)       
-:1/2(0.5)          
*:5/8(0.625)        
/:1/2(0.5)

For a better result take a look at Perl high precision.

C# Calculator

This is an old calculator program I wrote in C# :

using System;
using System.Threading;
namespace Rechner
{
	class MainClass
	{
		public static double vari1=1,vari2=1;
		public static void Main (string[] args)
		{

			do{
			Einführung();
			string einlese = Convert.ToString(Console.ReadLine());
			Double ergebniss=0;
			switch(einlese)
			{
			case "+" : ergebniss = plus();break;
			case "-" : ergebniss = minus();break;
			case "*" : ergebniss = Mal();break;
			case "/" : ergebniss = Div();break;
			case "var1" : var1();break;
			case "var2" : var2();break;
			}
			Console.WriteLine("Die Lösung ist nach Adam Rieße "+ergebniss);
				Console.WriteLine("");
			Console.WriteLine ("Enter zum fortsetzen,var1 oder var2 zum speichern in Variablen !");
			try{
					string speichern = Console.ReadLine();
					if(speichern == "var1")
					{vari1 = ergebniss;Console.WriteLine("Variable 1 ist "+ergebniss);Console.ReadLine();} 
					if(speichern == "var2")
					{vari2 = ergebniss;Console.WriteLine("Variable 2 ist "+ergebniss);Console.ReadLine();} 
			}catch{}
			}while(true);
		}
		static Double plus()
		{
			Double summe=0;
			do{
				Console.Clear();
			Console.WriteLine("= zum beenden eingeben !");
			Console.WriteLine("Ergebnis = "+summe);
								Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
			Console.Write("Zahl (+):");
				
			string a = Console.ReadLine(); 
           if (a == "=")
           {
               break;
           }
				           if (a == "var1")
           {
               summe = summe + vari1;
           }
				           if (a == "var2")
           {
               summe = summe + vari2;
           }
           else
           {
               try
               {
                   summe = summe + Convert.ToDouble(a);
               }
               catch { }
           }		
			}while(true);
			
			return (summe);
			
		}
		static Double minus()
		{
			Double summe=0;
			do{Console.Clear();
			Console.WriteLine("= zum beenden eingeben !");
			Console.WriteLine("Ergebnis = "+summe);
								Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
			Console.Write("Zahl (-):");
				
			string a = Console.ReadLine(); 
           if (a == "=")
           {
               break;
           }
				           if (a == "var1")
           {
               summe = summe - vari1;
           }
				           if (a == "var2")
           {
               summe = summe - vari2;
           }
           else
           {
               try
               {
                   summe = summe - Convert.ToDouble(a);
               }
               catch { }
           }		
			}while(true);
			
			return(summe);
			
		}
		static void var1()
		{			Console.Clear();	Console.Write ("Variable 1 ändern:");
			try{
			string einlese = Convert.ToString(Console.ReadLine());
			vari1 = Convert.ToDouble(einlese);
			Console.WriteLine ("Variable 1 geändert:"+vari1);
								if(vari2 == 0){vari2 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
				if(vari1 == 0){vari1 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
			}catch{Console.WriteLine ("ERROR !");}
		}
		static void var2()
		{			Console.Clear();	Console.Write ("Variable 2 ändern:");
			try{
			string einlese = Convert.ToString(Console.ReadLine());
				vari2 = Convert.ToDouble( einlese);
			Console.WriteLine ("Variable 2 geändert:"+vari2);
								if(vari2 == 0){vari2 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
				if(vari1 == 0){vari1 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
			}catch{Console.WriteLine ("ERROR !");}
		}
		static Double Mal()
		{Double summe = 0;
			try{
						Console.Write("Zahl :");
			 summe=Convert.ToDouble(Console.ReadLine());
			}catch{}
			do{
				Console.Clear();
			Console.WriteLine("= zum beenden eingeben !");
			Console.WriteLine("Ergebnis = "+summe);
								Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
			Console.Write("Zahl (*):");
				
			string a = Console.ReadLine(); 
           if (a == "=")
           {
               break;
           }
				           if (a == "var1")
           {
               summe = summe * vari1;
           }
				           if (a == "var2")
           {
               summe = summe * vari2;
           }
           else
           {
               try
               {
                   summe = summe * Convert.ToDouble(a);
               }
               catch { }
           }		
			}while(true);
			
			return(summe);
			
		}
		static Double Div()
		{Double summe = 0;string summe1 = "";
			try{
			do{	
			Console.WriteLine("");
			Console.Write("Zahl :");
			summe1 =Convert.ToString( Console.ReadLine());
			}while(Convert.ToDouble(summe1) <= 0);
				summe = Convert.ToDouble(summe1);
			}catch{}
			if(summe <= 0)
			{summe = 1;}
			do{
				Console.Clear();
			Console.WriteLine("= zum beenden eingeben !");
			Console.WriteLine("Ergebnis = "+summe);
								Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
			Console.Write("Zahl (/):");
				
			string a = Console.ReadLine(); 
           if (a == "=")
           {
               break;
           }
				           if (a == "var1")
           {
               summe = summe / vari1;
           }
				           if (a == "var2")
           {
               summe = summe / vari2;
           }
           else
           {
               try
               {
						if(Convert.ToDouble(a) != 0)
						{
                   summe = summe / Convert.ToDouble(a);
						}else{Console.WriteLine("nicht möglich !");
						System.Threading.Thread.Sleep(2400);
						}
               }
               catch { }
           }		
			}while(true);
			
			return(summe);
			
		}
		static void Einführung()
		{Console.Clear();
			Console.WriteLine ("+ Plus rechnen");
			Console.WriteLine ("- Minus rechnen");
				Console.WriteLine ("* Mal rechnen");
				Console.WriteLine ("/ Dividiert rechnen");
				Console.WriteLine ("");
				Console.WriteLine ("var1 variable 1 ändern ");
				Console.WriteLine ("var2 variable 2 ändern ");
				Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
				Console.WriteLine ("");
				Console.Write (">");
				if(vari2 == 0){vari2 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
				if(vari1 == 0){vari1 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
		}

		
	}

}

Feel free to copy and share.

Calculator in C++

This is my calculator in C++ with in-line assembler:

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main( void )
{
	int nZahl1 = 0, nZahl2 = 0, nVar3 = 0;string strOp="";
do{
	
   	cout << "calculate (+,-,*,/) :" ;
   	cin >> strOp;

   	cout << "number  1: " ;
   	cin >> nZahl1;
	
   	cout << "number  2: " ;
   	cin >> nZahl2;

	if("+"==strOp){
	asm("ADD %%edx, %%eax \n":"=a"(nZahl1):"d"(nZahl2),"a"(nZahl1));
	cout <<" = "<<(nZahl1)<<endl;
	}else if("-"==strOp){
	asm("SUB %%edx, %%eax \n":"=a"(nZahl1):"d"(nZahl2),"a"(nZahl1));
	cout <<" = "<<(nZahl1)<<endl;
	}else if("*"==strOp){
	asm("MOV %%edx, %%eax \n":"=a"(nVar3):"d"(nZahl1),"a"(nVar3));
	for(int i = 2 ;i<=nZahl2;i++)
	asm("ADD %%edx, %%eax \n":"=a"(nZahl1):"d"(nVar3),"a"(nZahl1));
	cout <<" = "<<(nZahl1)<<endl;
	}else if("/"==strOp){
	asm("MOV %%edx, %%eax \n XOR  %%edx,%%edx \n":"=a"(nVar3),"=d"(nZahl1):"d"(nZahl1),"a"(nVar3));
	for(int i = nZahl2 ;nVar3>=i;nZahl1++)
	asm("SUB %%edx, %%eax \n":"=a"(nVar3):"d"(nZahl2),"a"(nVar3));
	cout <<" = "<<(nZahl1)<<",";
	for(int i = 0 ;i<=10;i++){
	nVar3*=10;nZahl1=0;
	for(int i = nZahl2 ;nVar3>=i;nZahl1++)
	asm("SUB %%edx, %%eax \n":"=a"(nVar3):"d"(nZahl2),"a"(nVar3));
	cout <<(nZahl1);
	}
	cout << endl;
	}

}while(1);

return(0);
}

Compile the code:

g++ -o main test.cpp
./main

output:

calculate (+,-,*,/) :+ 
number  1: 4           
number  2: 5           
 = 9                   
calculate (+,-,*,/) :- 
number  1: 9           
number  2:             
4                      
 = 5                   
calculate (+,-,*,/) :^C
sh-4.3# main           
calculate (+,-,*,/) :+ 
number  1: 4           
number  2: 5           
 = 9                   
calculate (+,-,*,/) :- 
number  1: 9           
number  2: 4           
 = 5                   
calculate (+,-,*,/) :* 
number  1: 4           
number  2: 5           
 = 20                  
calculate (+,-,*,/) :/ 
number  1: 20          
number  2: 4           
 = 5,00000000000

Calculator JavaScript

This is a little example about a JavaScript Calculator, he could calculate some simple things another solution is to use eval.





JavaScript code:
var Wert = 0;
var OP = ":";
function CalcPlus ()
{
if(document.Eingabe.TB.value != ""&&OP!=":"){CalcErg();}
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
default:
Wert = document.Eingabe.TB.value;
document.Eingabe.TB.value="";
}
OP="+";
}
function CalcMinus ()
{
if(document.Eingabe.TB.value != ""&&OP!=":"){CalcErg();}
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
default:
Wert = document.Eingabe.TB.value;
document.Eingabe.TB.value="";
}
OP="-";
}
function CalcMal ()
{
if(document.Eingabe.TB.value != ""&&OP!=":"){CalcErg();}
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
default:
Wert = document.Eingabe.TB.value;
document.Eingabe.TB.value="";
}
OP="*";
}
function CalcDiv ()
{
if(document.Eingabe.TB.value != ""&&OP!=":"){CalcErg();}
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
default:
Wert = document.Eingabe.TB.value;
document.Eingabe.TB.value="";
}
OP="/";
}
function CalcErg ()
{
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;
break;
default:
document.Eingabe.TB.value=Wert;
}
OP=":";
Wert=document.Eingabe.TB.value;
}
function CalcClear ()
{
document.Eingabe.TB.value ="";OP=":";Wert=0;
}
  HTML code:
<form name="Eingabe">
<input type="text" name="TB">
<br>
<input type="button" onclick="javascript:CalcPlus()" value="+">
<input type="button" onclick="javascript:CalcMinus()" value="-">
<input type="button" onclick="javascript:CalcMal()" value="*">
<input type="button" onclick="javascript:CalcDiv()" value="/">
<input type="button" onclick="javascript:CalcErg()" value="=">
<br>
<input type="button" onclick="document.Eingabe.TB.value=Math.sqrt(document.Eingabe.TB.value)" value="sqrt">
<input type="button" onclick="document.Eingabe.TB.value=Math.sin(document.Eingabe.TB.value)" value="sin">
<input type="button" onclick="document.Eingabe.TB.value=Math.cos(document.Eingabe.TB.value)" value="cos">
<input type="button" onclick="document.Eingabe.TB.value=Math.tan(document.Eingabe.TB.value)" value="tan">
<br>
<input type="button" onclick="document.Eingabe.TB.value=1/document.Eingabe.TB.value" value="1/x">
<input type="button" onclick="document.Eingabe.TB.value=document.Eingabe.TB.value*1e3" value="x*1k">
<input type="button" onclick="document.Eingabe.TB.value=document.Eingabe.TB.value*1e-3" value="x*1m">
<input type="button" onclick="document.Eingabe.TB.value=Math.PI" value="Pi">
<br>
<input type="button" onclick="javascript:CalcClear()" value=" --< Clear >-- ">
<br>
</form>

JavaScript series resistor circuit calculator

This is a JavaScript calculator how calculate with the ohm's law the voltage, ampere and the resistance.Its only an example but feel free to copy change or reuse this script. Try it:










UG-U1 U2
UG-U2 U1
U1+U2 UG
RG-R1 R2
RG-R2 R1
R1+R2 RG
(UG*R1)/RG U1
(UG*R2)/RG U2
UG/RG IG
U1/R1 IG
U2/R2 IG
(R1*U2)/R2 U1
(R2*U1)/R1 U2
(R2*U1)/U2 R1
(R1*U2)/U1 R2
UG/IG RG
UG/RG IG
RG*IG UG
Code:
<script>
function Berechne()
{
if(document.Eg.R1.value != "" && document.Eg.R2.value != "")//RG
{document.Eg.RG.value = 1*document.Eg.R1.value + document.Eg.R2.value*1;
document.getElementById("F0").style.backgroundColor = "#00FF00";
document.getElementById("FF0").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F0").style.backgroundColor = "#FF0000";
document.getElementById("FF0").style.backgroundColor = "#FF0000";
}
if(document.Eg.RG.value != "" && document.Eg.R1.value != "")//R2
{document.Eg.R2.value = 1*document.Eg.RG.value - document.Eg.R1.value*1;
document.getElementById("F1").style.backgroundColor = "#00FF00";
document.getElementById("FF1").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F1").style.backgroundColor = "#FF0000";
document.getElementById("FF1").style.backgroundColor = "#FF0000";
}
if(document.Eg.RG.value != "" && document.Eg.R2.value != "")//R1
{document.Eg.R1.value = 1*document.Eg.RG.value - document.Eg.R2.value*1;
document.getElementById("F2").style.backgroundColor = "#00FF00";
document.getElementById("FF2").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F2").style.backgroundColor = "#FF0000";
document.getElementById("FF2").style.backgroundColor = "#FF0000";
}
if(document.Eg.U1.value != "" && document.Eg.U2.value != "")//UG
{document.Eg.UG.value = 1*document.Eg.U1.value + document.Eg.U2.value*1;
document.getElementById("F3").style.backgroundColor = "#00FF00";
document.getElementById("FF3").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F3").style.backgroundColor = "#FF0000";
document.getElementById("FF3").style.backgroundColor = "#FF0000";
}
if(document.Eg.UG.value != "" && document.Eg.U1.value != "")//U2
{document.Eg.U2.value = 1*document.Eg.UG.value - document.Eg.U1.value*1;
document.getElementById("F4").style.backgroundColor = "#00FF00";
document.getElementById("FF4").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F4").style.backgroundColor = "#FF0000";
document.getElementById("FF4").style.backgroundColor = "#FF0000";
}
if(document.Eg.UG.value != "" && document.Eg.U2.value != "")//U1
{document.Eg.U1.value = 1*document.Eg.UG.value - document.Eg.U2.value*1;
document.getElementById("F5").style.backgroundColor = "#00FF00";
document.getElementById("FF5").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F5").style.backgroundColor = "#FF0000";
document.getElementById("FF5").style.backgroundColor = "#FF0000";
}
//end
if(document.Eg.R1.value != "" && document.Eg.R2.value != ""&& document.Eg.UG.value != "")//U1
{document.Eg.U1.value = (1*document.Eg.UG.value * document.Eg.R1.value*1)/(document.Eg.RG.value);
document.getElementById("F6").style.backgroundColor = "#00FF00";
document.getElementById("FF6").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F6").style.backgroundColor = "#FF0000";
document.getElementById("FF6").style.backgroundColor = "#FF0000";
}
if(document.Eg.R1.value != "" && document.Eg.R2.value != ""&& document.Eg.UG.value != "")//U2
{document.Eg.U2.value = (1*document.Eg.UG.value * document.Eg.R2.value*1)/(document.Eg.RG.value);
document.getElementById("F7").style.backgroundColor = "#00FF00";
document.getElementById("FF7").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F7").style.backgroundColor = "#FF0000";
document.getElementById("FF7").style.backgroundColor = "#FF0000";
}
if(document.Eg.UG.value != "" && document.Eg.RG.value != "")//IG
{document.Eg.IG.value = 1*document.Eg.UG.value / document.Eg.RG.value*1;
document.getElementById("F8").style.backgroundColor = "#00FF00";
document.getElementById("FF8").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F8").style.backgroundColor = "#FF0000";
document.getElementById("FF8").style.backgroundColor = "#FF0000";
}
if(document.Eg.U1.value != "" && document.Eg.R1.value != "")//IG
{document.Eg.IG.value = 1*document.Eg.U1.value / document.Eg.R1.value*1;
document.getElementById("F9").style.backgroundColor = "#00FF00";
document.getElementById("FF9").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F9").style.backgroundColor = "#FF0000";
document.getElementById("FF9").style.backgroundColor = "#FF0000";
}
if(document.Eg.U2.value != "" && document.Eg.R2.value != "")//IG
{document.Eg.IG.value = 1*document.Eg.U2.value / document.Eg.R2.value*1;
document.getElementById("F10").style.backgroundColor = "#00FF00";
document.getElementById("FF10").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F10").style.backgroundColor = "#FF0000";
document.getElementById("FF10").style.backgroundColor = "#FF0000";
}
if(document.Eg.U1.value != "" && document.Eg.R1.value != ""&& document.Eg.R2.value != "")
{document.Eg.U2.value = (document.Eg.R2.value*document.Eg.U1.value)/document.Eg.R1.value;
document.getElementById("F11").style.backgroundColor = "#00FF00";
document.getElementById("FF11").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F11").style.backgroundColor = "#FF0000";
document.getElementById("FF11").style.backgroundColor = "#FF0000";
}
if(document.Eg.U2.value != "" && document.Eg.R1.value != ""&& document.Eg.R2.value != "")
{document.Eg.U1.value = (document.Eg.R1.value * document.Eg.U2.value)/document.Eg.R2.value;
document.getElementById("F12").style.backgroundColor = "#00FF00";
document.getElementById("FF12").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F12").style.backgroundColor = "#FF0000";
document.getElementById("FF12").style.backgroundColor = "#FF0000";
}
if(document.Eg.U1.value != "" && document.Eg.U2.value != ""&& document.Eg.R1.value != "")
{document.Eg.R2.value = (document.Eg.R1.value *document.Eg.U2.value)/document.Eg.U1.value;
document.getElementById("F14").style.backgroundColor = "#00FF00";
document.getElementById("FF14").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F14").style.backgroundColor = "#FF0000";
document.getElementById("FF14").style.backgroundColor = "#FF0000";
}
if(document.Eg.U1.value != "" && document.Eg.U2.value != ""&& document.Eg.R2.value != "")
{document.Eg.R1.value = (document.Eg.R2.value*document.Eg.U1.value)/document.Eg.U2.value;
document.getElementById("F15").style.backgroundColor = "#00FF00";
document.getElementById("FF15").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F15").style.backgroundColor = "#FF0000";
document.getElementById("FF15").style.backgroundColor = "#FF0000";
}
if(document.Eg.UG.value != "" && document.Eg.IG.value != "")
{document.Eg.RG.value=document.Eg.UG.value/document.Eg.IG.value;
document.getElementById("F16").style.backgroundColor = "#00FF00";
document.getElementById("FF16").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F16").style.backgroundColor = "#FF0000";
document.getElementById("FF16").style.backgroundColor = "#FF0000";
}
if(document.Eg.UG.value != "" && document.Eg.RG.value != "")
{document.Eg.IG.value=document.Eg.UG.value/document.Eg.RG.value;
document.getElementById("F17").style.backgroundColor = "#00FF00";
document.getElementById("FF17").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F17").style.backgroundColor = "#FF0000";
document.getElementById("FF17").style.backgroundColor = "#FF0000";
}
if(document.Eg.RG.value != "" && document.Eg.IG.value != "")
{document.Eg.UG.value=document.Eg.IG.value*document.Eg.RG.value;
document.getElementById("F18").style.backgroundColor = "#00FF00";
document.getElementById("FF18").style.backgroundColor = "#00FF00";
}else{
document.getElementById("F18").style.backgroundColor = "#FF0000";
document.getElementById("FF18").style.backgroundColor = "#FF0000";
}
}
</script>

<form name="Eg">
<br>
<label>R1 :</label>
<input type="text" name="R1">
<br>
<label>R2 :</label>
<input type="text" name="R2">
<br>
<label>RG :</label>
<input type="text" name="RG">
<br>
<label>U1 :</label>
<input type="text" name="U1">
<br>
<label>U2 :</label>
<input type="text" name="U2">
<br>
<label>UG :</label>
<input type="text" name="UG">
<br>
<label>IG :</label>
<input type="text" name="IG">
<br>
<input type="button" onclick="javascript:Berechne()" value="Berechne">
<br>
<br>
<table id="tom" width="20%" border="2">
<colgroup>
<col width="4*">
<col width="2*">
<col width="1*">
</colgroup>
<tbody>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F4">UG-U1</td>
<td id="FF4">U2</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F5">UG-U2</td>
<td id="FF5">U1</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F3">U1+U2</td>
<td id="FF3">UG</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F0">RG-R1</td>
<td id="FF0">R2</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F2">RG-R2</td>
<td id="FF2">R1</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F1">R1+R2</td>
<td id="FF1">RG</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F6">(UG*R1)/RG</td>
<td id="FF6">U1</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F7">(UG*R2)/RG</td>
<td id="FF7">U2</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F8">UG/RG</td>
<td id="FF8">IG</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F9">U1/R1</td>
<td id="FF9">IG</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F10">U2/R2</td>
<td id="FF10">IG</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F12">(R1*U2)/R2</td>
<td id="FF12">U1</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F11">(R2*U1)/R1</td>
<td id="FF11">U2</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F15">(R2*U1)/U2</td>
<td id="FF15">R1</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F14">(R1*U2)/U1</td>
<td id="FF14">R2</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F16">UG/IG</td>
<td id="FF16">RG</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F17">UG/RG</td>
<td id="FF17">IG</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F18">RG*IG</td>
<td id="FF18">UG</td>
</tr>
</tbody>
</table>
</form>

JavaScript parallel resistor circuit calculator

This is a JavaScript calculator how calculate with the ohm's law the voltage, ampere and the resistance.Its only an example but feel free to copy change or reuse this script. Try it:










RG=1/(1/R1+1/R2)
R1=-1/(1/R2-1/RG)
R2=-1/(1/R1-1/RG)
I1=(I2*R1)/R2
I2=(I1*R2)/R1
R1=(I1*R2)/I2
R2=(I2*R1)/I1
I2=(IG/(R1+R2))*R1
I1=(IG/(R1+R2))*R2
Code:
<script>
function Berechne()
{
if(document.Eg.I1.value != "" && document.Eg.I2.value != "")
{document.Eg.IG.value = 1*document.Eg.I1.value + document.Eg.I2.value*1;}
if(document.Eg.IG.value != "" && document.Eg.I1.value != "")
{document.Eg.I2.value = 1*document.Eg.IG.value - document.Eg.I1.value*1;}
if(document.Eg.IG.value != "" && document.Eg.I2.value != "")
{document.Eg.I1.value = 1*document.Eg.IG.value - document.Eg.I2.value*1;}
if(document.Eg.R1.value != "" && document.Eg.R2.value != "")
{document.Eg.RG.value = 1/(1/document.Eg.R1.value + 1/document.Eg.R2.value);
document.getElementById("F1").style.backgroundColor = "#00FF00";
}else{document.getElementById("F1").style.backgroundColor = "#FF0000";}
if(document.Eg.R2.value != "" && document.Eg.RG.value != "")
{document.Eg.R1.value = -1/(1/document.Eg.R2.value-1/document.Eg.RG.value);
document.getElementById("F2").style.backgroundColor = "#00FF00";
}else{document.getElementById("F2").style.backgroundColor = "#FF0000";}
if(document.Eg.R1.value != "" && document.Eg.RG.value != "")
{document.Eg.R2.value = -1/((1/document.Eg.R1.value)-(1/document.Eg.RG.value));
document.getElementById("F3").style.backgroundColor = "#00FF00";
}else{document.getElementById("F3").style.backgroundColor = "#FF0000";}
if(document.Eg.I2.value != "" && document.Eg.R1.value != ""&& document.Eg.R2.value != "")
{document.Eg.I1.value = (document.Eg.I2.value*document.Eg.R1.value)/document.Eg.R2.value;
document.getElementById("F4").style.backgroundColor = "#00FF00";
}else{document.getElementById("F4").style.backgroundColor = "#FF0000";}
if(document.Eg.I1.value != "" && document.Eg.R1.value != ""&& document.Eg.R2.value != "")
{document.Eg.I2.value = (document.Eg.I1.value*document.Eg.R2.value)/document.Eg.R1.value;
document.getElementById("F5").style.backgroundColor = "#00FF00";
}else{document.getElementById("F5").style.backgroundColor = "#FF0000";}
if(document.Eg.I1.value != "" && document.Eg.I2.value != ""&& document.Eg.R2.value != "")
{document.Eg.R1.value = (document.Eg.I1.value*document.Eg.R2.value)/document.Eg.I2.value;
document.getElementById("F6").style.backgroundColor = "#00FF00";
}else{document.getElementById("F6").style.backgroundColor = "#FF0000";}
if(document.Eg.I1.value != "" && document.Eg.I2.value != ""&& document.Eg.R1.value != "")
{document.Eg.R2.value = (document.Eg.I2.value*document.Eg.R1.value)/document.Eg.I1.value;
document.getElementById("F7").style.backgroundColor = "#00FF00";
}else{document.getElementById("F7").style.backgroundColor = "#FF0000";}
if(document.Eg.RG.value != "" && document.Eg.UG.value != "")
{document.Eg.IG.value = document.Eg.UG.value/document.Eg.RG.value;
}
if(document.Eg.IG.value != "" && document.Eg.R1.value != "" && document.Eg.R2.value != "")
{document.Eg.I2.value = (document.Eg.IG.value/(document.Eg.R1.value*1+document.Eg.R2.value*1))*document.Eg.R1.value;
document.getElementById("F8").style.backgroundColor = "#00FF00";
}else{document.getElementById("F8").style.backgroundColor = "#FF0000";}
if(document.Eg.IG.value != "" && document.Eg.R2.value != "" && document.Eg.R2.value != "")
{document.Eg.I1.value = (document.Eg.IG.value/(document.Eg.R1.value*1+document.Eg.R2.value*1))*document.Eg.R2.value;
document.getElementById("F9").style.backgroundColor = "#00FF00";
}else{document.getElementById("F9").style.backgroundColor = "#FF0000";}
}
</script>

<form name="Eg">
<br>
<label>R1 :</label>
<input type="text" name="R1">
<br>
<label>R2 :</label>
<input type="text" name="R2">
<br>
<label>RG :</label>
<input type="text" name="RG">
<br>
<label>I1 :</label>
<input type="text" name="I1">
<br>
<label>I2 :</label>
<input type="text" name="I2">
<br>
<label>IG :</label>
<input type="text" name="IG">
<br>
<label>UG :</label>
<input type="text" name="UG">
<br>
<input type="button" onclick="javascript:Berechne()" value="Berechne">
<br>
<br>
<table id="tom" width="20%" border="2">
<colgroup>
<col width="4*">
<col width="2*">
<col width="1*">
</colgroup>
<tbody>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F1">RG=1/(1/R1+1/R2)</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F2">R1=-1/(1/R2-1/RG)</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F3">R2=-1/(1/R1-1/RG)</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F4">I1=(I2*R1)/R2</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F5">I2=(I1*R2)/R1</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F6">R1=(I1*R2)/I2</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F7">R2=(I2*R1)/I1</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F8">I2=(IG/(R1+R2))*R1</td>
</tr>
<tr bgcolor="#E1E1E1" onmouseout="this.bgColor='#E1E1E1'" onmouseover="this.bgColor='#CCCCFF'">
<td id="F9">I1=(IG/(R1+R2))*R2</td>
</tr>
</tbody>
</table>
</form>

JavaScript Ohm’s law calculator

This is the Ohm's law calculator in Javascript:


Javscript:
function Berechne(){
//allgemein
if(document.Eg.R.value != "" && document.Eg.U.value != "")
{document.Eg.I.value = document.Eg.U.value / document.Eg.R.value;}
if(document.Eg.I.value != "" && document.Eg.U.value != "")
{document.Eg.R.value = document.Eg.U.value / document.Eg.I.value;}
if(document.Eg.I.value != "" && document.Eg.R.value != "")
{document.Eg.U.value = document.Eg.R.value * document.Eg.I.value;}
if(document.Eg.I.value != "" && document.Eg.U.value != "")
{document.Eg.P.value = document.Eg.U.value * document.Eg.I.value;}
//P-Formeln
if(document.Eg.U.value != "" && document.Eg.R.value != "")
{document.Eg.P.value = (document.Eg.U.value*document.Eg.U.value) / document.Eg.R.value;}
if(document.Eg.I.value != "" && document.Eg.R.value != "")
{document.Eg.P.value = (document.Eg.I.value*document.Eg.I.value) * document.Eg.R.value;}
//I-Formeln
if(document.Eg.P.value != "" && document.Eg.U.value != "")
{document.Eg.I.value = document.Eg.P.value / document.Eg.U.value;}
if(document.Eg.P.value != "" && document.Eg.R.value != "")
{document.Eg.I.value = Math.sqrt(document.Eg.P.value / document.Eg.R.value);}
//R-Formeln
if(document.Eg.P.value != "" && document.Eg.U.value != "")
{document.Eg.R.value = (document.Eg.U.value*document.Eg.U.value) / document.Eg.P.value;}
if(document.Eg.I.value != "" && document.Eg.P.value != "")
{document.Eg.R.value = document.Eg.P.value / (document.Eg.I.value*document.Eg.I.value);}
//U-Formeln
if(document.Eg.I.value != "" && document.Eg.P.value != "")
{document.Eg.U.value = document.Eg.P.value / document.Eg.I.value;}
if(document.Eg.P.value != "" && document.Eg.R.value != "")
{document.Eg.U.value = Math.sqrt(document.Eg.P.value * document.Eg.R.value);}
}
And the HTML:
<form name="Eg"><label>R (Ohm) :</label>
<input name="R" type="text" />
<label>U (V) :</label>
<input name="U" type="text" />
<label>I (A) :</label>
<input name="I" type="text" />
<label>P (W) :</label>
<br>
<input name="P" type="text" /><input type="button" value="Berechne" />

Perl percent calculation

If you have an array in Perl with values and want to check what the percentages of them you could use this little function:
#!/usr/bin/perl
use strict;
#example data
my @arr = (3,5,7,8,0,20,2,1,-1);
#test sub
print percentage(@arr);
# calculate percentage
sub percentage {
    my @arr = @_;
    my $out = "";
    @arr = sort { $b <=> $a } @arr;
    my $max_value=$arr[0];
    for my $i (0..$#arr){
        $out .= $arr[$i];
        $out .= " = ".sprintf("%.2f",(($arr[$i]/$max_value)*100))."%".$/;
    }
    return($out);
}
result looks like this:
20 = 100.00%
8 = 40.00%
7 = 35.00%
5 = 25.00%
3 = 15.00%
2 = 10.00%
1 = 5.00%
0 = 0.00%
-1 = -5.00%
  To make a table to compare all together:
#!/usr/bin/perl
use strict;
#example data
my @arr = (3,5,7,8,0,20,2,1,-1);
#test sub
print percentage(@arr);
# calculate percentage
sub percentage {
    my @arr = @_;
    my $space = 10;
    my $out = len_format("Percent",$space);
    @arr = sort { $b <=> $a } @arr;
    for my $x (0..$#arr){$out .= len_format($arr[$x],$space);}
    $out .= $/;
    for my $i (0..$#arr){
        $out .= len_format($arr[$i],$space);
        for my $x (0..$#arr){
            if($arr[$x] != 0 && $arr[$i] != 0){
                $out .= len_format(sprintf("%.2f",(($arr[$x]/$arr[$i])*100))."%",$space);
            }else{
                $out .= len_format("?%",$space);
            }
        }
        $out .= $/;
    }
    return($out);
}
#format strings
sub len_format{
    my $str = shift;
    my $len = shift;
    return((" "x($len-length($str))).$str);
}
For the format subs look at Perl format string to the same size. result looks like this:
   Percent        20         8         7         5         3         2         1         0        -1
        20   100.00%    40.00%    35.00%    25.00%    15.00%    10.00%     5.00%        ?%    -5.00%
         8   250.00%   100.00%    87.50%    62.50%    37.50%    25.00%    12.50%        ?%   -12.50%
         7   285.71%   114.29%   100.00%    71.43%    42.86%    28.57%    14.29%        ?%   -14.29%
         5   400.00%   160.00%   140.00%   100.00%    60.00%    40.00%    20.00%        ?%   -20.00%
         3   666.67%   266.67%   233.33%   166.67%   100.00%    66.67%    33.33%        ?%   -33.33%
         2  1000.00%   400.00%   350.00%   250.00%   150.00%   100.00%    50.00%        ?%   -50.00%
         1  2000.00%   800.00%   700.00%   500.00%   300.00%   200.00%   100.00%        ?%  -100.00%
         0        ?%        ?%        ?%        ?%        ?%        ?%        ?%        ?%        ?%
        -1 -2000.00%  -800.00%  -700.00%  -500.00%  -300.00%  -200.00%  -100.00%        ?%   100.00%
  If you want some colours see this:
use strict;
#example data
my @arr = (3,5,7,8,0,20,2,1,-1);
#test sub
print percentage(@arr);
# calculate percentage
sub percentage {
    my @arr = @_;
    my $space = 10;
    my $out = len_format("Percent",$space);
    @arr = sort { $b <=> $a } @arr;
    for my $x (0..$#arr){$out .= "e[33m".len_format($arr[$x],$space)."e[37m";}
    $out .= $/;
    for my $i (0..$#arr){
        $out .= "e[33m".len_format($arr[$i],$space)."e[37m";
        for my $x (0..$#arr){
            $out .= "e[36m" if($i%2 == 0);
            if($arr[$x] != 0 && $arr[$i] != 0){
                $out .= len_format(sprintf("%.2f",(($arr[$x]/$arr[$i])*100))."%",$space);
            }else{
                $out .= len_format("?%",$space);
            }
            $out .= "e[37m";
        }
        $out .= $/;
    }
    return($out);
}
#format strings
sub len_format{
    my $str = shift;
    my $len = shift;
    return((" "x($len-length($str))).$str);
}
And now we have the result with colours: Screenshot_percentageFor more infos about colours see at Perl Terminal Colours.   And now we use Perl Formater for Terminal and HTML to make this table:
Percent 20 8 7 5 3 2 1 0 -1
20 100.00% 40.00% 35.00% 25.00% 15.00% 10.00% 5.00% ?% -5.00%
8 250.00% 100.00% 87.50% 62.50% 37.50% 25.00% 12.50% ?% -12.50%
7 285.71% 114.29% 100.00% 71.43% 42.86% 28.57% 14.29% ?% -14.29%
5 400.00% 160.00% 140.00% 100.00% 60.00% 40.00% 20.00% ?% -20.00%
3 666.67% 266.67% 233.33% 166.67% 100.00% 66.67% 33.33% ?% -33.33%
2 1000.00% 400.00% 350.00% 250.00% 150.00% 100.00% 50.00% ?% -50.00%
1 2000.00% 800.00% 700.00% 500.00% 300.00% 200.00% 100.00% ?% -100.00%
0 ?% ?% ?% ?% ?% ?% ?% ?% ?%
-1 -2000.00% -800.00% -700.00% -500.00% -300.00% -200.00% -100.00% ?% 100.00%