Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms.DataVisualization.Charting;
- namespace Projekt3_Gwiazdowski51192
- {
- public partial class Analizator : Form
- {
- public Analizator()
- {
- InitializeComponent();
- }
- bool PKpobranieWynik(out float PKx, out float PKepsX)
- {
- float PKgranXd = -1.0F;
- float PKgranXg = 1.0F;
- float PKgranEpsXd = 0.0F;
- float PKgranEpsXg = 1.0F;
- PKx = 0.0F; PKepsX = 0.0F;
- if (string.IsNullOrEmpty(txtX.Text) || !float.TryParse(txtX.Text, out PKx))
- {
- errorProvider1.SetError(txtX, "Podaj poprawną wartość X");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- if (PKgranXd > PKx || PKx >= PKgranXg)
- {
- errorProvider1.SetError(txtX, "X musi spełniać wyznaczone warunki!");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if (string.IsNullOrEmpty(txtEpsX.Text) || !float.TryParse(txtEpsX.Text, out PKepsX))
- {
- errorProvider1.SetError(txtEpsX, "Wartość EpsX wykracza poza wytyczne");
- return false;
- }
- if (PKgranEpsXd >= PKepsX || PKepsX >= PKgranEpsXg)
- {
- errorProvider1.SetError(txtEpsX, "Wartość wykracza poza granice");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- return true;
- }
- bool PKpobranieTabela(out float PKxd, out float PKxg, out float PKh, out float PKepsXdlaTablicy)
- {
- float PKgranHd = 0.0F;
- float PKgranHg = 1.0F;
- PKxd = 0.0F;
- PKxg = 0.0F;
- PKh = 0.0F;
- PKepsXdlaTablicy = 0.0F;
- if (string.IsNullOrEmpty(txtXd.Text) || !float.TryParse(txtXd.Text, out PKxd))
- {
- errorProvider1.SetError(txtXd, "Wartość Xd wykracza poza wytyczne");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if (string.IsNullOrEmpty(txtXg.Text) || !float.TryParse(txtXg.Text, out PKxg))
- {
- errorProvider1.SetError(txtXg, "Wartość Xg wykracza poza wytyczne");
- return false;
- }
- if (PKxg < PKxd)
- {
- errorProvider1.SetError(txtXg, "Xg nie może być mniejsze niż Xd");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if (string.IsNullOrEmpty(txtH.Text) || !float.TryParse(txtH.Text, out PKh))
- {
- errorProvider1.SetError(txtH, "Wartość H wykracza poza wytyczne");
- return false;
- }
- if (PKgranHd >= PKh || PKh >= PKgranHg)
- {
- errorProvider1.SetError(txtH, "H nie spełnia reguł zadania");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- float PKgranEpsXdt = 0.0F;
- float PKgranEpsXgt = 1.0F;
- if (string.IsNullOrEmpty(txtEpsTablica.Text) || !float.TryParse(txtEpsTablica.Text, out PKepsXdlaTablicy))
- {
- errorProvider1.SetError(txtEpsTablica, "Wartość EpsX wykracza poza wytyczne");
- return false;
- }
- if (PKepsXdlaTablicy <= PKgranEpsXdt || PKepsXdlaTablicy >= PKgranEpsXgt)
- {
- errorProvider1.SetError(txtEpsTablica, "Wartość wykracza poza granice");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- return true;
- }
- bool PKpobranieCalka(out float PKepsCalka, out float PKcd, out float PKcg, out float PKdokladnosc)
- {
- float PKgranEpsXdCalka = 0.0F;
- float PKgranEpsXgCalka = 1.0F;
- float PKgranCd = -1.0F;
- float PKgranCg = 1.0F;
- PKepsCalka = 0.0F; PKcd = 0.0F; PKcg = 0.0F; PKdokladnosc = 0.0F;
- if (string.IsNullOrEmpty(txtEpsCalka.Text) || !float.TryParse(txtEpsCalka.Text, out PKepsCalka))
- {
- errorProvider1.SetError(txtEpsCalka, "Wartość EpsX wykracza poza wytyczne");
- return false;
- }
- if (PKepsCalka <= PKgranEpsXdCalka || PKepsCalka >= PKgranEpsXgCalka)
- {
- errorProvider1.SetError(txtEpsCalka, "Wartość wykracza poza granice");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if (string.IsNullOrEmpty(txtCD.Text) || !float.TryParse(txtCD.Text, out PKcd))
- {
- errorProvider1.SetError(txtCD, "Wartość dolnej granicy nie jest liczbą");
- return false;
- }
- if (PKcd <= PKgranCd || PKcd >= PKgranCg)
- {
- errorProvider1.SetError(txtCD, "Wartość nie jest w zakresie");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if (string.IsNullOrEmpty(txtCG.Text) || !float.TryParse(txtCG.Text, out PKcg))
- {
- errorProvider1.SetError(txtCG, "Wartość górnej granicy nie jest liczbą");
- return false;
- }
- if (PKgranCd >= PKcg || PKcg >= PKgranCg)
- {
- errorProvider1.SetError(txtCD, "Wartość nie jest w zakresie");
- return false;
- }
- if (PKcg < PKcd)
- {
- errorProvider1.SetError(txtCG, "Górna granica nie może być mniejsza niż dolna");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if (string.IsNullOrEmpty(txtDokladnosc.Text) || !float.TryParse(txtDokladnosc.Text, out PKdokladnosc))
- {
- errorProvider1.SetError(txtDokladnosc, "Wartość dokładności nie jest liczbą");
- return false;
- }
- if (PKdokladnosc <= PKgranEpsXdCalka || PKdokladnosc > PKgranEpsXgCalka)
- {
- errorProvider1.SetError(txtDokladnosc, "Wartość wykracza poza granice");
- return false;
- }
- else
- {
- errorProvider1.Dispose();
- }
- return true;
- }
- float PKsumaSzeregu(float KGx, float PKepsX, out ushort KGlicznik)
- {
- float KGw, KGsuma;
- KGlicznik = 1;
- KGw = (float)Math.Pow(KGx, (2 * (KGlicznik + 1))) / (2 * (KGlicznik + 1));
- KGsuma = 0.0F;
- do
- {
- KGsuma += KGw;
- KGlicznik++;
- KGw = KGw * (float)Math.Pow(KGx, (2 * (KGlicznik + 1))) / (2 * (KGlicznik + 1));
- } while (Math.Abs(KGw) > PKepsX);
- KGsuma = KGsuma * 2;
- return KGsuma;
- }
- float PKtablicowanie(float PKxd, float PKxg, float PKh, float PKepsXdlaTablicy, out float[,] Tablica)
- {
- int ile = 0;
- for (float i = PKxd; i <= PKxg; i += PKh)
- {
- ile++;
- }
- Tablica = new float[ile, 3];
- for (int j = 0; j < Tablica.GetLength(0); j++)
- {
- Tablica[j, 0] = PKxd;
- Tablica[j, 1] = PKsumaSzeregu(PKxd, PKepsXdlaTablicy, out ushort PKlicznikTablica);
- Tablica[j, 2] = PKlicznikTablica;
- PKxd = PKxd + PKh;
- dgvTablicaSzeregu.Rows.Add();
- dgvTablicaSzeregu.Rows[j].Cells[0].Value = string.Format("{0:0.000000}", Tablica[j, 0]);
- dgvTablicaSzeregu.Rows[j].Cells[1].Value = string.Format("{0:0.0000000000000}", Tablica[j, 1]);
- dgvTablicaSzeregu.Rows[j].Cells[2].Value = string.Format("{0:0}", Tablica[j, 2]);
- if (j % 2 == 0)
- {
- dgvTablicaSzeregu.Rows[j].DefaultCellStyle.BackColor = Color.LightGray;
- }
- else
- {
- dgvTablicaSzeregu.Rows[j].DefaultCellStyle.BackColor = Color.White;
- }
- }
- return PKxd;
- }
- float PKcalkowanieProstokat(float PKepsCalka, float PKcd, float PKcg, float PKdokladnosc, out int PKlicznikPrzedzialu, out float PKszerokosc)
- {
- float PKh, PKci, PKci_1, PKsumaFx;
- ushort PKlicznikWyrazowSzeregu;
- float PKxxx;
- PKlicznikPrzedzialu = 1;
- PKci = (PKcg - PKcd) * PKsumaSzeregu((PKcd + PKcg) / 2.0F, PKepsCalka, out PKlicznikWyrazowSzeregu);
- do
- {
- PKci_1 = PKci;
- PKlicznikPrzedzialu = PKlicznikPrzedzialu + PKlicznikPrzedzialu;
- PKh = (PKcg - PKcd) / PKlicznikPrzedzialu;
- PKxxx = PKcd + PKh / 2.0F;
- PKsumaFx = 0.0F;
- for (ushort i = 0; i < PKlicznikPrzedzialu; i++)
- {
- PKsumaFx += PKsumaSzeregu(PKxxx + i * PKh, PKepsCalka, out PKlicznikWyrazowSzeregu);
- }
- PKci = PKh * PKsumaFx;
- } while (Math.Abs(PKci - PKci_1) > PKdokladnosc);
- PKszerokosc = PKh;
- return PKci;
- }
- float PKcalkowanieTrapez(float PKepsCalka, float PKcd, float PKcg, float PKdokladnosc, out int PKlicznikPrzedzialu, out float PKszerokosc)
- {
- float PKX;
- float PKh, PKci, PKci_1, PKsumaFx;
- PKlicznikPrzedzialu = 0;
- ushort PKlicznikWyrazowSzeregu;
- float PKxxx;
- float PKsumaTrapez = 0;
- for (int i = 1; i < PKdokladnosc; i++)
- {
- PKX = PKcd + (i / PKdokladnosc) * (PKcg - PKcd);
- PKsumaTrapez += PKsumaSzeregu((PKcd + PKcg) / 2.0F, PKepsCalka, out PKlicznikWyrazowSzeregu); ;
- }
- PKX = PKcd;
- float KPPierwszaFunkcja = PKsumaSzeregu((PKX) / 2.0F, PKepsCalka, out PKlicznikWyrazowSzeregu);
- PKX = PKcd + (PKcg - PKcd);
- float KPOstatniaFunkcja = PKsumaSzeregu((PKX) / 2.0F, PKepsCalka, out PKlicznikWyrazowSzeregu);
- PKsumaTrapez += (KPPierwszaFunkcja + KPOstatniaFunkcja) / 2;
- PKszerokosc = ((PKcg - PKcd) / PKdokladnosc);
- return PKszerokosc * PKsumaTrapez;
- }
- private void btnWynik_Click(object sender, EventArgs e)
- {
- float PKx, PKepsX;
- ushort PKlicznik;
- if (!PKpobranieWynik(out PKx, out PKepsX))
- {
- return;
- }
- txtWynik.Text = string.Format("{0:0.00000000}", PKsumaSzeregu(PKx, PKepsX, out PKlicznik));
- txtN.Text = PKlicznik.ToString();
- pbRownosc.Visible = true;
- txtWynik.Visible = true;
- lblN.Visible = true;
- txtN.Visible = true;
- }
- private void btnTabelaryczna_Click(object sender, EventArgs e)
- {
- float PKxd, PKxg, PKh;
- float PKepsXdlaTablicy;
- if (!PKpobranieTabela(out PKxd, out PKxg, out PKh, out PKepsXdlaTablicy))
- {
- return;
- }
- else
- {
- PKtablicowanie(PKxd, PKxg, PKh, PKepsXdlaTablicy, out float[,] Tablica);
- }
- dgvTablicaSzeregu.Visible = true;
- btnTabelaryczna.Visible = false;
- chWykresFunkcji.Visible = false;
- }
- private void btnGraficzna_Click(object sender, EventArgs e)
- {
- this.chWykresFunkcji.Visible = true;
- btnTabelaryczna.Visible = true;
- dgvTablicaSzeregu.Visible = false;
- btnGraficzna.Visible = false;
- float PKxd, PKxg, PKh;
- float PKepsXdlaTablicy;
- if (!PKpobranieTabela(out PKxd, out PKxg, out PKh, out PKepsXdlaTablicy))
- {
- return;
- }
- else
- {
- PKtablicowanie(PKxd, PKxg, PKh, PKepsXdlaTablicy, out float[,] Tablica);
- chWykresFunkcji.Series.Clear();
- chWykresFunkcji.Titles.Clear();
- chWykresFunkcji.Titles.Add("Wykres sumy szeregów funkcji");
- chWykresFunkcji.Series.Add("Series");
- chWykresFunkcji.ChartAreas[0].AxisX.Title = "Numer obliczenia";
- chWykresFunkcji.ChartAreas[0].AxisY.Title = "Wartość sumy";
- chWykresFunkcji.Series[0].IsVisibleInLegend = true;
- chWykresFunkcji.Legends.FindByName("Legend1").Docking = Docking.Bottom;
- chWykresFunkcji.Series[0].Name = "Wykres zmiany sumy szeregu";
- chWykresFunkcji.Series[0].ChartType = SeriesChartType.Line;
- chWykresFunkcji.Series[0].Color = Color.Red;
- chWykresFunkcji.Series[0].BorderDashStyle = ChartDashStyle.Dot;
- chWykresFunkcji.Series[0].BorderWidth = 2;
- for (int i = 0; i < Tablica.GetLength(0); i++)
- {
- chWykresFunkcji.Series[0].Points.AddXY(i, Tablica[i, 1]);
- }
- }
- }
- private void btnObliczCalke_Click(object sender, EventArgs e)
- {
- float PKepsCalka, PKcd, PKcg, PKdokladnosc;
- if (!PKpobranieCalka(out PKepsCalka, out PKcd, out PKcg, out PKdokladnosc))
- {
- return;
- }
- if (cbMetody.SelectedIndex == 0)
- {
- txtWynikCalki.Text = string.Format("{0:0.00000000}", PKcalkowanieProstokat(PKepsCalka, PKcd, PKcg, PKdokladnosc, out int PKlicznikPrzedzialu, out float PKszerokosc));
- lblWynikCalki.Visible = true;
- txtWynikCalki.Visible = true;
- }
- else if (cbMetody.SelectedIndex == 1)
- {
- txtWynikCalki.Text = string.Format("{0:0.00000000}", PKcalkowanieTrapez(PKepsCalka, PKcd, PKcg, PKdokladnosc, out int PKlicznikPrzedzialu, out float PKszerokosc));
- lblWynikCalki.Visible = true;
- txtWynikCalki.Visible = true;
- lblWynikCalki.Visible = true;
- txtWynikCalki.Visible = true;
- }
- else
- {
- errorProvider1.SetError(cbMetody, "Wybierz metodę!");
- }
- }
- private void btnResetuj_Click(object sender, EventArgs e)
- {
- Application.Restart();
- }
- /* Opisy lini wykresu */
- private void OpisLini_CheckedChanged(object sender, EventArgs e)
- {
- chWykresFunkcji.Titles.Clear();
- chWykresFunkcji.Titles.Add("Wykres sumy szeregów funkcji");
- chWykresFunkcji.Legends.Clear();
- chWykresFunkcji.Legends.Add("Legend1").Docking = Docking.Bottom;
- chWykresFunkcji.Series[0].Name = "Wykres zmiany sumy szeregu";
- chWykresFunkcji.ChartAreas[0].AxisX.Title = "Numer obliczenia";
- chWykresFunkcji.ChartAreas[0].AxisY.Title = "Wartość sumy";
- }
- private void BrakOpisuLini_CheckedChanged(object sender, EventArgs e)
- {
- chWykresFunkcji.Titles.Clear();
- chWykresFunkcji.Legends.Clear();
- chWykresFunkcji.ChartAreas[0].AxisX.Title = "";
- chWykresFunkcji.ChartAreas[0].AxisY.Title = "";
- }
- /* Kolory menu */
- private void kolorTłaWykresuToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ColorDialog PKkolorTla = new ColorDialog();
- PKkolorTla.Color = chWykresFunkcji.BackColor;
- if (PKkolorTla.ShowDialog() == DialogResult.OK)
- {
- chWykresFunkcji.BackColor = PKkolorTla.Color;
- }
- }
- private void kolorLiniiWykresuToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ColorDialog PKkolorLinia = new ColorDialog();
- PKkolorLinia.Color = chWykresFunkcji.Series[0].Color;
- if (PKkolorLinia.ShowDialog() == DialogResult.OK)
- {
- chWykresFunkcji.Series[0].Color = PKkolorLinia.Color;
- }
- }
- /*Grubość lini na wykresie */
- private void toolStripMenuItem2_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].BorderWidth = 1;
- }
- private void toolStripMenuItem3_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].BorderWidth = 2;
- }
- private void toolStripMenuItem4_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].BorderWidth = 3;
- }
- private void toolStripMenuItem5_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].BorderWidth = 4;
- }
- /*Styl lini na wykresie */
- private void kropkowaToolStripMenuItem_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].BorderDashStyle = ChartDashStyle.Dot;
- }
- private void kreskowaToolStripMenuItem_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].BorderDashStyle = ChartDashStyle.Dash;
- }
- private void kreskowokropkowaToolStripMenuItem_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].BorderDashStyle = ChartDashStyle.DashDot;
- }
- private void ciagłaToolStripMenuItem_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].BorderDashStyle = ChartDashStyle.Solid;
- }
- /*Typy wykresu */
- private void punktowyToolStripMenuItem_Click_1(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].ChartType = SeriesChartType.Point;
- }
- private void liniowyToolStripMenuItem_Click_1(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].ChartType = SeriesChartType.Line;
- }
- private void kolumnowyToolStripMenuItem_Click_1(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].ChartType = SeriesChartType.Column;
- }
- private void słupkowyToolStripMenuItem_Click(object sender, EventArgs e)
- {
- chWykresFunkcji.Series[0].ChartType = SeriesChartType.Bar;
- }
- /*Event zamykania */
- private void zamknijToolStripMenuItem_Click(object sender, EventArgs e)
- {
- DialogResult PKpytanie = MessageBox.Show("Czy rzeczywiście chcesz zamknąć ten formularz?", this.Text,
- MessageBoxButtons.YesNoCancel,
- MessageBoxIcon.Question,
- MessageBoxDefaultButton.Button3
- );
- switch (PKpytanie)
- {
- case DialogResult.Yes:
- MessageBox.Show("Teraz nastąpi zamknięcie formularza: " + this.Text);
- this.Close();
- break;
- case DialogResult.No:
- MessageBox.Show("Formularz nie będzie jednak zamknięty!!! ");
- break;
- case DialogResult.Cancel:
- MessageBox.Show("Anulowanie zamknięcia formularza!");
- break;
- }
- }
- /*zapis/odczyt pliku */
- private void zapiszTablicęWPlikuToolStripMenuItem_Click(object sender, EventArgs e)
- {
- float PKxd, PKxg, PKh;
- float PKepsXdlaTablicy;
- if (!PKpobranieTabela(out PKxd, out PKxg, out PKh, out PKepsXdlaTablicy))
- {
- return;
- }
- else
- {
- PKtablicowanie(PKxd, PKxg, PKh, PKepsXdlaTablicy, out float[,] Tablica);
- SaveFileDialog PKoknoZapisuPliku = new SaveFileDialog();
- PKoknoZapisuPliku.Filter = "txt files (*.txt)|*.txt|All Files(*,*)|*.*";
- PKoknoZapisuPliku.FilterIndex = 1;
- PKoknoZapisuPliku.RestoreDirectory = true;
- PKoknoZapisuPliku.InitialDirectory = "D:\\";
- PKoknoZapisuPliku.Title = "Zapisanie wyników";
- if (PKoknoZapisuPliku.ShowDialog() == DialogResult.OK)
- {
- StreamWriter PKplikZnakow = new StreamWriter(PKoknoZapisuPliku.OpenFile());
- for (int i = 0; i < Tablica.GetLength(0); i++)
- {
- PKplikZnakow.Write(string.Format(" {0,8:0.000000} \t", Tablica[i, 0]));
- PKplikZnakow.Write(string.Format(" {0,6:0.0000000000000} \t", Tablica[i, 1]));
- PKplikZnakow.Write(string.Format(" {0,8:0} \t", Tablica[i, 2]));
- PKplikZnakow.WriteLine("\t");
- }
- PKplikZnakow.Dispose();
- PKplikZnakow.Close();
- }
- }
- }
- private void odczytajTablicęZPlikuToolStripMenuItem_Click(object sender, EventArgs e)
- {
- dgvTablicaSzeregu.Rows.Clear();
- OpenFileDialog PKoknoOdczytu = new OpenFileDialog();
- PKoknoOdczytu.Title = "Otwarcie pliku z rozliczeniem lokaty kapitałowej";
- if (PKoknoOdczytu.ShowDialog() == DialogResult.OK)
- {
- string PKnazwaPlik = PKoknoOdczytu.FileName;
- string[] PKzawartosc = File.ReadAllLines(PKnazwaPlik);
- dgvTablicaSzeregu.RowTemplate.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
- foreach (string PKwiersz in PKzawartosc)
- {
- dgvTablicaSzeregu.Rows.Add(PKwiersz.Split('\t'));
- }
- dgvTablicaSzeregu.Visible = true;
- }
- }
- private void Analizator_Load(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement