有何办法动态生成公式?

andygirl 2001-05-24 10:27:00
我正在做一个计价系统,每种产品有一个计价公式,怎样能够每增加一种产品,便动态产生一个公式?能否提供些例子!(公式中包含运算符和各种参数)
...全文
93 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
logical 2001-06-05
  • 打赏
  • 举报
回复
还要不要,我也有。
andygirl 2001-05-25
  • 打赏
  • 举报
回复
to starfish(海星);很有帮助,谢谢你!
andygirl 2001-05-24
  • 打赏
  • 举报
回复
我在网页上有个文本框,接收管理者为新增产品要求的公式,将其存入数据库中,但它只是个字符串,不能根据它来直接进行计算,怎样把它变成机器可执行的运算公式呢?
andygirl 2001-05-24
  • 打赏
  • 举报
回复
同类产品有些相似,但不同类时差别很大。比如有两种产品,公式分别为:
1. 尺寸*数量*面数*系数
2. (版费+加工费)*系数
记得当初学数据结构时,有关栈那部分有讲过,但我不知道应该怎样实现!
GoldWood 2001-05-24
  • 打赏
  • 举报
回复
在DOS下有类式程序,请说的详细一点
windindance 2001-05-24
  • 打赏
  • 举报
回复
你的各公式之间是相似的吗?如果不是那就没有办法了。(最好能举个例子)
starfish 2001-05-24
  • 打赏
  • 举报
回复
说明一下,上面那个程序用了我习惯的结构,主程序只有一个函数fundament,这样所有的变量可以用delphi的local varible看到,省得再使用watch了,这只是个人习惯问题,大家可能觉得不太习惯,这是无关紧要的。
starfish 2001-05-24
  • 打赏
  • 举报
回复
就是一个表达式求值问题么,很简单呀,我这里有个写好的程序,在delphi下面编译通过,如果你需要C++的或java的自己修改一下就可以了。该程序从expr.in文件里读入一系列表达式,对其进行求值,输出结果到expr.out文件中,如果表达式格式错误,程序会输出error!

///////////////////////////////////////////
//
// 表达式求值
//
// copyright starfish
//
///////////////////////////////////////////

{$apptype console}
program expr;

uses
math,
SysUtils,
Windows;

procedure fundament;
const
infile='expr.in';
outfile='expr.out';
var
fin,fout:text;
expression:string;


procedure outprint(x:real);
begin
writeln(fout,x:0:3);
end;

function expr(s:string):real; //计算表达式S
const
maxN=100;
var
res,ch:char;
oprTop,numTop:integer;
opr:array[1..maxN] of char;
num:array[1..maxN] of real;

procedure error;
begin
write(fout,'error!');
exit;
end;

function comp(a,b:char):char;
begin
if not(a in ['+','-','*','/','(',')','#']) or not(b in ['+','-','*','/','(',')','#']) then result:='e'
else
case a of
'+','-': case b of
'*','/','(':result:='<';
else result:='>';
end;
'*','/':if b='(' then result:='<' else result:='>';
'(': if b=')' then result:='=' else if b='#' then result:='e' else result:='<';
')': if b='(' then result:='e' else result:='>';
'#': if b=')' then result:='e' else if b='#' then result:='R' else result:='<';
end;
end;

function cal(a,b:real;p:char):real;
begin
case p of
'+': result:=a+b;
'-': result:=a-b;
'*': result:=a*b;
'/': if b=0 then begin error;result:=0;end else result:=a/b;
end;
end;

function GetNumber(var s:string):real;
var
i,code:integer;
begin
i:=1;
while (i<=length(s))and(s[i] in ['0'..'9','.']) do inc(i);
val(copy(s,1,i-1),result,code);
delete(s,1,i-1);
end;

begin
opr[1]:='#';
s:=s+'#';
oprTop:=1;
numTop:=0;
res:=' ';
repeat
if s[1] in ['0'..'9'] then
begin
inc(numTop);
num[numTop]:=GetNumber(s);
end
else
begin
ch:=s[1];
delete(s,1,1);
repeat
res:=comp(opr[oprTop],ch);
case res of
'>':begin
num[numTop-1]:=cal(num[numTop-1],num[numtop],opr[oprTop]);
dec(numTop);
dec(oprTop);
end;
'<':begin
inc(oprTop);
opr[oprTop]:=ch;
end;
'=':begin
dec(oprTop);
end;
'e':begin
error;
result:=0;
exit;
end;
'R':begin
result:=num[numTop];
end;
end;
until res<>'>';
end;
until res='R';
end;


function init:boolean;
begin
init:=false;
if eof(fin) then exit;
readln(fin,expression);
init:=true;
end;


procedure main;
begin
outprint(expr(expression));
end;


begin
assign(fin,infile);
assign(fout,outfile);
reset(fin);rewrite(fout);

while init do main;

close(fin);
close(fout);
end;

begin
fundament;
end.





33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧