-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD2b8.f90
124 lines (112 loc) · 3.8 KB
/
D2b8.f90
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
program D2b8_EnJnDeSIgn2024
implicit none
character(len=30) :: binary1, binary2, result
integer :: int1, int2, difference, sum
! Function prototypes
!integer function bin2dec(binary)
! implicit none
! character(len=*), intent(in) :: binary
!end function bin2dec
!character(len=30) function dec2bin(decimal)
! implicit none
! integer, intent(in) :: decimal
!end function dec2bin
! Example binary numbers (replace these with actual output from Db4.exe)
! binary1 = "011001011101011000000110011000"
! binary2 = "011001011101011000000110011001"
call execute_command_line('Db8.exe > output7.txt')
call execute_command_line('Db8.exe > output8.txt')
! Open the output files and read the binary numbers
open(unit=10, file='output7.txt', status='old')
read(10, '(A30)') binary1
close(10)
open(unit=11, file='output8.txt', status='old')
read(11, '(A30)') binary2
close(11)
! Print the binary numbers being compared
print *, "Binary1: ", binary1
print *, "Binary2: ", binary2
! Convert binary strings to integers
int1 = bin2dec(binary1)
int2 = bin2dec(binary2)
! Compare the integers
if (int1 == int2) then
print *, "The binary numbers are equal."
! Check if the sum will be too large
if (binary1(1:1) == '1' .and. binary2(1:1) == '1') then
print *, "Error: Sum of binary numbers will be to large."
else
! Add the integers
sum = int1 + int2
! Convert the sum back to binary
result = dec2bin(sum)
print *, "Sum of binary numbers: ", result
end if
else if (int1 > int2) then
print *, "Binary1 is greater than Binary2."
! Subtract the integers
difference = int1 - int2
! Convert the difference back to binary
result = dec2bin(difference)
print *, "Difference of binary numbers: ", result
! Check if the sum is too large
if (binary1(1:1) == '1' .and. binary2(1:1) == '1') then
print *, "Error: Sum of binary numbers will be to large."
else
! Add the integers
sum = int1 + int2
! Convert the sum back to binary
result = dec2bin(sum)
print *, "Sum of binary numbers: ", result
end if
else
print *, "Binary1 is less than Binary2."
! Subtract the integers
difference = int2 - int1
! Convert the difference back to binary
result = dec2bin(difference)
print *, "Difference of binary numbers: ", result
! Check if the sum is too large
if (binary1(1:1) == '1' .and. binary2(1:1) == '1') then
print *, "Error: Sum of binary numbers will be to large."
else
! Add the integers
sum = int2 + int1
! Convert the sum back to binary
result = dec2bin(sum)
print *, "Sum of binary numbers: ", result
end if
end if
contains
! Function to convert binary string to integer
integer function bin2dec(binary)
implicit none
character(len=*), intent(in) :: binary
integer :: i, len
bin2dec = 0
len = len_trim(binary)
do i = 1, len
if (binary(i:i) == '1') then
bin2dec = bin2dec + 2**(len-i)
end if
end do
end function bin2dec
! Function to convert integer to binary string
character(len=30) function dec2bin(decimal)
implicit none
integer, intent(in) :: decimal
integer :: i, temp
character(len=30) :: bin
bin = '000000000000000000000000000000'
temp = decimal
i = 30
do while (temp > 0)
if (mod(temp, 2) == 1) then
bin(i:i) = '1'
end if
temp = temp / 2
i = i - 1
end do
dec2bin = bin
end function dec2bin
end program D2b8_EnJnDeSIgn2024