Lesson 4 (Exercise 2)

Mini calculator

From CS100 Summer Homework

文档内容

In order to make programs that are more interesting than just a simple “Hello world” example that does the same thing everytime, we need control statements that depend on conditions that may possibly change each time we run the program. Two such control statements have already been introduced (for-­‐loops and if-­‐else-­‐statements). The conditions may for example depend on a variable user input to the program. The simplest way to provide such a variable user input is through the console. Similar to the function printf, the header <stdio.h> also provides a function scanf to read user input from the console. The function simply reads characters input through the console, and in particular tries to read in a sequence of characters that matches a pre-­‐defined format. The function furthermore lets us indicate variables to which the input will be copied.

For example, the following statements will aim at reading in a sequence of a floating point number, a character, and another floating point number from the console, and copy the result to three variables that have been declared upfront:

float var1;
float var2;
char op;
scanf("%f %c %f", &var1, &op, &var2);
getchar();

"%f %c %f" declares the format of the entire sequence of the characters to be read from the console, and "%x" is a special format specifier where x is to be replaced by either f for floating point numbers, d for integer numbers, or c for a single character (to again name just a few, the idea is similar than for the printf function). "getchar()" is added to empty the input queue and remove the EOL (End of Line) character added by hitting “Enter” on entering the sequence. Note the added character "&" in front of each variable. This is the “address-­‐of”-­‐operator that makes sure that we pass the address of the variables (i.e. pointers) rather than a copy of the variable itself. It ensures that we really modify the original variables. You will learn more on this topic during the class.

To create a condition based on the value of a certain character, you may for example write:

if( op == ‘+’ ) {
  ...
} else {
  ...
}

Note that the operator == here is not an assignment operator, it is a comparison operator that produces either 0 or 1 depending on whether or not the left and right hand side expressions are identical.

Ok, you should now have all necessary tools together to solve the second exercise. Your task is to write a simple C program to evaluate arithmetic expressions. It should read in simple arithmetic expressions composed of two numbers and one of the four basic arithmetic operations, evaluate the result, and print it to the console. An example session will look as follows:

Laurents­‐MacBook‐Pro:summer_homework laurent$ ./ex1
Enter the arithmetic expression to be evaluated (e.g. 5 + 6): 
5 + 6
11.000000
Did you want to enter another expression? (Enter 'y' or 'n'): y
Enter the arithmetic expression to be evaluated (e.g. 5 + 6): 
3.67 -­ 5.12
-­1.450000
Did you want to enter another expression? (Enter 'y' or 'n'): y
Enter the arithmetic expression to be evaluated (e.g. 5 + 6): 
12 / 4.234
2.834199
Did you want to enter another expression? (Enter 'y' or 'n'): y
Enter the arithmetic expression to be evaluated (e.g. 5 + 6):  
3 * 9
27.000000
Did you want to enter another expression? (Enter 'y' or 'n'): n
Laurents­‐MacBook-­Pro:summer_homework laurent$

Red text represents the user input. Note that rather than entering examples by hand, you may forward the content of a text-­‐file to the console input. The output may also be redirected into a file. This is achieved as follows:

./myProgram < input.txt > output.txt

This is useful as you may use our examples provided on the webpage and check whether or not the produced output file exactly matches the provided example output. The online judge will also make such a direct comparison of the produced and the expected output, and it needs to match 100% (Note however that—in contrast to the text in the console—the text in the output file does not contain a copy of the user input including the line breaks added by the user). Use the provided test cases to figure out the problem if your test cases do not pass (note that differences as small as a missing or an added end-­‐of-­‐line command towards the end of the file may be sufficient to make the test cases fail).

Please follow the instructions on the webpage to login to our online grading system and submit your solution there. Please follow the exact instructions on how to register such that we are able to unambiguously identify you. This is very important as the summer-­homeworks will be graded and already make up for a small part of your overall grade.

在线测试 (语言:C;时间限制:1s)
当前状态

无提交记录

有话想说?