En esta sección se resolverán dudas sobre C sharp, el lenguaje de programación de Microsoft, compatible con Unity
Moderator: julianmartinez16
-
xacarana
- Site Admin
- Posts: 1029
- Joined: Fri Jan 15, 2016 6:13 pm
Post
by xacarana » Wed Oct 19, 2016 11:00 am
Funciones para crear una calculadora
Ejercicios
- Crear las funciones Restar, Multiplicar y Dividir
- Terminar la programación del menú
- Imprimir la respuesta de las operaciones
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calculadora
{
class Program
{
public static double Sumar(double a, double b)
{
return a + b;
}
public static void Menu()
{
Console.WriteLine("Ingrese una opción así:");
Console.WriteLine(" 1. Sumar");
Console.WriteLine(" 2. Restar");
Console.WriteLine(" 3. Multiplicar");
Console.WriteLine(" 4. Dividir");
Console.WriteLine("-1. Salir");
}
public static int PedirDatoMenu()
{
string temp = "";
int ans = 0;
bool dato_leido_correctamente = false;
while(!dato_leido_correctamente)
{
Console.WriteLine("Ingrese una opción");
temp = Console.ReadLine();
if (int.TryParse(temp,out ans))
{
dato_leido_correctamente = true;
}
}
return ans;
}
public static double LeerNumeroDecimal()
{
string temp = "";
double ans = 0;
bool dato_leido_correctamente = false;
while (!dato_leido_correctamente)
{
Console.WriteLine("Ingrese un número");
temp = Console.ReadLine();
if (double.TryParse(temp, out ans))
{
dato_leido_correctamente = true;
}
}
return ans;
}
public static void RealizarOperacion(int opc)
{
double a, b,c;
switch (opc)
{
case 1: a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Sumar(a, b);
break;
}
}
static void Main(string[] args)
{
int opc = 0;
//Console.WriteLine(Sumar(5, 4.3));
do
{
Menu();
opc = PedirDatoMenu();
if (opc != -1)
{
RealizarOperacion(opc);
}
} while (opc != -1);
Console.ReadKey();
}
}
}
Andrés Bedoya Tobón
Profesor
"I only smile in the dark, I only smile when it's complicated" Raybiez
-
David Ballesteros V
- Posts: 61
- Joined: Fri Jul 22, 2016 11:04 am
Post
by David Ballesteros V » Wed Oct 19, 2016 11:35 am
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static double Sumar(double a, double b)
{
return a + b;
}
public static double Resta(double a, double b)
{
return a - b;
}
public static double Multiplicacion(double a, double b)
{
return a * b;
}
public static double Division(double a, double b)
{
return a / b;
}
public static void Menu()
{
Console.WriteLine(" 1. Sumar");
Console.WriteLine(" 2. Restar");
Console.WriteLine(" 3. Multiplicar");
Console.WriteLine(" 4. Dividir");
Console.WriteLine("-1. Salir");
}
public static int PedirDatoMenu()
{
string temp = "";
int ans = 0;
bool dato_leido_correctamente = false;
while(!dato_leido_correctamente)
{
Console.WriteLine(" -Ingrese una opción");
temp = Console.ReadLine();
if (int.TryParse(temp, out ans))
{
dato_leido_correctamente = true;
}
else
{
Console.WriteLine("Opcion invalida");
}
}
return ans;
}
public static double LeerNumeroDecimal()
{
string temp = "";
double ans = 0;
bool dato_leido_correctamente = false;
while (!dato_leido_correctamente)
{
Console.WriteLine(" -Ingrese un número");
temp = Console.ReadLine();
if (double.TryParse(temp, out ans))
{
dato_leido_correctamente = true;
}
else
{
Console.WriteLine("Dato invalido");
}
}
return ans;
}
public static void RealizarOperacion(int opc)
{
double a, b, c;
switch (opc)
{
case 1: a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Sumar(a, b);
Console.WriteLine(" La suma es: " + c);
break;
case 2: a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Resta(a, b);
Console.WriteLine(" La resta es: " + c);
break;
case 3: a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Multiplicacion(a, b);
Console.WriteLine(" La multiplicacion es: " + c);
break;
case 4: a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Division(a, b);
if (b != 0)
{
Console.WriteLine(" La division es: " + c);
}
else
{
Console.WriteLine("Numero invalido");
}
break;
}
}
static void Main(string[] args)
{
int opc = 0;
do
{
Menu();
opc = PedirDatoMenu();
if (opc != -1)
{
RealizarOperacion(opc);
}
} while (opc != -1);
Console.ReadKey();
}
}
}
-
ManuelTheo
- Posts: 22
- Joined: Fri Jul 22, 2016 10:11 am
Post
by ManuelTheo » Wed Oct 19, 2016 11:40 am
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calculadora
{
class Program
{
public static double Sumar(double a, double b)
{
return a + b;
}
public static double Restar(double a, double b)
{
return a - b;
}
public static double Multiplicar(double a, double b)
{
return a * b;
}
public static double Dividir(double a, double b)
{
return a / b;
}
public static void Menu()
{
Console.WriteLine("Ingrese una opción así:");
Console.WriteLine(" 1. Sumar");
Console.WriteLine(" 2. Restar");
Console.WriteLine(" 3. Multiplicar");
Console.WriteLine(" 4. Dividir");
Console.WriteLine("-1. Salir");
}
public static int PedirDatoMenu()
{
string temp = "";
int ans = 0;
bool dato_leido_correctamente = false;
while (!dato_leido_correctamente)
{
Console.WriteLine("Ingrese una opción");
temp = Console.ReadLine();
if (int.TryParse(temp, out ans))
{
dato_leido_correctamente = true;
}
}
return ans;
}
public static double LeerNumeroDecimal()
{
string temp = "";
double ans = 0;
bool dato_leido_correctamente = false;
while (!dato_leido_correctamente)
{
Console.WriteLine("Ingrese un número");
temp = Console.ReadLine();
if (double.TryParse(temp, out ans))
{
dato_leido_correctamente = true;
}
}
return ans;
}
public static void RealizarOperacion(int opc)
{
double a, b, c;
switch (opc)
{
case 1:
a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Sumar(a, b);
Console.WriteLine(c);
break;
case 2:
a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Restar(a, b);
Console.WriteLine(c);
break;
case 3:
a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Multiplicar(a, b);
Console.WriteLine(c);
break;
case 4:
a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Dividir(a, b);
Console.WriteLine(c);
break;
default:
Console.WriteLine("El Valor es incorrecto");
break;
}
}
static void Main(string[] args)
{
int opc = 0;
do
{
Menu();
opc = PedirDatoMenu();
if (opc != -1)
{
RealizarOperacion(opc);
}
} while (opc != -1);
Console.ReadKey();
}
}
}
-
Agustín gutierrez
- Posts: 39
- Joined: Fri Jul 22, 2016 10:11 am
Post
by Agustín gutierrez » Wed Oct 19, 2016 11:44 am
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
public static double Sumar (double a, double b)
{
return a + b;
}
public static double Restar (double a, double b)
{
return a - b;
}
public static double Multiplicar(double a, double b)
{
return a * b;
}
public static double Dividir(double a, double b)
{
return a/b;
}
public static void Menu()
{
Console.WriteLine("ingrese una opción así:");
Console.WriteLine("1 para sumar.");
Console.WriteLine("2 para restar");
Console.WriteLine("3 para multiplicar");
Console.WriteLine("4 para dividir");
Console.WriteLine("-1 para salir");
}
public static int LeerDatoMenu()
{
string dato = "";
int ans = 0;
bool buena_lectura = false;
while(!buena_lectura)
{
Console.WriteLine("Ingrese una opción.");
dato = Console.ReadLine();
if(int.TryParse(dato, out ans))
{
buena_lectura = true;
}
}
return ans;
}
public static double LeerNumeroDecimal()
{
string dato = "";
double ans = 0;
bool buena_lectura = false;
while (!buena_lectura)
{
Console.WriteLine("Ingrese un número");
dato = Console.ReadLine();
if (double.TryParse(dato, out ans))
{
buena_lectura = true;
}
}
return ans;
}
public static void RealizarOperacion(int opc)
{
double a, b, c;
switch (opc)
{
case 1:
a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Sumar(a, b);
Console.WriteLine("La suma es:" + c);
break;
case 2:
a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Restar(a, b);
Console.WriteLine("La resta es:" + c);
break;
case 3:
a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
c = Multiplicar(a, b);
Console.WriteLine("La multiplicación es:" + c);
break;
case 4:
a = LeerNumeroDecimal();
b = LeerNumeroDecimal();
if(b!=0)
{
c = Dividir(a, b);
Console.WriteLine("La división es:" + c);
}
else
{
Console.WriteLine("Error, no se puede realizar una división entre cero");
}
break;
}
}
static void Main(string[] args)
{
int opc = 0;
do
{
Menu();
opc = LeerDatoMenu();
if (opc != -1)
{
RealizarOperacion(opc);
}
} while (opc != -1);
Console.ReadKey();
}
}
}
Agustín Gutiérrez
Fundamentos de programación