Skip to content

Instantly share code, notes, and snippets.

@NikitaShkaruba
Created September 19, 2014 03:13
Show Gist options
  • Save NikitaShkaruba/df59a36a91dc3d519047 to your computer and use it in GitHub Desktop.
Save NikitaShkaruba/df59a36a91dc3d519047 to your computer and use it in GitHub Desktop.
Labs
using System;
using System.IO;
namespace Labs
{
class Program
{
static Random dice = new Random();
static bool Loop = true;
static string bstr = "";
static int LabNum;
static int buff;
static void lab1()
{
double x,y = 0,R;
Console.WriteLine("\n(1)* График пьяной функции\n");
Console.Write("Введи Радиус окружностей R [R>0.0]&[R<6.0]:");
bstr = Console.ReadLine();
R = Double.Parse(bstr);
if (R > 6.0 || R < 0.0)
{ Console.WriteLine("(!)Ошибка ввода данных, внимательно читай условие.\n"); return; }
Console.Write("Введи координату X [x>{0}]&[x<=9.0]:", -6.0-R);
bstr = Console.ReadLine();
x = Double.Parse(bstr);
if (x <= -6.0-R || x > 9.0)
{ Console.WriteLine("(!)Ошибка ввода данных, внимательно читай условие.\n"); return; }
if (x < -6.0 && x > -6.0 - R)
{
y = Math.Sqrt((R-x-6)*(R+x+6));
}
if (x <= -3.0 && x >= -6.0)
{
y = x + 3;
}
if (x > -3 && x <=0)
{
y = Math.Sqrt((R-x)*(R+x));
}
if (x >= 0 && x <= 3)
{
y = -(x-3);
}
if (x > 3 && x <= 9)
{
y = 0.5 * x - 1.5;
}
Console.WriteLine("Y = {0}", y);
}
static void lab2()
{
int PosS = 0, max = -101, min = 101;
int indmin = 1, indmax = -1, pr = 1, N = 20;
int[] arr = new int[N];
Console.WriteLine("\n(2)* Одномерный массивы\n");
Console.WriteLine("Первоначальный массив:");
for (int i = 0; i < arr.Length; i++)
{
while(arr[i] == 0)
arr[i] = (int)dice.Next(-50, 50);
if (arr[i] > 0)
PosS += arr[i];
if (arr[i] > max)
indmax = i;
if (arr[i] < min)
if (indmax != indmin) indmin = i;
Console.WriteLine("arr[{0}] = {1}", i, arr[i]);
}
for (; indmin <= indmax; indmin++)
{
pr *= arr[indmin];
}
Array.Sort(arr);
Console.WriteLine("\nОтсортированный массив:");
for (int i = 0; i < arr.Length; i++)
Console.WriteLine("arr[{0}] is {1}", i, arr[i]);
Console.WriteLine("\nPositive Sum is: " + PosS);
Console.WriteLine("Product inside min and max is: " + pr + "\n");
}
static void lab3()
{
int N = 7, M = 8, nonumi = 0;
bool flag;
int[,] Arr = new int[N,M];
Console.WriteLine("\n(3)* Работа с матрицами\n");
for (int i = 0; i < N; i++)
{
flag = true;
for (int j = 0; j < M; j++)
{
Arr[i,j] = (int)dice.Next(-50, 50);
if (Arr[i, j] == 0) flag = false;
Console.WriteLine("Arr[{0},{1}] = {2}", i, j, Arr[i, j]);
}
if (flag) nonumi++;
}
int plentyMax = -101, l = 0;
int[] bArr = new int[N*M];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
bArr[l++] = Arr[i, j];
}
Array.Sort(bArr);
for (l = 0; l < bArr.Length-1; l++ )
if (bArr[l] == bArr[l + 1] && bArr[l] > plentyMax)
{
plentyMax = bArr[l];
}
Console.WriteLine("Кол-во строк без нулевых элементов: " + nonumi);
if (plentyMax == -101)
Console.WriteLine("Одинаковых элементов не обнаружилось!");
else Console.WriteLine("Максимальный элемент, встречающийся в матрице более 1го раза: " + plentyMax + "\n");
}
static void lab4()
{
string text = "";
Console.WriteLine("\n(4)* Работа с текстовыми фаилами\n");
buff = (int)dice.Next(1,3);
switch (buff)
{
case 1: { text = File.ReadAllText(@"C:\New Nick\Example.txt"); break; }
case 2: { text = File.ReadAllText(@"C:\New Nick\Example2.txt"); break; }
case 3: { text = File.ReadAllText(@"C:\New Nick\Example3.txt"); break; }
}
Console.WriteLine("Текст, считанный из фаила:\n{0}\n", text);
Console.Write("Перевёрнутый текст:\n");
for (int i = text.Length-1; i >= 0; i--)
{
Console.Write(text[i]);
}
Console.WriteLine();
}
static void Main(string[] args)
{
Console.WriteLine("Библиотека лабораторных работ\nСтудента ИТМО, ФКТиУ спец.Программная инженерия 1121\nШкаруба Никиты Евгеньевича\n");
while (Loop)
{
Console.Write("\n\nВведи номер Лабораторной работы: ");
bstr = Console.ReadLine();
try { LabNum = Int32.Parse(bstr); }
catch (System.FormatException)
{
LabNum = (int) dice.Next(2,4);
Console.WriteLine("\n\a!!!!!!!!!!!!!!!!!!!!!!!!!!\nСимвол не был введён, так что я аварийно запускаю лабу№ {0}", LabNum);
}
switch(LabNum)
{
case 1: lab1(); break;
case 2: lab2(); break;
case 3: lab3(); break;
case 4: lab4(); break;
default: Console.WriteLine("\nОшибка, такой л.работы не существеут."); break;
}
//Доп.выпендрёж
/*Console.Write("\nПовторить? Введи 'Да'/'Нет': ");
bstr = Console.ReadLine();
bstr = bstr.ToUpper();
if (bstr == "ДА")
Loop = true;
if (bstr == "НЕТ")
Loop = false;*/
}
Console.WriteLine("Конец программы.");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment