哇 好像滿好玩的說
我沒做過這個作業耶
可惜我下禮拜還有三科要考 滿重的
SO這次可能沒辦法幫你弄了
先說抱歉囉 ><
※ 引述《j1976 (凡事太盡緣分早盡)》之銘言:
: 你是不是有個作業跟這個很像啊
: 是的話版本來一個吧
: 救我啊
: The C-expression calculator
: Purpose
: In this project, you are to implement a C-expression calculator to evaluate the user-inputted expressions.
: The second purpose is to learn how to use pointers, structures, unions, and function pointers in C language.
: The third purpose is to utilize self-referential structures, including trees and linked lists; you need to design a symbol table to store information of variables as well.
: The final purpose is to open files to save and restore data.
: Description
: In the final project for C programming, you needs to implement a C-expression calculator to evaluate the user-inputted expressions. We know that C expressions are infix expressions in general, but how to process C expressions? You need not to worry about : it, there is sample code helping to process various C expressions and converting them to corresponding expression trees. The following function prototype is declared for that:
: CNode *DoParse(char* exprbuf);
: You just read a C expression into a character array, and pass it while calling DoParse(). If it is a valid C expression, DoParse() will return the root of an expression tree to represent the expression, else it will return NULL.
: So you just deal the expression tree to traverse it, and evaluate the result among different operators. The type CNode (in node.h) is defined as following:
: typedef struct tagCNode CNode;
: struct tagCNode
: {
: // Attributes
: char* text;
: NodeType variant;
: OpType op;
: int id;
: union {
: struct {
: CNode *Node1;
: CNode *Node2;
: CNode *Node3;
: CFunParaList *funParaList;
: } op;
: int ival;
: double dval;
: } u;
: };
: Basically, it defines the type of nodes of a tree structure. The type OpType is an enumeration:
: typedef enum tagNodeType {
: INT_VAL,
: DOUBLE_VAL,
: CHAR_VAL,
: STRING_VAL,
: ID,
: //// Operators:
: UNARY_OP,
: BINARY_OP,
: TERNARY_OP,
: //// Function call:
: FUNCTION_CALL,
: } NodeType;
: We can see that nodes can contain constants (INT_VAL, OUBLE_VAL, CHAR_VAL, STRING_VAL), identifiers (ID), operators (UNARY_OP, BINARY_OP, TERNARY_OP), and function call (FUNCTION_CALL).
: For a function call, it uses member funParaList of type CFunParaList to store its arguments. The type CFunParaList (in node.h) represents a linked list:
: typedef struct tagCNode CNode;
: struct tagCFunParaList
: {
: CNode *Node1;
: CFunParaList *next;
: };
: The file test.c in sample code demonstrates how to deal with them. The sample code has been compiled and tested successfully by the following compilers:
: · gcc 3.0 (Notice: gcc version 2.95.3 cannot handle anonymous unions.)
: · Microsoft Visual C++ 6.0
: · Borland C++ 5.5
: Requirements
: Your calculator should deal with two types: integer and double. In general, if both of two operands are type integer in a binary operator, the type of the evaluated result is integer; if one of operands is type double in a binary operator, the type of th: e evaluated result is double.
: For example:
: 10 / 4 + 3; is 5
: and 10.0 / 4 + 3; is 5.5
:
: Your program should maintain a table that contains the information of variables, e.g. variable names, types, and values. In other words, users can assign values to variables, and use them in expressions. I suggest using tree structure, or hash table with: linked list to implement it. Note that a variable can have different type and value at different time.
: For example:
: i = 10;
: x = i / 4 + 3; then x is 5
: and d = 10.0;
: x = d / 4 + 3; then x is 5.5
:
: Your program should support the following function calls:
: Mathematical Functions:
: The follow functions have prototypes in <math.h>, just use them. If you are using gcc, remember to add the command line option -lm to link with math library.
: sin(x)
: sine of x
: cos(x)
: cosine of x
: tan(x)
: tangent of x
: log10(x)
: base 10 logarithm log10(x), x>0.
: pow(x,y)
: xy. A domain error occurs if x=0 and y<=0, or if x<0 and y is not an integer.
: sqrt(x)
: sqare root of x, x>=0.
: fmod(x,y)
: floating-point remainder of x/y, with the same sign as x. If y is zero, the result is implementation-defined.
: Table Functions:
: delete(vara)
: delete a variable named vara in variable table
: clear()
: clear all items in variable table
: listall()
: print all variables' information
: File Functions:
: save("vars.dat")
: save all variables' information in variable table to file named vars.dat
: load("vars.dat")
: load all variables' information from file named vars.dat into variable table
:
: Sample input and output
: (5 > 3 && 9 <= 20) ? 200 * 10 : 200 / 10;
: 2000
: Result
: I = 10 / 2.0;
: 5.0
: Variable i is type double
: I = 10;
: 10
: Variable i is now type integer
: x = i / 4 + 3;
: 5
: d = 10.0;
: 10.0
: x = d / 4 + 3;
: 5.5
: log10(pow(10, x));
: Function call
: 5.5
: x;
: 5.5
: x + y;
: Variable y is undefined.
: http://pllab.cs.nthu.edu.tw/cs240202/assignments/cexpr2.zip
: 這是sample code
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 61.217.125.109