Help with my Batch Graphing Calculator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Help with my Batch Graphing Calculator

#1 Post by batnoob » 27 Mar 2018 16:52

I Wanted more practice/experience working with batch, and I needed a graphing calculator for class, so i decided to make one from scratch. This has probably been done already, but I don't see it any where.
--> It is incredibly slow, plotting the points only two at a time, but it is sped up by the use of the TAB BS BS trick rather than the timeout trick. Is there any way to speed it up more? (there must be, since Snake.bat can be so fast)
-->it also uses a goto loop with an if statement as a "DO WHILE" loop. Is there a more efficient/faster way to do this?
-->Is there a more efficient way to make sure the equation is valid?
--> I made an attempt to let the graph finish both the positive and negative lines (if you'll notice there are two separate lines working together to form one graph) but only the !nX!-!nY! line or the !X!-!Y! line can finish, it seems like my if statement isn't working like I expected? but then how would it work?
--> on that note, is it necessary that i use two lines, or can there be just one, like is supposed to be.
--> Other than that any feedback or comments are welcome.

Code: Select all

:setup
@echo off
setlocal enableDelayedExpansion
REM capture escape char for VT100
for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
REM find out WinVer to see if vt100 escapes work
for /f "tokens=4 delims= " %%a in ('ver') do set j=%%a
set i=%j:~0,2%
if "%i%" == "10" set "isWinTen=true"
title Graphing Calculator by BatNoob
color 9f
mode 120,120
set "TAB=	"
set ^"LF=^

^" Above empty line is required - do not remove
set ^"$n=^^^%LF%%LF%^%LF%%LF%^^"


pause
:menu
cls
echo ÛÛÛÛÛ  ÛÛÛÛÛ  ÛÛÛÛÛ  ÛÛÛÛÛ  Û   Û  ÛÛÛÛÛ  Û   Û  ÛÛÛÛÛ
echo Û      Û   Û  Û   Û  Û   Û  Û   Û    Û    ÛÛ  Û  Û
echo Û ÛÛÛ  ÛÛÛÛÛ  ÛÛÛÛÛ  ÛÛÛÛÛ  ÛÛÛÛÛ    Û    Û Û Û  Û ÛÛÛ
echo Û   Û  Û  Û   Û   Û  Û      Û   Û    Û    Û  ÛÛ  Û   Û
echo ÛÛÛÛÛ  Û   Û  Û   Û  Û      Û   Û  ÛÛÛÛÛ  Û   Û  ÛÛÛÛÛ
echo/
echo ÛÛÛÛÛ  ÛÛÛÛÛ  ÛÛ     ÛÛÛÛÛ  Û   Û  ÛÛ     ÛÛÛÛÛ  ÛÛÛÛÛ  ÛÛÛÛÛ  ÛÛÛÛÛ  ÛÛ
echo Û      Û   Û  Û      Û      Û   Û  Û      Û   Û    Û    Û   Û  Û   Û  ÛÛ
echo Û      ÛÛÛÛÛ  Û      Û      Û   Û  Û      ÛÛÛÛÛ    Û    Û   Û  ÛÛÛÛÛ  ÛÛ
echo Û      Û   Û  Û      Û      Û   Û  Û      Û   Û    Û    Û   Û  Û  Û    
echo ÛÛÛÛÛ  Û   Û  ÛÛÛÛÛ  ÛÛÛÛÛ   ÛÛÛ   ÛÛÛÛÛ  Û   Û    Û    ÛÛÛÛÛ  Û   Û  ÛÛ

echo/
echo/
set "op1=1. Graphing Calc "
set "op2=2. Options"
if exist "File.txt" (
	for /f "tokens=1-2 delims=," %%t in (File.txt) do (
		set "max=%%t"
		color %%u
	)
)
set "op3=3. Exit "
set "op4=4. Restart "

echo ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
echo Û %op1%%TAB%Û
echo Û %op2%%TAB%%tab%Û
echo Û %op3%%TAB%%tab%Û
echo Û %op4%%TAB%%TAB%Û
echo ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ

set "label="
for /f "eol=1 delims=" %%i in ('xcopy /lwq "%~f0" :\') do set "label=%%i"
set "label=Thing_!label:~-1!"
goto :%label%
exit /b


:Thing_4
start cmd /c %0
exit



goto :menu

:Thing_2
cls
echo ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
echo Û 1. Size          Û
echo Û 2. Color         Û
echo Û 3. Clear         Û
echo ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ

set "key="
for /F "delims=" %%k in ('xcopy /W "%~F0" "%~F0" 2^>NUL') do if not defined key set "key=%%k"
for /F %%k in ("!key:~-1!") do (set nkey=%%k)
If !nkey! == 1 (
	set /p "max=Size (1-58): "
) Else if !nkey! == 2 (
	set /p "Col=Color: "
) else if "!nkey!" == "3" (
	ERASE File.txt
	goto :menu
) else (
	set max=50
	goto :Thing_2
)
if defined Col (color %Col%)
<nul set /p "=!max!" >File.txt
<nul set /p "=," >> File.txt
<nul set /p "=!Col!" >>File.txt
goto :menu

:Thing_3
exit

:Thing_1
cls
::*****************************************************************MAIN***********************************************************************************
::::::::::::::::::::::::::::::::::::::::::
::setting <tab>  and <bs>
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
set "TAB=	"
::
::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::
::set the math functions from https://www.dostips.com/forum/viewtopic.phpÛf=3&t=5819
set "Abs[x]=(((x)>>31|1)*(x))"	& REM Absolute Value
set "Sign[x]=(x)>>31 | -(x)>>31 & 1"	& REM Returns the sign of the number
set "AVG[xy]=(X&Y)+(X^Y)/2"	& REM Average
:: they don't work to type in.
::::::::::::::::::::::::::::::::::::::::::

set "start=0" 		::part of my improvised Do While Loop
If not defined max (set max=55)
set /a min=max*(-1)
set /a ymax=(!max!/2)-5
set /a ymin=(ymax * -1)
set /a nmax=2*!max!+2
if not defined char (set "char=2")
set "eq="
set/p "eq=Type equation, Put spaces between all characters: "
:::::::::::::::::::::::::::::::::::::::::::::Checking Validity::::::::::::::::::
set "check=%eq: =.%"
set fact=good
call :strlen len eq
::make sure that equation isn't too long for the "checker"
if %len% GTR 48 (set fact=bad)
:: make sure there are spaces
if !eq! == !check! (set fact=bad)
set "bad=A B C D E F G H I J K L M N O P Q R S T U V W Z"
:: Check if any of the "bad" chars are in the equation.
set "stop=False"
for %%a in (%eq%) do (
	for %%A in (%bad%) do (
		if /i "%%a" == "%%A" (set "stop=true")
	)
)
:: go back to beginning if the equation is bad
if "!fact!"=="bad" (goto :Thing_1)
if "%stop%" == "true" (goto :Thing_1)
::make sure %eq% isn't empty and got though the "checker"
If "%eq%" == " " goto :Thing_1

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::Begin actual work
set "neq=!eq:x=((-1)*x)!"	:: Graphing on both sides of the y axis
set "X=0"
set "Y=0"
set /a "nX=X* (-1)"
set /a "nY=Y* (-1)"
cls
::echo/
:call
Call :start & rem << to stop the random exiting that occurred whenever !X! was greater than !max!
pause
Call :ClearVars &REM reset the graph
goto :menu

:start 
REM Goto loop Acting as a DO WHILE loop, is there something more efficientÛ
set /a "start=!start!+1"
if !start! GEQ 2 (set /a "X+=1")
set /a "Y=%eq%" 	 & REM thanks to @aGerman and @Aacini for helping me with this.
set /a "nX=x*(-1)"
set /a "nY=%neq%"
if "!x!" NEQ "0" (if "!y!" neq "0" (if "!nx!" NEQ "0" (if "!ny!" neq "0" (set "char=Û")))) else (set "char=Û")
set !x!-!y!=!char!
set !nX!-!nY!=!char!
For /l %%a in (0 1 !nmax!) do (<nul set/p "=Û")
echo/
rem draw the graph.
for /l %%y in (!ymax! -1 !ymin!) do (
	<nul set/p "=Û"
	for /l %%x in (!min! 1 !max!) do (
		If not defined %%x-%%y (
			If "%%x" Neq "0" (
				If "%%y" NEQ "0" (
					set "%%x-%%y=Û"
				) Else (
					set "%%x-%%y=Û"
				)
			) Else (
				set "%%x-%%y=Û"
			)
		) 	& rem if statement marks the axes
		<nul set/p "=!%%x-%%y!"
	)
	echo Û%%y
)
For /l %%a in (0 1 !nmax!) do (<nul set/p "=Û")
echo/
set /a "nBSs=(!ymax!+5)*28"
set "BSs="
for /l %%# in (1 1 !nBSs!) do (set "BSs=!BSs!!BS!") & Rem <tab><bs><bs> method described by Aacini is faster than Timeout method,
::														also explained by Aacini

if %isWinTen% NEQ true (
	echo %TAB%%BSs%@ 2>nul & Rem Moving cursor to screen home.
) else (
	echo %ESC%[H & REM use vt100 escape sequences if isWinTen = true
)
If !nx! LSS !max! (
	If !ny! LSS !ymax! (
		If !nx! GTR !min! (
			If !ny! GTR !ymin! (
				REM 					Second half
				If !X! LSS !max! (
					If !Y! LSS !ymax! (
						If !X! GTR !min! (
							If !Y! GTR !ymin! (
								goto :start &REM                   Troublesome if statement, first half works, but not the second
							)
						)
					)
				)
			)
		)
	)
) Else (exit /b)
endlocal

::--------------------------------------------------Functions------------------------------------------------------------------------
goto :eof

:ColorText color text
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

:ClearVars 

for /l %%y in (!ymax! -1 !ymin!) do (
	for /l %%x in (!min! 1 !max!) do (
	set "%%x-%%y="
	)
)

exit /b

:strlen <resultVar> <stringVar>
(   
    setlocal EnableDelayedExpansion
    set "s=!%~2!#"
    set "len=0"
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if "!s:~%%P,1!" NEQ "" ( 
            set /a "len+=%%P"
            set "s=!s:~%%P!"
        )
    )
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)
I also found that you can put comments directly after setting a variable without &REM.


::Batnoob
'Batnoob
//Batnoob
Last edited by batnoob on 03 Apr 2018 13:35, edited 1 time in total.

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: Help with my Batch Graphing Calculator

#2 Post by batnoob » 30 Mar 2018 12:08

Also, the plotted character is supposed to change if it is on the axis, but it doesn't, any way to fix this?

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: Help with my Batch Graphing Calculator

#3 Post by batnoob » 30 Mar 2018 15:50

I solved the "checker" problem, finding that for without any tags (/f, /l, etc.) iterates for each item delimited by spaces. Here is the code:

Code: Select all

:: Check if any of the "bad" chars are in the equation.
set "stop=False"
for %%a in (%eq%) do (
	for %%A in (%bad%) do (
		if /i "%%a" == "%%A" (set "stop=true")
	)
)

Post Reply