# CSE 3100 Systems Programming

CSE 3100 Systems Programming Fall 2019
Homework Assignment #1 Due date: 09/12/2019
For instructions on how to work on an assignment on Mimir, please review materials in Lab 0. Do not forget
to submit your work before the deadline. You only need to submit two C files. Do not submit executables.
Problem 1. (50 points) Calculator. calc is a simple calculator that performs the following steps in a
loop.
1) Read from the standard input two integers, a and b, separated by an operator,
2) Perform the operation specified by the operator and save the result in r, and
3) Print a, b, and r as in four formats: binary, hexadecimal, signed integer, and unsigned integer.
The operator can be any of the following:
+ - * / % & | ^ l r
where l means shift left, and r means shift right, and others are the same as the operators in C.
The program can use the following loop to read the two integers of int type and the operator. If scanf()
has read all three values correctly, it returns 3. The program continues to perform the specified operation.

Otherwise, the program gets out of the loop and exits. The user can press Ctrl-D (or Ctrl-C) to terminate
the program.
while (scanf("%i %c%i", &a, &c, &b) == 3) { // Note the space in the format string
Note that %i in the format string can read integers in hexadecimal or octal.
You can use the following structure to check operators. You can also try switch statement.
if (c == '+') {
}
else if (c == '-') {
// perform subtraction
}
// continue for more cases
else {
printf("Operator %c is not supported.\n", c);
}
One of purposes of this exercise is to study C operators. The operators in test cases are always one of
operators listed above. You can also assume the input is always correct for the operation. For example, you
do not need to worry about errors like division by 0.
A function print int() is provided to print an integer in different formats. The following statement is
an example that calls the function to print variable a.
print_int(a, "a");
Another purpose of this exercise is to study format string in scanf() and printf(). Although you just
call print int() to print an integer in your code, you should study how the integer is printed in function
print int().
The following is an example session of running the program. More examples are provided in file
calc-output.txt.
In this exercise you will implement a C program that reads a double floating point number x and an
integer n from the standard input and prints the value of the series up to n
th term (which is x
n−1
(n−1)!).
To make the results consistent with solutions, use double during computation although n is an integer.
You cannot call any math functions. Note that you can compute a term easily based on the term before
it. For example, if you already know x
2
, you can compute x
3 with one multiplication.
Below is an sample session of running the program. You may notice that only first 10 digits after the
decimal point are printed (by using %.10f).
\$ ./my-exp
1 5
exp(1.0000000000)=2.7083333333
1 10
exp(1.0000000000)=2.7182815256
1 15
exp(1.0000000000)=2.7182818285
0.5 10
exp(0.5000000000)=1.6487212704
0.5 15
exp(0.5000000000)=1.6487212707
3 30
exp(3.0000000000)=20.0855369232
10 50
exp(10.0000000000)=22026.4657948067
-5 30
exp(-5.0000000000)=0.0067379470
Notes. The loop needed to read the input is similar to that used in Problem 1. Suppose x is a double and
n is an int. You can read both x and n in one scanf() call.
while (scanf("%lf%d", &x, &n) == 2) { // pay attention to %lf and %d
...
};

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
05-29
05-29
05-29
05-29
05-29
05-29
05-29

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。