[编者注:]编程是最能体现创新的,一点不假。学分、成绩、积点,这三项数据,可写出不同的程序,特别是积点的实现。今天再发一个这样的大作业,因为发太专业的作业,参考意义受限,还是以大家都能理解的数据为计算对象,包括图像处理,以前发的也偏多,其实总占比并不是那么多。当然对于大多数同学来说,还是以专业为背景完成大作业的。在有添加记录功能的应用程序中,输入有限选项的内容(比如:性别,男、女),一般采用弹出框选择的方法,一是录入速度快,且方便程序处理,因为内容输入标准化,不会出现录入内容的不统一,造成在查询环节,不能完成匹配,这是该作业最值得推荐的地方。
一、程序功能
本程序为一个绩点计算器,通过输入课程数据,选择不同的绩点算法,可以进行绩点的计算。
1. 数据的输入
通过点击右上方的“导入数据”按钮或者在编辑框中逐个添加课程条目可以进行数据的输入,输入的课程数据会显示在左侧的表格中,每行包括课程的名称、百分制得分、课程所占学分、课程类型以及修读类型。
2. 数据的修改
如果需要对数据进行修改,可以直接在表格上修改。
3. 数据的删除
通过选中表格的一行再点击右侧的“删除”按钮可以删除表格中的一行数据,也可以通过右上方的“清除数据”按钮来删除整个表格的数据。
4. 数据的计算
点击“计算”按钮,表格中数据的计算结果会显示在左下方。还可以通过修改下拉选择框“绩点算法”进行不同类型的绩点计算,包含“标准 4.0 值算法”、“改进 4.0 值算法”、“4.8 制算法”以及“百分制均绩”。
5. 窗口的关闭
二、程序演示
1、 程序启动界面
2、导入数据
3、完成导入后成绩显示在uitable中,Excel文件的界面显示。
4、选择绩点制式,点击计算按钮,可得结果
5、添加课程,特别是课程类型和修读类型的录入采用弹出菜单选择的方式,是一个亮点。
6、输入新课程后可重新计算总绩点
三、程序设计界面(GUI.fig)
四、程序文件(主程序加2个小程序)
1、主程序文件(GUI.m)
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% Last Modified by GUIDE v2.5 01-Dec-2021 10:48:28
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_OpeningFcn, ...
'gui_OutputFcn', @GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
%初始化
set(handles.scores, 'Data', []);
global subject score weight type time algorithm;
type = "必修";
time = "首修";
algorithm = "标准4.0制算法";
subject = "";
score = "";
weight = "";
guidata(hObject, handles);
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
%根据不同算法计算绩点
function cal_score_Callback(hObject, eventdata, handles)
global algorithm grade;
data = get(handles.scores, 'Data');
[m, n] = size(data);
num = 0;
sum = 0;
if(algorithm == "标准4.0制算法")
for i = 1:m
if(cell2mat(data(i, 4)) == "选修")
continue;
end
s = str2double(cell2mat(data(i, 2)));
w = str2double(cell2mat(data(i, 3)));
num = num + w;
if(s >= 90)
sum = sum + w * 4.0;
elseif(s >= 80)
sum = sum + w * 3.0;
elseif(s >= 70)
sum = sum + w * 2.0;
elseif(s >= 60)
sum = sum + w * 1.0;
end
end
grade = sum / num;
grade = num2str(grade);
set(handles.result_text, 'String', "总绩点:" + grade);
elseif(algorithm == "4.8制算法")
for i = 1:m
if(cell2mat(data(i, 4)) == "选修")
continue;
end
s = str2double(cell2mat(data(i, 2)));
w = str2double(cell2mat(data(i, 3)));
num = num + w;
if(s >= 96)
sum = sum + w * 4.8;
elseif(s >= 93)
sum = sum + w * 4.5;
elseif(s >= 90)
sum = sum + w * 4.0;
elseif(s >= 86)
sum = sum + w * 3.8;
elseif(s >= 83)
sum = sum + w * 3.5;
elseif(s >= 80)
sum = sum + w * 3.0;
elseif(s >= 76)
sum = sum + w * 2.8;
elseif(s >= 73)
sum = sum + w * 2.5;
elseif(s >= 70)
sum = sum + w * 2.0;
elseif(s >= 66)
sum = sum + w * 1.8;
elseif(s >= 63)
sum = sum + w * 1.5;
elseif(s >= 60)
sum = sum + w * 1.0;
end
end
grade = sum / num;
grade = num2str(grade);
set(handles.result_text, 'String', "总绩点:" + grade);
elseif(algorithm == "改进4.0制算法")
for i = 1:m
if(cell2mat(data(i, 4)) == "选修")
continue;
end
s = str2double(cell2mat(data(i, 2)));
w = str2double(cell2mat(data(i, 3)));
num = num + w;
if(s >= 85)
sum = sum + w * 4.0;
elseif(s >= 70)
sum = sum + w * 3.0;
elseif(s >= 60)
sum = sum + w * 2.0;
end
end
grade = sum / num;
grade = num2str(grade);
set(handles.result_text, 'String', "总绩点:" + grade);
else
for i = 1:m
if(cell2mat(data(i, 4)) == "选修")
continue;
end
s = str2double(cell2mat(data(i, 2)));
w = str2double(cell2mat(data(i, 3)));
num = num + w;
sum = sum + s * w;
end
grade = sum / num;
grade = num2str(grade);
set(handles.result_text, 'String', "均绩:" + grade);
end
%关闭窗口
function exit_Callback(hObject, eventdata, handles)
close
%输入课程名称
function subject_edit_Callback(hObject, eventdata, handles)
global subject;
subject = get(handles.subject_edit, 'String');
function subject_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%输入百分制分数
function score_edit_Callback(hObject, eventdata, handles)
global score;
score = get(handles.score_edit, 'String');
function score_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%输入所占学分
function weight_edit_Callback(hObject, eventdata, handles)
global weight;
weight = get(handles.weight_edit, 'String');
function weight_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%选择课程类型
function type_choose_Callback(hObject, eventdata, handles)
global type
t = get(handles.type_choose, 'value');
switch t
case 1
type = "必修";
case 2
type = "限选";
case 3
type = "选修";
end
function type_choose_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%选择选修类型
function time_choose_Callback(hObject, eventdata, handles)
global time;
t = get(handles.time_choose, 'value');
switch t
case 1
time = "首修";
case 2
time = "重修";
end
function time_choose_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function scores_DeleteFcn(hObject, eventdata, handles)
%向表格中添加数据
function add_Callback(hObject, eventdata, handles)
global subject score weight type time;
rs = testScore(score);
rw = testWeight(weight);
if(subject == "" || rs == 0 || rw == 0)
f = msgbox("输入数据错误!", "错误", 'error');
else
newData = {char(subject), char(score), char(weight), char(type), char(time)};
oldData = get(handles.scores, 'Data');
newData = [oldData;newData];
set(handles.scores, 'Data', newData);
set(handles.subject_edit, 'String', "");
set(handles.score_edit, 'String', "");
set(handles.weight_edit, 'String', "")
subject = "";
score = "";
weight = "";
end
%选择计算所用的算法
function algorithm_choose_Callback(hObject, eventdata, handles)
global algorithm;
a = get(handles.algorithm_choose, 'value');
switch a
case 1
algorithm = "标准4.0制算法";
case 2
algorithm = "4.8制算法";
case 3
algorithm = "改进4.0制算法";
case 4
algorithm = "百分制均绩";
end
function algorithm_choose_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%删除选中行
function delete_Callback(hObject, eventdata, handles)
hangIndex = handles.hangIndex;
newData = get(handles.scores, 'Data');
newData(hangIndex,:) = [];
set(handles.scores, 'Data', newData);
disp(get(handles.scores, 'Data'));
function scores_CreateFcn(hObject, eventdata, handles)
%选中行
function scores_CellSelectionCallback(hObject, eventdata, handles)
hangIndex = eventdata.Indices(1);
handles.hangIndex = hangIndex;
guidata(hObject, handles);
%导入数据
function import_Callback(hObject, eventdata, handles)
[fileName,pathName] = uigetfile({'*.xls';'*.xlsx'});
file = [pathName,fileName];
[data text] = xlsread(file);
[m, n] = size(data);
for i = 1:m
newData = {cell2mat(text(i, 1)), num2str(data(i, 1)), num2str(data(i, 2)), cell2mat(text(i, 4)), cell2mat((text(i, 5)))};
oldData = get(handles.scores, 'Data');
newData = [oldData;newData];
set(handles.scores, 'Data', newData);
end
%清空数据
function clear_Callback(hObject, eventdata, handles)
set(handles.scores, 'Data', []);
set(handles.result_text, 'String', "总绩点:");
2、变量score处理程序
function r = testScore(score)
if score == ""
r = 0;
end
for i = 1:size(score)
if score(i) < '0' || score(i) > '9'
r = 0;
return;
end
end
if str2num(score) < 0 || str2num(score) > 100
r = 0;
else
r = 1;
3、变量weight处理程序
function r = testWeight(weight)
if weight == ""
r = 0;
for i = 1:size(weight)
if weight(i) < '0' || weight(i) > '9'
r = 0;
return;
if str2num(weight)
r = 0;
else
r = 1;
五、原设计报告
六、文件目录结构
领取专属 10元无门槛券
私享最新 技术干货