Principle of variable parameter functions

xiaoxiao2021-03-06  39

#include #include

// Take the T by one in 4 units #define runk_size (((SIZEOF (T) SizeOf (int) - 1) / sizeof (int)) * Sizeof (int))

// Add address of the first parameter #define va_start (p, t) (p = (char *) & (t) round_size (t)))

// Remove a parameter #define va_next (p, t) (p = round_size (t), * ((t *) (p - round_size (t)))))))

/ / Clear #define va_end (p) (p = 0)

ENUM SIGN {UNKNOWN = -1, add = 0, SUB, MUL, DIV, LBR, RBR, SHP}; Enum priority {small = -1, equal, big, error}; priority prior [7] [7] = { // - * / () # {BIG, BIG, Small, Small, Small, BIG, BIG}, / {BIG, BIG, Small, Small, Small, Big, Big}, // - {BIG, BIG, BIG, BIG, SMALL, BIG, BIG}, BIG, Small, Big, BIG}, // / {small, small, small, small, small, equall, ire} , // ({Error, Error, Error, Error, Error, Error, Error}, //) {Small, Small, Small, Small, Small, Small, Equal} // #};

double caculate (double loprd, sign oprt, double roprd) {switch (oprt) {case ADD: return loprd roprd; case SUB: return loprd - roprd; case MUL: return loprd * roprd; case DIV: return roprd == 0 ? 0: loprd / roprd; default: return 0;}}

Sign SignMap (CAAR OPRT) {Switch (OPRT) {CASE ' ': Return Add; Case '-': Return Sub; Case '*': Return Mul; Case '/': Return Div; Case '(': return Lbr; Case ')': return rbr; Case '=': Return Shp; default: return unknown;}}

/ / Receive variable parameters to calculate expression value // FMT LIKE: (% i * (% C % S) -% L) /% F % D =%? // c = char; s = short; i = int; l = long; f = float; d = doubleint Arithmetic (char * buf, const char * fmt, ...) {char * p = 0; VA_START (P, FMT); char * str = buf; Sign Operatorstack [128] = {SHP}; Sign * SIGNTOP = OperatorStack; * SIGNTOP = SHP; Double OperandStack [128] = {0}; double * Numtop = OPERANDSTACK; for (; * fmt; fmt) {//// Take the operator, store the operator stamp, and output to the IF (* fmt! = '%') {* STR = * fmt; Sign Sn = SIGNMAP (* FMT); if (sn! = Unknown) { UNWIND: // Symbol Stack Different Switch (Prior [* (SIGNTOP-1)] [SN]) {Case Small: * SIGNTOP = Sn; Break; Case Equal: --signtop; Break; Case Big: * Numtop = Cacolate (* - Numtop, * - SIGNTOP, * - NUMTOP); Goto unwind; Case Error: --Signtop; Break;}} Continue;} // Take the operating number, deposit the operating count stack, and output into the independent character fmt after Skip: // jump%; if (* fmt == '/ 0') Break; switch (* FMT) {case 'c': // char type {char C = VA_NEXT (P, char); char TMP [16] = {'/ 0'}; Sprintf (TMP, "% D", C); for Char * pt = tmp; * pt; pt) * STR = * pt;

* Numtop = C; Break;} case 's': // short type {Short S = VA_Next (p, short); char TMP [16] = {'/ 0'}; Sprintf (TMP, "% D", s); for (char * pt = tmp; * pt; pt) * STR = * pt; * Numtop = S; Break;} case 'i': // int type {INT i = VA_NEXT (p, int ); Char TMP [16] = {'/ 0'}; Sprintf (TMP, "% D", I); for (char * pt = tmp; * pt; pt) * STR = * pt; * Numtop = I; Break;} case 'L': // long type {long L = VA_NEXT (P, long); char TMP [16] = {'/ 0'}; Sprintf (TMP, "% LD", L); for (char * pt = TMP; * pt; pt) * STR = * pt; * Numtop = L; Break;} case 'f'

: // Float type {float f = va_next (p, float); char TMP [16] = {'/ 0'}; sprintf (tmp, "% f", f); for (char * pt = tmp; * Pt; PT) * STR = * pt; * Numtop = f; Break;} case 'd': // double type {double D = VA_NEXT (P, Double); char TMP [16] = {'/ 0 '}; Sprintf (TMP, "% f", d); for (char * pt = tmp; * pt; pt) * STR = * pt; * Numtop = D; Break;} case'? ': / / Output expression value {char TMP [16] = {'/ 0'}; Sprintf (TMP, "% f", * - Numtop) For (char * pt = tmp; * pt; pt) * Str = * pt; break;} default: // other characters after jump% goto Skip;}} * str = '/ 0'; VA_END (p); return str-buf;}

转载请注明原文地址:https://www.9cbs.com/read-53665.html

New Post(0)