
- •«Колледж бизнеса и права»
- •Лабораторная работа 1 Тема: Линейные алгоритмы
- •Лабораторная работа 2 Тема: Функции ввода-вывода. Форматы преобразования данных
- •Лабораторная работа 3.4.5 Тема: Циклы while, for, do while.
- •Лабораторная работа 6. Тема: Одномерные и прямоугольные массивы
- •Лабораторная работа 8 Тема: Одномерные и прямоугольные массивы
- •Лабораторная работа 9 Тема: Одномерные и прямоугольные массивы
- •Лабораторная работа 10.11.12 Тема: Программирование с использованием строк (обычный вариант и с использованием 3 видов строк)
- •Лабораторная работа 13: Тема: Пользовательские методы
- •Лабораторная работа 14: Тема: Функции
- •Лабораторная работа 14: Тема: Указатели
- •Лабораторная работа 15: Тема: Файлы
- •Лабораторная работа 16 Тема: Структуры
Лабораторная работа 14: Тема: Указатели
Составить программу расположения элементов массива в следующем порядке – положительные, отрицательные и нулевые
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace laba5_1
{
class Program
{
static unsafe void Main()
{
const int n = 8;
int[] a = new int[n] { 6, -1, 12, -80, 3, -6, 5, -9 };
int[] x = new int[n];
fixed (int*b=a)
{
Console.WriteLine("Исходный массив: ");
for (int i = 0; i < n; ++i)
Console.Write("\t" + b[i]);
Console.WriteLine();
for (int j = 0; j < a.Length; j++)
{
x[j] = b[j];
}
Array.Sort(x);
Array.Reverse(x);
Console.WriteLine("Отсортированный массив:");
for (int j = 0; j < a.Length; j++)
{
Console.Write("{0}\t",x[j]);
}
Console.ReadKey();
} } }}
Результат работы программы
Лабораторная работа 15: Тема: Файлы
Байтовые файлы
using System;
using System.IO;
namespace ConsoleApplication1
{
class Class1
{
static void Main()
{
string fullName = "Petkevich Alesia";
int yearBirth = 1995;
int currentYear = DateTime.Now.Year;
string fileOut = fullName + " " + yearBirth + " my age is ";
Console.WriteLine(fullName + " " + yearBirth);
Console.ReadKey();
string directoryPath = @"D:\\Laba10";
string fileName = "Laba10.txt";
DirectoryInfo dir = new DirectoryInfo(directoryPath);
try
{
dir.Create();
// Запись в файл (имя и год)
FileStream f = new FileStream(directoryPath + "\\" + fileName, FileMode.Create, FileAccess.ReadWrite);
for (int i = 0; i < fileOut.Length; i++)
{
f.WriteByte((byte)fileOut[i]);
}
// Чтение из файла (год)
byte[] yearBirthByte = new byte[4];
f.Seek(fullName.Length + 1, SeekOrigin.Begin); //текущий указатель – на начало
f.Read(yearBirthByte, 0, yearBirthByte.Length);
string restored = "";
foreach (byte elem in yearBirthByte)
{
restored += (char)elem;
}
int restoredInt = Int32.Parse(restored);
int myAge = currentYear - restoredInt;
// Обратная запись (кол-во лет)
Console.WriteLine(myAge - 1);
Console.ReadKey();
f.Seek(fileOut.Length, SeekOrigin.Begin);
for (int i = 0; i < myAge.ToString().Length; i++)
{
f.WriteByte((byte)myAge.ToString()[i]);
}
// Закрытие
f.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadKey();
return;
}
}}}
Результат работы программы
Текстовые файлы
using System;
using System.IO;
namespace ConsoleApplication1
{
class Class1
{
static void Main()
{
string a = "Мне";
string fullName = "Петкевич Алеся";
int yearBirth = 1995;
int currentYear = DateTime.Now.Year;
string fileOut = fullName + " " + yearBirth;
Console.WriteLine(fullName + " " + yearBirth);
string directoryPath = @"D:\\Lab10";
string fileName = "Lab10.txt";
DirectoryInfo dir = new DirectoryInfo(directoryPath);
try
{
dir.Create();
// Запись в файл (имя и дата)
StreamWriter fw = new StreamWriter(directoryPath + "\\" + fileName);
fw.WriteLine(fileOut);
// Закрытие
fw.Close();
Console.ReadKey();
// Чтение из файла (дата)
StreamReader fr = new StreamReader(directoryPath + "\\" + fileName);
string restored = fr.ReadLine().Split(' ')[2]; // При условии что задаётся только имя и фамилия разделённые пробелами
// Закрытие
fr.Close();
Console.ReadKey();
int restoredInt = Int32.Parse(restored);
int myAge = currentYear - restoredInt;
Console.WriteLine("Мне: {0}",myAge-1);
Console.ReadKey();
// Обратная запись (полный возраст)
StreamWriter fw2;
FileInfo fi = new FileInfo(directoryPath + "\\" + fileName);
fw2 = fi.AppendText();
fw2.Write(a+" "+myAge);
// Закрытие
fw2.Close();
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadKey();
return;
} } }}
Результат работы программы
Двоичные файлы
using System;
using System.IO;
namespace ConsoleApplication1
{
class Class1
{
static void Main()
{
string fullName = "Petkevich Alecia";
int yearBirth = 1995;
int currentYear = DateTime.Now.Year;
string fileOut = fullName + " " + yearBirth + " my age is ";
Console.WriteLine(fullName + " " + yearBirth);
string directoryPath = @"D:\\Lab10";
string fileName = "Lab10.txt";
DirectoryInfo dir = new DirectoryInfo(directoryPath);
try
{
dir.Create();
// Запись в файл (имя и год)
BinaryWriter fw = new BinaryWriter(new FileStream(directoryPath + "\\" + fileName, FileMode.Create));
for (int i = 0; i < fileOut.Length; i++)
{
fw.Write((byte)fileOut[i]);
}
fw.Close();
// Чтение из файла (год)
BinaryReader fr = new BinaryReader(new FileStream(directoryPath + "\\" + fileName, FileMode.Open));
byte[] yearBirthByte = new byte[fileOut.Length];
fr.Read(yearBirthByte, 0, fileOut.Length);
fr.Close();
string restored = "";
foreach (byte elem in yearBirthByte)
{
restored += (char)elem;
}
int restoredInt = Int32.Parse(restored.Split(' ')[2]);
int myAge = currentYear - restoredInt;
Console.WriteLine(restoredInt);
Console.ReadKey();
// Обратная запись (кол-во лет)
BinaryWriter fw2 = new BinaryWriter(new FileStream(directoryPath + "\\" + fileName, FileMode.Append));
for (int i = 0; i < myAge.ToString().Length; i++)
{
fw2.Write((byte)myAge.ToString()[i]);
}
fw2.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message + " " + e.Data);
Console.ReadKey();
return;
}}}}
Результат работы программы