Biết đến đâu thì làm đến đó bạn nhé.
Ban đầu, với phép cộng, ta viết lệnh:
Mã:
write("Nhap so thu nhat: ");
readln(a);
để nhập số thứ nhất.
tiếp theo viết lệnh:
Mã:
write("Nhap so thu hai: ");
readln(b);
để nhập số thứ hai.
rồi viết lệnh:
Mã:
writeln("Ket qua: a + b = :", a + b);
để trả ra kết quả.
Hoàn thiện chương trình này còn cần:
- Kiểm tra dữ liệu nhập vào có đúng là số hay không?
- Phạm vi của dữ liệu số mà chương trình xử lý được (byte, Integer, word...)
- Lựa chọn phép toán để thực hiện (+, -, x,
.
- Xử lý lỗi toán học (chia cho 0)...
- Môi trường đồ họa.
Thanks bạn nhiều, nhưng mình đã viết được phần mềm tính như vầy (tuy nhiên chỉ tính được phép tính một dấu)
Uses crt;
Label dau;
Const S=['1','2','3','4','5','6','7','8','9','0'];
D=['*','+','-','/','^'];
Ddb=['*','^','/'];
dt=['-','+'];
dau1=['1','2','3','4','5','6','7','8','9','0','*','/','+','-','^'];
Var bt,so1,so2,vt,tl:string;
l,lt,i,m,code,k: integer;
s1,s2,kqdb: qword;
kq: currency;
Begin
repeat
dau: clrscr;
bt:='';
Writeln('Nhap bieu thuc mot dau (nhan, chia, cong, tru, luy thua), neu thoat thi nhap exit: ');
readln(bt);
If bt='exit' then exit;
l:=length(bt);
i:=0;
m:=0;
kq:=0;
s1:=0;
s2:=0;
so1:='';
so2:='';
Repeat
i:=i+1;
If (m=1) and (bt
in ddb) then
Begin
Writeln('Xin loi ban, chuong trinh chi thuc hien phep tinh mot dau!');
readln;
goto dau;
End;
If bt in d then m:=m+1;
If (bt in d) and (bt[i+1] in d) then
Begin
Writeln('Xin loi ban, chuong trinh chi thuc hien phep tinh so duong!');
readln;
goto dau;
End;
If (bt[1]='*') or (bt[1]='/') or (bt[1]='^') then
Begin
writeln('Bieu thuc sai!');
delay(1000);
goto dau;
End;
If (bt in dau1) then read else
Begin
Writeln('Bieu thuc sai!');
delay(1000);
goto dau;
End;
If (bt[l] in d) then
Begin
Writeln('Bieu thuc sai!');
delay(1000);
Goto dau;
End;
If (bt[l] in ddb) and (bt[l+1] in ddb) then
Begin
Writeln('Bieu thuc sai!');
delay(1000);
goto dau;
End;
Until i=l;
for k:=1 to l do
Begin
If bt[k] in d then
begin
lt:=k;
for i:=1 to k-1 do
so1:=so1+bt;
read;
end;
Val(so1,s1,code);
End;
For i:=lt+1 to l do
Begin
so2:=so2+bt;
read;
End;
Val(so2,s2,code);
Case bt[lt] of
'+':
Begin
kq:=s1+s2;
Writeln('= ',kq:0:0);
End;
'-':
Begin
kq:=s1-s2;
writeln('= ',kq:0:0);
End;
'*':
Begin
kq:=s1*s2;
Writeln('= ',kq:0:0);
End;
'/':
Begin
kq:=s1/s2;
Writeln('= ',kq:0:2);
End;
'^':
Begin
If s2=1 then writeln('= ',s1) else
If s2=0 then writeln('= 1') else
Begin
If s2<0 then s2:=-s2;
kq:=1;
i:=0;
repeat
i:=i+1;
kq:=kq*s1;
Until i=s2;
If s2<0 then writeln('= ',1/kq:0:0);
If s2>0 then Writeln('= ',kq:0:0);
End;
End;
End;
If m=0 then writeln('= ',bt);
Writeln('Nhap e de thoat.');
tl:=readkey;
If tl='e' then exit;
Until m=10203;
End.