Solved–ASSIGNMENT 3 –SOLUTION

$35.00 $24.00

Assignment overview We would like students to understand and use C types such as char, int, and float, and to experience with the input and output, the flow control structures, the recursive functions, and the arrays. This assignment consists of two parts. 在第一部分的时候 你需要写一个简易计算器 In part one, you are required to write a C programs…

You’ll get a: . zip file solution

 

 
Categorys:

Description

5/5 – (2 votes)

Assignment overview

We would like students to understand and use C types such as char, int, and float, and to experience with the input and output, the flow control structures, the recursive functions, and the arrays.

This assignment consists of two parts. 在第一部分的时候 你需要写一个简易计算器

In part one, you are required to write a C programs to evaluate simple arithmetic expression.

In part two, you are to wrote a C program to display integers using seven-segment display.
在第二部分 你需要使用seven segment 来 print数
Part one: 70%

The goal of the exercise is to implement a simple calculator, called ”evaluate”, to evaluate

simple arithmetic expression. 求值

(1) An arithmetic expression is an expression that results in a numeric value. We consider numeric value to be real or floating point numbers. A simple arithmetic expression involves numeric values connected by arithmetic operators. In this exercise, numeric value will be real or floating point numbers and operators will be +, −, ∗, and /.

For example, 4 + 35 is a simple arithmetic expression and 2.3 ∗ 4 − 5 − -7.8 + 9 / 3 is another simple arithmetic expression.

In evaluating arithmetic expression, ∗ and / have higher precedence than + and −. With operators of the same precedence, the order of evaluation is from left to right. We will use s exp to represent simple arithmetic expression, m exp to represent simple arithmetic expression in which all operators are ∗ or /, l op to represent operators + and −, h op to represent operators ∗ and /, and num to represent numeric value.

In the following, simple arithmetic expression is represented recursively, where | rep-resent OR relationship. This recursive definition would be useful in designing your

所以说 sexp适应所有式子 主要function?然后 mexp是负责处理完全是乘除的式子 lop 专门负责处理加法和减法的式子

然后hop是处理乘除法个例的 num代表数

1

solution for this exercise.
s exp → m exp

s exp → s exp l op m exp

m exp → num
m exp → m exp h op num

l op → + | − 输入带有整个式子的

h op → ∗ | / 句子 比如4*5+5…

首先问数

(2) In your program, first the user is asked to input a simple arithmetic expression. In the inputted simple arithmetic expression, there could be space characters before a number

or an operator, The input numbers could be either integers or floating numbers and 要对付好空 we assume that the user will always enter valid numbers. Your program should handle 格 遇到就跳
non-valid input character for operators. 呗

(3) After user input, the program will calculate and print the numeric value of the inputted

simple arithmetic expression. The program does not read the whole expression before 读完整个之
its calculation. The calculation proceeds while reading numbers and operators of the 后再算 最后
inputted simple arithmetic expression. 再给出数据
In evaluating simple arithmetic expression, + and − have the same precedence and
都是从左到右
加减同优先级 the evaluation order is from left to right, and ∗ and / have the same precedence and
the evaluation order is from left to right.
乘除同优先级
We will use two recursive functions to perform the evaluation. The implementation of

these recursive functions should follow the recursive definition for simple expression in
(1). To guide you toward this goal, we provide a template function. We ask you to
use this template and fill in the missing code.

/ Input: ’sub_exp’: the value of the sub s_expression to the left of ’op’

/ location in stdin.

/ ’op’ : an operator, ’+’ or ’-’. ’op’ could also be

// ’\n’ indicating the end of the s_expression

/ the rest of the expression will be read in from stdin

/ Effect: the whole s_expression is evaluated using recursion:

/ get next_num with m_exp() and then get next_op with get_op()

/ use ’sub_exp op next_num’ and ’next_op’ to do recursive call

/ Output: this function returns the value of the s_expression

float s_exp(float sub_exp, char op) {

}

2

// Input: ’sub_exp’: the value of the current sub m_expression

// to the left of ’op’ location in stdin.

/ ’op’ : an operator, ’*’ or ’/’. ’op’ could also be

// ’+’, ’-’, or ’\n’ indicating the end of the m_expression.

// “+’, ’-’, or ’\n’ should be pushed back to stdin.

/ the rest of the m_expression will be read in from stdin

/ Effect: the m_expression is evaluated using recursion:

/ get next_num with get_num() and then get next_op with get_op()

/ use ’sub_exp op next_num’ and ’next_op’ to do recursive call

/ Output: this function returns the value of the current m_expression

float m_exp(float sub_exp, char op) {

}

The following two functions should also be used to simplify the programming task.

/ Input: none, read from stdin

/ Effect: get the next operator of the expression

/ this operator can be +, -, *, /, or ’\n’

/ ’\n’ indicates the end of the expression input

/ leading spaces should skipped

/ Output: return the next operator of the expression.

char get_op() {

}

/ Input: none, read from stdin

/ Effect: get the next numeric value of the expression

/ Output: return the next numeric value of the expression.

float get_num() {

}

To push back one character in ch to stdin, use ungetc(ch, stdin). ungetc() is a library function that can push back a character to a specified stream.

When an error is detected, you can exit your program by a function call exit(EX I T F AI LU RE).

To use this function, the required header is:

#include < stdlib.h >

3

(4) Then the user is asked if she/he wants to continue. Two characters can be entered, each corresponding to a possible action.

• Y for continuing inputting a simple arithmetic expression

• N for quit

(5) Evaluate and print values, or report input format errors, for the following simple arith-metic expressions with your program.

• 5

• 3 ∗ 4

• 4 + 3

• 2.6/2−1.5∗10

• -2.0∗2+1.5∗10−100

• 3.5−-1.5∗10/3.0+2∗3

• 3.5 − 1.5 % 10 − 2.0

Part two: 30%

The goal of this exercise is to implement a C program to display integer in seven-segment display format.

因为最多的情况是8 有七个segment

(1) Calculators, watches, and other electronic devices often rely on seven-segment displays for numerical output. To form a digit, such devices “turn on” some of the seven segments while leaving others “off”.

_ _ _ _ _ _ _ _ 因为是array 所以想要叫某个数字可以直接

| | | _| _| |_| |_ |_ | |_| |_| call array【】【】【】来找到相应数字
|_| | |_ _| | _| |_| | |_| _|

输入int 我们返回相应数字

(2) In your program, the user is asked to input an integer. After reading the input, the program will output the inputted integer using seven-segment displays.

You should use a three dimensional array of characters to store the 10 digits.

Here is what the array may look like:

const char segements[10][3][3] =

{ { {’ ’, ’_’, ’ ’}, {’|’, ’ ’, ’|’}, {’|’, ’_’, ’|’} }, … };

Only the initialization for segments[0] is given and you should fill in the rest.

Do not forget the sign of the integer.

4

(3) Then the user is asked if she/he wants to continue. Two characters can be entered, each corresponding to a possible action.

• Y for continuing inputting an integer

• N for quit

(4) Testing your program by inputing several representative integers.

Testing your program

You should test your program by running it on Gaul. Capture the screen of your testing by using script command. There should be two script files, one for each part.

Your program should follow good programming styles, i.e. write clear code, choose good variable names, use appropriate functions, make proper comments, and etc.

5