I'll make it simple. If the bear wants food, don't do anything stupid or crazy to stop it. Throw rocks near it (near not at), try to discourage it from taking your food. To keep a bear out of your food, hang it up in a tree. Ask a ranger for directions on how best to do that.
(why ask a ranger? Well, it's not like it's that hard to explain, I'm just too lazy to write it down in an understandable way)