|
brglng
银牌会员
     永遠的DOS~~~
积分 1200
发帖 466
注册 2005-2-1 来自 上海
状态 离线
|
『楼 主』:
一个可以计算简单表达式的批处理
使用 LLM 解释/回答一下
我编了一个有趣的批处理,用于2K/XP等NT类的系统,可以计算简单的表达式。只能算作入门级的吧,所以就贴在这里了。
@echo off
echo 欢迎使用2K/XP表达式计算批处理
echo Made by Brglng 2005.8.15
echo 下载地址:http://brglng.ys168.com
echo 有bug请及时汇报!Email:sky-0310@sohu.com
echo.
echo 注意:本批处理只能用于NT类的系统,不能用于Win9x/Me/DOS!
echo 如果在命令行下使用,请用附件里的“命令提示符”或者使用
echo cmd.exe,而不要使用command.com!
echo This batch file can only be used in Chinese version of
echo Windows 2K/XP/2003...!
echo Do not use command.com instead of cmd.exe!
pause
:inputExpr
set Expr=
set EvalExpr=
echo.
echo 请输入表达式(输入"?"查看帮助):
set /p Expr=
if "%Expr%"=="" (
goto inputExpr
)
if "%Expr%"=="?" (
echo 本表达式计算批处理很简单并以递减的优先权顺序支持下列操作:
echo ^(^) ^- 分组
echo ^! ^~ ^- ^- 一元运算符
echo ^* ^/ ^% ^- 算数运算符
echo ^+ ^- ^- 算数运算符
echo ^<^< ^>^> ^- 逻辑移位
echo ^- 按位“与”
echo ^^ ^- 按位“异”
echo ^| ^- 按位“或”
echo ^= ^*^= ^/^= ^%^= ^+^= ^-^= ^- 赋值
echo ^&^= ^^^= ^|^= ^<^<^= ^>^>^=
echo ^, ^- 表达式分隔符
echo 如果您使用任何逻辑或取余操作符, 您需要将表达式字符串用
echo 引号扩起来。在表达式中的任何非数字字符串键作为环境变量
echo 名称,这些环境变量名称的值已在使用前转换成数字。如果指定
echo 了一个环境变量名称,但未在当前环境中定义,那么值将被定为
echo 零。这使您可以使用环境变量值做计算而不用键入那些 % 符号
echo 来得到它们的值。除十六进制有 0x 前缀,八进制有 0 前缀的,
echo 数字值为十进位数字。因此, 0x12 与 18 和 022 相同。请注意
echo 八进制公式可能很容易搞混: 08 和 09 是无效的数字,因为 8
echo 和 9 不是有效的八进制位数。
goto inputExpr
)
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" (
echo 输入错误!
goto inputExpr
)
echo 计算结果为:%EvalExpr%
:if_save
set SaveEval=
echo.
echo 是否将表达式和计算结果存入文件?
set /p SaveEval=
if "%SaveEval%"=="Y" goto save
if "%SaveEval%"=="y" goto save
if "%SaveEval%"=="N" goto next
if "%SaveEval%"=="n" goto next
goto if_save
:save
echo 请输入文件路径及文件名(如C:\Windows\file.txt。若不输入路径则表示当前文件夹,留空则为上一次输入的路径):
set /p SaveEvalPath=
echo %Expr%=%EvalExpr% >>%SaveEvalPath%
echo 表达式以及计算结果已存入%SaveEvalPath%!
:next
set EvalNext=
echo 1.继续计算 2.退出
set /p EvalNext=
if "%EvalNext%"=="1" goto inputExpr
if "%EvalNext%"=="2" goto end
goto next
:end
echo 谢谢使用!按任意键退出……
pause >nul
set Expr=
set EvalExpr=
set SaveEval=
set SaveEvalPath=
set EvalNext=
这个批处理的最新版可以在我的E盘的“DIY”目录里下载到。
Last edited by namejm on 2007-2-3 at 09:49 PM ]
I've compiled an interesting batch script for NT-based systems like 2K/XP, which can calculate simple expressions. It's just an entry-level one, so I'm posting it here.
@echo off
echo Welcome to use the 2K/XP expression calculation batch script
echo Made by Brglng 2005.8.15
echo Download address: http://brglng.ys168.com
echo Please report bugs in time! Email:sky-0310@sohu.com
echo.
echo Note: This batch script can only be used in NT-based systems, not in Win9x/Me/DOS!
echo If using in the command line, please use "Command Prompt" in the accessories or use cmd.exe, not command.com!
echo This batch file can only be used in Chinese version of
echo Windows 2K/XP/2003...!
echo Do not use command.com instead of cmd.exe!
pause
:inputExpr
set Expr=
set EvalExpr=
echo.
echo Please enter the expression (enter "?" to view help):
set /p Expr=
if "%Expr%"=="" (
goto inputExpr
)
if "%Expr%"=="?" (
echo This expression calculation batch script is very simple and supports the following operations in decreasing order of priority:
echo ^(^) ^- Grouping
echo ^! ^~ ^- ^- Unary operators
echo ^* ^/ ^% ^- Arithmetic operators
echo ^+ ^- ^- Arithmetic operators
echo ^<^< ^>^> ^- Logical shift
echo ^- Bitwise "AND"
echo ^^ ^- Bitwise "XOR"
echo ^| ^- Bitwise "OR"
echo ^= ^*^= ^/^= ^%^= ^+^= ^-^= ^- Assignment
echo ^&^= ^^^= ^|^= ^<^<^= ^>^>^=
echo ^, ^- Expression separator
echo If you use any logical or remainder operators, you need to enclose the expression string in quotes. Any non-numeric string key in the expression is taken as an environment variable name, and the values of these environment variable names are converted to numbers before use. If an environment variable name is specified but not defined in the current environment, the value will be set to zero. This allows you to use environment variable values for calculations without typing those % symbols to get their values. Except for hexadecimal numbers with 0x prefix and octal numbers with 0 prefix, numeric values are decimal numbers. Therefore, 0x12 is the same as 18 and 022. Please note that octal formulas can be easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits.
goto inputExpr
)
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" (
echo Input error!
goto inputExpr
)
echo The calculation result is: %EvalExpr%
:if_save
set SaveEval=
echo.
echo Do you want to save the expression and calculation result to a file ?
set /p SaveEval=
if "%SaveEval%"=="Y" goto save
if "%SaveEval%"=="y" goto save
if "%SaveEval%"=="N" goto next
if "%SaveEval%"=="n" goto next
goto if_save
:save
echo Please enter the file path and file name (such as C:\Windows\file.txt. If no path is entered, it means the current folder, leaving it blank means the last entered path):
set /p SaveEvalPath=
echo %Expr%=%EvalExpr% >>%SaveEvalPath%
echo The expression and calculation result have been saved to %SaveEvalPath%!
:next
set EvalNext=
echo 1. Continue calculation 2. Exit
set /p EvalNext=
if "%EvalNext%"=="1" goto inputExpr
if "%EvalNext%"=="2" goto end
goto next
:end
echo Thank you for using! Press any key to exit...
pause >nul
set Expr=
set EvalExpr=
set SaveEval=
set SaveEvalPath=
set EvalNext=
The latest version of this batch script can be downloaded from the "DIY" directory on my E drive.
Last edited by namejm on 2007-2-3 at 09:49 PM ]
|

32位才是DOS未来的希望
个人网志:http://sololand.moe |
|
2005-8-15 15:53 |
|
|
venchia
初级用户
 
积分 125
发帖 11
注册 2004-1-25
状态 离线
|
|
2005-8-15 18:02 |
|
|
merlin
初级用户
  KMGT
积分 143
发帖 18
注册 2005-2-11
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
怎么不写一个Win9x/me/DOS的啊!
Why not write one for Win9x/me/DOS!
|

坚持就是胜利! |
|
2005-8-15 19:00 |
|
|
Michael
钻石会员
       
积分 10046
发帖 3039
注册 2002-11-11
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
也许只有2k/xp才有现成的表达式求值语句吧。
Maybe only 2k/xp has ready-made expression evaluation statements.
|

简单就是美 |
|
2005-8-15 19:44 |
|
|
brglng
银牌会员
     永遠的DOS~~~
积分 1200
发帖 466
注册 2005-2-1 来自 上海
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Originally posted by Michael at 2005-8-15 07:44 PM:
也许只有2k/xp才有现成的表达式求值语句吧。
嗯,没错
其实,要真正实现更好的表达式计算,只要用那些命令行计算器就可以了。它们的使用都很方便。NT的set命令的/a参数也有很多限制的,功能并不强,比如不支持浮点数,不支持复杂的数学函数(如sin、cos)等。所以,这个批处理实际上没什么实用价值,只能作为学习学习吧。我自认为这个批处理对2K/XP新的命令开关和语法的综合运用还是不错的。
Last edited by brglng on 2005-8-16 at 10:18 ]
Originally posted by Michael at 2005-8-15 07:44 PM:
Maybe only 2k/xp have ready-made expression evaluation statements.
Well, that's right.
In fact, to really achieve better expression calculation, you can just use those command-line calculators. Their usage is very convenient. The /a parameter of the set command in NT also has many limitations, and the function is not strong. For example, it does not support floating-point numbers, does not support complex mathematical functions (such as sin, cos), etc. So, this batch processing is actually not very practical, and can only be used for learning. I think this batch processing has a good comprehensive application of new command switches and syntax for 2K/XP.
Last edited by brglng on 2005-8-16 at 10:18 ]
|

32位才是DOS未来的希望
个人网志:http://sololand.moe |
|
2005-8-16 09:41 |
|
|
Michael
钻石会员
       
积分 10046
发帖 3039
注册 2002-11-11
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
开始还真吓了我一跳,我还以为表达式计算也是批处理完成的呢。
C++编一个类似程序还得需要这么多行呢:
#include<iostream>
#include<string>
#include<map>
#include<cctype>
#include<sstream>
using namespace std;
namespace Parser
{
double prim(bool);
double term(bool);
double expr(bool);
}
namespace Lexer
{
enum Token_value
{
NAME, NUMBER, END,
PLUS='+', MINUS='-', MUL='*', DIV='/',
PRINT=';', ASSIGN='=', LP='(', RP=')'
};
Token_value curr_tok;
double number_value;
string string_value;
map<string,double>table;
Token_value get_token();
}
namespace Driver
{
int no_of_errors;
istream* input;
void skip();
void sta();
}
namespace Error
{
struct Syntax_error
{
const char* p;
Syntax_error(const char* q) {p=q;}
};
struct Zero_divide{};
}
using namespace Driver;
using namespace Parser;
using namespace Lexer;
using namespace Error;
double Parser::expr(bool get)
{
double left=term(get);
for(;;)
switch(curr_tok)
{
case PLUS:
left+=term(true);
break;
case MINUS:
left-=term(true);
break;
default:
return left;
}
}
double Parser::term(bool get)
{
double left=prim(get);
for(;;)
switch(curr_tok)
{
case MUL:
left*=prim(true);
break;
case DIV:
if(double d=prim(true))
{
left/=d;
break;
}
throw Zero_divide();
default:
return left;
}
}
double Parser::prim(bool get)
{
if (get) get_token();
switch(curr_tok)
{
case NUMBER:
{
double v=number_value;
get_token();
return v;
}
case NAME:
{
double& v=table;
if(get_token()==ASSIGN)v=expr(true);
return v;
}
case MINUS:
return -prim(true);
case LP:
{
double e=expr(true);
if(curr_tok!=RP) throw Syntax_error("')'expected");
get_token();
return e;
}
default:
throw Syntax_error("primary expected");
}
}
Token_value Lexer::get_token()
{
char ch;
do
{
if(!input->get(ch))return curr_tok=END;
}while(ch!='\n'&&isspace(ch));
switch(ch)
{
case '\n':
return curr_tok=PRINT;
case ';':
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok=Token_value(ch);
case '0':case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':
input->putback(ch);
*input>>number_value;
return curr_tok=NUMBER;
default:
if(isalpha(ch))
{
string_value=ch;
while(input->get(ch)&&isalnum(ch))string_value.push_back(ch);
input->putback(ch);
return curr_tok=NAME;
}
throw Syntax_error("bad token");
}
}
void Driver::sta()
{
while(*input){
try
{
get_token();
if(curr_tok==END)break;
if(curr_tok==PRINT)continue;
cout<<expr(false)<<endl;
}
catch(Zero_divide)
{
cerr<<"attempt to divide by zero\n";
if(curr_tok!=PRINT)skip();
}
catch(Syntax_error e)
{
cerr<<"syntax error:"<<e.p<<endl;
if(curr_tok!=PRINT)skip();
}
}
}
void Driver::skip()
{
no_of_errors++;
while(*input)
{
char ch;
input->get(ch);
switch(ch)
{
case '\n':
case ';':
return;
}
}
}
int main(int argc, char*argv)
{
switch(argc)
{
case 1:
input=&cin;
break;
case 2:
input=new istringstream(argv);
break;
default:
cerr<<"too many arguments\n";
return 1;
}
sta();
if(input!=&cin)delete input;
return no_of_errors;
}
Last edited by Michael on 2005-8-16 at 21:37 ]
I was really startled at first. I thought the expression calculation was also done by batch processing.
Writing a similar program in C++ still needs so many lines:
#include<iostream>
#include<string>
#include<map>
#include<cctype>
#include<sstream>
using namespace std;
namespace Parser
{
double prim(bool);
double term(bool);
double expr(bool);
}
namespace Lexer
{
enum Token_value
{
NAME, NUMBER, END,
PLUS='+', MINUS='-', MUL='*', DIV='/',
PRINT=';', ASSIGN='=', LP='(', RP=')'
};
Token_value curr_tok;
double number_value;
string string_value;
map<string,double>table;
Token_value get_token();
}
namespace Driver
{
int no_of_errors;
istream* input;
void skip();
void sta();
}
namespace Error
{
struct Syntax_error
{
const char* p;
Syntax_error(const char* q) {p=q;}
};
struct Zero_divide{};
}
using namespace Driver;
using namespace Parser;
using namespace Lexer;
using namespace Error;
double Parser::expr(bool get)
{
double left=term(get);
for(;;)
switch(curr_tok)
{
case PLUS:
left+=term(true);
break;
case MINUS:
left-=term(true);
break;
default:
return left;
}
}
double Parser::term(bool get)
{
double left=prim(get);
for(;;)
switch(curr_tok)
{
case MUL:
left*=prim(true);
break;
case DIV:
if(double d=prim(true))
{
left/=d;
break;
}
throw Zero_divide();
default:
return left;
}
}
double Parser::prim(bool get)
{
if (get) get_token();
switch(curr_tok)
{
case NUMBER:
{
double v=number_value;
get_token();
return v;
}
case NAME:
{
double& v=table;
if(get_token()==ASSIGN)v=expr(true);
return v;
}
case MINUS:
return -prim(true);
case LP:
{
double e=expr(true);
if(curr_tok!=RP) throw Syntax_error("')'expected");
get_token();
return e;
}
default:
throw Syntax_error("primary expected");
}
}
Token_value Lexer::get_token()
{
char ch;
do
{
if(!input->get(ch))return curr_tok=END;
}while(ch!='\n'&&isspace(ch));
switch(ch)
{
case '\n':
return curr_tok=PRINT;
case ';':
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok=Token_value(ch);
case '0':case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':
input->putback(ch);
*input>>number_value;
return curr_tok=NUMBER;
default:
if(isalpha(ch))
{
string_value=ch;
while(input->get(ch)&&isalnum(ch))string_value.push_back(ch);
input->putback(ch);
return curr_tok=NAME;
}
throw Syntax_error("bad token");
}
}
void Driver::sta()
{
while(*input){
try
{
get_token();
if(curr_tok==END)break;
if(curr_tok==PRINT)continue;
cout<<expr(false)<<endl;
}
catch(Zero_divide)
{
cerr<<"attempt to divide by zero\n";
if(curr_tok!=PRINT)skip();
}
catch(Syntax_error e)
{
cerr<<"syntax error:"<<e.p<<endl;
if(curr_tok!=PRINT)skip();
}
}
}
void Driver::skip()
{
no_of_errors++;
while(*input)
{
char ch;
input->get(ch);
switch(ch)
{
case '\n':
case ';':
return;
}
}
}
int main(int argc, char*argv)
{
switch(argc)
{
case 1:
input=&cin;
break;
case 2:
input=new istringstream(argv);
break;
default:
cerr<<"too many arguments\n";
return 1;
}
sta();
if(input!=&cin)delete input;
return no_of_errors;
}
Last edited by Michael on 2005-8-16 at 21:37 ]
|

简单就是美 |
|
2005-8-16 21:36 |
|
|
brglng
银牌会员
     永遠的DOS~~~
积分 1200
发帖 466
注册 2005-2-1 来自 上海
状态 离线
|
|
2005-8-17 15:46 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Re brglng:
很好的代码,提三个建议:
1、加入命令行参数支持,即在允许在命令行参数中写入表达式或者/?;
2、set /p 的提示字符串可以写在 set 语句中,比如
set /p SaveEval=是否将表达式和计算结果存入文件?
3、if 语句中可以使用开关 /i 进行忽略大小写判断,比如:
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto next
另外,待代码完善后,我有意将它收入我的批处理收集帖 {8905}《批处理编程的异类》,但是因为版面限制,可能会删除大部分的说明性文档代码,不知 brglng 兄是否同意?
Re: brglng:
Very good code, three suggestions are put forward:
1. Add command-line parameter support, that is, allow writing expressions or /? in command-line parameters;
2. The prompt string of set /p can be written in the set statement, for example
set /p SaveEval=Do you want to save the expression and calculation result to the file ?
3. The switch /i can be used in the if statement for case-insensitive judgment, for example:
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto next
In addition, after the code is improved, I am interested in including it in my batch processing collection post {8905} "Abnormalities in Batch Processing Programming", but due to layout restrictions, most of the explanatory document code may be deleted. I wonder if Brother brglng agrees?
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-8-19 20:31 |
|
|
agboy888
新手上路

积分 1
发帖 1
注册 2005-8-20
状态 离线
|
|
2005-8-20 01:20 |
|
|
brglng
银牌会员
     永遠的DOS~~~
积分 1200
发帖 466
注册 2005-2-1 来自 上海
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by willsort at 2005-8-19 08:31 PM:
Re brglng:
很好的代码,提三个建议:
1、加入命令行参数支持,即在允许在命令行参数中写入表达式或者/?;
2、set /p 的提示字笠...
谢谢Willsort高手的建议。关于收录,我没什么意见,反正本来就是随便写写的。
Originally posted by willsort at 2005-8-19 08:31 PM:
Re brglng:
Good code, three suggestions:
1. Add command-line parameter support, that is, allow writing expressions or /? in command-line parameters;
2. The prompt word of set /p...
Thanks to expert Willsort for the suggestions. Regarding inclusion, I have no opinion, anyway, it was just written casually.
|

32位才是DOS未来的希望
个人网志:http://sololand.moe |
|
2005-8-23 14:56 |
|
|
brglng
银牌会员
     永遠的DOS~~~
积分 1200
发帖 466
注册 2005-2-1 来自 上海
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
以下是根据willsort建议改进后的代码(增加了很多功能):
@echo off
echo 本表达式计算批处理很简单并以递减的优先权顺序支持下列操作: >EvalExpr.hlp
echo. >>EvalExpr.hlp
echo ^(^) ^- 分组 >>EvalExpr.hlp
echo ^! ^~ ^- ^- 一元运算符 >>EvalExpr.hlp
echo ^* ^/ %% ^- 算数运算符 >>EvalExpr.hlp
echo ^+ ^- ^- 算数运算符 >>EvalExpr.hlp
echo ^<^< ^>^> ^- 逻辑移位 >>EvalExpr.hlp
echo ^- 按位“与” >>EvalExpr.hlp
echo ^^ ^- 按位“异” >>EvalExpr.hlp
echo ^| ^- 按位“或” >>EvalExpr.hlp
echo ^= ^*^= ^/^= %%^= ^+^= ^-^= ^- 赋值 >>EvalExpr.hlp
echo ^&^= ^^^= ^|^= ^<^<^= ^>^>^= >>EvalExpr.hlp
echo ^, ^- 表达式分隔符 >>EvalExpr.hlp
echo. >>EvalExpr.hlp
echo 如果您使用任何逻辑或取余操作符, 您需要将表达式字符串用引号扩起来。在表达式中的任何非数字字符串键作为环境变量名称,这些环境变量名称的值已在使用前转换成数字。如果指定了一个环境变量名称,但未在当前环境中定义,那么值将被定为零。这使您可以使用环境变量值做计算而不用键入那些 %% 符号来得到它们的值。除十六进制有 0x 前缀,八进制有 0 前缀的,数字值为十进位数字。因此, 0x12 与 18 和 022 相同。请注意八进制公式可能很容易搞混: 08 和 09 是无效的数字,因为 8 和 9 不是有效的八进制位数。 >>EvalExpr.hlp
::cls
if "%1"=="" goto start
if "%1"=="/?" goto start
if /i "%1"=="/?:Expr" goto start
if /i "%1"=="/A" if "%2"=="" (
echo 必须指定文件路径!
goto end
)
if /i "%1"=="/A" goto start
set /a EvalExpr=%1
if == (
echo 表达式输入错误!
goto end
)
echo %1=%EvalExpr%
if == goto end
echo %1=%EvalExpr% >>
echo 算式和计算结果已存入!
goto end
:start
if /i
if /i == goto end
echo 欢迎使用2K/表达式计算批处理
echo
echo 下载地址://
echo 有请及时汇报!
echo.
echo 注意:本批处理只能用于类的系统,不能用于/Me/
echo 如果在命令行下使用,请用附件里的“命令提示符”或者使用,而不要使用
echo /XP/
echo
echo.
if
if
echo 用法:
echo
echo
echo
echo
echo.
echo 要计算的表达式
echo 使用此参数将表达式和计算结果存入指定的文件
echo 非使用参数计算时总是将计算结果自动存入指定文件
echo 注:文件路径将作为第二个参数,必须和/A参数配合使用。
echo : 查看此帮助
echo : 查看表达式书写方面的帮助
goto end
:inputExpr
echo.
set Expr=
set EvalExpr=
set /请输入表达式输入查看帮助 退出:
if
if
echo.
more
goto
)
if /i == goto end
set /
if
echo 输入错误!
goto
)
echo 计算结果为:
if /i == goto autosave
:if_save
set SaveEval=
echo.
set /是否将表达式和计算结果存入文件?
if /i == goto save
if /
goto
:save
set /p SaveEvalPath=请输入文件路径及文件名(如C:\Windows\file.txt。若不输入路径则表示当前文件夹,留空则为上一次输入的路径):
echo =%EvalExpr% >>
echo 表达式以及计算结果已存入!
goto inputExpr
:autosave
echo =%EvalExpr% >>
goto inputExpr
:end
if / 所有的表达式和计算结果都已存入!
del /q EvalExpr.hlp >nul 2>nul
set Expr=
set EvalExpr=
set SaveEval=
set SaveEvalPath=
set EvalNext=
Here is the translation of the provided code's relevant description part:
The following is the improved code according to the suggestions of willsort (added many functions):
@echo off
echo This simple expression calculation batch processing supports the following operations in descending priority order: >EvalExpr.hlp
echo. >>EvalExpr.hlp
echo ^(^) ^- Grouping >>EvalExpr.hlp
echo ^! ^~ ^- ^- Unary operators >>EvalExpr.hlp
echo ^* ^/ %% ^- Arithmetic operators >>EvalExpr.hlp
echo ^+ ^- ^- Arithmetic operators >>EvalExpr.hlp
echo ^<^< ^>^> ^- Logical shift >>EvalExpr.hlp
echo ^- Bitwise "AND" >>EvalExpr.hlp
echo ^^ ^- Bitwise "XOR" >>EvalExpr.hlp
echo ^| ^- Bitwise "OR" >>EvalExpr.hlp
echo ^= ^*^= ^/^= %%^= ^+^= ^-^= ^- Assignment >>EvalExpr.hlp
echo ^&^= ^^^= ^|^= ^<^<^= ^>^>^= >>EvalExpr.hlp
echo ^, ^- Expression separator >>EvalExpr.hlp
echo. >>EvalExpr.hlp
echo If you use any logical or remainder operators, you need to enclose the expression string in quotes. Any non-numeric string in the expression is taken as an environment variable name, and the value of these environment variable names is converted to a number before use. If an environment variable name is specified but not defined in the current environment, the value will be set to zero. This allows you to use environment variable values for calculations without having to type those %% symbols to get them. Numeric values are in decimal, except that hexadecimal has a 0x prefix and octal has a 0 prefix. So, 0x12 is the same as 18 and 022. Please note that octal formulas can be easily confusing: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits. >>EvalExpr.hlp
::cls
if "%1"=="" goto start
if "%1"=="/?" goto start
if /i "%1"=="/?:Expr" goto start
if /i "%1"=="/A" if "%2"=="" (
echo A file path must be specified!
goto end
)
if /i "%1"=="/A" goto start
set /a EvalExpr=%1
if "%EvalExpr%"=="" (
echo Expression input error!
goto end
)
echo %1=%EvalExpr%
if "%2"=="" goto end
echo %1=%EvalExpr% >>"%2"
echo The formula and calculation result have been stored in "%2"!
goto end
:start
if /i "%1"=="/?:Expr" more EvalExpr.hlp
if /i "%1"=="/?:Expr" goto end
echo Welcome to use the 2K/XP expression calculation batch processing
echo Made by Brglng 2005.8.25
echo Download address: http://brglng.ys168.com
echo Please report bugs in time! Email:sky-0310@sohu.com
echo.
echo Note: This batch processing can only be used in NT-based systems, not in Win9x/Me/DOS!
echo If using it on the command line, please use "Command Prompt" in Accessories or use cmd.exe, not command.com!
echo This batch file can only be used in Chinese version of Windows 2K/XP/2003...!
echo Do not use command.com instead of cmd.exe!
echo.
if not "%1"=="/?" pause
if not "%1"=="/?" goto inputExpr
echo Usage:
echo EvalExpr
echo EvalExpr
echo EvalExpr
echo EvalExpr
echo.
echo : The expression to calculate
echo : Use this parameter to store the expression and calculation result in the specified file
echo : When not using parameters for calculation, always automatically store the calculation result in the specified file
echo Note: The file path will be used as the second parameter and must be used in conjunction with the /A parameter.
echo : View this help
echo : View help on expression writing
goto end
:inputExpr
echo.
set Expr=
set EvalExpr=
set /p Expr=Please enter the expression (enter "?" to view help, "Exit" to exit):
if "%Expr%"=="" goto inputExpr
if "%Expr%"=="?" (
echo.
more EvalExpr.hlp
goto inputExpr
)
if /i "%Expr%"=="Exit" goto end
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" (
echo Input error!
goto inputExpr
)
echo The calculation result is: %EvalExpr%
if /i "%1"=="/A" goto autosave
:if_save
set SaveEval=
echo.
set /p SaveEval=Do you want to store the expression and calculation result in a file ?
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto inputExpr
goto if_save
:save
set /p SaveEvalPath=Please enter the file path and file name (such as C:\Windows\file.txt. If you don't enter a path, it means the current folder, leaving it blank means the last entered path):
echo "%Expr%"=%EvalExpr% >>"%SaveEvalPath%"
echo The expression and calculation result have been stored in "%SaveEvalPath%"!
goto inputExpr
:autosave
echo "%Expr%"=%EvalExpr% >>"%2"
goto inputExpr
:end
if /i "%1"=="/A" if not "%2"=="" echo All expressions and calculation results have been stored in "%2"!
del /q EvalExpr.hlp >nul 2>nul
set Expr=
set EvalExpr=
set SaveEval=
set SaveEvalPath=
set EvalNext=
|

32位才是DOS未来的希望
个人网志:http://sololand.moe |
|
2005-8-25 11:39 |
|
|
defrag
中级用户
   痴迷DOS者
积分 456
发帖 570
注册 2004-10-9
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
用GW-BASIC编计算器并不是很难。。。
这个没测试过
没有错误处理
main.bas:
10 if flag=-1 goto 80
15 input "A=",expres$
20 if expres$="" goto 90
20 open "temp.bas" for output as #1
30 print #1,"10 A=";expres$
40 print #1,"20 PRINT ";CHR$(34);expres$;"=";chr$(34);"A"
50 print #1,"30 flag=-1"
60 print #1,"40 chain ";CHR$(34);"main.bas";CHR$(34)
70 close #1
75 chain "temp.bas"
80 goto 10
90 end
用QB7.1也可以(要用QBX运行,不能编译)
有错误处理
也没测试
main.bas:
if flag=-1 goto 70
10 input "A=",expres$
if expres$="" goto 80
open "temp.bas" for output as #1
print #1,"ON ERROR RESUME NEXT
print #1,"10 A=";expres$
PRINT #1,"IF ERR>0 THEN PRINT";CHR$(34);"Error!";CHR$(34);":goto 30"
print #1,"20 PRINT ";CHR$(34);expres$;"=";chr$(34);"A"
print #1,"30 flag=-1"
print #1,"40 chain ";CHR$(34);"main.bas";CHR$(34)
close #1
chain "temp.bas"
80 goto 10
90 end
Last edited by defrag on 2005-8-30 at 19:41 ]
It's not very difficult to compile a calculator with GW-BASIC...
This hasn't been tested
No error handling
main.bas:
10 if flag=-1 goto 80
15 input "A=",expres$
20 if expres$="" goto 90
20 open "temp.bas" for output as #1
30 print #1,"10 A=";expres$
40 print #1,"20 PRINT ";CHR$(34);expres$;"=";chr$(34);"A"
50 print #1,"30 flag=-1"
60 print #1,"40 chain ";CHR$(34);"main.bas";CHR$(34)
70 close #1
75 chain "temp.bas"
80 goto 10
90 end
It can also be done with QB7.1 (needs to be run with QBX, can't be compiled)
Has error handling
Also not tested
main.bas:
if flag=-1 goto 70
10 input "A=",expres$
if expres$="" goto 80
open "temp.bas" for output as #1
print #1,"ON ERROR RESUME NEXT
print #1,"10 A=";expres$
PRINT #1,"IF ERR>0 THEN PRINT";CHR$(34);"Error!";CHR$(34);":goto 30"
print #1,"20 PRINT ";CHR$(34);expres$;"=";chr$(34);"A"
print #1,"30 flag=-1"
print #1,"40 chain ";CHR$(34);"main.bas";CHR$(34)
close #1
chain "temp.bas"
80 goto 10
90 end
Last edited by defrag on 2005-8-30 at 19:41 ]
|

DOS不是万能的,没有DOS是万万不能的
自古系统谁无死?留取胆清照汗青!uploadImages/20035317345478982.png |
|
2005-8-26 00:54 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Re Brglng:
在结构上作了一些变化,让程序执行流程更加清晰,核心代码并为做出更改,请 Brglng 兄做一下测试吧。关于表达式用法帮助,我起初使用了 for 的语句以节省代码,但后来考虑到可读性的问题,最后舍弃了,你也可以一起测试一下。
:: Eval.bat - 2K/XP表达式计算批处理
:: Brglng - 2005.8.25
:: Modified: Will Sort - 2005.8.26
@echo off
setlocal
if /i "%1"=="/A" if "%2"=="" echo 必须指定文件路径!& goto end
if /i not "%1"=="/A" if not "%1"=="/?" if not "%1"=="" goto EvalArgument
:EvalLogo
cls
echo.
echo 欢迎使用2K/XP表达式计算批处理
echo Made by Brglng 2005.8.25
echo 下载地址:http://brglng.ys168.com
echo 有bug请及时汇报!Email:sky-0310@sohu.com
echo.
echo 注意:本批处理只能用于NT类的系统,不能用于Win9x/Me/DOS!
echo 如果在命令行下使用,请用附件里的“命令提示符”或者使用cmd.exe,而不要使用command.com!
echo This batch file can only be used in Chinese version of Windows 2K/XP/2003...!
echo Do not use command.com instead of cmd.exe!
echo.
if not "%1"=="/?" goto inputExpr
:Help
pause>nul
cls
echo 命令行书写帮助:
echo.
echo %0
echo %0
echo %0
echo %0
echo.
echo : 要计算的表达式
echo : 使用此参数将表达式和计算结果存入指定的文件
echo : 非使用参数计算时总是将计算结果自动存入指定文件
echo 注:文件路径将作为第二个参数,必须和/A参数配合使用。
echo : 查看命令行书写帮助
echo : 查看命令行书写帮助与表达式书写帮助
echo.
if /i not "%2"=="Expr" goto end
pause>nul
:ExprHelp
cls
echo 表达式书写帮助:
echo.
::set /?> sethelp.txt
::for /f "skip=29 delims=" %%h in (sethelp.txt) do echo %%h & if "%%h"=="因为 8 和 9 不是有效的八进制位数。" goto end
echo 本表达式计算批处理很简单并以递减的优先权顺序支持下列操作:
echo.
echo ^(^) ^- 分组
echo ^! ^~ ^- ^- 一元运算符
echo ^* ^/ %% ^- 算数运算符
echo ^+ ^- ^- 算数运算符
echo ^<^< ^>^> ^- 逻辑移位
echo ^- 按位“与”
echo ^^ ^- 按位“异”
echo ^| ^- 按位“或”
echo ^= ^*^= ^/^= %%^= ^+^= ^-^= ^- 赋值
echo ^&^= ^^^= ^|^= ^<^<^= ^>^>^=
echo ^, ^- 表达式分隔符
echo.
echo 如果您使用任何逻辑或取余操作符, 您需要将表达式字符串用引号扩起来。在表达式中的任何非数字字符串键作为环境变量名称,这些环境变量名称的值已在使用前转换成数字。如果指定了一个环境变量名称,但未在当前环境中定义,那么值将被定为零。这使您可以使用环境变量值做计算而不用键入那些 %% 符号来得到它们的值。除十六进制有 0x 前缀,八进制有 0 前缀的,数字值为十进位数字。因此, 0x12 与 18 和 022 相同。请注意八进制公式可能很容易搞混: 08 和 09 是无效的数字,因为 8 和 9 不是有效的八进制位数。
goto end
:EvalArgument
set /a EvalExpr=%1
if "%EvalExpr%"=="" echo 表达式输入错误!& pause & goto ExprHelp
echo %1=%EvalExpr%
if "%2"=="" goto end
echo %1=%EvalExpr% >>"%2"
echo 算式和计算结果已存入"%2"!
goto end
:inputExpr
set Expr=
set EvalExpr=
set /p Expr=请输入表达式(输入"?"查看帮助, "Q"退出):
if "%Expr%"=="" goto inputExpr
if "%Expr%"=="?" call:ExprHelp & goto inputExpr
if /i "%Expr%"=="Q" goto end
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" echo 输入错误!& goto inputExpr
echo 计算结果为:%EvalExpr%
if /i "%1"=="/A" goto autosave
:if_save
set SaveEval=
echo.
set /p SaveEval=是否将表达式和计算结果存入文件?
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto inputExpr
goto if_save
:save
set /p SaveEvalPath=请输入文件路径及文件名(如C:\Windows\file.txt。若不输入路径则表示当前文件夹,留空则为上一次输入的路径):
echo "%Expr%"=%EvalExpr% >>"%SaveEvalPath%"
echo 表达式以及计算结果已存入"%SaveEvalPath%"!
goto inputExpr
:autosave
echo "%Expr%"=%EvalExpr% >>"%2"
goto inputExpr
:end
Last edited by willsort on 2005-9-11 at 13:19 ]
Re Brglng:
Some structural changes have been made to make the program execution flow clearer, and the core code has not been changed. Please Brother Brglng do the testing. Regarding the expression usage help, I initially used the for statement to save code, but later considered the readability issue and finally discarded it. You can also test together.
:: Eval.bat - 2K/XP Expression Calculation Batch
:: Brglng - 2005.8.25
:: Modified: Will Sort - 2005.8.26
@echo off
setlocal
if /i "%1"=="/A" if "%2"=="" echo Must specify the file path!& goto end
if /i not "%1"=="/A" if not "%1"=="/?" if not "%1"=="" goto EvalArgument
:EvalLogo
cls
echo.
echo Welcome to use the 2K/XP expression calculation batch
echo Made by Brglng 2005.8.25
echo Download address: http://brglng.ys168.com
echo Please report bugs in time! Email:sky-0310@sohu.com
echo.
echo Note: This batch file can only be used in NT - class systems, not for Win9x/Me/DOS!
echo If using at the command line, please use "Command Prompt" in the accessories or use cmd.exe, not command.com!
echo This batch file can only be used in Chinese version of Windows 2K/XP/2003...!
echo Do not use command.com instead of cmd.exe!
echo.
if not "%1"=="/?" goto inputExpr
:Help
pause>nul
cls
echo Command line writing help:
echo.
echo %0
echo %0
echo %0
echo %0
echo.
echo : The expression to calculate
echo : Use this parameter to store the expression and calculation result in the specified file
echo : When not using the parameter calculation, always automatically store the calculation result in the specified file
echo Note: The file path will be used as the second parameter and must be used in conjunction with the /A parameter.
echo : View command line writing help
echo : View command line writing help and expression writing help
echo.
if /i not "%2"=="Expr" goto end
pause>nul
:ExprHelp
cls
echo Expression writing help:
echo.
::set /?> sethelp.txt
::for /f "skip=29 delims=" %%h in (sethelp.txt) do echo %%h & if "%%h"=="Because 8 and 9 are not valid octal digits." goto end
echo This expression calculation batch is very simple and supports the following operations in decreasing priority order:
echo.
echo ^(^) ^- Grouping
echo ^! ^~ ^- ^- Unary operator
echo ^* ^/ %% ^- Arithmetic operator
echo ^+ ^- ^- Arithmetic operator
echo ^<^< ^>^> ^- Logical shift
echo ^- Bitwise "AND"
echo ^^ ^- Bitwise "XOR"
echo ^| ^- Bitwise "OR"
echo ^= ^*^= ^/^= %%^= ^+^= ^-^= ^- Assignment
echo ^&^= ^^^= ^|^= ^<^<^= ^>^>^=
echo ^, ^- Expression separator
echo.
echo If you use any logical or remainder operator, you need to enclose the expression string in quotes. Any non-numeric string key in the expression is used as the environment variable name, and the value of these environment variable names is converted to a number before use. If an environment variable name is specified but not defined in the current environment, the value will be set to zero. This allows you to use environment variable values for calculations without having to type those %% symbols to get their values. Except that hexadecimal has a 0x prefix and octal has a 0 prefix, the numeric value is a decimal digit. Therefore, 0x12 is the same as 18 and 022. Please note that octal formulas can be easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits.
goto end
:EvalArgument
set /a EvalExpr=%1
if "%EvalExpr%"=="" echo Expression input error!& pause & goto ExprHelp
echo %1=%EvalExpr%
if "%2"=="" goto end
echo %1=%EvalExpr% >>"%2"
echo The formula and calculation result have been stored in "%2"!
goto end
:inputExpr
set Expr=
set EvalExpr=
set /p Expr=Please enter the expression (enter "?" to view help, "Q" to exit):
if "%Expr%"=="" goto inputExpr
if "%Expr%"=="?" call:ExprHelp & goto inputExpr
if /i "%Expr%"=="Q" goto end
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" echo Input error!& goto inputExpr
echo The calculation result is: %EvalExpr%
if /i "%1"=="/A" goto autosave
:if_save
set SaveEval=
echo.
set /p SaveEval=Do you want to store the expression and calculation result in a file ?
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto inputExpr
goto if_save
:save
set /p SaveEvalPath=Please enter the file path and file name (such as C:\Windows\file.txt. If no path is entered, it means the current folder, leaving it blank is the last input path):
echo "%Expr%"=%EvalExpr% >>"%SaveEvalPath%"
echo The expression and calculation result have been stored in "%SaveEvalPath%"!
goto inputExpr
:autosave
echo "%Expr%"=%EvalExpr% >>"%2"
goto inputExpr
:end
Last edited by willsort on 2005-9-11 at 13:19 ]
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-8-26 11:28 |
|
|
pfox
银牌会员
    
积分 1451
发帖 446
注册 2002-10-20
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
几位用其他语言编写的朋友,能不能随便编译成exe上传?
A few friends who wrote in other languages, can they compile into exe and upload casually?
|

我的下载空间 |
|
2005-8-26 15:01 |
|
|
defrag
中级用户
   痴迷DOS者
积分 456
发帖 570
注册 2004-10-9
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
说过,我那个只能在IDE(急成开发环境)里运行。。。
Said, mine can only run in the IDE (urgently become the development environment)...
|

DOS不是万能的,没有DOS是万万不能的
自古系统谁无死?留取胆清照汗青!uploadImages/20035317345478982.png |
|
2005-8-27 16:43 |
|
|