⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 awkcode.txt

📁 c programming pearls answer
💻 TXT
📖 第 1 页 / 共 5 页
字号:
sentgen function gen(sym,    i, j) {sentgen     if (sym in lhs) {       # a nonterminalsentgen         i = int(lhs[sym] * rand()) + 1   # random productionsentgen         for (j = 1; j <= rhscnt[sym, i]; j++) # expand rhs'ssentgen             gen(rhslist[sym, i, j])sentgen     } elsesentgen         printf("%s ", sym)sentgen }sentgen1 # sentgen1 - random sentence generator with probabilitiessentgen1 #   input:  grammar file; sequence of nonterminalssentgen1 #   output: random sentences generated by the grammarsentgen1 sentgen1 BEGIN {  # read rules from grammar filesentgen1     while (getline < "test-gram" > 0)sentgen1         if ($2 == "->") {sentgen1             i = ++lhs[$1]              # count lhssentgen1             rhsprob[$1, i] = $NF       # 0 <= probability <= 1sentgen1             rhscnt[$1, i] = NF-3       # how many in rhssentgen1             for (j = 3; j < NF; j++)   # record themsentgen1                rhslist[$1, i, j-2] = $jsentgen1         } elsesentgen1             print "illegal production: " $0sentgen1     for (sym in lhs)sentgen1         for (i = 2; i <= lhs[sym]; i++)sentgen1             rhsprob[sym, i] += rhsprob[sym, i-1]sentgen1 }sentgen1 sentgen1 {   if ($1 in lhs) {  # nonterminal to expandsentgen1         gen($1)sentgen1         printf("\n")sentgen1     } else sentgen1         print "unknown nonterminal: " $0   sentgen1 }sentgen1 sentgen1 function gen(sym,    i, j) {sentgen1     if (sym in lhs) {       # a nonterminalsentgen1         j = rand()          # random productionsentgen1         for (i = 1; i <= lhs[sym] && j > rhsprob[sym, i]; i++)sentgen1             ;sentgen1         for (j = 1; j <= rhscnt[sym, i]; j++) # expand rhs'ssentgen1             gen(rhslist[sym, i, j])sentgen1     } elsesentgen1         printf("%s ", sym)sentgen1 }sentgen2 # sentgen2 - random sentence generator (nonrecursive)sentgen2 #   input:  grammar file; sequence of nonterminalssentgen2 #   output: random sentences generated by the grammarsentgen2 sentgen2 BEGIN {  # read rules from grammar filesentgen2     while (getline < "grammar" > 0)sentgen2         if ($2 == "->") {sentgen2             i = ++lhs[$1]              # count lhssentgen2             rhscnt[$1, i] = NF-2       # how many in rhssentgen2             for (j = 3; j <= NF; j++)  # record themsentgen2                rhslist[$1, i, j-2] = $jsentgen2         } elsesentgen2             print "illegal production: " $0sentgen2 }sentgen2 sentgen2 {   if ($1 in lhs) {  # nonterminal to expandsentgen2         push($1)sentgen2         gen()sentgen2         printf("\n")sentgen2     } else sentgen2         print "unknown nonterminal: " $0   sentgen2 }sentgen2 sentgen2 function gen(    i, j) {sentgen2     while (stp >= 1) {sentgen2         sym = pop()sentgen2         if (sym in lhs) {       # a nonterminalsentgen2             i = int(lhs[sym] * rand()) + 1   # random productionsentgen2             for (j = rhscnt[sym, i]; j >= 1; j--) # expand rhs'ssentgen2                 push(rhslist[sym, i, j])sentgen2         } elsesentgen2             printf("%s ", sym)sentgen2     }sentgen2 }sentgen2 sentgen2 function push(s) { stack[++stp] = s }sentgen2 sentgen2 function pop() { return stack[stp--] }arith # arith - addition drillarith #   usage:  awk -f arith [ optional problem size ]arith #   output: queries of the form "i + j = ?"arith arith BEGIN {arith     maxnum = ARGC > 1 ? ARGV[1] : 10   # default size is 10arith     ARGV[1] = "-"  # read standard input subsequentlyarith     srand()        # reset rand from time of dayarith     do {arith         n1 = randint(maxnum)arith         n2 = randint(maxnum)arith         printf("%g + %g = ? ", n1, n2)arith         while ((input = getline) > 0)arith             if ($0 == n1 + n2) {arith                 print "Right!"arith                 breakarith             } else if ($0 == "") {arith                 print n1 + n2arith                 breakarith             } elsearith                 printf("wrong, try again: ")arith     } while (input > 0)arith }arith arith function randint(n) { return int(rand()*n)+1 }quiz.elems symbol:number:name|elementquiz.elems H:1:Hydrogenquiz.elems He:2:Heliumquiz.elems Li:3:Lithiumquiz.elems Be:4:Berylliumquiz.elems B:5:Boronquiz.elems C:6:Carbonquiz.elems N:7:Nitrogenquiz.elems O:8:Oxygenquiz.elems F:9:Fluorinequiz.elems Ne:10:Neonquiz.elems Na:11:Sodium|Natriumquiz.elems Mg:12:Magnesiumquiz.elems Al:13:Aluminumquiz.elems Si:14:Siliconquiz.elems P:15:Phosphorusquiz.elems S:16:Sulphur|Sulfurquiz.elems Cl:17:Chlorinequiz.elems Ar:18:Argonquiz.elems K:19:Potassium|Kaliumquiz.elems Ca:20:Calciumquiz.elems Sc:21:Scandiumquiz.elems Ti:22:Titaniumquiz.elems V:23:Vanadiumquiz.elems Cr:24:Chromiumquiz.elems Mn:25:Manganesequiz.elems Fe:26:Iron|Ferrumquiz.elems Co:27:Cobaltquiz.elems Ni:28:Nickelquiz.elems Cu:29:Copper|Cuprumquiz.elems Zn:30:Zincquiz.elems Ga:31:Galliumquiz.elems Ge:32:Germaniumquiz.elems As:33:Arsenicquiz.elems Se:34:Seleniumquiz.elems Br:35:Brominequiz.elems Kr:36:Kryptonquiz.elems Rb:37:Rubidiumquiz.elems Sr:38:Strontiumquiz.elems Y:39:Yttriumquiz.elems Zr:40:Zirconiumquiz.elems Nb:41:Niobiumquiz.elems Mo:42:Molybdenumquiz.elems Tc:43:Technetiumquiz.elems Ru:44:Rutheniumquiz.elems Rh:45:Rhodiumquiz.elems Pd:46:Palladiumquiz.elems Ag:47:Silver|Argentumquiz.elems Cd:48:Cadmiumquiz.elems In:49:Indiumquiz.elems Sn:50:Tin|Stannumquiz.elems Sb:51:Antimony|Stibiumquiz.elems Te:52:Telluriumquiz.elems I:53:Iodinequiz.elems Xe:54:Xenonquiz.elems Cs:55:Cesiumquiz.elems Ba:56:Bariumquiz.elems La:57:Lanthanumquiz.elems Ce:58:Ceriumquiz.elems Pr:59:Praseodymiumquiz.elems Nd:60:Neodymiumquiz.elems Pm:61:Promethiumquiz.elems Sm:62:Samariumquiz.elems Eu:63:Europiumquiz.elems Gd:64:Gadoliniumquiz.elems Tb:65:Terbiumquiz.elems Dy:66:Dysprosiumquiz.elems Ho:67:Holmiumquiz.elems Er:68:Erbiumquiz.elems Tm:69:Thuliumquiz.elems Yb:70:Ytterbiumquiz.elems Lu:71:Lutetiumquiz.elems Hf:72:Hafniumquiz.elems Ta:73:Tantalumquiz.elems W:74:Tungsten|Wolframquiz.elems Re:75:Rheniumquiz.elems Os:76:Osmiumquiz.elems Ir:77:Iridiumquiz.elems Pt:78:Platinumquiz.elems Au:79:Gold|Aurumquiz.elems Hg:80:Mercuryquiz.elems Tl:81:Thalliumquiz.elems Pb:82:Lead|Plumbumquiz.elems Bi:83:Bismuthquiz.elems Po:84:Poloniumquiz.elems At:85:Astatinequiz.elems Rn:86:Radonquiz.elems Fr:87:Franciumquiz.elems Ra:88:Radiumquiz.elems Ac:89:Actiniumquiz.elems Th:90:Thoriumquiz.elems Pa:91:Protactiniumquiz.elems U:92:Uraniumquiz.elems Np:93:Neptuniumquiz.elems Pu:94:Plutoniumquiz.elems Am:95:Americiumquiz.elems Cm:96:Curiumquiz.elems Bk:97:Berkeliumquiz.elems Cf:98:Californiumquiz.elems Es:99:Einsteiniumquiz.elems Fm:100:Fermiumquiz.elems Md:101:Mendeleviumquiz.elems No:102:Nobeliumquiz.elems Lw:103:Lawrenciumquiz # quiz - present a quizquiz #   usage: awk -f quiz topicfile question-subj answer-subjquiz quiz BEGIN {quiz     FS = ":"quiz     if (ARGC != 4)quiz         error("usage: awk -f quiz topicfile question answer")quiz     if (getline <ARGV[1] < 0)    # 1st line is subj:subj:...quiz         error("no such quiz as " ARGV[1])quiz     for (q = 1; q <= NF; q++)quiz         if ($q ~ ARGV[2])quiz             breakquiz     for (a = 1; a <= NF; a++)quiz         if ($a ~ ARGV[3])quiz             breakquiz     if (q > NF || a > NF || q == a)quiz         error("valid subjects are " $0)quiz     while (getline <ARGV[1] > 0) # load the quizquiz         qa[++nq] = $0quiz     ARGC = 2; ARGV[1] = "-"      # now read standard inputquiz     srand()quiz     do {quiz         split(qa[int(rand()*nq + 1)], x)quiz         printf("%s? ", x[q])quiz         while ((input = getline) > 0)quiz             if ($0 ~ "^(" x[a] ")$") {quiz                 print "Right!"quiz                 breakquiz             } else if ($0 == "") {quiz                 print x[a]quiz                 breakquiz             } elsequiz                 printf("wrong, try again: ")quiz     } while (input > 0)quiz }quiz quiz function error(s) { printf("error: %s\n", s); exit }wordfreq # wordfreq - print number of occurrences of each wordwordfreq #   input:  textwordfreq #   output: number-word pairs sorted by numberwordfreq wordfreq     { gsub(/[.,:;!?(){}]/, "")    # remove punctuationwordfreq       for (i = 1; i <= NF; i++)wordfreq           count[$i]++wordfreq     }wordfreq END { for (w in count)wordfreq           print count[w], w | "sort -rn"wordfreq     }fmt # fmt - formatfmt #    input:  textfmt #    output: text formatted into lines of <= 60 charactersfmt fmt /./  { for (i = 1; i <= NF; i++) addword($i) }fmt /^$/ { printline(); print "" }fmt END  { printline() }fmt fmt function addword(w) {fmt     if (length(line) + length(w) > 60)fmt         printline()fmt     line = line " " wfmt }fmt fmt function printline() {fmt     if (length(line) > 0) {fmt         print substr(line, 2)   # removes leading blankfmt         line = ""fmt     }fmt }fmt.just # fmt.just - formatter with right justificationfmt.just fmt.just BEGIN { blanks = sprintf("%60s", " ") }fmt.just /./   { for (i = 1; i <= NF; i++) addword($i) }fmt.just /^$/  { printline("no"); print "" }fmt.just END   { printline("no") }fmt.just fmt.just function addword(w) {fmt.just     if (cnt + size + length(w) > 60)fmt.just         printline("yes")fmt.just     line[++cnt] = wfmt.just     size += length(w)fmt.just }fmt.just fmt.just function printline(f,    i, nb, nsp, holes) {fmt.just     if (f == "no" || cnt == 1) {fmt.just         for (i = 1; i <= cnt; i++)fmt.just             printf("%s%s", line[i], i < cnt ? " " : "\n")fmt.just     } else if (cnt > 1) {fmt.just         dir = 1 - dir        # alternate side for extra blanksfmt.just         nb = 60 - size       # number of blanks neededfmt.just         holes = cnt - 1      # holesfmt.just         for (i = 1; holes > 0; i++) {fmt.just             nsp = int((nb-dir) / holes) + dirfmt.just             printf("%s%s", line[i], substr(blanks, 1, nsp))fmt.just             nb -= nspfmt.just             holes--fmt.just         }fmt.just         print line[cnt]fmt.just     }fmt.just     size = cnt = 0fmt.just }xref.data .#Fig _quotes_xref.data Figure _quotes_ gives two brief quotations from famous books.xref.data xref.data                         Figure _quotes_:xref.data xref.data .#Bib _alice_xref.data   "... `and what is the use of a book,' thought Alice,xref.data   `without pictures or conversations?'" [_alice_]xref.data xref.data .#Bib _huck_xref.data   "... if I'd a knowed what a trouble it was to make a bookxref.data   I wouldn't a tackled it and ain't agoing to no more." [_huck_]xref.data xref.data xref.data [_alice_] Carroll, L., Alice's Adventures in Wonderland,xref.data     Macmillan, 1865.xref.data [_huck_] Twain, M., Adventures of Huckleberry Finn,xref.data     Webster & Co., 1885.xref # xref - create numeric values for symbolic namesxref #    input:  text with definitions for symbolic namesxref #    output: awk program to replace symbolic names by numbers          xref xref /^\.#/ { printf("{ gsub(/%s/, \"%d\") }\n", $2, ++count[$1]) }xref END    { printf("!/^[.]#/\n") }xref.ans /^\.#/ { printf("{ gsub(/%s/, \"%d\") }\n", $2, ++count[$1])xref.ans          if (saw[$2])xref.ans              print NR ": redefinition of", $2, "from line", saw[$2]xref.ans          saw[$2] = NRxref.ans        }xref.ans END    { printf("!/^[.]#/\n") }xref1.ans /^\.#/ { s[$2] = ++count[$1]; next }xref1.ans        { for (i in s)xref1.ans              gsub(i, s[i])xref1.ans          printxref1.ans        }say.in.kwic All's well that ends well.say.in.kwic Nature abhors a vacuum.say.in.kwic Every man has a price.kwic awk 'kwic # kwic - generate kwic indexkwic kwic {   print $0kwic     for (i = length($0); i > 0; i--) # compute length only oncekwic         if (substr($0,i,1) == " ")kwic             # prefix space suffix ==> suffix tab prefixkwic             print substr($0,i+1) "\t" substr($0,1,i-1)kwic } ' |kwic sort -f |kwic awk 'kwic BEGIN { FS = "\t"; WID = 30 }kwic       { printf("%" WID "s  %s\n", substr($2,length($2)-WID+1),kwic             substr($1,1,WID))kwic } 'kwic.ans

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -