Autor Beitrag
Daether
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Mi 26.11.03 17:20 
Hi,
hab ein riesen Problem, morgen shcreiben wir eine Arbeit. Wir sollen bis dahin eine UnitMathe gemacht haben, die Addieren,Subtrahieren,Multiplizieren und Dividieren können muss. Das soll sie allerdings mit Strings machen. Addieren,Subtrahieren und Multiplizieren habe ich schon geschafft, weiß einer wie das mit Dividieren geht?
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Mi 26.11.03 17:28 
Also ich weiß ja nicht, was man heute in der Schule lernt, aber wir haben früher noch gelernt schriftlich zu dividieren. Und dieses Verfahren sollte sich eigentlich auch für Dein Problem eignen.

barfuesser
Daether Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Mi 26.11.03 18:50 
Genau darum geht es, wir sollen ein Programm entwickeln welches "schriftlich" Dividiert.
Krieg momentan immer eine Fehlermeldung.
/Edit HABS GESCHAFFT!!!!!!
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
unit UnitDividieren;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    Edit4: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    hinterKomma :integer;
    function Addieren(Zahl1,Zahl2 : string) : string;
    function Multiplizieren(Zahl1,Zahl2 : string): string;
    function Komplimentaer(Zahl2 : string) : string;
    function Subtrahieren(Zahl1,Zahl2 : String) : String;
    function Dividieren(Zahl1,Zahl2 : String) : String;
    function Vergleich(Zahl1,Zahl2 : String) : String;
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.Addieren(Zahl1,Zahl2 : string) : string;
var  Ergebnis,Teilergebnis,sum1,sum2,ueber : string;
     n,laenge,blocklaenge,help1,help2,helpTeil,helpueber : integer;
begin
blocklaenge:=3;
helpueber:=0;
ueber:='';
Ergebnis:='';
if length(Zahl1)>length(Zahl2)
  then
    begin
      laenge:=length(Zahl1);
        while laenge mod blocklaenge <> 0
          do
            begin
              Zahl1:='0'+Zahl1;
              laenge:=length(Zahl1);
            end;
        while laenge > length(Zahl2)
          do Zahl2:='0'+Zahl2;
    end
  else
    begin
      laenge:=length(Zahl2);
        while laenge mod blocklaenge <> 0
          do
            begin
              Zahl2:='0'+Zahl2;
              laenge:=length(Zahl2);
            end;
        while laenge > length(Zahl1)
          do Zahl1:='0'+Zahl1;
    end;
for n:=1 to length(Zahl1) div blocklaenge do
  begin
    sum1:=copy(Zahl1,length(Zahl1)-3*n+1,3);
    sum2:=copy(Zahl2,length(Zahl2)-3*n+1,3);
    help1:=strtoint(sum1);
    help2:=strtoint(sum2);
    help1:=help1+helpueber ;
    helpTeil:=help1+help2;
    Teilergebnis:=inttostr(helpTeil);
    case helpteil of
    1..9 : Teilergebnis:='00'+inttostr(helpTeil);
    10..99 : Teilergebnis:='0'+inttostr(helpTeil);
    end;
    Teilergebnis:='0'+Teilergebnis;
    if length(Teilergebnis)=4
      then
        begin
          ueber:=copy(Teilergebnis,1,1);
          Teilergebnis:=copy(Teilergebnis,2,3)
        end
      else
        begin
          ueber:=copy(Teilergebnis,2,1);
          Teilergebnis:=copy(Teilergebnis,3,3)
        end;
    helpueber:=strtoint(ueber);
    Ergebnis:=Teilergebnis+Ergebnis;
end;
Ergebnis:=ueber+Ergebnis;
while Ergebnis[1]='0' do
delete(Ergebnis,1,1);
result:=Ergebnis;
end;

{**********************************************************}
function TForm1.komplimentaer(Zahl2:string):string;
var ZahlX,Ergebnis,Teilergebnis: string;
    helpZahl,n,m,helpTeil,helperg: integer;
begin
m:=1;
for n:=1 to length(Zahl2) do
  begin
    zahlX:=copy(Zahl2,m,1);
    helpZahl:=strtoint(ZahlX);
    helpTeil:=9-helpzahl;
    Teilergebnis:=inttostr(helpTeil);
    Ergebnis:=Ergebnis+Teilergebnis;
    m:=m+1;
  end;
result:=Ergebnis;
end;
{-----------------------------------------------------}

function TForm1.Subtrahieren(Zahl1,Zahl2:string):string;
Var Ergebnis:string;
begin
if vergleich(Zahl1,Zahl2)='größer' then
begin
Ergebnis:=addieren(komplimentaer(Zahl1),'1');
Ergebnis:=addieren(Zahl2,Ergebnis);
Ergebnis:=komplimentaer(Ergebnis);
Ergebnis:=addieren(Ergebnis,'1');
if Ergebnis<>'0'then
while Ergebnis[1]='0' do
delete(Ergebnis,1,1);
result:=Ergebnis;
end;
if vergleich(Zahl1,Zahl2)='kleiner' then
begin
Ergebnis:=addieren(komplimentaer(Zahl2),'1');
Ergebnis:=addieren(Zahl1,Ergebnis);
Ergebnis:=komplimentaer(Ergebnis);
Ergebnis:=addieren(Ergebnis,'1');
if Ergebnis<>'0'then
while Ergebnis[1]='0' do
delete(Ergebnis,1,1);
Ergebnis:='-'+Ergebnis;
result:=Ergebnis;
end;
if Zahl1=Zahl2 then
result:='0';
end; 

{**********************************************************}
function TForm1.Multiplizieren(Zahl1,Zahl2 : string) : string;
var I,J,N:integer;
    Ersatz:Array[0..9] of string;
begin
Result:='';
Ersatz[0]:='0';
for I:=1 to 9 do for J:=1 to I do Ersatz[I]:=Addieren(Ersatz[I],Zahl1);
  for N:=1 to Length(Zahl2) do
    begin
    Result:=Addieren(Result+'0',Ersatz[StrToInt(Zahl2[N])]);
    end;
end;

{**********************************************************}
function TForm1.Vergleich(Zahl1,Zahl2 : String) : string;
var i,n:integer;
    helpZahl1,helpZahl2:string;
begin
if length(Zahl1)>length(Zahl2) then
result:='größer';
if length(Zahl1)<length(Zahl2) then
result:='kleiner';
if length(Zahl1)=length(Zahl2) then
begin
n:=1;
 for i:=1 to length(Zahl1) do
  begin
    if Zahl1[n]>Zahl2[n] then
     begin
     result:='größer';
     exit;
     end;
    if Zahl1[n]<Zahl2[n] then
     begin
     result:='kleiner';
     exit;
     end;
    if Zahl1[n]=Zahl2[n] then
     begin
     result:='gleich';
     end;
   n:=n+1;
   end;
end;
;
end;


{**********************************************************}
function TForm1.Dividieren(Zahl1,Zahl2 : string) : string;
Var Divident : array[0..9] of string;
    Ergebnis,ZwischenErgebnis,helpZahl,Divisor : string;
    i,n,j,m : integer;
begin
Ergebnis:='0';
Divident[0]:='0'; Divident[1]:=Zahl2;
Divident[2]:=multiplizieren('2',Zahl2);
Divident[3]:=multiplizieren('3',Zahl2);
Divident[4]:=multiplizieren('4',Zahl2);
Divident[5]:=multiplizieren('5',Zahl2);
Divident[6]:=multiplizieren('6',Zahl2);
Divident[7]:=multiplizieren('7',Zahl2);
Divident[8]:=multiplizieren('8',Zahl2);
Divident[9]:=multiplizieren('9',Zahl2);
Divisor:=copy(Zahl1,j,1);
j:=1;
i:=9;
for n:=1 to length(Zahl1) do
begin
    repeat
    vergleich(Divisor,Divident[i]);
    i:=i-1;
    until
    (vergleich(Divisor,Divident[i])='größer') or
    (vergleich(Divisor,Divident[i])='gleich');
  j:=j+1;
  Divisor:=subtrahieren(Divisor,Divident[i]);
  Divisor:=Divisor+copy(Zahl1,j,1);
  if (Divisor='0')or(Divisor='00')then
  else
  begin
  while Divisor[1]='0' do
  delete(Divisor,1,1);
  end;
  if Divisor='00' then
  Divisor:='0';
  Ergebnis:=Ergebnis+inttostr(i);
  i:=9;
end;
if (Divisor='0')or(Divisor='00')then
  else
  begin
  while Divisor[1]='0' do
  delete(Divisor,1,1);
  end;
if (Ergebnis='0')or(Ergebnis='00')then
  else
  begin
  while Ergebnis[1]='0' do
  delete(Ergebnis,1,1);
  end;
result:=Ergebnis;
end;


{**********************************************************}
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit3.text:={subtrahieren}dividieren(Edit1.text,Edit2.text);
end;


end.