Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

задачи

.docx
Скачиваний:
4
Добавлен:
10.02.2015
Размер:
56.77 Кб
Скачать

return true;

return false;

}

static public implicit operator string(Set ob)

{

string res = "";

for (int i = 0; i < ob.n; i++)

res = res + ob.constraints[i] + "\n";

return res;

}

}

class Program

{

static void Main(string[] args)

{

Set set = new Set(3);

double x1 = 0, y1 = 1;

double x2 = 1, y2 = 1;

if (set.Belongs(x1, y1))

Console.WriteLine("Точка (0,1) принадлежит множеству");

else

Console.WriteLine("Точка (0,1) не принадлежит множеству");

if (set.IsOnBound(x1, y1))

Console.WriteLine("Точка (0,1) лежит на границе множества");

else

Console.WriteLine("Точка (0,1) не лежит на границе множества");

if (set.Belongs(x2, y2))

Console.WriteLine("Точка (1,1) принадлежит множеству");

else

Console.WriteLine("Точка (1,1) не принадлежит множеству");

if (set.IsOnBound(x2, y2))

Console.WriteLine("Точка (1,1) лежит на границе множества");

else

Console.WriteLine("Точка (1,1) не лежит на границе множества");

Console.ReadLine();

}

}

}

  1. Рациональная дробь

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace РРрр

{

class Ration

{

int chisl;

int znam;

int znak;

public Ration(int pchisl, int pznam, int pznak)

{

chisl = pchisl;

znam = pznam;

znak = pznak;

}

public Ration r (Ration ob)//один объект

{

Ration res = new Ration(ob.chisl, ob.znam, ob.znak);

res.chisl = chisl * ob.znam - ob.chisl * znam;

res.znam = znam * ob.znam;

return res;

}

public static implicit operator string(Ration ob)

{

string str = " ";

if (ob.znak == -1)

str = "-";

str = str + Convert.ToString(ob.chisl) + "/" + Convert.ToString(ob.znam);

return str;

}

public static Ration Parse(string str)

{

int pchisl;

int pznam;

int pznak;

//str = str.Substring(1, str.Length - 2);

string[] s = str.Split('/');

pchisl = int.Parse(s[0]);

pznam = int.Parse(s[1]);

//pznak = int.Parse(s[0]);

pznak = 1;

if (pchisl < 0)

{

pchisl = -pchisl;

pznak = -1;

}

Ration res = new Ration(pchisl, pznam, pznak);

return res;

}

public static Ration operator +(Ration ob1,Ration ob2)//один объект

{

Ration res = new Ration(ob1.chisl,ob1.znam,ob1.znak);

res.chisl = ob1.chisl * ob2.znam + ob2.chisl * ob1.znam;

res.znam = ob1.znam * ob2.znam;

return res;

}

public static Ration operator*(Ration ob1,Ration ob2)

{

Ration res = new Ration(ob1.chisl, ob1.znam, ob1.znak);

res.chisl = ob1.chisl * ob2.chisl;

res.znam = ob1.znam * ob2.znam;

return res;

}

}

class Vector

{

int n;

Ration[] a;

public Vector(int n1)

{

n = n1;

a = new Ration[n];

}

public static Vector Parse(string str)

{

str = str.Substring(1, str.Length - 2);

string[] s = str.Split(',');

Vector res = new Vector(s.Length);

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

res.a[i] = Ration.Parse(s[i]);

return res;

}

public static implicit operator string(Vector v)

{

string str = "(";

for (int i = 0; i < v.n - 1; i++)

str = str + Convert.ToString(v.a[i]) + ",";

str = str + Convert.ToString(v.a[v.n - 1] + ")");

return str;

}

public void Print()

{

Console.WriteLine();

}

public static Vector operator +(Vector ob,Vector ob1)

{

if (ob.n != ob1.n)

throw new Exception("Такие вектора нельзя");

Vector res = new Vector(ob.n);

for (int i = 0; i < ob.n; i++)

res.a[i] = ob.a[i] + ob1.a[i];

return res;

}

public static Ration operator *(Vector ob, Vector ob1) //скaлярное произведение

{

if (ob.n != ob1.n)

throw new Exception("Такие вектора умножать нельзя");

Ration res = new Ration(0,1,1);

for (int i = 0; i < ob.n; i++)

res = res+ ob.a[i] * ob1.a[i];

return res;

}

}

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Введите вектор!");

Vector v = Vector.Parse(Console.ReadLine());

Console.WriteLine(" " + v);

Console.WriteLine("Введите ещё один вектор!");

Vector w = Vector.Parse(Console.ReadLine());

Console.WriteLine(" " + w);

Vector u = v+ w;

Console.WriteLine("" + u);

try

{

Console.WriteLine("" + v + "*" + w + "=" + (v * w));

}

catch (Exception e) { Console.WriteLine(e.Message); }

string s = Console.ReadLine();

Ration d = Ration.Parse(s);

}

}

}

  1. Вектор

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Sample02

{

class Vector

{

protected int n;

protected int[] a;

public Vector(int n1)

{

n = n1;

a = new int[n];

}

public static Vector Parse(string str)

{

str = str.Substring(1, str.Length - 2);

string[] s = str.Split(',');

Vector res = new Vector(s.Length);

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

res.a[i] = int.Parse(s[i]);

return res;

}

public static implicit operator string(Vector v)

{

string str = "(";

for (int i = 0; i < v.n-1; i++)

str=str+ Convert.ToString(v.a[i])+",";

str=str+ Convert.ToString(v.a[v.n-1])+")";

//str=str+" Длина вектора=" + (double)v;

return str;

}

public static Vector operator +(Vector ob1, Vector ob2)

{

if(ob1.n!=ob2.n)

throw new Exception("Такие вектора складывать нельзя");

Vector res = new Vector(ob1.n);

for (int i = 0; i < ob1.n; i++)

res.a[i] = ob1.a[i] + ob2.a[i];

return res;

}

public static int operator *(Vector v, Vector w) //сколярное произведение

{

if(v.n!=w.n)

throw new Exception("Такие вектора умножать нельзя");

int res = 0;

for (int i = 0; i < v.n; i++)

res += v.a[i] * w.a[i];

return res;

}

public static Vector operator *(Vector v, int t) //умножение с числом

{

Vector res = new Vector(v.n);

for (int i = 0; i < v.n; i++)

res.a[i] = v.a[i] * t;

return res;

}

public static explicit operator double(Vector v)

{

double sum = 0;

for (int i = 0; i < v.n; i++)

sum = sum + v.a[i] * v.a[i];

return Math.Sqrt(sum);

}

public static bool operator >(Vector v, Vector w)

{

if ((double)v > (double)w)

return true;

return false;

}

public static bool operator <(Vector v, Vector w)

{

if ((double)v < (double)w)

return true;

return false;

}

}

class Program

{

public static void Main(string[] args)

{

Console.WriteLine("Введите вектор");

Vector v = Vector.Parse(Console.ReadLine());

Console.WriteLine("" + v);

Console.WriteLine("Введите еще один вектор");

Vector w = Vector.Parse(Console.ReadLine());

Console.WriteLine("" + w);

Vector u;

try

{

u = v + w;

Console.WriteLine("" + u);

}

catch (Exception e) { Console.WriteLine(e.Message); }

u=v*3;

Console.WriteLine("" + u);

try

{

Console.WriteLine(""+v+"*"+w+"=" + (v * w));

}

catch (Exception e) { Console.WriteLine(e.Message); }

if (v > w)

Console.WriteLine("больше");

else

Console.WriteLine("меньше");

}

}

}

  1. Деление

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace деление

{

class Ration

{

int chisl;

int znam;

int k;

public Ration()

{

chisl = 0;

znam = 1;

}

public Ration(int ch, int zn)

{

chisl = ch;

znam = zn;

}

~Ration()

{

Console.WriteLine("Дробь{0}/{1} удалена", chisl, znam);

}

public Ration Delenie(Ration ob)

{

Ration res = new Ration();

res.chisl = chisl * ob.znam ;

res.znam = znam * ob.chisl;

return res;

}

public Ration Y(Ration ob)

{

Ration res = new Ration();

res.chisl = chisl * ob.chisl;

res.znam = ob.znam * znam;

return res;

}

public bool sravn(Ration ob)

{

Ration res = r(ob);

if (res.chisl * res.znam > 0)

return true;

return false;

}

public Ration r(Ration ob)//один объект

{

Ration res = new Ration();

res.chisl = chisl * ob.znam - ob.chisl * znam;

res.znam = znam * ob.znam;

return res;

}

public void Print()

{

Console.WriteLine("{0}/{1}", chisl, znam);

}

public static Ration operator +(Ration ob1, Ration ob2)

{

Ration res = new Ration();

res.chisl = ob1.chisl * ob2.znam + ob1.znam * ob2.chisl;

res.znam = ob1.znam * ob2.znam;

return res;

}

public static Ration operator +(Ration ob1, int a)

{

Ration res = new Ration();

res.chisl = ob1.chisl + ob1.znam * a;

res.znam = ob1.znam;

return res;

}

public static Ration operator +(int a, Ration ob1)

{

return ob1+a;

}

}

class Program

{

static void Main(string[] args)

{

Ration r1 = new Ration(3, 4);

Ration r2 = new Ration(5, 6);

Ration r3 = 3+r1;

r3.Print();

}

}

}

  1. Операторы

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace операторы

{

class Ration

{

int chisl;int znam;int k;

public Ration() //ПУСТЫШКА

{

chisl = 0;

znam = 1;

}

public Ration(int ch, int zn) //СОЗДАНИЕ ДРОБИ

{

chisl = ch;

znam = zn;

}

~Ration() //СБОРЩИК МУСОРА

{

Console.WriteLine("Дробь{0}/{1} удалена", chisl, znam);

}

public static Ration Parse(string s) // ПЕРЕВОД из строки в дробь

{

string[] str = s.Split('/');

Ration r = new Ration();

r.chisl = int.Parse(str[0]);

r.znam = int.Parse(str[1]);

return r;

}

public static implicit operator string(Ration r) //СТРОКА ДЛЯ ВЫВОДА implicit-неявное ,explicit-явное

{

string s = "" + r.chisl + " / " + r.znam;

return s;

}

public static Ration Enter()

{

Console.WriteLine("Введи дробь!");

Ration r = Ration.Parse(Console.ReadLine());

return r;

}

public void Print() //ПЕЧАТЬ

{

Console.WriteLine("{0}/{1}", chisl, znam);

}

public static Ration operator +(Ration ob1, Ration ob2) //ДРОБЬ + ДРОБЬ

{

Ration res = new Ration();

res.chisl = ob1.chisl * ob2.znam + ob1.znam * ob2.chisl;

res.znam = ob1.znam * ob2.znam;

return res;

}

public static Ration operator +(Ration ob1, int a) //ДРОБЬ + ЦЕЛОЕ

{

Ration res = new Ration();

res.chisl = ob1.chisl + ob1.znam * a;

res.znam = ob1.znam;

return res;

}

public static Ration operator +(int a, Ration ob1) //ЦЕЛОЕ + ДРОБЬ

{

return ob1 + a;

}

public static Ration operator -(Ration ob1, Ration ob2) //ДРОБЬ - ДРОБЬ

{

Ration res = new Ration();

res.chisl = ob1.chisl * ob2.znam - ob1.znam * ob2.chisl;

res.znam = ob1.znam * ob2.znam;

return res;

}

public static Ration operator -(Ration ob1, int a) //ДРОБЬ - ЦЕЛОЕ

{

Ration res = new Ration();

res.chisl = ob1.chisl - ob1.znam * a;

res.znam = ob1.znam;

return res;

}

public static Ration operator -(int a, Ration ob1) //ЦЕЛОЕ - ДРОБЬ

{

return ob1-a;

}

public static Ration operator /(Ration ob1, Ration ob2) //ДРОБЬ / ДРОБЬ

{

Ration res = new Ration();

res.chisl = ob1.chisl * ob2.znam;

res.znam = ob1.znam * ob2.chisl;

return res;

}

}

class vektor

{

int n; int []a;

public vektor(int n)

{

this.n = n;

a = new int[this.n];

}

public vektor(params int[] a)

{

n = a.Length;

this.a = new int[n];

for (int x = 0; x < n; x++)

{ this.a[x] = a[x]; }

}

public static implicit operator string(vektor r) //СТРОКА ДЛЯ ВЫВОДА implicit-неявное ,explicit-явное

{

string s = "(";

for (int x = 0; x < r.n-1; x++)

{ s += (r.a[x] + ","); }

s += (r.a[r.n-1] + ")");

return s;

}

}

class Program

{

static void Main(string[] args)

{

//ВВОД

//ВЫЗОВ КЛАССА

//ВЫВОД

//Ration r1 = Ration.Enter();

//Ration r2 = Ration.Enter();

////Ration r1 = new Ration(3, 4);

////Ration r2 = new Ration(5, 6);

//Ration r3 = 3 + r1;

//Console.WriteLine("3 + "+r1+" = "+r3);

vektor v = new vektor(5);

vektor v2 = new vektor(1, 2, 3, 4, 5, 6, 7, 8, 9);

Console.WriteLine("" + v);

Console.WriteLine("" + v2);

}

}

}

  1. Слияние ветора с дробью

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace операторы

{

class Ration

{

int chisl;int znam;int k;

public Ration() //ПУСТЫШКА

{

chisl = 0;

znam = 1;

}

public Ration(int ch, int zn) //СОЗДАНИЕ ДРОБИ

{

chisl = ch;

znam = zn;

}

~Ration() //СБОРЩИК МУСОРА

{

Console.WriteLine("Дробь{0}/{1} удалена", chisl, znam);

}

public static Ration Parse(string s) // ПЕРЕВОД из строки в дробь

{

string[] str = s.Split('/');

Ration r = new Ration();

r.chisl = int.Parse(str[0]);

r.znam = int.Parse(str[1]);

return r;

}

public static implicit operator string(Ration r) //СТРОКА ДЛЯ ВЫВОДА implicit-неявное ,explicit-явное

{

string s = "" + r.chisl + " / " + r.znam;

return s;

}

public static Ration Enter()

{

Console.WriteLine("Введи дробь!");

Ration r = Ration.Parse(Console.ReadLine());

return r;

}

public void Print() //ПЕЧАТЬ

{

Console.WriteLine("{0}/{1}", chisl, znam);

}

public static Ration operator +(Ration ob1, Ration ob2) //ДРОБЬ + ДРОБЬ

{

Ration res = new Ration();

res.chisl = ob1.chisl * ob2.znam + ob1.znam * ob2.chisl;

res.znam = ob1.znam * ob2.znam;

return res;

}

public static Ration operator +(Ration ob1, int a) //ДРОБЬ + ЦЕЛОЕ

{

Ration res = new Ration();

res.chisl = ob1.chisl + ob1.znam * a;

res.znam = ob1.znam;

return res;

}

public static Ration operator +(int a, Ration ob1) //ЦЕЛОЕ + ДРОБЬ

{

return ob1 + a;

}

public static Ration operator -(Ration ob1, Ration ob2) //ДРОБЬ - ДРОБЬ

{

Ration res = new Ration();

res.chisl = ob1.chisl * ob2.znam - ob1.znam * ob2.chisl;

res.znam = ob1.znam * ob2.znam;

return res;

}

public static Ration operator -(Ration ob1, int a) //ДРОБЬ - ЦЕЛОЕ

{

Ration res = new Ration();

res.chisl = ob1.chisl - ob1.znam * a;

res.znam = ob1.znam;

return res;

}

public static Ration operator -(int a, Ration ob1) //ЦЕЛОЕ - ДРОБЬ

{

return ob1-a;

}

public static Ration operator /(Ration ob1, Ration ob2) //ДРОБЬ / ДРОБЬ

{

Ration res = new Ration();

res.chisl = ob1.chisl * ob2.znam;

res.znam = ob1.znam * ob2.chisl;

return res;

}

}

class vektor

{

int n; Ration []a;

public vektor(int n)

{

this.n = n;

a = new Ration[this.n];

}

public vektor(params Ration[] a)

{

n = a.Length;

this.a = new Ration[n];

for (int x = 0; x < n; x++)

{ this.a[x] = a[x]; }

}

public static implicit operator string(vektor r) //СТРОКА ДЛЯ ВЫВОДА implicit-неявное ,explicit-явное

{

string s = "(";

for (int x = 0; x < r.n - 1; x++)

{ s += (r.a[x] + ","); }

s += (r.a[r.n - 1] + ")");

return s;

}

}

class Program

{

static void Main(string[] args)

{

//ВВОД

//ВЫЗОВ КЛАССА

//ВЫВОД

//Ration r1 = Ration.Enter();

//Ration r2 = Ration.Enter();

Ration r1 = new Ration(3, 4);

Ration r2 = new Ration(5, 6);

//Ration r3 = 3 + r1;

//Console.WriteLine("3 + "+r1+" = "+r3);

vektor v = new vektor(r1,r2);

//vektor v2 = new vektor(1, 2, 3, 4, 5, 6, 7, 8, 9);

Console.WriteLine("" + v);

//Console.WriteLine("" + v2);

}

}

}