BỘ GIÁO DỤC VÀ ĐÀO TẠO
TRƯỜNG ĐẠI HỌC TIỀN GIANG
KHOA KỸ THUẬT CÔNG NGHỆ
NGÀNH: CƠ ĐIỆN TỬ
BÀI TIỂU LUẬN
MÔN: NGÔN NGỮ LẬP TRÌNH
GVHD: TRẦN QUỐC CƯỜNG
Thành viên nhóm 4 gồm:
Nguyễn Thanh Cao 022146002
Đặng Nguyễn Vũ Trường An 022146012
Lê Minh Phú 022146015
Nguyễn Nhựt Tân 022146001
Nguyễn Trần Thoại Vương 022146021
Năm 2024
BỘ GIÁO DỤC VÀ ĐÀO TẠO
TRƯỜNG ĐẠI HỌC TIỀN GIANG
KHOA KỸ THUẬT CÔNG NGHỆ
NGÀNH: CƠ ĐIỆN TỬ
BÀI TIỂU LUẬN
MÔN: NGÔN NGỮ LẬP TRÌNH
GVHD: TRẦN QUỐC CƯỜNG
Thành viên nhóm 6 gồm:
Nguyễn Thanh Cao 022146002
Đặng Nguyễn Vũ Trường An 022146012
Lê Minh Phú 022146015
Nguyễn Nhựt Tân 022146001
Nguyễn Trần Thoại Vương 022146021
Năm 2024
MỤC LỤC
PHẦN I. BÀI TẬP VỀ LẬP TRÌNH C....................................................................
PHẦN 2. BÀI TẬP VỀ LẬP TRÌNH MATLAB....................................................
BÀI TẬP VỀ LẬP TRÌNH C
#include <stdio.h>
#define MAXRESISTORS 9
#define MAXNAMELENGTH 50
struct Circuit
{
char members[MAXNAMELENGTH];
float voltage;
float resistors[MAXRESISTORS];
int num_resistors;
};
void input_team_info(struct Circuit *circuit)
{
printf("Nhap Ho va Ten Thanh vien: ");
scanf("%s", circuit->members);
printf("Nhap dien ap nguon cung cap: ");
scanf("%f", &circuit->voltage);
}
void input_resistors(struct Circuit *circuit)
{
printf("Nhap so luong dien tro mac noi tiep: ");
scanf("%d", &circuit->num_resistors);
if (circuit->num_resistors > MAXRESISTORS)
{
printf("*Canh bao: So phan tu phai nho hon %d Hay nhap lai !\n",
MAXRESISTORS);
input_resistors(circuit);
} else {
printf("Nhap gia tri dien tro cho tung dien tro:\n");
for (int i = 0; i < circuit->num_resistors; i++)
{
printf(" Hay nhap gia tri dien tro R%d: ", i+1);
scanf("%f", &circuit->resistors[i]);
}
}
}
void calculate_current(struct Circuit circuit)
{
printf("\n*********Ket Qua Da Nhap Cac Thong So Vao Chuong
trinh*********\n");
printf("* Ho ten: %s\n", circuit.members);
printf(" *Dien ap nguon: %.2f (v)\n", circuit.voltage);
printf(" \n");
for (int i = 0; i < circuit.num_resistors; i++)
{
printf(" --> Dien tro R%d = %.2f (Ohm)\n", i+1,
circuit.resistors[i]);
}
printf(" \n");
float total_resistance = 0;
for (int i = 0; i < circuit.num_resistors; i++)
{
total_resistance += circuit.resistors[i];
}
float current = circuit.voltage / total_resistance;
printf("*******Ket Qua Chuong trinh Thuc Hien**************\
n");
printf(" --> Tong tro R_td %.2f (Ohm)\n", total_resistance);
printf(" --> Dong dien chay qua mach: I = %.2f (A)\n", current);
printf("*********KET THUC CHUONG
TRINH****************\n");
}
int main()
{
printf("CHUONG TRINH DONG DIEN TRONG DOAN
MACH CO CAC DIEN TRO MAC NOI TIEP\n");
struct Circuit circuit;
input_team_info(&circuit);
input_resistors(&circuit);
calculate_current(circuit);
return 0;
}
BÀI TẬP VỀ PHẦN MATLAB
1/ File.m
function varargout = D4(varargin)
% D4 MATLAB code for D4.fig
% D4, by itself, creates a new D4 or
raises the existing
% singleton*.
%
% H = D4 returns the handle to a new D4 or
the handle to
% the existing singleton*.
%
%
D4('CALLBACK',hObject,eventData,handles,...)
calls the local
% function named CALLBACK in D4.M with the
given input arguments.
%
% D4('Property','Value',...) creates a new
D4 or raises the
% existing singleton*. Starting from the
left, property value pairs are
% applied to the GUI before D4_OpeningFcn
gets called. An
% unrecognized property name or invalid
value makes property application
% stop. All inputs are passed to
D4_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 D4
% Last Modified by GUIDE v2.5 12-May-2024
13:45:14
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename,
...
'gui_Singleton',
gui_Singleton, ...
'gui_OpeningFcn',
@D4_OpeningFcn, ...
'gui_OutputFcn',
@D4_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
% --- Executes just before D4 is made visible.
function D4_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 D4 (see
VARARGIN)
% Choose default command line output for D4
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes D4 wait for user response (see
UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned
to the command line.
function varargout = D4_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;
% --- Executes on button press in plotbutton.
function plotbutton_Callback(hObject,
eventdata, handles)
global type1 type2 type3 color1 color2 color3
width1 width2 width3;
axis padded;
label = {};
% hObject handle to plotbutton (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
b_val = str2num(get(handles.const_b_edit,
'String'));
t_val = str2double(get(handles.t_val,
'String'))*pi;
x = linspace(-1*t_val,1*t_val,100);
if get(handles.f1, 'Value') == 1
hold on
y1 = b_val*sin(x);
stem(handles.axes1,x,y1,'LineStyle',type1,'Line
Width',width1,'Color',color1);
label = [label,'$y = b*sin(x)$'];
end
if get(handles.f2, 'Value') == 1
hold on
y1 = b_val*sin(x-3);
stem(handles.axes1,x,y1,'LineStyle',type2,'Line
Width',width2,'Color',color2);
label = [label,'$y = b*sin(x-3)$'];
end
if get(handles.f3, 'Value') == 1
hold on
y1 = b_val*sin(x-5);
stem(handles.axes1,x,y1,'LineStyle',type3,'Line
Width',width3,'Color',color3);
label = [label,'$y = b*sin(x-5)$'];
end
legend(label,'Interpreter','latex');
hold off
function lw_in_Callback(hObject, eventdata,
handles)
% hObject handle to lw_in (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 lw_in as text
% str2double(get(hObject,'String'))
returns contents of lw_in as a double
% --- Executes during object creation, after
setting all properties.
function lw_in_CreateFcn(hObject, eventdata,
handles)
% hObject handle to lw_in (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 const_b_edit_Callback(hObject,
eventdata, handles)
% hObject handle to const_b_edit (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 const_b_edit as text
% str2double(get(hObject,'String'))
returns contents of const_b_edit as a double
% --- Executes during object creation, after
setting all properties.
function const_b_edit_CreateFcn(hObject,
eventdata, handles)
% hObject handle to const_b_edit (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
% --- If Enable == 'on', executes on mouse
press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5
pixel border or over plotbutton.
function plotbutton_ButtonDownFcn(hObject,
eventdata, handles)
% hObject handle to plotbutton (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
function t_val_Callback(hObject, eventdata,
handles)
% hObject handle to t_val (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 t_val as text
% str2double(get(hObject,'String'))
returns contents of t_val as a double
% --- Executes during object creation, after
setting all properties.
function t_val_CreateFcn(hObject, eventdata,
handles)
% hObject handle to t_val (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 selection change in
linecolor.
function linecolor_Callback(hObject, eventdata,
handles)
% hObject handle to linecolor (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
linecolor contents as cell array
% contents{get(hObject,'Value')} returns
selected item from linecolor
% --- Executes during object creation, after
setting all properties.
function linecolor_CreateFcn(hObject,
eventdata, handles)
% hObject handle to linecolor (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles empty - handles not created until
after all CreateFcns called
% Hint: popupmenu 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 selection change in linetype.
function linetype_Callback(hObject, eventdata,
handles)
% hObject handle to linetype (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 linetype
contents as cell array
% contents{get(hObject,'Value')} returns
selected item from linetype
% --- Executes during object creation, after
setting all properties.
function linetype_CreateFcn(hObject, eventdata,
handles)
% hObject handle to linetype (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles empty - handles not created until
after all CreateFcns called
% Hint: popupmenu 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 selection change in
update_popup.
function update_popup_Callback(hObject,
eventdata, handles)
% hObject handle to update_popup (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
global type1 type2 type3 color1 color2 color3
width1 width2 width3;
line_type = "";
line_color = "";
update_queue = get(handles.update_popup,
'Value');
type = get(handles.linetype, 'Value');
line_width = str2double(get(handles.lw_in,
'String'));
color = get(handles.linecolor, 'Value');
if type == 1
line_type = "-";
elseif type == 2
line_type = "--";
elseif type == 3
line_type = ":";
elseif type == 4
line_type = "-.";
end
if color == 1
line_color = 'r';
elseif color == 2
line_color = 'g';
elseif color == 3
line_color = 'b';
elseif color == 4
line_color = 'c';
end
if update_queue == 1
type1 = line_type;
color1 = line_color;
width1 = line_width;
elseif update_queue == 2
type2 = line_type;
color2 = line_color;
width2 = line_width;
elseif update_queue == 3
type3 = line_type;
color3 = line_color;
width3 = line_width;
end
% --- Executes during object creation, after
setting all properties.
function update_popup_CreateFcn(hObject,
eventdata, handles)
% hObject handle to update_popup (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles empty - handles not created until
after all CreateFcns called
% Hint: popupmenu 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
2/ File Figure
-----------------------------THE END-------------------------