Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

138 Part III: Giving Your Programs the Ability to Run Amok

Increasing the value of a variable in C happens all the time. It involves using this funky equation:

i=i+1;

This math problem serves one purpose: It adds 1 to the value of the variable i. It looks funny, but it works.

Suppose that i equals 3. Then i+1 (which is 3 + 1) equals 4. Because the right side of the equal sign is worked out first in C, the value 4 is slid over and put into the i variable. The preceding statement increments the value of the i variable by 1.

You can also use the equation to add more than 1 to a value. For example:

i=i+6;

This equation increments the value of the i variable by 6. (Purists will argue, though, that the word increment means strictly to “add one to.” Then again, true purists wouldn’t put any dressing on their salad, so what do they know anyway?)

To add 1 to a variable — i, in this instance — you use the following C language mathematical-statement thing:

i=i+1;

This is known as incrementation.

No, that’s not incrimination. Different subject.

Some examples of incrementing values are altitude as a plane (or space­ ship) climbs; miles on an odometer; your age before and after your birthday; the number of fish the cat has eaten; and your weight over the holidays.

Incrementation — i=i+1 — works because C figures out what’s on the right side of the equal sign first. i+1 is done first. Then it replaces the original value of the i variable. It’s when you look at the whole thing all at once (from left to right) that it messes with your brain.

Unhappily incrementing your weight

The following program is LARDO.C, a rather rude interactive program that uses math to increment your weight. You enter what you weigh, and then LARDO calculates your newfound bulk as you consume your holiday feast:

Chapter 11: C More Math and the Sacred Order of Precedence 139

#include <stdio.h> #include <stdlib.h>

int main()

{

char weight[4]; int w;

printf(“Enter your weight:”); gets(weight); w=atoi(weight);

printf(“Here is what you weigh now: %d\n”,w); w=w+1;

printf(“Your weight after the potatoes: %d\n”,w); w=w+1;

printf(“Here you are after the mutton: %d\n”,w); w=w+8;

printf(“And your weight after dessert: %d pounds!\n”,w); printf(“Lardo!\n”);

return(0);

}

Type the preceding source code into your text editor. The only truly new material in this example is the w=w+1 equation, which increments the value of the w variable by one. The final equation, w=w+8, adds eight to the value of the w variable.

Check your typing and be mindful of semicolons and double quotes. Save the file to disk as LARDO.C.

Compile LARDO.C. Fix any errors, if need be.

The following sample of the program’s final run uses 175 as the user’s weight:

Enter your weight:175

Here is what you weigh now: 175

Your weight after the potatoes: 176

Here you are after the mutton: 177

And your weight after dessert: 185 pounds!

Lardo!

This program doesn’t need to be insulting — but what the hey! The idea in this example is to show how the w=w+1 equation is used to add 1 to the value of a variable. It’s called incrementation. (It’s what God does to your weight every morning that you lug your pudgy legs onto the scale.)

Yeah, 175 pounds! I’m sure that you typed an equally modest value rather than something more representative of your true girth.

140 Part III: Giving Your Programs the Ability to Run Amok

Bonus program! (One that may even have a purpose in life)

Monopoly is perhaps one of the greatest board games ever invented, and it can be terrific fun — especially when you own rows of hotels and your pitiful opponents land on them like witless flies on a discarded all-day sucker. The only problem at that point is drawing the Community Chest card that pro­ claims the following:

You are assessed for street repairs — $40 per house, $115 per hotel.

You count up all your houses and multiply that number by $40 and all the hotels by $115 (which is a strange number), and then you add the two values. It’s a terrible thing to do to one’s brain in the middle of a Monopoly game. But the mental drudgery can be easily abated by a simple computer program, one such as ASSESSED.C:

#include <stdio.h> #include <stdlib.h>

int main()

{

int houses, hotels, total; char temp[4];

printf(“Enter the number of houses:”); gets(temp);

houses=atoi(temp);

printf(“Enter the number of hotels:”); gets(temp);

hotels=atoi(temp);

total=houses*40+hotels*115;

printf(“You owe the bank $%d.\n”,total); return(0);

}

Carefully type this program into your editor on a new screen. Double-check your semicolons, parentheses, and quotes. Then save it to disk as ASSESSED.C.

Compile! Fix any errors, if need be. Then run the program. Suppose that you have nine houses and three hotels. Here’s what your output looks like:

Enter the number of houses:9 Enter the number of hotels:3 You owe the bank $705.