3.4.2 if结构举例
我们将用两个例子来说明if结构的用途
例3.2
求一元二次方程的根
设计并编写一个程序,用来求解一元二次方程的根。
答案:
我们将本章开头介绍的方法进行编程。
1.陈述问题这个问题的陈述非常的简单,我们要求一元二次方程的根,不管它的根是实根还是复根,有一个根还是两个根。
2.定义输入和输出
本程序的输入应为系数a,b,c
ax2 + bx + c = 0 (3.1)
输出量应为两个不相等的实数。两个相等的实数或两个复数。
3.写出算法本程序可分为三大块,它的函数分别为输入,运算过程和输出。
我们把每一个大块分解成更小的,更细微的工作。根据判别式的值,可能有三种计算途径,
读取输入的数据 计算出根 输入出根
所以我们要用到有三种选项的if结构。产生的伪代码如下
Prompt the user for the coefficients a, b, and c.
Read a, b, and c
discriminant ← b^2 - 4*a*c
if discriminat > 0
x1 ← (-b + sqrt(discriminant)) / (2*a)
x1 ← (-b - sqrt(discriminant)) / (2*a)
Write msg that equation hastwo distinct real roots.
Write out the two roots.
elseif discriminant == 0
x1 ← -b / (2*a)
Write msg that equation hastwo identical real roots.
Write out the repeatedroots.
else
real_part ← -b / (2*a)
imag_part ← sqrt(abs(discriminant)) / (2*a)
Write msg that equation hastwo complex roots.
Write out the two roots.
end
4.把算法转化为MATLAB语言
%Script file: calc_roots.m
%
% Purpose:
% This program solves for the roots of aquadratic equation
% of the form a*x^2 + b*x + c = 0. Itcalculates the answers
% regardless of the type of roots that theequation possesses.
%
% Record of revisions:
%DateProgrammerDescription of change
%=================================
% 12/04/98S. J. ChapmanOriginal code
%
% Define variables:
% a --Coefficientof x^2 term of equation
% b--Coefficientof x term of equation
% c--Constantterm of equation
% discriminant --Discriminant of the equation
% imag_part--Imagpart of equation (for complex roots)
% real_part--Realpart of equation (for complex roots)
% x1--Firstsolution of equation (for real roots)
% x2--Secondsolution of equation (for real roots)
% Prompt the user for the coefficients ofthe equation 格式非常棒!!!
disp ('This program solves for the roots ofa quadratic ');
disp ('equation of the form A*X^2 + B*X + C= 0.');
a = input('Enter the coefficient A: ');
b = input('Enter the coefficient B: ');
c = input('Enter the coefficient C: ');
% Calculate discriminant
discriminant = b^2 - 4 * a * c;
% Solve for the roots, depending on thevlaue of the discriminant.
if discriminant > 0% there are two real roots, so ...
x1= (-b + sqrt(discriminant)) / (2*a);
x2= (-b - sqrt(discriminant)) / (2*a);
disp('Thisequation has two real roots:');
fprintf('x1= %f\n', x1);
fprintf('x2 = %f\n', x2);
elseif discriminant == 0 % there is onerepeated root, so ...
x1= ( -b ) / (2*a);
disp('This equation has two identical real roots:');
fprintf('x1 = x2 = %f\n', x1);
else % there are complex roots, so ...
real_part = (-b) / (2*a);
imag_part= sqrt( abs(discriminant)) / (2*a);
disp('Thisequation has complex roots:');
fprintf('x1 = %f + i %f \n',real_part, imag_part);
fprintf('x1 + %f - i %f \n', real_part, imag_part);
end
5.检测这个程序
下一步,我们必须输入实数来检测这个程序。因这个程序有三个可能的路径。所以在我们确信每一人路径都工作正常之前,必须把这三个路径检测一遍。从式子(3.2)中,我们可以有用下面的方法来验证程序的正确性。
x2 + 5x + 6 = 0 x= -2, and x = -=3
x2 + 4x + 4 = 0 x= -2
x2 + 2x + 5 = 0 x= -1 ± i2
如果输入上面三个方程的系数得到对应的结果,则说明程序是正确的。
>> calc_roots
This program solves for the roots of aquadratic
equation of the form A*X^2 + B*X + C = 0.
Enter the coefficient A: 1
Enter the coefficient B: 5
Enter the coefficient C: 6
This equation has two real roots:
x1 = -2.000000
x2 = -3.000000
>> calc_roots
This program solves for the roots of aquadratic
equation of the form A*X^2 + B*X + C = 0.
Enter the coefficient A: 1
Enter the coefficient B: 4
Enter the coefficient C: 4
This equation has two identical real roots:
x1 = x2 = -2.000000
>> calc_roots
This program solves for the roots of aquadratic
equation of the form A*X^2 + B*X + C = 0.
Enter the coefficient A: 1
Enter the coefficient B: 2
Enter the coefficient C: 5
This equation has complex roots:
x1 = -1.000000 + i 2.000000
x1 + -1.000000 - i 2.000000
在三种不同的情况下,程序都给出了正确的结果。
例3.3
编写一个程序,求以x,y为自变量函数f(x,y)的值。函数f(x,y)的定义如下:
f(x,y) =
答案:
根据自变量x和y的正负符号的不同,而采取不同的求值表达式。为选取合适的表达式,检查用户输入的x,y的正负符号是必要的。
1.陈述问题
这个问题的陈述非常简单:根据用户输入的x,y,求函数f(x,y)的值。
2.确定输入输出量
程序的输入量为函数的自变量x,y。输出量为函数值f(x,y)。
3.设计算法这个问题可以把他分解成三个大块,即输入,计算过程,和输出。
我们把这三大块再分解成小的,精细的工作。在计算f(x,y)时,我们有4种选择,选哪一种取决于x,y的值。所以及逻辑上我们要用4个选择的if结构来实现。产生的伪代码如下:
Prompt the user for the values x and y
Read x and y
if x≥0 and y≥0
fun ← x + y
elseif x≥0 and y<0
fun ← x + y^2
elseif x<0 and y≥0
fun ← x^2 + y
else
fun ← x^2 + y^2
end
Write out f(x,y)
4.转化为MATLAB语句
最终的代码如下
% Scripte file: funxy.m
%
% Purpose:
% This program solves the function f(x,y)for a
% user-specified x and y, where f(x,y) isdefined as:
%_
%|
%|x + yx>=0 andy>=0
%|x + y^2x>=0 and y<0
%f(x,y)=| x^2 + yx<0and y>=0
%|x^2 + y^2x<0 andy<0
%|_
%
% Record of revisions:
%DateProgrammerDescription of change
%==============================
%12/05/98S.J.ChapmanOriginal code
%
% Define variables:
% x--Firstindependent variable
% y--Secondindependent variable
% fun--Resultingfunction
% Prompt the user for the values x and y
x = input('Enter the x coefficient: ');
y = input('Enter the y coefficient: ');
% Calculate the function f(x,y) based upon
% the signs of x and y.
if x>=0 & y>=0
fun = x + y;
elseif x>=0 & y<0
fun = x + y^2;
elseif x<0 & y>=0
fun = x^2 + y;
else
fun = x^2 + y^2;
end
% Write the value of the function.
disp(['The vlaue of the function is 'num2str(fun)]);
5.检测程序
下一步,我们必须输入实数来检测这个程序。因这个程序有四个可能的路径。所以在我们确信每一人路径都工作正常之前,必须把这四个路径检测一遍。我们分别取4个象限内的值(2,3),(­2,3),(2,­3)和(­2,­3)。我们用手工计算可得
f(2,3) = 2 + 3 = 5
f(2,-3) = 2 + (-3)2 = 11
f(-2,3) = (-2)2 + 3 = 7
f(-2, -3) = (-2)2 + (-3)2 = 13
当程序被编程后,运行4次并输入相应的值,运算结果如下:
>> funxy
Enter the x coefficient: 2
Enter the y coefficient: 3
The vlaue of the function is 5
>> funxy
Enter the x coefficient: 2
Enter the y coefficient: -3
The vlaue of the function is 11
>> funxy
Enter the x coefficient: -2
Enter the y coefficient: 3
The vlaue of the function is 7
>> funxy
Enter the x coefficient: -2
Enter the y coefficient: -3
The vlaue of the function is 13 程序在4种可能的情况下均产生了正确的结果。
|