Simple method to calculate the precedence function
Manual calculations are cumbersome and error-prone.
By definition, computer-based, fast and convenient, accurate and expandable
0505 precedence function. cpp: The entry point that defines the console application. /* Instructions: 1, change the value of # 2, change char m[n+1][n+1] value 3, change F[n] g[n] The value of 4, when the last two times the output is not the same, the priority function is not convergent, need to increase the number of training. (In this case, there is no priority function, pay attention to OH) */#include "stdafx.h" #include <stdio.h> #include <iostream> using namespace std; Number of #define N 7//terminator//#define N 4//terminator//#define N 6//Terminator number//#define N 2//terminator number #define N 8//Terminator//int Initial value of]={1,1,1,1,1,1,1};//f//int g[n]={1,1,1,1,1,1,1};//g Initial value//int f[n]={1,1,1,1};//f initial value//int G[N]={1,1,1,1};// The initial value of G//int The initial value of the f[n]={1,1,1,1,1,1};//f//int g[n]={1,1,1,1,1,1};//g, the initial value of the/int f[n]={1,1};//f//int g[n]={1,1};// G initial value int f[n]={1,1,1,1,1,1,1,1};//f initial value of int g[n]={1,1,1,1,1,1,1,1};//g//Operator precedence table "1" 2 = 0 */char m[n+1][n+1]={{' ', ' + ', ' * ', ' | ', ' i ', ' (', ' ', ' # '}, {' + ', ' 1 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 1 ', ' 1 '}, {' * ', ' 1 ', ' 1 ', ' 2 ', ' 2 ', ' 2 ', ' 1 ', ' 1 '}, {' | ', ' 1 ', ' 1 ', ' 2 ', ' 2 ', ' 2 ', ' 1 ', ' 1 '}, {' I ', ' 1 ', ' 1 ', ' 1 ', ', ', ' 1 ', ' 1 '}, {' (', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 0 ', ' '}, {') ', ' 1 ', ' 1 ', ' 1 ', ', '', ' 1 ', ' 1 '}, {' # ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ', ' 0 '}};//from the textbook P111, reference P118 */* char m[n+1][n+1]={{", ' I ', ' * ', ' + ', ' # '}, {' I ', ' ', ' 1 ', ' 1 ', ' 1 '}, {' * ', ' 2 ', ' 1 ', ' 1 ', ' 1 '}, {' + ', ' 2 ', ' 2 ', ' 1 ', ' 1 '}, {' # ', ' 2 ', ' 2 ', ' 2 ', ' 0 '},};//from the textbook p 119 */* char m[n+1][n+1]={{', ' a ', ' ^ ', ' (', ') ', ', ', ' # '}, {' A ', ' ', ' ', ' ', ' 1 ', ' 1 ', ' 1 '}, {' ^ ', ', ', ' , ' 1 ', ' 1 ', ' 1 '}, {' (', ' 2 ', ' 2 ', ' 2 ', ' 0 ', ' 2 ', ' '}, {') ', ' ', ' ', ' ', ' 1 ', ' 1 ', ' 1 '}, ' 2 ', ' 2 ' and ' 2 ' and ' 1 ', '}, {' # ', ' 2 ', ' 2 ', ' 2 ', ' ', ' ', ' 0 '},};//the 1th question from the textbook P122 */* char m[n+1][n+1]={{", ' a ', ' B ',}, {' A ', ' 0 ', ' 1 ',}, {' B ', ' 0 ', ' 0 ',}};//from textbook P120 */char m[n+1][n+1]={{', ' o ', ' a ', ' N ', ' (', ') ', ' t ', ' f ', ' # '}, {' O ', ' 1 ', ' 2 ', ' 2 ', ' 2 ', ' 1 ', ' 2 ', ' 2 ', ' 1 '}, {' A ', ' 1 ', ' 1 ', ' 2 ', ' 2 ', ' 1 ', ' 2 ', ' 2 ', ' 1 '}, {' n ', ' 1 ', ' 1 ', ' 2 ', ' 2 ', ' 1 ', ' 2 ', ' 2 ', ' 1 '}, {' (' , ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 0 ', ' 2 ', ' 2 ', '}, {') ', ' 1 ', ' 1 ', ' ', ' ', ' 1 ', ', ' ', ' 1 '}, {' t ', ' 1 ', ' 1 ', ', ', ' 1 ', ' ', ', ' 1 '}, {' F ', ' 1 ', ' 1 ', ' ', ' ', ' 1 ', ' ', ' ', ' 1 '}, {' # ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ', ' 2 ', ' 2 ', ' 0 '}};//from textbook P121 Void Construction ()//Construction Precedence function {for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (m[i][j]== ' 1 ' && f[i-1]<=g[j-1]) {f[i-1]=g[j-1 ]+1; Break } if (m[i][j]== ' 2 ' && f[i-1]>=g[j-1]) {g[j-1]=f[i-1]+1; break;} if (m[i][j]== ' 0 ' && f[i-1]!=g[j-1]) { if (F[i-1]<=g[j-1]) {f[i-1]= (f[i-1]>g[j-1]?) F[i-1]:g[j-1]); } else {g[j-1]= (f[i-1]>g[j-1]? F[i-1]:g[j-1]); } break; }}}} void Outputm ()//Output operator precedence relation table {for (int i=0;i<=n;i++) {for (int j=0;j<=n;j++) {printf ("%c", M[i][j]);} printf ("/n"); }} void Outputfg ()//output precedence function f and g {printf ("/n precedence function F's value:"); for (int i=0;i<n;i++) {printf ("%d", F[i]);} printf (the value of "/N precedence function G:"); for (int i=0;i<n;i++) {printf ("%d", G[i]);} printf ("/n"); } int _tmain (int argc, _tchar* argv[]) {OUTPUTM (); OUTPUTFG (); for (int count=1;count<=10;count++)//maximum number of calculations, no end condition set (i.e. F and G Convergence) {construction (); printf ("/n"%d times constructionThe following F and G are as follows: ", count); OUTPUTFG (); } system ("Pause"); return 0; }