Commit a39aad93 authored by David Duran Prieto's avatar David Duran Prieto
Browse files

Práctica finalizada

parent d2a93477
......@@ -3,28 +3,30 @@
import sys
from calcoohija import CalculadoraHija
def plus(op1, op2):
""" Function to sum the operands """
return op1 + op2
def minus(op1, op2):
""" Function to substract the operands """
return op1 - op2
ddp = CalculadoraHija()
if __name__ == "__main__":
try:
operando1 = int(sys.argv[1])
operando2 = int(sys.argv[3])
except ValueError:
sys.exit("Error: Non numerical parameters")
if sys.argv[2] == "suma":
result = plus(operando1, operando2)
elif sys.argv[2] == "resta":
result = minus(operando1, operando2)
else:
sys.exit('Operación sólo puede ser sumar o restar.')
print(result)
try:
operando1 = int(sys.argv[1])
operando2 = int(sys.argv[3])
except ValueError:
sys.exit("Error: Non numerical parameters")
if sys.argv[2] == "suma":
result = ddp.plus(operando1, operando2)
elif sys.argv[2] == "resta":
result = ddp.minus(operando1, operando2)
elif sys.argv[2] == "multiplica":
result = ddp.mult(operando1, operando2)
elif sys.argv[2] == "divide":
if operando2 != 0:
result = ddp.divide(operando1, operando2)
else:
sys.exit("Error: Division by zero is not allowed")
else:
sys.exit('Operación sólo puede ser sumar, restar, multiplicar o dividir')
print(result)
File added
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
class Calculadora:
def plus(self, op1, op2):
""" Function to sum the operands """
return op1 + op2
def minus(self, op1, op2):
""" Function to substract the operands """
return op1 - op2
File added
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from calcoo import Calculadora
class CalculadoraHija(Calculadora):
def mult(self, op1, op2):
""" Function to multiply the operands """
return op1 * op2
def divide(self, op1, op2):
""" Function to divide the operands """
return op1 / op2
File added
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from calcoohija import CalculadoraHija
ddp = CalculadoraHija()
if __name__ == "__main__":
fich = open('fichero.csv','r')
lines = fich.readlines()
for line in lines:
trozo=line.split(',')
resultado=int(trozo[1])
if (line[:line.find(',')]) == "suma":
for n in range(2,len(trozo)):
resultado = ddp.plus(resultado, int(trozo[n]))
print(resultado)
elif (line[:line.find(',')]) == "resta":
for n in range(2,len(trozo)):
resultado = ddp.minus(resultado, int(trozo[n]))
print(resultado)
elif (line[:line.find(',')]) == "multiplica":
for n in range(2,len(trozo)):
resultado = ddp.mult(resultado, int(trozo[n]))
print(resultado)
elif (line[:line.find(',')]) == "divide":
for n in range(2,len(trozo)):
if int(trozo[n]) != 0:
resultado = ddp.divide(resultado, int(trozo[n]))
else:
sys.exit("Error: Division by zero is not allowed")
print(resultado)
else:
print ("La operacion no es valida para nuestra calculadora.")
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import csv
from calcoohija import CalculadoraHija
ddp = CalculadoraHija()
with open('fichero.csv','r') as d
lines = csv.lines(d)
for row in lines:
trozo=row.split(',')
resultado=int(trozo[1])
if (row[:row.find(',')]) == "suma":
for n in range(2,len(trozo)):
resultado = ddp.plus(resultado, int(trozo[n]))
print(resultado)
elif (row[:row.find(',')]) == "resta":
for n in range(2,len(trozo)):
trozo=row.split(',')
resultado = ddp.minus(resultado, int(trozo[n]))
print(resultado)
elif (row[:row.find(',')]) == "multiplica":
for n in range(2,len(trozo)):
trozo=row.split(',')
resultado = ddp.mult(resultado, int(trozo[n]))
print(resultado)
elif (row[:row.find(',')]) == "divide":
for n in range(2,len(trozo)):
trozo=row.split(',')
if int(trozo[n]) != 0:
resultado = ddp.divide(resultado, int(trozo[n]))
else:
sys.exit("Error: Division by zero is not allowed")
print(resultado)
else:
print ("La operacion no es valida para nuestra calculadora.")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment