C sharp – hesap makinesi örneği

Ana sayfa Forumlar PROGRAMLAMA C# C sharp – hesap makinesi örneği

2 yazı görüntüleniyor - 1 ile 2 arası (toplam 2)
  • Yazar
    Yazılar
  • #12320

    using System.Collections.Generic;
    using System;

    public static class GlobalMembersCalculator
    {

    public static int no_of_errors;

    public static double error(string s)
    {
    no_of_errors++;
    cerr < < "error: " << s << 'n'; return 1; } public static Token_value curr_tok = Token_value.PRINT; public static double number_value; public static string string_value; public static Token_value get_token() { sbyte ch; do { if (!cin.get(ch)) return curr_tok = Token_value.END; } while (ch != 'n' && char.IsWhiteSpace(ch)); switch (ch) { case ';': case 'n': return curr_tok = Token_value.PRINT; case '*': case '/': case '+': case '-': case '(': case ')': case '=': return curr_tok = Token_value(ch); case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': cin.putback(ch); cin >> number_value;
    return curr_tok = Token_value.NUMBER;
    default:
    if (char.IsLetter(ch))
    {
    string_value = ch;
    while (cin.get(ch) && char.IsLetterOrDigit(ch))
    string_value += ch;

    cin.putback(ch);
    return curr_tok = Token_value.NAME;
    }
    GlobalMembersCalculator.error(“bad token”);
    return curr_tok = Token_value.PRINT;
    }
    }

    public static Dictionary table = new Dictionary();

    public static double expr(bool get)
    {
    double left = GlobalMembersCalculator.term(get);

    for (;;)
    switch (curr_tok)
    {
    case Token_value.PLUS:
    left += GlobalMembersCalculator.term(true);
    break;
    case Token_value.MINUS:
    left -= GlobalMembersCalculator.term(true);
    break;
    default:
    return left;
    }
    }

    public static double prim(bool get)
    {
    if (get)
    GlobalMembersCalculator.get_token();

    switch (curr_tok)
    {
    case Token_value.NUMBER:
    {
    double v = number_value;
    GlobalMembersCalculator.get_token();
    return v;
    }
    case Token_value.NAME:
    {
    double & v = table[string_value];
    if (GlobalMembersCalculator.get_token() == Token_value.ASSIGN)
    v = GlobalMembersCalculator.expr(true);
    return v;
    }
    case Token_value.MINUS:
    return -GlobalMembersCalculator.prim(true);
    case Token_value.LP:
    {
    double e = GlobalMembersCalculator.expr(true);
    if (curr_tok != Token_value.RP)
    return GlobalMembersCalculator.error(“) expected”);
    GlobalMembersCalculator.get_token();
    return e;
    }
    default:
    return GlobalMembersCalculator.error(“primary expected”);
    }
    }

    public static double term(bool get)
    {
    double left = GlobalMembersCalculator.prim(get);

    for (;;)
    switch (curr_tok)
    {
    case Token_value.MUL:
    left *= GlobalMembersCalculator.prim(true);
    break;
    case Token_value.DIV:
    if (double d = GlobalMembersCalculator.prim(true))
    {
    left /= d;
    break;
    }
    return GlobalMembersCalculator.error(“divide by 0”);
    default:
    return left;
    }
    }

    static int Main()
    {

    table.Add(“pi”, 3.1415926535897932385);
    table.Add(“e”, 2.7182818284590452354);

    while (cin)
    {
    GlobalMembersCalculator.get_token();
    if (curr_tok == Token_value.END)
    break;
    if (curr_tok == Token_value.PRINT)
    continue;
    Console.Write(GlobalMembersCalculator.expr(false));
    Console.Write(‘n’);
    }

    return no_of_errors;
    }
    }

    public enum Token_value
    {
    NAME,
    NUMBER,
    END,
    PLUS = ‘+’,
    MINUS = ‘-‘,
    MUL = ‘*’,
    DIV = ‘/’,
    PRINT = ‘;’,
    ASSIGN = ‘=’,
    LP = ‘(‘,
    RP = ‘)’
    }

    #12359
    özgür
    Katılımcı

    daha kısa kodlarla da yapılabilir kardeşim teşekkürler

2 yazı görüntüleniyor - 1 ile 2 arası (toplam 2)
  • Bu konuyu yanıtlamak için giriş yapmış olmalısınız.
Bana Ders Anlat © 2008-2017