¿Is this program correct and well written?

Hello everyone, I need some help, I've just written the following code but I don't know if this is written correctly according to the text. I hope you can see if there are mistakes.
Sorry, english is not my native language.
TEXT:
A computer provider offers 10% discount, if the product costs $ 1 million or more. Also, independently offers a 5% discount if the brand is ABACO. Determine how much will pay a client for purchasing a computer, including tax.
TAX=Product Price * 0.16
CODE:
clear all
clc
M = input('Enter de computer''s brand: ', 's');
P = input('Enter the computer''s price: ');
TAX = P*0.16;
ABACO = 0.05;
if M==ABACO;
PA=P-(P*ABACO)+TAX;
fprintf('The purchase price is %d.\n',PA)
elseif M~=ABACO;
PB=P+TAX;
fprintf('The purchase price is %d.\n',PB)
elseif P>=1000000;
PC=P-(P*0.1)+TAX;
fprintf('The purchase price is %d.\n',PC)
end
Thanks.

10 comentarios

Arthur
Arthur el 20 de Nov. de 2012
Any reason to assume that's not correct? Does it work?
José-Luis
José-Luis el 20 de Nov. de 2012
Editada: José-Luis el 21 de Nov. de 2012
This is homework. Your program is incorrect. Here is a pointer:
M == ABACO
makes no sense, you are comparing a string to a scalar. While that might have its uses, I am pretty sure that is not what you intended to do. You might want to look into strcmp
And there should be PD. If ABACO and P>=1e6
another hint:
doc strcmpi
Jan
Jan el 20 de Nov. de 2012
Do not "clear all", when you want to "clear variables". Removing all loaded functions from the memory is a brute waste of time.
Jimmy
Jimmy el 20 de Nov. de 2012
Thanks everybody, I just did this with strcmpi and it works.
Sean de Wolski
Sean de Wolski el 20 de Nov. de 2012
And using input will get old after about two iterations. Either use a function to accept inputs or use an inputdlg.
Jimmy
Jimmy el 20 de Nov. de 2012
How can I implement inputdlg into this code?
Evan
Evan el 20 de Nov. de 2012
Editada: Evan el 20 de Nov. de 2012
Replace the calls to "input" with "inputdlg."
You could make things simpler by getting both inputs from the same dialog box. Just pass a cell array that contains your two fields to inputdlg, like so:
D = inputdlg({'Enter de computer''s brand:','Enter the computer''s price:'});
The data will be returned as a cell array of strings, so you'll have to get it in the form you're using:
M = D{1};
P = str2double(D{2});
To learn more about input dialog boxes:
help inputdlg
Jimmy
Jimmy el 21 de Nov. de 2012
Awesome Evan, thanks.
Evan
Evan el 21 de Nov. de 2012
No prob. :)

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 21 de Nov. de 2012

0 votos

I can see you're eventually going to need to know this FAQ entry: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F because you're comparing floating point numbers.

Más respuestas (0)

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Preguntada:

el 20 de Nov. de 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by