A data file called “mathfile.dat” stores three characters on each line: an operand (a single digit number), an operator (a one character operator, such as +, -, /, \\, *, ^), and then another operand (a single digit number). For example, it might look like this:
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:9| Question number:16.9
All Answers
total answers (1)
Ch9Ex16.m
% Read in math operations from a data file,
% perform the operation and print the result
fid = fopen('mathfile.dat');
if fid == -1
disp('File open not successful')
else
while feof(fid) == 0
% Get operand, operator, operand
% and perform the operation!
aline = fgetl(fid);
res = eval(aline);
fprintf('%s = %d\n', aline, res)
end
closeresult = fclose(fid);
if closeresult == 0
disp('File close successful')
else
disp('File close not successful')
end
end
need an explanation for this answer? contact us directly to get an explanation for this answer