Skip to content
Snippets Groups Projects
Commit 0d747477 authored by Jens Albrecht's avatar Jens Albrecht
Browse files

.

parent 7cee676b
No related branches found
No related tags found
No related merge requests found
// Erstellen Sie einen Aufzähltyp Color mit den Werten Red, Blue, Green.
enum Color { Red, Blue, Green }
class Program
{
// Schreiben Sie eine Funktion ColorCount, die ein Color-Array sowie eine einzelne Farbe als Parameter bekommt
// und die Anzahl der Elemente mit der entsprechenden Farbe in dem Array zurückgibt.
static int ColorCount(Color[] colors, Color searchColor)
{
int num = 0;
foreach (Color c in colors)
{
if (c == searchColor)
num++;
}
return num;
}
static void Main()
{
// Erzeugen Sie ein Array vom Typ Color der Länge 10
Color[] colors = new Color[10];
// und initialisieren Sie es mit zufälligen Farb-Werten
Random rnd = new Random();
for (int i = 0; i < colors.Length; i++)
{
colors[i] = (Color)rnd.Next(3);
Console.WriteLine($"{i,3}: {colors[i]}");
}
// Testen Sie die Funktion mit dem erzeugten Array
Color color = Color.Blue;
Console.WriteLine($"{ColorCount(colors, Color.Blue)}x {color} gefunden");
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
namespace Helgoland;
using System.Globalization; // für CultureInfo
// Das Programm ist so gestaltet, dass es auf deutschen/englischen Systemen
// sowie auf Windows und Unix/MacOS läuft.
class Program
{
static void Niedrigwasser(string datum, out double hoehe, out string zeit)
{
// Stellt sicher, dass bei Convert.ToDouble ein Komma als Dezimalzeichen
// interpretiert wird (kein Klausurwissen)
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("de-de");
hoehe = 0;
zeit = "";
// Path.Combine trennt Verzeichnisebenen unter Windows durch \
// und unter Unix durch / (kein Klausurwissen)
StreamReader sr = new StreamReader(Path.Combine("..","..","..","Helgoland.txt"));
while (!sr.EndOfStream)
{
string[] felder = sr.ReadLine()!.Split('\t');
// gesuchten Tag finden
if (felder[0] == datum)
{
// Min. Höhe und Zeit dazu bestimmen
hoehe = Convert.ToDouble(felder[2]);
for (int i = 2; i < felder.Length; i += 2)
{
double aktHoehe = Convert.ToDouble(felder[i]);
if (aktHoehe < hoehe)
{
hoehe = aktHoehe;
zeit = felder[i - 1];
}
}
return;
}
}
}
static void Main(string[] args)
{
double h;
string t;
string datum = "0707";
Niedrigwasser(datum, out h, out t);
Console.WriteLine($"Niedrigwasserhöhe an {datum} ist {h} um Zeit {t}");
}
}
bool[] isPrime = new bool[1000];
for (int i = 2; i < isPrime.Length; i++)
isPrime[i] = true;
for (int i = 2; i < isPrime.Length; i++)
{
if (isPrime[i])
{
Console.WriteLine($"{i} ist prim.");
for (int j = i+i; j < isPrime.Length; j+=i)
isPrime[j] = false;
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
class Produktverkauf
{
static void VerkaufProArtikelAusgeben(int[,] v)
{
Console.WriteLine("Durchschnittlicher Verkauf für:");
for (int i = 0; i < v.GetLength(0); i++)
{
double summe = 0;
for (int j = 0; j < v.GetLength(1); j++)
summe += v[i, j];
Console.WriteLine($"- Artikel {i}: {summe / v.GetLength(1):f2}");
}
}
static void VerkaufProMonatAusgeben(int[,] v, int monat)
{
if (monat > v.GetLength(1) || monat <= 0)
{
Console.WriteLine("Ungültiger Monat!");
return;
}
double summe = 0;
for (int i = 0; i < v.GetLength(0); i++)
summe += v[i, monat - 1];
Console.Write($"Durchschnittlicher Verkauf für Monat {monat}: {summe / v.GetLength(0):f2}");
}
static void GesamtVerkaufAusgeben(int[,] v)
{
double summe = 0;
for (int i = 0; i < v.GetLength(0); i++)
for (int j = 0; j < v.GetLength(1); j++)
summe += v[i, j];
Console.WriteLine($"Insgesamt wurden {summe} Artikel verkauft.");
}
static void Main(string[] args)
{
int[,] verkauf = { { 3, 5, 6 },
{ 7, 8, 2 },
{ 4, 5, 3 },
{ 9, 7, 8 } };
string auswahl;
do
{
Console.WriteLine("\n\nHauptmenü\n");
Console.WriteLine("1) Durchschnittliche Verkaufszahl pro Artikel ausgeben");
Console.WriteLine("2) Durchschnittliche Verkaufszahl für einen Monat ausgeben");
Console.WriteLine("3) Summe aller verkauften Artikel ausgeben");
Console.WriteLine("4) Beenden");
Console.Write("Ihre Auswahl: ");
auswahl = Console.ReadLine()!;
switch (auswahl)
{
case "1":
VerkaufProArtikelAusgeben(verkauf);
break;
case "2":
Console.Write("Welcher Monat? ");
int monat = Convert.ToInt32(Console.ReadLine());
VerkaufProMonatAusgeben(verkauf, monat);
break;
case "3":
GesamtVerkaufAusgeben(verkauf);
break;
case "4":
break;
default:
Console.WriteLine("Ungültige Eingabe!");
break;
}
} while (auswahl != "4");
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
class Program
{
static void Replace(ref string s, char suchZeichen, char ersatzZeichen)
{
// Einfachste Variante
char[] ergebnis = new char[s.Length];
for (int i = 0; i < s.Length; i++)
if (s[i] == suchZeichen)
ergebnis[i] = ersatzZeichen;
else
ergebnis[i] = s[i];
s = new String(ergebnis);
}
static void Main(string[] args)
{
string text = "Anne";
Console.WriteLine("Text vorher: " + text);
Replace(ref text, 'n', 'm');
Console.WriteLine("Text nachher: " + text);
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
class Program
{
static bool Teilmenge(int[] a, int[] b)
{
// prüfe für jedes Element in b, ob es auch in a vorkommt
for (int j = 0; j < b.Length; j++)
{
bool gefunden = false;
for (int i = 0; i < a.Length && !gefunden; i++)
{
if (b[j] == a[i])
gefunden = true;
}
if (!gefunden)
return false;
}
return true;
}
static void Main(string[] args)
{
int[] a = { 8, 6, 1, 7, 4, 9 };
int[] b = { 1, 6, 9 };
if (Teilmenge(a, b))
Console.WriteLine("b ist Teilmenge von a");
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1706.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ColorArray", "ColorArray\ColorArray.csproj", "{C118DF43-943D-4E0D-A0FA-CE8EE2D16A48}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DateiHelgoland", "DateiHelgoland\DateiHelgoland.csproj", "{A4CD45D4-59F2-4F44-8AF6-F8A5444B06F3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProduktVerkauf", "ProduktVerkauf\ProduktVerkauf.csproj", "{12AA6AA5-CFD9-478A-A38B-7B5B1A674627}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Teilmenge", "Teilmenge\Teilmenge.csproj", "{8C4796B7-B07A-4445-AC7F-FEE914849115}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace", "Replace\Replace.csproj", "{D452875E-3BED-45CF-A71A-15178F110CCE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Erasthostenes", "Erasthostenes\Erasthostenes.csproj", "{00F85AB7-95E8-4C41-A6FA-D6C1F8A3F487}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C118DF43-943D-4E0D-A0FA-CE8EE2D16A48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C118DF43-943D-4E0D-A0FA-CE8EE2D16A48}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C118DF43-943D-4E0D-A0FA-CE8EE2D16A48}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C118DF43-943D-4E0D-A0FA-CE8EE2D16A48}.Release|Any CPU.Build.0 = Release|Any CPU
{A4CD45D4-59F2-4F44-8AF6-F8A5444B06F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4CD45D4-59F2-4F44-8AF6-F8A5444B06F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4CD45D4-59F2-4F44-8AF6-F8A5444B06F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4CD45D4-59F2-4F44-8AF6-F8A5444B06F3}.Release|Any CPU.Build.0 = Release|Any CPU
{12AA6AA5-CFD9-478A-A38B-7B5B1A674627}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12AA6AA5-CFD9-478A-A38B-7B5B1A674627}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12AA6AA5-CFD9-478A-A38B-7B5B1A674627}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12AA6AA5-CFD9-478A-A38B-7B5B1A674627}.Release|Any CPU.Build.0 = Release|Any CPU
{8C4796B7-B07A-4445-AC7F-FEE914849115}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C4796B7-B07A-4445-AC7F-FEE914849115}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C4796B7-B07A-4445-AC7F-FEE914849115}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C4796B7-B07A-4445-AC7F-FEE914849115}.Release|Any CPU.Build.0 = Release|Any CPU
{D452875E-3BED-45CF-A71A-15178F110CCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D452875E-3BED-45CF-A71A-15178F110CCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D452875E-3BED-45CF-A71A-15178F110CCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D452875E-3BED-45CF-A71A-15178F110CCE}.Release|Any CPU.Build.0 = Release|Any CPU
{00F85AB7-95E8-4C41-A6FA-D6C1F8A3F487}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{00F85AB7-95E8-4C41-A6FA-D6C1F8A3F487}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00F85AB7-95E8-4C41-A6FA-D6C1F8A3F487}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00F85AB7-95E8-4C41-A6FA-D6C1F8A3F487}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {956BF058-FD07-431E-AEFA-2915F58510ED}
EndGlobalSection
EndGlobal
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment