Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Отчет C# Петкевич.docx
Скачиваний:
0
Добавлен:
01.04.2025
Размер:
1.83 Mб
Скачать

Лабораторная работа 8 Тема: Одномерные и прямоугольные массивы

Составить программу нахождения произведения отрицательных (положительных) элементов массива

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication7

{

class Program

{

static void Main(string[] args)

{

const int z = 3, h = 3;

int[,] a = new int[z, h]

{

{1,-1,0},

{5,0,8},

{3,-3,-3}

};

for (int i = 0; i < z; i++)

{

for (int j = 0; j < h; j++)

{

Console.Write("\t" + a[i,j]);

}

Console.WriteLine();

}

int max = 1;

int min = 1;

for (int i = 1; i < z; i++)

{

for (int j = 0; j < h; j++)

{

if (0 <= a[i, j]) max =max + a[i, j];

if (0 > a[i, j]) min = min - a[i, j];

}

}

Console.ReadKey();

Console.WriteLine("");

Console.WriteLine("сумма положительных = " + max);

Console.WriteLine("сумма отрицательных = - " + min);

Console.ReadKey();

}}}

Результат работы программы

Лабораторная работа 9 Тема: Одномерные и прямоугольные массивы

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main()

{

int[] a = { 5, 2, -4, 7, -26, 2, 6 };

foreach (int o in a) Console.Write("\t" + o);

int max = 0;

int min = 0;

int z = 7;

for (int i = 0; i < z; i++)

{

if (0 < a[i]) max = max + a[i];

if (0 > a[i]) min = min + a[i];

}

Console.ReadKey();

Console.WriteLine("");

Console.WriteLine("сумма отриц. = " + min);

Console.WriteLine("сумма полож = " + max);

Console.ReadKey();

}}}

Лабораторная работа 10.11.12 Тема: Программирование с использованием строк (обычный вариант и с использованием 3 видов строк)

  1. Char

В строке символов заменить каждый второй символ ! на $. 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace sdobnov2

{

class Program

{

static void Main(string[] args)

{

int count = 0;

Console.WriteLine("введите строку =");

string s = Console.ReadLine();

char[] ch = new char[s.Length];

for (int i = 0; i < s.Length; i++)

{

if (s[i] == '!')

{

count++;

if (count % 2 == 0)

ch[i] = '$';

else ch[i] = s[i];

}

else ch[i] = s[i];

}

string sNew = new string(ch);

Console.WriteLine(sNew);

Console.ReadKey();

}}}

Результат работы программы

  1. String

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace sdobnov1

{

class Program

{

static void Main(string[] args)

{

int count = 0;

Console.WriteLine("введите строку =");

string s = Console.ReadLine();

for (int i = 0; i < s.Length; i++)

{

if (s[i] == '!')

{

count++;

if (count % 2 == 0)

Console.Write('$');

else

Console.Write(s[i]);

}

else

Console.Write(s[i]);

}

Console.ReadKey();

}}}

Результат работы программы

  1. StringBuilder

using System;

namespace sdobnov3

{

class Program

{

static void Main(string[] args)

{

int count = 0;

Console.WriteLine("введите строку =");

StringBuilder s = new StringBuilder(Console.ReadLine());

StringBuilder d = new StringBuilder();

for (int i = 0; i < s.Length; i++)

{

if (s[i] == '!')

{

count++;

if (count % 2 == 0)

d.Append('$');

else

d.Append(s[i]);

}

else

d.Append(s[i]);

}

Console.Write(d);

Console.ReadKey();

}}}

Результат работы программы