func zapfenHandler(w io.Writer, req *http.Request, lang string) error { page := &zapfenPage{Number: strings.TrimSpace(req.URL.Query().Get("number")), rootPage: rootPage{CurrLang: lang, URL: req.URL.Path}} languages := []string{lang, defaultlang} tpl, idx, err := loadTemplate("DivisionSetup", templzapfenfuncMap, languages, conf_roottemplatedir()+roottplfilename, conf_roottemplatedir()+"%s."+zapfenfilename) if err != nil { panic(err) } if idx > 0 { errstring := fmt.Sprintf("Template file for language '%s' not found, resorting to default language '%s'", lang, defaultlang) log.Printf(errstring) page.Error = append(page.Error, errstring) } if len(page.Number) >= 1 { if num, ok := big.NewInt(0).SetString(page.Number, 10); ok { result := schoolcalc.ZapfenRechnung(num) page.Zapfen = result dividend := result.Multzapfen[7].String() for i := 0; i < 8; i++ { divresult, _ := schoolcalc.SchoolDivide(dividend, strconv.Itoa(i+2), 0) page.IntermedZapfen[i] = divresult dividend = result.Divzapfen[i].String() } } else { page.Error = append(page.Error, fmt.Sprintf("Not a valid integer: '%s'", page.Number)) } } return tpl.Execute(w, page) }
func main() { var lines int var instring string reader := bufio.NewReaderSize(os.Stdin, 1000) for data, prefix, err := reader.ReadLine(); err != io.EOF; data, prefix, err = reader.ReadLine() { if err != nil { fmt.Fprintf(os.Stderr, "sdivcon %d: %s\n", lines, err) continue } if prefix { fmt.Fprintf(os.Stderr, "sdivcon %d: too long, exceeding 1000 characters (ignoring)\n", lines) continue } lines++ instring = normalizeSDivInput(string(data)) splitstrings := strings.Split(instring, ":") if slen := len(splitstrings); slen != 2 { fmt.Fprintf(os.Stderr, "sdivcon %d: not a valid divisor:dividend (ignoring)\n", lines) continue } result, _ := schoolcalc.SchoolDivide(splitstrings[0], splitstrings[1], schoolcalc.SDivPrecReached|2) fmt.Fprint(os.Stdout, result) fmt.Fprintln(os.Stdout) } }
func divisionHandler(w io.Writer, req *http.Request, lang string) error { dividend := strings.TrimSpace(req.URL.Query().Get("dividend")) divisor := strings.TrimSpace(req.URL.Query().Get("divisor")) page := &divisionPage{Dividend: dividend, Divisor: divisor, Boxed: true, StopRemz: true, rootPage: rootPage{CurrLang: lang, URL: req.URL.Path}} prec := 0 page.Precision = strings.TrimSpace(req.URL.Query().Get("prec")) stopremzs := req.URL.Query().Get("stopremz") if len(stopremzs) > 0 { stopremz, err := strconv.ParseBool(stopremzs) if err != nil { page.Error = append(page.Error, fmt.Sprintf("Parameter 'stopremz' tainted: %s", err)) } else { page.StopRemz = stopremz } } boxes := req.URL.Query().Get("boxed") if len(boxes) > 0 { boxed, err := strconv.ParseBool(boxes) if err != nil { page.Error = append(page.Error, fmt.Sprintf("Parameter 'boxed' tainted: %s", err)) } else { page.Boxed = boxed } } languages := []string{lang, defaultlang} tpl, idx, err := loadTemplate("DivisionSetup", templdivfuncMap, languages, conf_roottemplatedir()+roottplfilename, conf_roottemplatedir()+"%s."+divisionfilename) if err != nil { panic(err) } if idx > 0 { errstring := fmt.Sprintf("Template file for language '%s' not found, resorting to default language '%s'", lang, defaultlang) log.Printf(errstring) page.Error = append(page.Error, errstring) } func() { defer func() { // want to handle division by zero if err := recover(); err != nil { log.Printf("%s", debug.Stack()) page.Error = append(page.Error, fmt.Sprint(err)) } }() if len(page.Precision) > 0 { if prec, err = strconv.Atoi(page.Precision); err != nil { page.Error = append(page.Error, fmt.Sprint(err)) } } if len(dividend) > 0 || len(divisor) > 0 { if page.StopRemz { prec = int(schoolcalc.SDivPrecReached | uint8(prec)) } result, err := schoolcalc.SchoolDivide(dividend, divisor, uint8(prec)) if err != nil { page.Error = append(page.Error, fmt.Sprint(err)) } else { page.SDivide = result } } }() return tpl.Execute(w, page) }