This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
GradeCalc/GradeCalc/MainForm.cs

137 lines
6.1 KiB
C#
Raw Normal View History

2020-02-29 13:20:21 +01:00
using System;
2019-12-01 19:33:28 +01:00
using System.Collections.Generic;
using System.ComponentModel;
2020-02-29 13:20:21 +01:00
using System.Drawing;
2020-03-11 21:50:16 +01:00
using System.Globalization;
2020-02-29 13:20:21 +01:00
using System.IO;
2019-12-01 19:33:28 +01:00
using System.Linq;
using System.Windows.Forms;
2020-04-07 17:28:28 +02:00
using CC_Functions.W32.Forms;
2020-02-29 13:20:21 +01:00
using NCalc2;
2019-12-01 21:41:32 +01:00
using OfficeOpenXml;
using OfficeOpenXml.Style;
2020-03-11 21:50:16 +01:00
using LicenseContext = OfficeOpenXml.LicenseContext;
2019-12-01 19:33:28 +01:00
namespace GradeCalc
{
public partial class MainForm : Form
{
2020-03-11 21:50:16 +01:00
private readonly DataGridViewTextBoxColumn _gradeColumn;
private readonly DataGridViewTextBoxColumn _nameColumn;
2020-02-29 13:20:21 +01:00
private readonly List<DataGridViewNumericUpDownColumn>
2020-03-11 21:50:16 +01:00
_taskColumns = new List<DataGridViewNumericUpDownColumn>();
2019-12-01 19:33:28 +01:00
public MainForm()
{
InitializeComponent();
2020-03-11 21:50:16 +01:00
_nameColumn = new DataGridViewTextBoxColumn {HeaderText = "Name", Name = "Name"};
dataGridView.Columns.Add(_nameColumn);
_gradeColumn = new DataGridViewTextBoxColumn {HeaderText = "Grade", Name = "Grade", ReadOnly = true};
dataGridView.Columns.Add(_gradeColumn);
2019-12-01 19:33:28 +01:00
tasksNum_ValueChanged(null, null);
}
2020-03-11 21:50:16 +01:00
private static int Round(double val) => (int) Math.Round(val);
2020-02-29 13:20:21 +01:00
2020-03-11 21:50:16 +01:00
private static Color GetColor(double x, double max) =>
2020-03-16 13:48:04 +01:00
Color.FromArgb(Round(255 * (x / max)), Round(255 * (1 - (x / max))), 0);
2019-12-01 21:41:32 +01:00
2019-12-01 19:33:28 +01:00
private void calcButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView.Rows)
{
2020-03-11 21:50:16 +01:00
row.Cells[_nameColumn.Name].Style.BackColor = Color.White;
DataGridViewTextBoxCell gradeCell = (DataGridViewTextBoxCell) row.Cells[_gradeColumn.Name];
2019-12-01 19:41:55 +01:00
try
{
Expression ex = new Expression(algorithmBox.Text);
2019-12-01 21:41:32 +01:00
decimal maxScore = 0;
decimal totalScore = 0;
2020-03-11 21:50:16 +01:00
_taskColumns.ForEach(s =>
2019-12-01 21:41:32 +01:00
{
2020-02-29 13:20:21 +01:00
DataGridViewNumericUpDownCell cell = (DataGridViewNumericUpDownCell) row.Cells[s.Name];
2020-03-11 21:50:16 +01:00
if (string.IsNullOrWhiteSpace((string) cell.FormattedValue)) return;
maxScore += cell.Maximum;
totalScore += decimal.Parse((string) cell.FormattedValue);
ex.Parameters["score"] = decimal.Parse((string) cell.FormattedValue);
ex.Parameters["maxScore"] = cell.Maximum;
cell.Style.BackColor = GetColor((float) NCalcDoubleParser.Parse(ex.Evaluate()), 1);
2019-12-01 21:41:32 +01:00
});
2020-02-29 13:20:21 +01:00
ex.Parameters["score"] = (double) totalScore;
ex.Parameters["maxScore"] = (double) maxScore;
2019-12-01 19:41:55 +01:00
double grade = 6 - (NCalcDoubleParser.Parse(ex.Evaluate()) * 5);
2020-03-11 21:50:16 +01:00
gradeCell.Value = (grade.ToString(CultureInfo.InvariantCulture).Length > 13
? grade.ToString(CultureInfo.InvariantCulture).Remove(13)
: grade.ToString(CultureInfo.InvariantCulture)) +
" " + TexGrade(grade);
gradeCell.Style.BackColor = GetColor(grade - 1, 5);
2019-12-01 19:41:55 +01:00
}
2020-03-11 21:50:16 +01:00
catch (Exception)
2019-12-01 19:41:55 +01:00
{
2019-12-01 21:41:32 +01:00
gradeCell.Value = "";
2019-12-01 19:41:55 +01:00
}
2019-12-01 19:33:28 +01:00
}
2020-03-11 21:50:16 +01:00
dataGridView.Sort(_nameColumn, ListSortDirection.Ascending);
2019-12-01 19:33:28 +01:00
}
private void tasksNum_ValueChanged(object sender, EventArgs e)
{
2020-03-11 21:50:16 +01:00
while (tasksNum.Value < _taskColumns.Count)
2019-12-01 19:33:28 +01:00
{
2020-03-11 21:50:16 +01:00
DataGridViewNumericUpDownColumn col = _taskColumns[_taskColumns.Count - 1];
2019-12-01 19:33:28 +01:00
dataGridView.Columns.Remove(col);
2020-03-11 21:50:16 +01:00
_taskColumns.Remove(col);
2019-12-01 19:33:28 +01:00
col.Dispose();
}
2020-03-11 21:50:16 +01:00
while (tasksNum.Value > _taskColumns.Count)
2019-12-01 19:33:28 +01:00
{
2020-03-11 21:50:16 +01:00
DataGridViewNumericUpDownColumn clm = new DataGridViewNumericUpDownColumn
{
Minimum = 0, Maximum = 10, Name = "Task " + (_taskColumns.Count + 1)
};
2019-12-01 19:33:28 +01:00
dataGridView.Columns.Add(clm);
2020-03-11 21:50:16 +01:00
_taskColumns.Add(clm);
2019-12-01 19:33:28 +01:00
}
2020-03-11 21:50:16 +01:00
_gradeColumn.DisplayIndex = dataGridView.Columns.Count - 1;
2019-12-01 19:33:28 +01:00
}
2020-03-11 21:50:16 +01:00
private static string TexGrade(double g)
2019-12-01 19:33:28 +01:00
{
2020-03-11 21:50:16 +01:00
double gGrade = new[] {-0.5, 0, 0.5}.OrderBy(x => Math.Abs((x - (g - Round(g))) + 1)).First();
return (char) (('A' + Round(g)) - 1) +
2020-02-29 13:20:21 +01:00
(g == 1 || gGrade == -0.5 ? "+" : g == 6 || gGrade == 0.5 ? "-" : "");
2019-12-01 21:41:32 +01:00
}
private void saveButton_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog
{
Filter = "Excel Spreadsheet|*.xlst"
};
2020-03-11 21:50:16 +01:00
if (dialog.ShowDialog() != DialogResult.OK) return;
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using ExcelPackage excel = new ExcelPackage();
ExcelWorksheet worksheet = excel.Workbook.Worksheets.Add("Worksheet1");
List<string[]> headerRow = new List<string[]>
{
dataGridView.Columns.OfType<DataGridViewColumn>().Select(s => s.HeaderText).ToArray()
};
string headerRange = "A1:" + char.ConvertFromUtf32(headerRow[0].Length + 64) + "1";
worksheet.Cells[headerRange].LoadFromArrays(headerRow);
dataGridView.Rows.OfType<DataGridViewRow>()
.Reverse().Skip(1).Reverse().ToList().ForEach(s =>
2019-12-01 21:41:32 +01:00
{
2020-03-11 21:50:16 +01:00
foreach (DataGridViewCell cell in s.Cells)
2019-12-01 21:41:32 +01:00
{
2020-03-11 21:50:16 +01:00
worksheet.Cells[cell.RowIndex + 2, cell.ColumnIndex + 1].Value = cell.Value.ToString();
worksheet.Cells[cell.RowIndex + 2, cell.ColumnIndex + 1].Style.Fill.PatternType =
ExcelFillStyle.Solid;
worksheet.Cells[cell.RowIndex + 2, cell.ColumnIndex + 1].Style.Fill.BackgroundColor
.SetColor(cell.Style.BackColor);
}
});
excel.SaveAs(new FileInfo(dialog.FileName));
2019-12-01 19:33:28 +01:00
}
}
2020-02-29 13:20:21 +01:00
}