Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
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<string,double> table = new Dictionary<string,double>();
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 = ')'
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team