Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6a5f9a0

Browse files
update
1 parent7581dc1 commit6a5f9a0

File tree

561 files changed

+1317112
-92
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

561 files changed

+1317112
-92
lines changed

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.vs/
22
obj/
3-
bin/
3+

‎ebOS/Calc.cs‎

Lines changed: 150 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,124 +3,187 @@
33
usingSystem.Text;
44
//using System.IO;
55
usingSys=Cosmos.System;
6+
// C# program to evaluate a given expression
7+
68
namespaceebOS
79
{
8-
publicclassCalc
10+
publicclassEvaluateString
911
{
10-
stringexpr;
11-
intpos;
12-
boolIsError=true;
13-
publicintRun()
12+
publicstaticstringEvaluate(stringexpression)
1413
{
15-
if(IsError){
16-
intans;
17-
do
14+
char[]tokens=expression.ToCharArray();
15+
16+
// Stack for numbers: 'values'
17+
Stack<float>values=newStack<float>();
18+
19+
// Stack for Operators: 'ops'
20+
Stack<char>ops=newStack<char>();
21+
22+
for(inti=0;i<tokens.Length;i++)
23+
{
24+
// Current token is a whitespace, skip it
25+
if(tokens[i]==' ')
26+
{
27+
continue;
28+
}
29+
30+
// Current token is a number,
31+
// push it to stack for numbers
32+
if(tokens[i]>='0'&&tokens[i]<='9')
1833
{
19-
pos=0;
20-
Console.Write("Enter expression (0 to quit): ");
21-
expr=Console.ReadLine();
22-
ans=addsubt();
23-
if(expr[index:pos]!='0')
34+
StringBuildersbuf=newStringBuilder();
35+
36+
// There may be more than
37+
// one digits in number
38+
while(i<tokens.Length&&
39+
tokens[i]>='0'&&
40+
tokens[i]<='9')
2441
{
25-
error();
42+
sbuf.Append(tokens[i++]);
2643
}
27-
if(ans!=0)
44+
values.Push(int.Parse(sbuf.ToString()));
45+
46+
// Right now the i points to
47+
// the character next to the digit,
48+
// since the for loop also increases
49+
// the i, we would skip one
50+
// token position; we need to
51+
// decrease the value of i by 1 to
52+
// correct the offset.
53+
i--;
54+
}
55+
56+
// Current token is an opening
57+
// brace, push it to 'ops'
58+
elseif(tokens[i]=='(')
59+
{
60+
ops.Push(tokens[i]);
61+
}
62+
63+
// Closing brace encountered,
64+
// solve entire brace
65+
elseif(tokens[i]==')')
66+
{
67+
while(ops.Peek()!='(')
68+
{
69+
values.Push(applyOp(ops.Pop(),
70+
values.Pop(),
71+
values.Pop()));
72+
}
73+
ops.Pop();
74+
}
75+
76+
// Current token is an operator.
77+
elseif(tokens[i]=='+'||
78+
tokens[i]=='-'||
79+
tokens[i]=='*'||
80+
tokens[i]=='/')
81+
{
82+
83+
// While top of 'ops' has same
84+
// or greater precedence to current
85+
// token, which is an operator.
86+
// Apply operator on top of 'ops'
87+
// to top two elements in values stack
88+
while(ops.Count>0&&
89+
hasPrecedence(tokens[i],
90+
ops.Peek()))
2891
{
29-
Console.WriteLine(ans.ToString());
92+
values.Push(applyOp(ops.Pop(),
93+
values.Pop(),
94+
values.Pop()));
3095
}
96+
97+
// Push current token to 'ops'.
98+
ops.Push(tokens[i]);
3199
}
32-
while(ans!=0);
33-
return0;
34100
}
35-
else
101+
102+
// Entire expression has been
103+
// parsed at this point, apply remaining
104+
// ops to remaining values
105+
while(ops.Count>0)
36106
{
37-
return15;
107+
values.Push(applyOp(ops.Pop(),
108+
values.Pop(),
109+
values.Pop()));
38110
}
111+
112+
// Top of 'values' contains
113+
// result, return it
114+
returnexpression+" = "+values.Pop().ToString();
39115
}
40-
intaddsubt()
116+
117+
// Returns true if 'op2' has
118+
// higher or same precedence as 'op1',
119+
// otherwise returns false.
120+
publicstaticboolhasPrecedence(charop1,
121+
charop2)
41122
{
42-
if(IsError)
43-
{
44-
intrtn=Multdiv();
45-
while(expr[index:pos]=='+'&&expr[index:pos]=='-')
123+
if(op2=='('||op2==')')
46124
{
47-
intop=expr[index:pos++];
48-
intopr2=Multdiv();
49-
if(op=='+')
50-
{
51-
rtn+=opr2;
52-
}
53-
else
54-
{
55-
rtn-=opr2;
56-
}
125+
returnfalse;
57126
}
58-
returnrtn;
127+
if((op1=='*'||op1=='/')&&
128+
(op2=='+'||op2=='-'))
129+
{
130+
returnfalse;
59131
}
60132
else
61133
{
62-
return0;
134+
returntrue;
63135
}
64136
}
65-
intMultdiv()
137+
138+
// A utility method to apply an
139+
// operator 'op' on operands 'a'
140+
// and 'b'. Return the result.
141+
publicstaticfloatapplyOp(charop,
142+
floatb,floata)
66143
{
67-
if(IsError)
68-
{
69-
intrtn=number();
70-
while(expr[index:pos]=='*'&&expr[index:pos]=='/')
144+
switch(op)
71145
{
72-
intop=expr[index:pos++];
73-
intopr2=number();
74-
if(op=='*')
75-
{
76-
rtn*=opr2;
77-
}
78-
else
79-
{
80-
rtn/=opr2;
81-
}
82-
}
83-
returnrtn;
84-
}
85-
else
86-
{
87-
return0;
146+
case'+':
147+
returna+b;
148+
case'-':
149+
returna-b;
150+
case'*':
151+
returna*b;
152+
case'/':
153+
if(b==0)
154+
{
155+
thrownew
156+
System.NotSupportedException(
157+
"Cannot divide by zero");
158+
}
159+
returna/b;
88160
}
161+
return0;
89162
}
90-
intnumber()
163+
164+
// Driver method to test above methods
165+
publicstaticvoidRun()
91166
{
92-
if(IsError)
93-
{
94-
intrtn;
95-
if(expr[index:pos]=='(')
167+
stringi;
168+
stringans;
169+
boolKeepRunning=true;
170+
while(KeepRunning)
96171
{
97-
pos++;
98-
rtn=addsubt();
99-
if(expr[index:pos]!=')')
172+
Console.Write("> ");
173+
i=Console.ReadLine();
174+
try
175+
{
176+
ans=Evaluate(i).ToString();
177+
Console.WriteLine(ans);
178+
}
179+
catch(Exception)
100180
{
101-
error();
102-
returnrtn;
181+
KeepRunning=false;
182+
Console.WriteLine("Error: syntax error");
103183
}
184+
185+
104186
}
105-
if(!char.IsDigit(expr[index:pos]))
106-
{
107-
error();
108-
}
109-
rtn=int.Parse(expr+pos);
110-
while(char.IsDigit(expr[index:pos]))
111-
{
112-
pos++;
113-
}
114-
returnrtn;
115-
}
116-
else
117-
{
118-
return0;
119-
}
120-
}
121-
voiderror()
122-
{
123-
IsError=false;
124187
}
125188
}
126189
}

‎ebOS/Kernel.cs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void about()
2828
voidhelp()
2929
{
3030
Console.WriteLine("ebOS "+ver);
31-
Console.WriteLine(@"system commands:
31+
Console.WriteLine(@"system commands:
3232
shutdown - shut down
3333
reboot/restart - reboot the system
3434
@@ -38,6 +38,9 @@ void help()
3838
3939
other:
4040
zen - show the zen of python
41+
42+
apps:
43+
calc - calculator
4144
");
4245
//"hello - display greeting\n" +
4346
//"zen - display zen of python\n");
@@ -105,10 +108,13 @@ void help()
105108
about();
106109
break;
107110
*/
111+
//terminal
112+
case"cls":
113+
Console.Clear();
114+
break;
108115
//apps
109116
case"calc":
110-
Calccalc=newCalc();
111-
calc.Run();
117+
EvaluateString.Run();
112118
break;
113119
//no reason
114120
case"hello":
22.7 MB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set timeout=0
2+
3+
menuentry 'ebOS' {
4+
multiboot2 /boot/ebOS.bin
5+
boot
6+
}
7.79 KB
Binary file not shown.
9.72 KB
Binary file not shown.
1.28 KB
Binary file not shown.
5.63 KB
Binary file not shown.
6.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp