SmileAtMe
11-06-2006, 03:58 PM
I have write program in pascal, but don't knew how to write it in c++:confused: :confused: :confused: . My program should cout two complex numbers ( +, -, *, / ). in program I shoud use 'struct' and function. Can you help me?
my program in pascal:
program kompleksiniai_skaiciai;
type kompleksinis = record
r, m: real;
end;
var pasirinkimas: integer;
sk1, sk2, rez: kompleksinis;
procedure sum(var sk1, sk2, rez:kompleksinis);
begin
rez.r:=sk1.r+sk2.r;
rez.m:=sk1.m+sk2.m;
end;
procedure atimt(sk1, sk2, rez:kompleksinis);
begin
rez.r:=sk1.r-sk2.r;
rez.m:=sk1.m-sk2.m;
end;
procedure daug(sk1, sk2, rez:kompleksinis);
begin
rez.r:=sk1.r*sk2.r-sk1.m*sk2.m;
rez.m:=sk1.r*sk2.m+sk1.m*sk2.r;
end;
procedure dal(sk1, sk2, rez:kompleksinis);
begin
rez.r:=(sk1.r*sk2.r+sk1.m*sk2.m)/(sk2.r*sk2.r+sk2.m*sk2.m);
rez.m:=(sk1.m*sk2.r-sk1.r*sk2.m)/(sk2.r*sk2.r+sk2.m*sk2.m);
end;
begin
writeln ('Actions with complex numbers.');
writeln ('If you want to end program print 0');
writeln ('If you want to plus complex numbers 1,');
writeln ('If you want to subtract print 2, if multiply 3, if '/' print 4');
readln (pasirinkimas);
while pasirinkimas <> 0 do
begin
writeln ('Print first complex number real part');
readln (sk1.r);
writeln ('Print first complex number imaginary part');
readln (sk1.m);
writeln ('Print second complex number real part');
readln (sk2.r);
writeln ('Print second complex number imaginary part');
readln (sk2.m);
if pasirinkimas=1
then sum(sk1, sk2, rez);
if pasirinkimas=2
then atimt(sk1, sk2,rez);
if pasirinkimas=3
then daug(sk1, sk2,rez);
if pasirinkimas=4
then dal(sk1, sk2,rez);
writeln ('kompleksiniu skaiciu suma yra lygi:', rez.r:5:2, '+',
rez.m:5:2, 'i');
writeln ('If you want to end program print 0');
writeln ('If you want to plus complex numbers 1,');
writeln ('If you want to subtract print 2, if multiply 3, if '/' print 4');
readln (pasirinkimas);
end;
end.
my program in pascal:
program kompleksiniai_skaiciai;
type kompleksinis = record
r, m: real;
end;
var pasirinkimas: integer;
sk1, sk2, rez: kompleksinis;
procedure sum(var sk1, sk2, rez:kompleksinis);
begin
rez.r:=sk1.r+sk2.r;
rez.m:=sk1.m+sk2.m;
end;
procedure atimt(sk1, sk2, rez:kompleksinis);
begin
rez.r:=sk1.r-sk2.r;
rez.m:=sk1.m-sk2.m;
end;
procedure daug(sk1, sk2, rez:kompleksinis);
begin
rez.r:=sk1.r*sk2.r-sk1.m*sk2.m;
rez.m:=sk1.r*sk2.m+sk1.m*sk2.r;
end;
procedure dal(sk1, sk2, rez:kompleksinis);
begin
rez.r:=(sk1.r*sk2.r+sk1.m*sk2.m)/(sk2.r*sk2.r+sk2.m*sk2.m);
rez.m:=(sk1.m*sk2.r-sk1.r*sk2.m)/(sk2.r*sk2.r+sk2.m*sk2.m);
end;
begin
writeln ('Actions with complex numbers.');
writeln ('If you want to end program print 0');
writeln ('If you want to plus complex numbers 1,');
writeln ('If you want to subtract print 2, if multiply 3, if '/' print 4');
readln (pasirinkimas);
while pasirinkimas <> 0 do
begin
writeln ('Print first complex number real part');
readln (sk1.r);
writeln ('Print first complex number imaginary part');
readln (sk1.m);
writeln ('Print second complex number real part');
readln (sk2.r);
writeln ('Print second complex number imaginary part');
readln (sk2.m);
if pasirinkimas=1
then sum(sk1, sk2, rez);
if pasirinkimas=2
then atimt(sk1, sk2,rez);
if pasirinkimas=3
then daug(sk1, sk2,rez);
if pasirinkimas=4
then dal(sk1, sk2,rez);
writeln ('kompleksiniu skaiciu suma yra lygi:', rez.r:5:2, '+',
rez.m:5:2, 'i');
writeln ('If you want to end program print 0');
writeln ('If you want to plus complex numbers 1,');
writeln ('If you want to subtract print 2, if multiply 3, if '/' print 4');
readln (pasirinkimas);
end;
end.