Python Forum
Calculator I need quick help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator I need quick help
#1
calculator (operating on any number system) with the following operations:

- adding positive and negative numbers (i.e. the following combinations "-1 + 2", "-1 + -2", "1 + 2" and "1 + -2" work),

- subtraction of positive numbers with the result greater than or equal to zero (A - B> = 0),

- very simple multiplication of positive and negative numbers ,

- fast multiplication of positive and negative numbers.

maska = ""
for i in range(ord('0'), ord('9') + 1):
    maska += chr(i)
for i in range(ord('A'), ord('Z') + 1):
    maska += chr(i)
def konwersja(liczba, dlugosc):
    return ('0' * (dlugosc - len(liczba))) + liczba
def dodawanie(A, B, system):
    maxdl = max(len(A), len(B))
    A = konwersja(A, maxdl)
    B = konwersja(B, maxdl)
    p = 0
    wynik = ""
    for i in range(-1, -maxdl-1, -1):
        suma = maska.find(A[i]) + maska.find(B[i]) + p
        p = suma // system
        wynik += maska[suma % system]
    wynik = wynik[::-1]
    return wynik
A = input("Podaj pierwsza liczbe:")
B = input("Podaj druga liczbe:")
sys = int(input("Podaj system liczbowy:"))

print(dodawanie(A, B, sys))
def dodawanie(A, B, system):
    maxdl = max(len(A), len(B))
    A = konwersja(A, maxdl)
    B = konwersja(B, maxdl)
    p = 0
    wynik = ""
    for i in range(-1, -maxdl-1, -1):
        suma = maska.find(A[i]) + maska.find(B[i]) + p
        p = suma // system
        wynik += maska[suma % system]
    if p:
        wynik += maska[p]
    wynik = wynik[::-1]
    return wynik
buran write Feb-12-2021, 01:46 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
So, what exactly do you need help with?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
print(eval(input('Enter calculation: ')))
Reply
#4
Can the numbers be entered as decimal?
Czy liczby można wprowadzić ad decimal?
Przykład Binarnym : 5 - 4

Or do you need to enter them in the number system of the output?
Czy trzeba wprowadzić je w systemie liczbowym wyjścia?
Przykład Binarnym : 0b101 - 0b100

If you can enter the numbers in decimal, this code will work.
Jeśli można wprowadzić liczby w liczbie dziesiętnej, ten kod będzie działać.

pierwsz = input (' Podaj pierwsza liczbei : ')
operator = input ('EWprowadź operatora  (+, -, *, /) : ')
Drugi = input (' Wprowadź drugi numer : ')
Odpowiedź = eval (f'{pierwsz} {operator} {Drugi}')
print (Odpowiedź)
print (bin (Odpowiedź))
print (oct (Odpowiedź))
print (hex (Odpowiedź))
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020