ACTIVIDAD AUTÓNOMA
1.
Elaborar un
mentefacto de cadena
2. Valores de cadenas constantes
Cadena: Una cadena es un objeto de tipo String cuyo valor es texto. Internamente, el texto se
almacena como una colección secuencial de solo lectura de objetos Char. Al final de una cadena de C# no hay un carácter
null de terminación; por lo tanto, una cadena de C# puede contener cualquier
número de caracteres null incrustados ('\0').
Puede declarar e inicializar cadenas de varias
maneras, como se muestra en el ejemplo siguiente:
C#
// Declare
without initializing.
string message1;
// Initialize
to null.
string message2 = null;
// Initialize
as an empty string.
// Use the
Empty constant instead of the literal "".
string message3 =
System.String.Empty;
//Initialize
with a regular string literal.
string oldPath = "c:\\Program
Files\\Microsoft Visual Studio 8.0";
// Initialize
with a verbatim string literal.
string newPath = @"c:\Program
Files\Microsoft Visual Studio 9.0";
// Use
System.String if you prefer.
System.String
greeting = "Hello
World!";
// In local
variables (i.e. within a method body)
// you can use
implicit typing.
var temp = "I'm
still a strongly-typed System.String!";
// Use a const
string to prevent 'message4' from
// being used
to store another string value.
const string message4 = "You
can't get rid of me!";
// Use the
String constructor only when creating
// a string
from a char*, char[], or sbyte*. See
// System.String
documentation for details.
char[] letters = { 'A', 'B', 'C' };
string alphabet = new string(letters);
Las cadenas son
secuencias arbitrarias de caracteres
ASCII limitadas por comillas simples (" ' ", pe. 'Esto es
una cadena') SQL92 permite que las comillas simples puedan estar incluidos en una
cadena tecleando dos comillas simples adyacentes (pe. 'Dianne''s
horse').
En Postgres las comillas simples deben estar
precedidas por una contra barra ("\", pe.. 'Dianne\'s
horse').
para incluir una contra barra en una constante de tipo cadena, teclear dos
contra barras. Los caracteres no imprimibles también
deben incluir en la cadena precedida de una contra barra (pe’\tab’.
3. Funciones necesarias
para el manejo de cadenas adecuadas para
el desarrollo de la aplicación informática.
Manipulación
de cadenas
La clase String de .NET Framework
proporciona muchos métodos integrados para facilitar la comparación y
manipulación de cadenas. Ahora resulta sencillo obtener datos acerca de una
cadena, o crear nuevas cadenas mediante la manipulación de las cadenas
actuales. El lenguaje Visual Basic .NET también tiene métodos inherentes
que duplican muchas de estas funcionalidades.
Tipos de métodos de manipulación de cadenas
En esta sección se describen diferentes formas de
analizar y manipular cadenas. Algunos métodos son parte del lenguaje Visual
Basic, mientras que otros son inherentes a la clase String.
Los métodos de Visual Basic .NET se utilizan
como funciones inherentes al lenguaje. Pueden utilizarse sin calificación en el
código. En el siguiente ejemplo se muestra el uso habitual de un comando de
manipulación de cadenas de Visual Basic .NET:
Dim aString As String = "SomeString"
Dim bString As String
bString = Mid(aString, 3, 3)
En este ejemplo, la función Mid realiza una
operación directa en
aString y asigna el valor a bString.
También puede manipular cadenas con los métodos de
la clase String. Existen dos tipos de métodos en String: métodos compartidos
y métodos de instancia.
Un método compartido es un método que se deriva de
la propia clase String y no necesita una instancia de dicha clase para
funcionar. Estos métodos se pueden calificar con el nombre de la clase (String)
en vez de con una instancia de dicha clase. Por
ejemplo:
Dim aString As String
bString = String.Copy("A literal string")
En el ejemplo anterior, el método String.Copy
es un método estático, que actúa sobre una expresión dada y asigna el valor
resultante a
bString.
En contraste, los métodos de instancia se derivan
de una instancia concreta de String y deben calificarse con el nombre de
la instancia. Por ejemplo:
Dim aString As String = "A String"
Dim bString As String
bString = aString.SubString(2,6) ' bString = "String"
En este ejemplo, el método SubString es un
método de la instancia de String (es decir,
aString). Realiza una operación en aString y asigna ese valor a bString.
To
string: ToString()
Al igual que todos los objetos derivados de Object, las cadenas proporcionan el método ToString, que convierte un valor en una cadena. Este método se puede utilizar para convertir valores numéricos en cadenas de la siguiente manera.
int year = 1999;
string msg = "Eve was born in " + year.ToString();
System.Console.WriteLine(msg); // outputs "Eve was born in 1999"
Problema 2
using System;
using System.Globalization; // Important
class Program
{
static void Main()
{
int a = 4000;
int b = 654;
double c = 453.4;
double d = 50000.55555;
string a1 = a.ToString();
string a2 = a.ToString(NumberFormatInfo.InvariantInfo);
Console.WriteLine(a1 + " " + a2);
string b1 = b.ToString();
string b2 = b.ToString(NumberFormatInfo.InvariantInfo);
Console.WriteLine(b1 + " " + b2);
string c1 = c.ToString();
string c2 = c.ToString(NumberFormatInfo.InvariantInfo);
Console.WriteLine(c1 + " " + c2);
string d1 = d.ToString();
string d2 = d.ToString(NumberFormatInfo.InvariantInfo);
Console.WriteLine(d1 + " " + d2);
}
}
Replace:
Replace(),, Split() y Trim().
string s3 = "Visual C# Express";
System.Console.WriteLine(s3.Substring(7, 2)); // outputs "C#"
System.Console.WriteLine(s3.Replace("C#", "Basic")); // outputs "Visual Basic Express"
También es posible copiar los caracteres en una matriz de caracteres, tal como se muestra a continuación:
string s4 = "Hello, World";
char[] arr = s4.ToCharArray(0, s4.Length);
foreach (char c in arr)
{
System.Console.Write(c); // outputs "Hello, World"
}
Puede obtener acceso a los caracteres individuales de una cadena con un índice: string s5 = "Printing
backwards";
for (int i = 0; i < s5.Length; i++)
{
System.Console.Write(s5[s5.Length - i - 1]); // outputs "sdrawkcab gnitnirP"
}
Ejemplo 3
using System;
class Program
{
static void Main()
{
const string s = "Darth Vader is scary.";
Console.WriteLine(s);
// Note:
// You must assign the result of Replace to a new string.
string v = s.Replace("scary", "not scary");
Console.WriteLine(v);
}
}
Al igual que todos los objetos derivados de Object, las cadenas proporcionan el método ToString, que convierte un valor en una cadena. Este método se puede utilizar para convertir valores numéricos en cadenas de la siguiente manera.
int year = 1999;
string msg = "Eve was born in " + year.ToString();
System.Console.WriteLine(msg); // outputs "Eve was born in 1999"
Problema 2
using System;
using System.Globalization; // Important
class Program
{
static void Main()
{
int a = 4000;
int b = 654;
double c = 453.4;
double d = 50000.55555;
string a1 = a.ToString();
string a2 = a.ToString(NumberFormatInfo.InvariantInfo);
Console.WriteLine(a1 + " " + a2);
string b1 = b.ToString();
string b2 = b.ToString(NumberFormatInfo.InvariantInfo);
Console.WriteLine(b1 + " " + b2);
string c1 = c.ToString();
string c2 = c.ToString(NumberFormatInfo.InvariantInfo);
Console.WriteLine(c1 + " " + c2);
string d1 = d.ToString();
string d2 = d.ToString(NumberFormatInfo.InvariantInfo);
Console.WriteLine(d1 + " " + d2);
}
}
Replace:
Replace(),, Split() y Trim().
string s3 = "Visual C# Express";
System.Console.WriteLine(s3.Substring(7, 2)); // outputs "C#"
System.Console.WriteLine(s3.Replace("C#", "Basic")); // outputs "Visual Basic Express"
También es posible copiar los caracteres en una matriz de caracteres, tal como se muestra a continuación:
string s4 = "Hello, World";
char[] arr = s4.ToCharArray(0, s4.Length);
foreach (char c in arr)
{
System.Console.Write(c); // outputs "Hello, World"
}
Puede obtener acceso a los caracteres individuales de una cadena con un índice: string s5 = "Printing
backwards";
for (int i = 0; i < s5.Length; i++)
{
System.Console.Write(s5[s5.Length - i - 1]); // outputs "sdrawkcab gnitnirP"
}
Ejemplo 3
using System;
class Program
{
static void Main()
{
const string s = "Darth Vader is scary.";
Console.WriteLine(s);
// Note:
// You must assign the result of Replace to a new string.
string v = s.Replace("scary", "not scary");
Console.WriteLine(v);
}
}
Ejercicios propuestos.
1)
Escribir un programa que pida 10 números enteros por teclado y que imprima por
pantalla:
i. Cuántos
de esos números son pares.
ii.Cuál
es el valor del número máximo.
iii.Cuál
es el valor del número mínimo.
namespace numeros_enteros
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.Clear();
Console.WriteLine("Sistemas 3/ Josiel Ramos");
Console.WriteLine("\n\n\t\tNúmeros pares, impares, máximo y elmentos del array");
Console.WriteLine("\n\nIngrese 10 números:");
int[] val = new int[10];
int a, b, c, d, e, f, g, h, i, j;
int count=0;
a = val[0]; b = val[1]; c = val[2]; d = val[3]; e = val[4]; f = val[5]; g = val[6]; h= val[7]; i=val[8]; j=val[9];
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = int.Parse(Console.ReadLine());
d = int.Parse(Console.ReadLine());
e = int.Parse(Console.ReadLine());
f = int.Parse(Console.ReadLine());
g = int.Parse(Console.ReadLine());
h = int.Parse(Console.ReadLine());
i = int.Parse(Console.ReadLine());
j = int.Parse(Console.ReadLine());
//Cantidad de números pares
if (a % 2 == 0)
{
count = count + 1;
} if (b % 2 == 0)
{
count+=1;
}
if (c % 2 == 0)
{
count += 1;
}
if (d % 2 == 0)
{count += 1;}
if (e % 2 == 0)
{ count += 1; }
if (f % 2 == 0)
{ count += 1; }
if (g % 2 == 0)
{ count += 1; }
if (h % 2 == 0)
{ count += 1; }
if (i % 2 == 0)
{ count += 1; }
if (j % 2 == 0)
{ count += 1; }
Console.WriteLine("\nUsted ha ingresado " + count + " Números pares");
//VALOR del número máximo
if (a > b & a > c & a > d & a > e & a > f & a > g & a > h & a > i & a > j )
{
Console.WriteLine("\nNúmero máximo: " +a);
Console.WriteLine("\nValor del número máximo: 0");
}
else if (b > a & b > c & b > d & b > e & b > f & b > g & b > h & b > i & b > j)
{
Console.WriteLine("\nNúmero máximo: " + b);
Console.WriteLine("\nValor del número minimomáximo: 1");
}
else if (c > a & c >b & c > d & c > e & c > f & c > g & c > h & c > i & c > j )
{
Console.WriteLine("\nNúmero máximo: " + c);
Console.WriteLine("\nValor del número máximo: 2");
}
else if (d > a & d > b & d > c & d > e & d > f & d > g & d > h & d > i & d > j)
{
Console.WriteLine("\nNúmero máximo: " + d);
Console.WriteLine("\nValor del número máximo: 3");
}
else if (e > a & e > b & e > c & e > d & d > f & d > g & d > h & d > i & d > j)
{
Console.WriteLine("\nNúmero máximo: " + e);
Console.WriteLine("\nValor del número máximo: 4");
}
else if (f < a & f < b & f < c & f < d & f < e & f > g & f > h & f > i & f > j)
{
Console.WriteLine("\nNúmero máximo: " + f);
Console.WriteLine("\nValor del número máximo: 5");
}
else if (g > a & g > b & g > d & g > e & g > f & g > h & g > i & g > c & g > j)
{
Console.WriteLine("\nNúmero máximo: " + g);
Console.WriteLine("\nValor del número máximo: 6");
}
else if (h > a & h > b & h > c & h > d & h > e & h > g & h > f & h > i & h > j)
{
Console.WriteLine("\nNúmero máximo: " + h);
Console.WriteLine("\nValor del número máximo: 7");
}
else if (i > a & i > b & i > d & i > e & i > f & i > g & i > h & i > c & i > j)
{
Console.WriteLine("\nNúmero máximo: " + i);
Console.WriteLine("\nValor del número máximo: 8");
}
else if (j > a & j > b & j > d & j > e & j > f & j > f & j > h & j > i & j > c)
{
Console.WriteLine("\nNúmero máximo: " + j);
Console.WriteLine("\nValor del número máximo: 9");
} //Valor del número minimo
if (a < b & a < c & a < d & a < e & a < f & a < g & a < h & a < i & a < j)
{
Console.WriteLine("\nNúmero mínimo: " + a);
Console.WriteLine("\nValor del número mínimo: 0");
}
else if (b < a & b < c & b < d & b < e & b < f & b < g & b < h & b < i & b < j)
{
Console.WriteLine("\nNúmero mínimo: " + b);
Console.WriteLine("\nValor del número mínimo: 1");
}
else if (c < a & c < b & c < d & c < e & c < f & c < g & c < h & c < i & c < j)
{
Console.WriteLine("\nNúmero mínimo: " + c);
Console.WriteLine("\nValor del número mínimo: 2");
}
else if (d < a & d < b & d < c & d < e & d < f & d < g & d < h & d < i & d < j)
{
Console.WriteLine("\nNúmero mínimo: " + d);
Console.WriteLine("\nValor del número mínimo: 3");
}
else if (e < a & e < b & e < c & e < d & d < f & d < g & d < h & d < i & d < j)
{
Console.WriteLine("\nNúmero mínimo: " + e);
Console.WriteLine("\nValor del número mínimo: 4");
}
else if (f < a & f < b & f < c & f <d & f < e & f < g & f < h & f < i & f < j)
{
Console.WriteLine("\nNúmero mínimo: " + f);
Console.WriteLine("\nValor del número mínimo: 5");
}
else if (g < a & g < b & g < d & g < e & g < f & g < h & g < i & g < c & g < j)
{
Console.WriteLine("\nNúmero mínimo: " + g);
Console.WriteLine("\nValor del número máximomínimo: 6");
}
else if (h < a & h < b & h < c & h < d & h < e & h < g & h < f & h < i & h < j)
{
Console.WriteLine("\nNúmero mínimo: " + h);
Console.WriteLine("\nValor del número mínimo: 7");
}
else if (i < a & i < b & i < d & i < e & i < f & i < g & i < h & i < c & i < j)
{
Console.WriteLine("\nNúmero mínimo: " + i);
Console.WriteLine("\nValor del número mínimo: 8");
}
else if (j < a & j < b & j <d & j < e & j <f & j < f & j < h & j < i & j < c)
{
Console.WriteLine("\nNúmero mínimo: " + j);
Console.WriteLine("\nValor del número mínimo: 9");
}
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.Clear();
Console.WriteLine("Sistemas 3/ Josiel Ramos");
Console.WriteLine("\n\n\t\tNúmeros pares, impares, máximo y elmentos del array");
Console.WriteLine("\n\nIngrese 10 números:");
int[] val = new int[10];
int a, b, c, d, e, f, g, h, i, j;
int count=0;
a = val[0]; b = val[1]; c = val[2]; d = val[3]; e = val[4]; f = val[5]; g = val[6]; h= val[7]; i=val[8]; j=val[9];
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = int.Parse(Console.ReadLine());
d = int.Parse(Console.ReadLine());
e = int.Parse(Console.ReadLine());
f = int.Parse(Console.ReadLine());
g = int.Parse(Console.ReadLine());
h = int.Parse(Console.ReadLine());
i = int.Parse(Console.ReadLine());
j = int.Parse(Console.ReadLine());
//Cantidad de números pares
if (a % 2 == 0)
{
count = count + 1;
} if (b % 2 == 0)
{
count+=1;
}
if (c % 2 == 0)
{
count += 1;
}
if (d % 2 == 0)
{count += 1;}
if (e % 2 == 0)
{ count += 1; }
if (f % 2 == 0)
{ count += 1; }
if (g % 2 == 0)
{ count += 1; }
if (h % 2 == 0)
{ count += 1; }
if (i % 2 == 0)
{ count += 1; }
if (j % 2 == 0)
{ count += 1; }
Console.WriteLine("\nUsted ha ingresado " + count + " Números pares");
//VALOR del número máximo
if (a > b & a > c & a > d & a > e & a > f & a > g & a > h & a > i & a > j )
{
Console.WriteLine("\nNúmero máximo: " +a);
Console.WriteLine("\nValor del número máximo: 0");
}
else if (b > a & b > c & b > d & b > e & b > f & b > g & b > h & b > i & b > j)
{
Console.WriteLine("\nNúmero máximo: " + b);
Console.WriteLine("\nValor del número minimomáximo: 1");
}
else if (c > a & c >b & c > d & c > e & c > f & c > g & c > h & c > i & c > j )
{
Console.WriteLine("\nNúmero máximo: " + c);
Console.WriteLine("\nValor del número máximo: 2");
}
else if (d > a & d > b & d > c & d > e & d > f & d > g & d > h & d > i & d > j)
{
Console.WriteLine("\nNúmero máximo: " + d);
Console.WriteLine("\nValor del número máximo: 3");
}
else if (e > a & e > b & e > c & e > d & d > f & d > g & d > h & d > i & d > j)
{
Console.WriteLine("\nNúmero máximo: " + e);
Console.WriteLine("\nValor del número máximo: 4");
}
else if (f < a & f < b & f < c & f < d & f < e & f > g & f > h & f > i & f > j)
{
Console.WriteLine("\nNúmero máximo: " + f);
Console.WriteLine("\nValor del número máximo: 5");
}
else if (g > a & g > b & g > d & g > e & g > f & g > h & g > i & g > c & g > j)
{
Console.WriteLine("\nNúmero máximo: " + g);
Console.WriteLine("\nValor del número máximo: 6");
}
else if (h > a & h > b & h > c & h > d & h > e & h > g & h > f & h > i & h > j)
{
Console.WriteLine("\nNúmero máximo: " + h);
Console.WriteLine("\nValor del número máximo: 7");
}
else if (i > a & i > b & i > d & i > e & i > f & i > g & i > h & i > c & i > j)
{
Console.WriteLine("\nNúmero máximo: " + i);
Console.WriteLine("\nValor del número máximo: 8");
}
else if (j > a & j > b & j > d & j > e & j > f & j > f & j > h & j > i & j > c)
{
Console.WriteLine("\nNúmero máximo: " + j);
Console.WriteLine("\nValor del número máximo: 9");
} //Valor del número minimo
if (a < b & a < c & a < d & a < e & a < f & a < g & a < h & a < i & a < j)
{
Console.WriteLine("\nNúmero mínimo: " + a);
Console.WriteLine("\nValor del número mínimo: 0");
}
else if (b < a & b < c & b < d & b < e & b < f & b < g & b < h & b < i & b < j)
{
Console.WriteLine("\nNúmero mínimo: " + b);
Console.WriteLine("\nValor del número mínimo: 1");
}
else if (c < a & c < b & c < d & c < e & c < f & c < g & c < h & c < i & c < j)
{
Console.WriteLine("\nNúmero mínimo: " + c);
Console.WriteLine("\nValor del número mínimo: 2");
}
else if (d < a & d < b & d < c & d < e & d < f & d < g & d < h & d < i & d < j)
{
Console.WriteLine("\nNúmero mínimo: " + d);
Console.WriteLine("\nValor del número mínimo: 3");
}
else if (e < a & e < b & e < c & e < d & d < f & d < g & d < h & d < i & d < j)
{
Console.WriteLine("\nNúmero mínimo: " + e);
Console.WriteLine("\nValor del número mínimo: 4");
}
else if (f < a & f < b & f < c & f <d & f < e & f < g & f < h & f < i & f < j)
{
Console.WriteLine("\nNúmero mínimo: " + f);
Console.WriteLine("\nValor del número mínimo: 5");
}
else if (g < a & g < b & g < d & g < e & g < f & g < h & g < i & g < c & g < j)
{
Console.WriteLine("\nNúmero mínimo: " + g);
Console.WriteLine("\nValor del número máximomínimo: 6");
}
else if (h < a & h < b & h < c & h < d & h < e & h < g & h < f & h < i & h < j)
{
Console.WriteLine("\nNúmero mínimo: " + h);
Console.WriteLine("\nValor del número mínimo: 7");
}
else if (i < a & i < b & i < d & i < e & i < f & i < g & i < h & i < c & i < j)
{
Console.WriteLine("\nNúmero mínimo: " + i);
Console.WriteLine("\nValor del número mínimo: 8");
}
else if (j < a & j < b & j <d & j < e & j <f & j < f & j < h & j < i & j < c)
{
Console.WriteLine("\nNúmero mínimo: " + j);
Console.WriteLine("\nValor del número mínimo: 9");
}
Console.ReadKey();
}
}
}
2)
Escribir un programa que lea un vector de 10 elementos. Deberá imprimir el
mismo vector por pantalla pero invertido. Ejemplo: dado el vector 1 2 3 4 5 6 7
8 9 10 el programa debería imprimir 10 9 8 7 6 5 4 3 2 1.
namespace
numeros_invertidos
{
class
Program
{
static void Main(string[]
args)
{
Console.ForegroundColor
= ConsoleColor.DarkBlue;
Console.BackgroundColor
= ConsoleColor.DarkGray;
Console.Clear();
Console.WriteLine("\n\t\t\t\tNúmeros invertidos");
Console.WriteLine("Ingrese solo 10 dígitos:");
long
num = long.Parse(Console.ReadLine());
String
s = "";
while
(num != 0)
{
s += num % 10;
num /= 10;
}
Console.WriteLine("\n\tNúmero invertido: " + s);
Console.ReadLine();
}
}
}
3) Escribir un programa que lea 10 números por teclado.
Luego lea dos más e indique si éstos están entre los anteriores.
namespace _10numeros_por_teclado
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.Clear();
Console.WriteLine("\n\t\t\tIngreso de números por teclado");
Console.ReadLine();
Console.WriteLine("Ingrese el primer número:");
int val1 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el segundo número:");
int val2 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el tercer número:");
int val3 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el cuarto número:");
int val4 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el quinto número:");
int val5 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el sexto número:");
int val6 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el séptimo número:");
int val7 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el octavo número:");
int val8 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el noveno número:");
int val9 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el décimo número:");
int val10 = int.Parse(Console.ReadLine());
Console.WriteLine("\n\t\t\tMás números por teclado");
Console.ReadLine();
Console.WriteLine("\nIngrese un valor:");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese un valor:");
int num2 = int.Parse(Console.ReadLine());
if (num1 == val1)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val1)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val2)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val2)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val3)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val3)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val4)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val4)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val5)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val5)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val6)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val6)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val7)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val7)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val8)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val8)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val9)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val9)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val10)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val10)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.Clear();
Console.WriteLine("\n\t\t\tIngreso de números por teclado");
Console.ReadLine();
Console.WriteLine("Ingrese el primer número:");
int val1 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el segundo número:");
int val2 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el tercer número:");
int val3 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el cuarto número:");
int val4 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el quinto número:");
int val5 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el sexto número:");
int val6 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el séptimo número:");
int val7 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el octavo número:");
int val8 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el noveno número:");
int val9 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese el décimo número:");
int val10 = int.Parse(Console.ReadLine());
Console.WriteLine("\n\t\t\tMás números por teclado");
Console.ReadLine();
Console.WriteLine("\nIngrese un valor:");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("\nIngrese un valor:");
int num2 = int.Parse(Console.ReadLine());
if (num1 == val1)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val1)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val2)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val2)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val3)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val3)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val4)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val4)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val5)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val5)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val6)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val6)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val7)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val7)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val8)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val8)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val9)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val9)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
else
{
if (num1 == val10)
{
Console.WriteLine("\n\tSi se encuentra el:" + num1);
Console.ReadLine();
}
else
{
if (num2 == val10)
{
Console.WriteLine("\n\tSi se encuentra el:" + num2);
Console.ReadLine();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
4) Escribir un programa que lea una matriz de números
enteros y que devuelva la suma de los elementos positivos de la matriz y la
suma de los elementos negativos.
namespace positivos___negativos
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Clear();
int[,] matriz1 = new int[10,10];
int i=0,c=0,negativos=0,positivos=0;
Console.WriteLine("\n\t\t\tnúmeros positivos y negativos ");
Console.Write("\nIngrese los valores de la matriz: ");
for (i = 1; i <= matriz1.GetLength(0); i++)
{
for (c = 1; c <= matriz1.GetLength(1); c++)
{
matriz1[i,c] = int.Parse(Console.ReadLine()); ;
}
}
for (i = 1; i <= matriz1.GetLength(1); i++)
{
for (c = 1; c <= matriz1.GetLength(0); c++)
{
if (matriz1[i,c] > 0)
{
positivos = positivos + matriz1[i,c];
}
else
{
negativos = negativos + matriz1[i,c];
}
}
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\n\tSuma de los numeros positivos: " + positivos);
Console.WriteLine("\tSuma de los numeros negativos: " + negativos);
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Clear();
int[,] matriz1 = new int[10,10];
int i=0,c=0,negativos=0,positivos=0;
Console.WriteLine("\n\t\t\tnúmeros positivos y negativos ");
Console.Write("\nIngrese los valores de la matriz: ");
for (i = 1; i <= matriz1.GetLength(0); i++)
{
for (c = 1; c <= matriz1.GetLength(1); c++)
{
matriz1[i,c] = int.Parse(Console.ReadLine()); ;
}
}
for (i = 1; i <= matriz1.GetLength(1); i++)
{
for (c = 1; c <= matriz1.GetLength(0); c++)
{
if (matriz1[i,c] > 0)
{
positivos = positivos + matriz1[i,c];
}
else
{
negativos = negativos + matriz1[i,c];
}
}
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\n\tSuma de los numeros positivos: " + positivos);
Console.WriteLine("\tSuma de los numeros negativos: " + negativos);
Console.ReadKey();
}
}
}

No hay comentarios:
Publicar un comentario