public void Octal3() { IntX intX = 0x80000000; Assert.AreEqual(intX.ToString(8), "20000000000"); intX = 0x100000000; Assert.AreEqual(intX.ToString(8), "40000000000"); }
//Writes keys to file public static void writeKeys(IntX n, IntX e, IntX d, string privPath, string pubPath) { string[] pub = new string[2]; pub[0] = n.ToString(); pub[1] = e.ToString(); System.IO.File.WriteAllLines(pubPath, pub); string[] pri = new string[2]; pri[0] = n.ToString(); pri[1] = d.ToString(); System.IO.File.WriteAllLines(privPath, pri); }
public override string ToString() { IntX afterPoint = null; IntX beforePoint = IntX.DivideModulo(_integerPart, _scale, out afterPoint, DivideMode.AutoNewton); return(beforePoint.ToString() + "." + afterPoint.ToString()); }
public void CompareWithClassicRandom() { TestHelper.Repeat( RandomRepeatCount, delegate { string str = GetRandomChars(); IntX x = IntX.Parse(str, ParseMode.Fast); x.Settings.ToStringMode = ToStringMode.Fast; string strFast = x.ToString(); x.Settings.ToStringMode = ToStringMode.Classic; string strClassic = x.ToString(); Assert.AreEqual(str, strFast); Assert.AreEqual(strFast, strClassic); }); }
public DecimalX(IntX integerPart, IntX scale) { _integerPart = integerPart; _scale = scale; if (scale.ToString().Any(i => i != '0' && i != '1')) // dirty but works { throw new Exception("Scale must be 10^X"); } }
public void Big() { IntX intX = new IntX(int.MaxValue); intX += intX += intX += intX; long longX = int.MaxValue; longX += longX += longX += longX; Assert.AreEqual(intX.ToString(), longX.ToString()); }
public void CompareWithClassic() { TestHelper.Repeat( RepeatCount, delegate { string str = GetAllNineChars(_length); IntX x = IntX.Parse(str, ParseMode.Fast); x.Settings.ToStringMode = ToStringMode.Fast; string strFast = x.ToString(); x.Settings.ToStringMode = ToStringMode.Classic; string strClassic = x.ToString(); Assert.AreEqual(str, strFast); Assert.AreEqual(strFast, strClassic); _length += LengthIncrement; }); }
public String GenerateBinaryString(IntX number) { Char[] str = new Char[this.size]; for (int i = 0; i < str.Length; i++) str[i] = '0'; var temp = number.ToString(2); for (int i = 0; i < temp.Length; i++) str[i] = temp[i]; return new String(str); }
public string ToString(int radix, bool pad = false) { string result = _value.ToString((uint)radix); if (pad) { long maxDigits = Math.Max((long)Math.Ceiling((double)Size * Math.Log(2.0, radix)), 1); long npad = maxDigits - result.Length; result = StringHelpers.Zeros(npad) + result; } return(result); }
//Writes encrypted data to file. public static void writeToFile(IntX[] encrypted, string path, IntX key_n, IntX key_d) { string[] output = new string[encrypted.Length + 2]; output[0] = key_n.ToString(); output[1] = key_d.ToString(); int count = 2; for (int i = 0; i < encrypted.Length; i++) { output[count] = encrypted[i].ToString(); count++; } //System.IO.File.WriteAllText(path, key_n.ToString()); //System.IO.File.WriteAllText(path, key_d.ToString()); System.IO.File.WriteAllLines(path, output); }
//Writes encrypted data to file. public static void writeToFile(IntX[] encrypted, string path, IntX key_n, IntX key_d ) { string[] output = new string[encrypted.Length+2]; output[0] = key_n.ToString(); output[1] = key_d.ToString(); int count = 2; for(int i = 0; i < encrypted.Length; i++) { output[count] = encrypted[i].ToString(); count++; } //System.IO.File.WriteAllText(path, key_n.ToString()); //System.IO.File.WriteAllText(path, key_d.ToString()); System.IO.File.WriteAllLines(path, output); }
public override String ToString() { return(_v.ToString()); }
public void BigDec() { IntX intX = IntX.Parse("34589238954389567586547689234723587070897800300450823748275895896384753238944985"); Assert.AreEqual(intX.ToString(), "34589238954389567586547689234723587070897800300450823748275895896384753238944985"); }
public void Binary() { IntX intX = new IntX(19); Assert.AreEqual(intX.ToString(2), "10011"); }
public void OtherBase() { IntX intX = new IntX(-144); Assert.AreEqual(intX.ToString(140), "-{1}{4}"); }
public void HexLower() { IntX intX = new IntX(0xFF00FF00FF00FF); Assert.AreEqual(intX.ToString(16, false), "ff00ff00ff00ff"); }
public void Zero() { IntX intX = new IntX(); Assert.AreEqual(intX.ToString(), "0"); }
public static IntX GeneratePrime(IntX maximum) { return MathAlgs.GeneratePrime(maximum.ToString(2).Length - 1); }
public void Hex() { IntX intX = new IntX(0xABCDEF); Assert.AreEqual(intX.ToString(16), "ABCDEF"); }
public static IEnumerable <int> Digits(this IntX source) { return(source.ToString().Select(x => Convert.ToInt32("" + x))); }
public void Octal2() { IntX intX = new IntX(901); Assert.AreEqual(intX.ToString(8), "1605"); }
public void Octal() { IntX intX = new IntX(100); Assert.AreEqual(intX.ToString(8), "144"); }
private void button1_Click(object sender, EventArgs e) { string line; int start, end, brojac = 0; using (System.IO.StreamReader file = new System.IO.StreamReader("put.txt")) { while ((line = file.ReadLine()) != null) //čita kriptirani put i sprema brojke u niz "put" { start = line.IndexOf(".") + 1; end = line.Length; string brojs = line.Substring(start, end - start); brojs = brojs.Replace(" ", ""); put[brojac] = Int32.Parse(brojs); brojac++; } } brojac = 0; using (System.IO.StreamReader file = new System.IO.StreamReader("vrhovi.txt")) //čita i sprema ključeve { while ((line = file.ReadLine()) != null) { start = line.IndexOf("(") + 1; //čita N iz .txt end = line.IndexOf(","); string brojs = line.Substring(start, end - start); n[brojac] = Int32.Parse(brojs); start = line.IndexOf(",") + 1; //čita javni end = line.IndexOf(")"); brojs = line.Substring(start, end - start); pub[brojac] = uint.Parse(brojs); start = line.LastIndexOf(",") + 1; //čita privatni end = line.LastIndexOf(")"); brojs = line.Substring(start, end - start); priv[brojac] = uint.Parse(brojs); brojac++; } } int tren = 1; decPut[0] = "V01"; //početni vrh listBox1.Items.Add("v1"); string temp; for (int i = 0; i < 15; i++) { IntX dekript = (IntX.Pow(put[i], priv[tren - 1]) % n[tren - 1]); //RSA dekripcija sljedeceg u nizu temp = dekript.ToString(); tren = Int32.Parse(temp); if (tren < 10) { decPut[i + 1] = "V0" + tren.ToString(); //provjera da li je vrh manji od 10 samo radi ispisa s nulom ispred } else { decPut[i + 1] = "V" + tren.ToString(); } listBox1.Items.Add("v" + dekript.ToString()); } int suma = 0, tezina; for (int i = 0; i < 14; i++) { using (System.IO.StreamReader file = new System.IO.StreamReader("tezine.txt")) { while ((line = file.ReadLine()) != null) //čita težine { if (line.Contains(decPut[i]) && line.Contains(decPut[i + 1])) //ako linija sadrži trenutačnog i sljedeceg u nizu, uzima njihovu tezinu { tezina = int.Parse(line.Substring(line.LastIndexOf('=') + 1)); suma = suma + tezina; //tezina se pribraja sumi puta } } } } label2.Text = "Tezina: " + suma; //ispis tezine puta }
public void Simple() { IntX intX = new IntX(12345670); Assert.AreEqual(intX.ToString(), "12345670"); }
public void VerySimple() { IntX intX = new IntX(11); Assert.AreEqual(intX.ToString(), "11"); }
public void Neg() { IntX intX = new IntX(int.MinValue); Assert.AreEqual(intX.ToString(), int.MinValue.ToString()); }
private void button1_Click(object sender, EventArgs e) { string line; int start, end, x1, x2, pocetni, cilj; pocetni = Int32.Parse(textBox1.Text); cilj = Int32.Parse(textBox2.Text); for (int i = 0; i < 61; i++) //namjestiti inicijalne vrijednosti { brojac[i] = 0; oznacen[i] = 0; tren_tez[i] = 1000; } tren_tez[pocetni] = 0; //tezina pocetnog je nula oznacen[pocetni] = 1; //pocetni je posjecen odmah prethodni[pocetni] = 0; //pocetni nema prethodnog using (System.IO.StreamReader file = new System.IO.StreamReader("tezine.txt")) //citanje iz fajla { while ((line = file.ReadLine()) != null) { start = line.IndexOf("(") + 1; //parsira prvi vrh u zapisu u redu i pamti ga end = line.IndexOf(",", start); string prvi = line.Substring(start, end - start); prvi = prvi.Replace("V", ""); start = line.IndexOf(",") + 1; //drugi end = line.IndexOf(")", start); string drugi = line.Substring(start, end - start); drugi = drugi.Replace("V", ""); x1 = int.Parse(prvi); x2 = int.Parse(drugi); susjedi[x1, brojac[x1]] = x2; //susjed od prvog je drugi susjedi[x2, brojac[x2]] = x1; //susjed od drugog je prvi. Pamti se oba smjera radi lakseg izracuna brojac[x1]++; //brojac broja susjeda brojac[x2]++; veze[x1, x2] = int.Parse(line.Substring(line.LastIndexOf('=') + 1)); //sprema tezinu veze[x2, x1] = veze[x1, x2]; } } bool provjera = true; int tren_vrh = pocetni; //postavljanje pocetnog kao trenutnog vrha int temp_tez, min = 1000, k = 0; while (provjera) { if (prov_oznaceni()) { provjera = false; //funkcija provjerava da li su posjeceni svi vrhovi, ako jesu zavrsava petlju. } else { min = 0; for (int i = 0; i < 61; i++) { if (veze[tren_vrh, susjedi[tren_vrh, i]] != 0) //ako je tezina razlicita od nula, postoji veza izmedu vrhova { temp_tez = tren_tez[tren_vrh] + veze[tren_vrh, susjedi[tren_vrh, i]]; //uzima se ukupna tezina do svakog od susjeda od trenutacnog vrha if (temp_tez < tren_tez[susjedi[tren_vrh, i]]) //ako je manja od dosadanje ukupne tezine do dotičnog vrha, uzima se kao nova { prethodni[susjedi[tren_vrh, i]] = tren_vrh; //tren. vrh se postavlja kao novi prethodni vrh u najkracem putu tren_tez[susjedi[tren_vrh, i]] = temp_tez; } if (tren_tez[susjedi[tren_vrh, i]] < tren_tez[min] && oznacen[susjedi[tren_vrh, i]] == 0) { min = susjedi[tren_vrh, i]; //uzima se sljedeci najmanji susjed } } } oznacen[tren_vrh] = 1; //trenutačni vrh se postavlja da je posjecen if (prov_susjeda(tren_vrh)) //provjerava se posjecenost susjeda tren vrha. Ako su svi posjeceni, vraca se unatrag na prethodni vrh u privremenom putu. { if (k > 0) { k = k - 1; min = tren_put[k]; } } else { tren_put[k] = tren_vrh; //tren vrh se postavlja u privremeni put k++; } tren_vrh = min; //sljedeci vrh je odabrani najmanji susjed } } provjera = true; string ispis = pocetni.ToString(); int[] put = new int[61]; int prebroj = 0; tren_vrh = cilj; //najkraci put do cilja se iscitava od odozada while (provjera) { if (tren_vrh != pocetni) { put[prebroj] = tren_vrh; tren_vrh = prethodni[tren_vrh]; //iscitavaju se prethodni od vrha, i tako od cilja do pocetnog. prebroj++; } else { put[prebroj] = tren_vrh; tren_vrh = prethodni[tren_vrh]; //spremi se i pocetni prebroj++; provjera = false; } } for (int i = prebroj - 2; i >= 0; i--) { ispis += "->" + put[i].ToString(); //put od cilja do pocetnog se obrnuto sprema u ispis } label2.Text = "Put: " + ispis; label4.Text = "Tezina: " + tren_tez[cilj].ToString(); int[] n = new int[60]; uint[] pub = new uint[60]; uint[] priv = new uint[60]; int prebroj2 = 0; using (System.IO.StreamReader file = new System.IO.StreamReader("vrhovi.txt")) { while ((line = file.ReadLine()) != null) { start = line.IndexOf("(") + 1; //čita N iz .txt end = line.IndexOf(","); string brojs = line.Substring(start, end - start); n[prebroj2] = Int32.Parse(brojs); start = line.IndexOf(",") + 1; //čita javni end = line.IndexOf(")"); brojs = line.Substring(start, end - start); pub[prebroj2] = uint.Parse(brojs); start = line.LastIndexOf(",") + 1; //čita privatni end = line.LastIndexOf(")"); brojs = line.Substring(start, end - start); priv[prebroj] = uint.Parse(brojs); prebroj2++; } } listBox1.Items.Clear(); listBox1.Items.Add("Pocetni vrh: " + pocetni.ToString()); if (!File.Exists("ispis.txt")) { File.CreateText("ispis.txt").Close(); } using (StreamWriter sw = File.AppendText("ispis.txt")) { sw.WriteLine("Pocetni vrh: " + pocetni.ToString()); for (int i = prebroj - 2; i >= 0; i--) { IntX enkript = (IntX.Pow(put[i], pub[put[i + 1] - 1]) % n[put[i + 1] - 1]); //enkriptira broj sljedeceg vrha u putu s javnim kljucem trenutacnog listBox1.Items.Add(enkript.ToString()); sw.WriteLine(enkript.ToString()); } sw.WriteLine("Kraj puta"); } MessageBox.Show("Kreirana datoteka ispis.txt"); }
public static BitArray GenerateBitArray(IntX number) { string numberBinary = number.ToString(2); BitArray br = new BitArray(numberBinary.Length); for (int i = 0; i < br.Length; i++) { if (numberBinary[i] == '1') br[i] = true; } return br; }