从零开始写代码 2024-12-04 11:30 采纳率: 69.2%
浏览 18
已结题

matlab GUI实现一个绩点计算器

设计界面如图:

img

计算过程:选择科目->输入该科成绩->输入科目学分->选择下一个科目->再次输入成绩和学分->输入完所有科目之后,点击计算->求得平均学分绩点和绩点
说明:
如有未输入学分或未输入成绩的科目统一按0分计算

问题如图:

img

img

源码如下:
score.m

function varargout = score(varargin)
% SCORE MATLAB code for score.fig
%      SCORE, by itself, creates a new SCORE or raises the existing
%      singleton*.
%
%      H = SCORE returns the handle to a new SCORE or the handle to
%      the existing singleton*.
%
%      SCORE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SCORE.M with the given input arguments.
%
%      SCORE('Property','Value',...) creates a new SCORE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before score_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to score_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help score

% Last Modified by GUIDE v2.5 28-Nov-2024 16:35:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @score_OpeningFcn, ...
                   'gui_OutputFcn',  @score_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
% 初始化科目列表(假设已经有了一些科目)
set(handles.listbox1, 'Value', 1); % 默认选择第一个科目
% 初始化存储科目成绩和学分的结构体
handles.subjects = struct('scores', {}, 'credits', {});
guidata(hObject, handles);
% End initialization code - DO NOT EDIT


% --- Executes just before score is made visible.
function score_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to score (see VARARGIN)

% Choose default command line output for score
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes score wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = score_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function score_Callback(hObject, eventdata, handles)
% hObject    handle to score (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of score as text
%        str2double(get(hObject,'String')) returns contents of score as a double


% --- Executes during object creation, after setting all properties.
function score_CreateFcn(hObject, eventdata, handles)
% hObject    handle to score (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function gred_Callback(hObject, eventdata, handles)
% hObject    handle to gred (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of gred as text
%        str2double(get(hObject,'String')) returns contents of gred as a double


% --- Executes during object creation, after setting all properties.
function gred_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gred (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in compute.
function compute_Callback(hObject, eventdata, handles)
% hObject    handle to compute (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  % 获取当前选择的科目索引和名称
    selectedIndex = get(handles.listbox1, 'Value');
    selectedSubject = get(handles.listbox1, 'String'){selectedIndex};
 
    % 获取输入的成绩和学分
    try
        num1 = str2double(get(handles.score, 'String'));
    catch
        num1 = NaN; % 如果输入无效,则设置为NaN
    end
 
    try
        num2 = str2double(get(handles.gred, 'String'));
    catch
        num2 = 0; % 如果输入无效,则默认为0学分
    end
 
    % 存储成绩和学分到对应的科目结构体中
    handles.subjects.(selectedSubject).scores{end+1} = num1;
    handles.subjects.(selectedSubject).credits{end+1} = num2;
 
    % 更新handles结构
    guidata(hObject, handles);
 
    % 计算绩点和学分绩点,并存储(这里只计算最后一次输入的成绩和学分)
    if ~isnan(num1)
        if num1 >= 90
            result = 4.0;
        elseif num1 >= 80
            result = 3.0;
        elseif num1 >= 70
            result = 2.0;
        elseif num1 >= 60
            result = 1.0;
        else
            result = 0;
        end
 
        result1 = result * num2;
 
        % 显示结果(这里只显示最后一次输入的结果)
        set(handles.Result_gred1, 'String', num2str(result1));
        set(handles.Result_gred2, 'String', num2str(result));
 
        % 计算平均学分绩点和绩点(遍历所有科目和成绩)
        totalGPA = 0;
        totalCredits = 0;
        totalPoints = 0;
        numEntries = 0;
 
        for subject = fieldnames(handles.subjects)
            scores = handles.subjects.(subject).scores;
            credits = handles.subjects.(subject).credits;
            
            for i = 1:length(scores)
                if ~isnan(scores{i}) && credits{i} > 0
                    point = getGradePoint(scores{i});
                    totalGPA = totalGPA + point * credits{i};
                    totalCredits = totalCredits + credits{i};
                    totalPoints = totalPoints + point;
                    numEntries = numEntries + 1;
                end
            end
        end
 
        if numEntries > 0
            averageGPA = totalGPA / totalCredits;
            averagePoint = totalPoints / numEntries;
        else
            averageGPA = 0;
            averagePoint = 0;
        end
 
        % 显示平均学分绩点和绩点
        set(handles.Average_GPA, 'String', num2str(averageGPA));
        set(handles.Average_Point, 'String', num2str(averagePoint));
    end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over Average_GPA.
function Average_GPA_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to Average_GPA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 % 获取当前选择的科目索引
    selectedIndex = get(hObject, 'Value');
    selectedSubject = get(hObject, 'String'){selectedIndex};
 
    % 清除之前的成绩和学分输入框(如果需要)
    set(handles.score, 'String', '');
    set(handles.gred, 'String', '');
 
    % 更新存储结构以包含新科目的索引(如果不存在)
    if ~isfield(handles.subjects, selectedSubject)
        handles.subjects.(selectedSubject).scores = {};
        handles.subjects.(selectedSubject).credits = {};
    end
 
    % 更新handles结构
    guidata(hObject, handles);
% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function gradePoint = getGradePoint(score)
    % 根据成绩返回绩点
    if score >= 90
        gradePoint = 4.0;
    elseif score >= 80
        gradePoint = 3.0;
    elseif score >= 70
        gradePoint = 2.0;
    elseif score >= 60
        gradePoint = 1.0;
    else
        gradePoint = 0;
    end

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月12日
    • 创建了问题 12月4日