Batch algebra

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Batch algebra

#1 Post by Rileyh » 04 Dec 2011 03:00

I have managed to create an arithmetic program that does simple add, take, multiply, divide and modulus, and yet I can not seem to come up with how to do algebraic equations such as "solving for a pronumeral (finding the value of a pronumeral in an equation) and solving equations.

For the sake of it I will post my program to see if you could add in an algebraic-solving section to it:

Code: Select all

REM This is incomplete, so don't be alarmed if you see a "goto" string that goes to a tag that does not exist. :) 
@echo off
setlocal enableextensions
setlocal enabledelayedexpansion
:menu
color 1e
title Calculator
cls
echo MENU:
echo 1. Simple Calculations
echo 2. Intermediate Calculations
echo 3. Algebra
echo 4. Angles
echo 5. About
echo 6. Help
echo 7. Exit
echo _______
set /p a=I CHOICE:
if /i _"%a%"==_"1" (goto :simple)
if /i _"%a%"==_"2" (goto :harder)
if /i _"%a%"==_"3" (goto :algebra)
if /i _"%a%"==_"4" (goto :angles)
if /i _"%a%"==_"5" (goto :about)
if /i _"%a%"==_"6" (goto :help)
if /i _"%a%"==_"7" (goto :eof)
goto :fault

:fault
echo You entered an illegal choice. Please enter correct choices when prompted.
timeout /t 2 /nobreak >nul
goto :menu

REM SIMPLE MATHEMATICS FROM HERE:
:simple
cls
echo Options:
echo 1. Addition
echo 2. Subtraction
echo 3. Multiplication
echo 4. Division
echo 5. Go to menu.
echo _______
set /p a=I CHOICE:
if /i _"%a%"==_"1" (goto :add)
if /i _"%a%"==_"2" (goto :take)
if /i _"%a%"==_"3" (goto :times)
if /i _"%a%"==_"4" (goto :divide)
if /i _"%a%"==_"5" (goto :menu)
goto :fault

:add
:redo
cls
set /p "A= Enter first number:"
set /p "B= Enter second number:"
if /i _"%B%"==_"back" (goto :redo)
set "C=%A%+%B%" & call :simpleanswer
goto :simple

:take
:redo2
cls
set /p "A= Enter first number:"
set /p "B= Enter second number:"
if /i _"%B%"==_"back" (goto :redo2)
set "C=%A%-%B%" & call :simpleanswer
goto :simple

:times
:redo4
cls
set /p "A= Enter first number:"
set /p "B= Enter second number:"
if /i _"%B%"==_"back" (goto :redo4)
set "C=%A%*%B%" & call :simpleanswer
goto :simple

:divide
:redo6
cls
set /p "A= Enter first number:"
set /p "B= Enter second number:"
if /i _"%B%"==_"back" (goto :redo6)
set "C=%A%/%B%" & call :simpleanswer
goto :simple

:simpleanswer
set /a D=%C%
echo %C%=%D%
pause >nul

REM INTERMEDIATE MATHEMATICS:
:harder
cls
echo Options:
echo 1. More Complex Maths
echo 2. Percentage
echo 3. (Kind of) Fractions
echo 4. Go to menu
echo _______
set /p a=I CHOICE:
if /i _"%a%"==_"1" (goto :complex)
if /i _"%a%"==_"2" (goto :%)
if /i _"%a%"==_"3" (goto :bits)
goto :fault

:%
cls
echo This function works out percentages. Do you want to continue?
set /p a=Enter lowercase y/n:
if /i _"%a%"==_"y" (goto :percent)
if /i _"%a%"==_"n" (goto :harder)
goto :fault

:bits
cls
:redo7
cls
set /p "A= Enter numerator 1:
set /p "B= Enter denominator 1:
if /i _"%B%"==_"back" (goto :redo7)
set "C=%A%/%B%"
set "1=10"
set "2=%B%/%1%"
set /a 3=%2%
set "4=%A%*%3%"
set /a "5=%4%"
REM %5% now holds percentage result of fraction 1
cls
:redo8
set /p "A= Enter numerator 2:
set /p "B= Enter denominator 2:
if /i _"%B%"==_"back" (goto :redo8)
set "C=%A%/%B%"
set "1=10"
set "2=%B%/%1%"
set /a "3=%2%"
set "6=%A%*%3%"
set /a "7=%6%"
REM %7% now holds percentage result of fraction 2
REM Now to combine them:
set "pre=%5%+%7%"
set /a answer=%pre%
REM %answer% now has the percentage of the added fractions.
echo The result is %answer%%.
pause >nul
goto :harder

:%
cls
:redo9
cls
set /p "A= Enter number:
set /p "B= Enter "out of":
if /i _"%B%"==_"back" (goto :redo9)
set "C=%A%/%B%"
set "1=10"
set "2=%B%/%1%"
set /a 3=%2%
set "4=%A%*%3%"
set /a 5=%4%
REM %5% now holds percentage result of fraction 1
echo The result is %answer%%.
pause >nul
goto :harder

:complex
cls
set /p A=Enter calculatory string:
set /a B=%A%
echo %B%
pause >nul
goto :harder

:algebra
echo Options:
echo 1. Solve for x
echo 2. Sovle equation
echo 3. Go to Menu
echo _________
set /p a=I CHOICE:
if /i _"%a%"==_"1" (goto :solvex)
if /i _"%a%"==_"2" (goto :solvee)
goto :fault

:solvex
echo Options:
echo 1. Simple algebra:
echo 2. More complex algebra:
echo 3. Go to Menu
echo _________
set /p a=I CHOICE:
if /i _"%a%"==_"1" (goto :simplea)
if /i _"%a%"==_"2" (goto :harda)
pause >nul

:simplea
cls
:redo12
cls
set /p A= Enter equation:

if /i _"%B%"==_"back" (goto :redo12)
set "C=%A%+%B%" & call :simpleanswer
goto :simple


Regards,
Rileyh

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Batch algebra

#2 Post by alan_b » 05 Dec 2011 05:57

I have never heard of "pronumeral"
Google barely recognises it with only " About 13,400 results (0.16 seconds) "

A more recognisable and thus better choice is "algebraic variable"
Google found " 9,590,000 results (0.23 seconds) "
and even better - I would not have needed Google :)

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Batch algebra

#3 Post by trebor68 » 06 Dec 2011 17:20

In your batch code are some errors.

It is not possible: goto :%

Also you have two rows: :%

set 1=34
echo %1%
You will see "34"

set /a 2=%1%+1
That is not possible.


Any varible can not start with a number.


precent

Code: Select all

set num1=4
set num2=5
set /a result=num1*100/num2
echo %result%present

4 of 5 is 80%


numerator 1 is num1
denominator 1 is num2
numerator 2 is num3
denominator 2 is num4

Code: Select all

set /a num5=num1*num4+num2*num3
set /a num6=num2*num4
...
set gct= #######
...
set /a num5/=gcd
set /a num6/=gcd

Now we need the funtion gcd (greates common divisor).

I search a way for the function gcd. But not now.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Batch algebra

#4 Post by trebor68 » 10 Dec 2011 18:55

I will hope that this is the correct translation for the mathematic operations.

Here a code for fraction.

num1/num2 operation num3/num4 = num5/num6

Here the code:

Code: Select all

@echo off
cls
echo.
echo fractions
echo.
echo  1 - addition (plus)
echo  2 - subtraction (minus)
echo  3 - multiplication (multiplied by)
echo  4 - division (divided by)
echo  0 - end
echo.
:again0
set /p func=  Please enter operation (nummer):
if "%func%"=="0" goto :eof
if "%func%"=="1" goto :func0
if "%func%"=="2" goto :func0
if "%func%"=="3" goto :func0
if "%func%"=="4" goto :func0
goto :again0
:func0
echo.
set /p num1=nominator   1:
set /p num2=denominator 1:
set /p num3=nominator   2:
set /p num4=denominator 2:
goto :func%func%

:func1
set /a num5=num1*num4 + num3*num2
set /a num6=num2*num4
call :gcd %num5% %num6% gcd
if "%gcd%"=="0" (echo Error denominator is zero.) & exit /b
set /a num5/=gcd
set /a num6/=gcd
goto :view

:func2
set /a num5=num1*num4 - num3*num2
set /a num6=num2*num4
call :gcd %num5% %num6% gcd
if "%gcd%"=="0" (echo Error denominator is zero.) & exit /b
set /a num5/=gcd
set /a num6/=gcd
goto :view

:func3
set /a num5=num1*num3
set /a num6=num2*num4
call :gcd %num5% %num6% gcd
if "%gcd%"=="0" (echo Error denominator is zero.) & exit /b
set /a num5/=gcd
set /a num6/=gcd
goto :view

:func4
set /a num5=num1*num4
set /a num6=num2*num3
call :gcd %num5% %num6% gcd
if "%gcd%"=="0" (echo Error denominator is zero.) & exit /b
set /a num5/=gcd
set /a num6/=gcd
goto :view

:gcd
set h1=%1
set h2=%2
if "%h2%"=="0" (echo Error.) & exit /b
:gcd1
set /a "h3=h1 %% h2"
if %h3%==0 (set %~3=%h2%) & exit /b
set h1=%h2%
set h2=%h3%
goto :gcd1

:view
if %func%==1 set h1= ----- + ----- = -----
if %func%==2 set h1= ----- - ----- = -----
if %func%==3 set h1= ----- * ----- = -----
if %func%==4 set h1= ----- / ----- = -----
echo.
set num1=     %num1%
set num1=%num1:~-5%
set num2=     %num2%
set num2=%num2:~-5%
set num3=     %num3%
set num3=%num3:~-5%
set num4=     %num4%
set num4=%num4:~-5%
set num5=     %num5%
set num5=%num5:~-5%
set num6=     %num6%
set num6=%num6:~-5%
echo  %num1%   %num3%   %num5%
echo %h1%
echo  %num2%   %num4%   %num6%

Post Reply