李开元 秦崩微盘:求助:是PASCAL 的一道解一元一次方程的问题~

来源:百度文库 编辑:高考问答 时间:2024/04/28 22:51:36
呵呵,各位帮帮我哦 ~~~~:)
编写程序,解输入的一元一次方程,将解方程的结果(精确至小数点后三位)输出至屏幕。

你可假设对键入的方程的正确性的判断是由另一个程序员在做,或者说可认为键入的一
元一次方程均为合法的,且有唯一实数解。

样例

输入:
6a-5+1=2-2a

输出:
a=0.750
呵呵呵呵 ~~

program lkkjzp;
var
y,s,st,str:string;
kx,n,kc,k,z:int64;
i,j:longint;
code:integer;
t:real;
begin
readln(s);
while s<>'' do
begin
for i:=1 to length(s) do
if s[i] in ['a'..'z'] then y:=s[i];
j:=1;
kx:=0;
kc:=0;
code:=0;
for i:=1 to pos('=',s) do
begin
if ((s[i]='+') or (s[i]='-') or (s[i]='=')) and(i<>1) then
begin
str:=copy(s,j,i-j);
val(str,k,code);
if code=0 then kc:=kc-k;
if code<>0 then
begin
delete(str,code,1);
if str='' then k:=1 else
val(str,k,code);
if code<>0 then
if str[1]='+' then k:=1 else if str[1]='-'then k:=-1;
kx:=kx+k;
end;
j:=i;
end;
end;
code:=0;
j:=pos('=',s)+1;
for i:=pos('=',s)+1 to length(s) do
begin
if ((s[i]='+') or (s[i]='-') and(s[i-1]<>'=')) or (i=length(s)) then
begin
if i<> length(s) then str:=copy(s,j,i-j) else str:=copy(s,j,i-j+1);
val(str,k,code);
if code=0 then kc:=kc+k;
if code<>0 then
begin
delete(str,code,1);
if str='' then k:=1 else
val(str,k,code);
if code<>0 then
if str[1]='+' then k:=1 else if str[1]='-'then k:=-1;
kx:=kx-k;
end;
j:=i;
end;
end;
writeln(y,'=',kc/kx:0:3);
readln(s);
end;
end.
这是某年noip的题,原题为“计算器的改良”

难哦