创建一个具有1000行的数据文件。文件中的每行包含一个教职员工的姓,名,级别以及薪水。第i行的教职员工的姓和名为FirstNamei 和LastNamei。级别随机产生为assistant(助理),associate(副)以及full(正)。薪水为随机产生的数字,并且小数点后保留俩位数字。对于助理教授而言,薪水应该在50000到80000的范围内,对于副教授为60000到110000,对于正教授为75000到130000.将生成的数据保存文件为salary.txt。 下面是一些示例数据:
FirstNameI LastNamel assistant
60055.95
FirstName2 LastName2 associate
81112.45
FirstName1000 LastName1000 full
92255.21
大数据的分析
编写一个程序,分别显示salar.txt文件中助理教授,副教授,正教授各个类别的总薪水,各类别的平均薪水以及各类别的职工数。
用java语言写。
嗯?最基本的东西?哪儿的毕业设计?
这个是最基本的东西,建议学一下python的pandas库,很容易感觉
人家都说了用java,还py个锤子,
Ծ‸Ծ
对这个薪水比较感兴趣。
为啥搞txt?
效果
附上c#代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
namespace cnblog_132765
{
class Program
{
readonly static SalaryBand assistant = new() { Name = "assistant", Floor = 50000, Upper = 80000 };
readonly static SalaryBand associate = new() { Name = "associate", Floor = 60000, Upper = 110000 };
readonly static SalaryBand full = new() { Name = "full", Floor = 75000, Upper = 130000 };
readonly static SalaryBand[] salaryBands = new SalaryBand[3] { assistant, associate, full };
static void Main(string[] args)
{
var file = CreateData();
using var fileStream = new FileStream(file, FileMode.Open);
using var reader = new StreamReader(fileStream, Encoding.Default);
List<Teacher> teachers = new();
var line = string.Empty;
var lineSalary = string.Empty;
string[] t = new string[3];
while (!string.IsNullOrWhiteSpace(line = reader.ReadLine()))
{
lineSalary = reader.ReadLine();
t = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
teachers.Add(new Teacher(t[0], t[2], double.Parse(lineSalary)));
}
// 统计
var group = teachers.GroupBy(t => t.Type).Select(g => new
{
JobTitle = g.Key.PadRight(10, ' '),
// 总额
Total = Math.Round(g.Sum(s => s.Salary), 2, MidpointRounding.ToEven),
Avg = Math.Round(g.Average(s => s.Salary), 2, MidpointRounding.ToEven),
Count = g.Count()
}).OrderBy(g => g.Total).ToList();
Console.WriteLine("职称 \t总金额 \t平均工资 \t总人数");
foreach (var g in group)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}", g.JobTitle, g.Total, g.Avg, g.Count);
}
Console.ReadKey();
}
static string CreateData()
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var fileName = Path.Combine(path, Guid.NewGuid().ToString("N") + ".txt");
using var file = new FileStream(Path.Combine(path, fileName), FileMode.OpenOrCreate);
var sb = new StringBuilder();
int cnt = 1000;
Random random = new Random();
for (int i = 1; i <= cnt; i++)
{
var randomBands = salaryBands[random.Next(0, 3)];
sb.AppendLine($"FirstName{i} LastName{i} {randomBands.Name}");
sb.AppendLine($"{random.Next(randomBands.Floor, randomBands.Upper) + Math.Round(random.NextDouble(), 2)}");
}
var buffer = Encoding.Default.GetBytes(sb.ToString());
file.Write(buffer, 0, buffer.Length);
return fileName;
}
}
struct SalaryBand
{
public string Name;
public int Floor;
public int Upper;
}
public record Teacher
{
public string FirstName { get; }
public Teacher(string firstName, string type, double salary)
{
FirstName = firstName;
Type = type;
Salary = salary;
}
public string Type { get; }
public double Salary { get; }
}
}
这也可以是毕业设计吗
毕业设计,这个可以忽悠到老师吗?老师都觉得太简单了吧
所以连毕业也要蒙混过关吗
这是毕业设计 ????