0% found this document useful (0 votes)
4 views27 pages

Codigo Calculadora

The document contains SwiftUI code for a simple calculator app. It defines a ContentView struct with buttons for numbers and operations, allowing users to input numbers and perform basic arithmetic operations. The interface includes a text field for displaying the current input and buttons styled as circles for each number and operation.

Uploaded by

eliminador354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

Codigo Calculadora

The document contains SwiftUI code for a simple calculator app. It defines a ContentView struct with buttons for numbers and operations, allowing users to input numbers and perform basic arithmetic operations. The interface includes a text field for displaying the current input and buttons styled as circles for each number and operation.

Uploaded by

eliminador354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

//

// ContentView.swift
// Practica14_2025
//
// Created by Victor
Manuel Calderon Hernandez
on 28/03/25.
//

import SwiftUI

struct ContentView: View {


@State var pantalla =
""
@State var tamCirculo
= 80.0
@State var tamTexto =
40.0
@State var operacion =
""
@State var num1 = 0
@State var num2 = 0
var body: some View {
VStack {
TextField("",
text: $pantalla)

.background(.gray.opacity(
0.3))

.font(.system(size: 40))

.foregroundColor(.black)

.cornerRadius(20)
.multilineTextAlignment(.t
railing)
HStack{

Button(action: {

pantalla = pantalla + "7"


}, label:
{

Circle()

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("7")
.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})

Button(action: {

pantalla = pantalla + "8"


}, label:
{

Circle()

.frame(width: tamCirculo,
height: tamCirculo)
.overlay(content: {

Text("8")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})

Button(action: {

pantalla = pantalla + "9"


}, label:
{
Circle()

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("9")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})

Button(action: {
num1 =
Int(pantalla)!

operacion = "/"

pantalla = ""

}, label:
{

Circle()

.fill(Color.orange)

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {
Text("/")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})
}
HStack{

Button(action: {

pantalla = pantalla + "4"


}, label:
{
Circle()

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("4")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})

Button(action: {
pantalla = pantalla + "5"
}, label:
{

Circle()

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("5")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})
})

Button(action: {

pantalla = pantalla + "6"


}, label:
{

Circle()

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("6")

.foregroundColor(.white)
.font(.system(size:
tamTexto))
})

})

Button(action: {
num1 =
Int(pantalla)!

operacion = "*"

pantalla = ""
}, label:
{

Circle()
.fill(Color.orange)

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("*")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})
}
HStack{
Button(action: {

pantalla = pantalla + "1"


}, label:
{

Circle()

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("1")

.foregroundColor(.white)
.font(.system(size:
tamTexto))
})

})

Button(action: {

pantalla = pantalla + "2"


}, label:
{

Circle()

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {
Text("2")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})

Button(action: {

pantalla = pantalla + "3"


}, label:
{

Circle()
.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("3")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})

Button(action: {
num1 =
Int(pantalla)!
operacion = "-"

pantalla = ""
}, label:
{

Circle()

.fill(Color.orange)

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("-")

.foregroundColor(.white)
.font(.system(size:
tamTexto))
})

})
}
HStack{

Button(action: {
num2 =
Int(pantalla)!
switch
operacion{
case
"/":

pantalla = String(num1 /
num2)
break
case
"*":

pantalla = String(num1 *
num2)

break
case
"-":

pantalla = String(num1 -
num2)

break
case
"+":
pantalla = String(num1 +
num2)

break

default:

break
}

}, label:
{

Circle()
.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("=")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})

Button(action: {

pantalla = pantalla + "0"


}, label:
{

Circle()

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("0")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})
Button(action: {

pantalla = ""
num1 =
0
num2 =
0

operacion = ""

}, label:
{

Circle()

.frame(width: tamCirculo,
height: tamCirculo)
.overlay(content: {

Text("C")

.foregroundColor(.white)

.font(.system(size:
tamTexto))
})

})

Button(action: {
num1 =
Int(pantalla)!

operacion = "+"
pantalla = ""
}, label:
{

Circle()

.fill(Color.orange)

.frame(width: tamCirculo,
height: tamCirculo)

.overlay(content: {

Text("+")

.foregroundColor(.white)
.font(.system(size:
tamTexto))
})

})
}
}
//Spacer()
.padding()
}
}

#Preview {
ContentView()
}

You might also like