aboutsummaryrefslogtreecommitdiff
path: root/tests/sysy_lvX/079_calculator.sy
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.001@gmail.com>2026-09-15 08:16:43 +0800
committerRichard Qin <richard.qin.001@gmail.com>2026-09-15 08:16:43 +0800
commit4022e5c95210631e4aceb08eaf9d9071aa5aefc4 (patch)
treea5922559836e45fb3459a3d070367912e1035b1a /tests/sysy_lvX/079_calculator.sy
downloadrcc-master.tar.gz
rcc-master.tar.xz
Set up a git repoHEADmaster
Diffstat (limited to 'tests/sysy_lvX/079_calculator.sy')
-rw-r--r--tests/sysy_lvX/079_calculator.sy184
1 files changed, 184 insertions, 0 deletions
diff --git a/tests/sysy_lvX/079_calculator.sy b/tests/sysy_lvX/079_calculator.sy
new file mode 100644
index 0000000..f67a932
--- /dev/null
+++ b/tests/sysy_lvX/079_calculator.sy
@@ -0,0 +1,184 @@
+int ints[10000];
+int intt;
+int chas[10000];
+int chat;
+int i=0, ii=1;
+int c;
+int get[10000];
+int get2[10000];
+
+int isdigit(int x) {
+ if (x >= 48 && x <= 57)
+ return 1;
+ return 0;
+}
+
+int power(int b, int a) {
+ int result = 1;
+ while (a != 0) {
+ result = result * b;
+ a = a - 1;
+ }
+ return result;
+}
+
+int getstr(int get[]) {
+ int x = getch();
+ int length = 0;
+ while (x != 13 && x != 10) {
+ get[length] = x;
+ length = length + 1;
+ x = getch();
+ }
+ return length;
+}
+
+void intpush(int x)
+{
+ intt = intt + 1;
+ ints[intt] = x;
+}
+void chapush(int x)
+{
+ chat = chat + 1;
+ chas[chat] = x;
+}
+int intpop()
+{
+ intt = intt - 1;
+ return ints[intt + 1];
+}
+int chapop()
+{
+ chat = chat - 1;
+ return chas[chat + 1];
+}
+void intadd(int x)
+{
+ ints[intt] = ints[intt] * 10;
+ ints[intt] = ints[intt] + x;
+}
+
+int find()
+{
+ c = chapop();
+ get2[ii] = 32;
+ get2[ii + 1] = c;
+ ii = ii + 2;
+ if (chat == 0) return 0;
+ return 1;
+}
+
+int main()
+{
+ intt=0;
+ chat=0;
+ int lengets = getstr(get);
+ while (i < lengets)
+ {
+ if (isdigit(get[i]) == 1)
+ {
+ get2[ii] = get[i];
+ ii = ii + 1;
+ }
+ else
+ {
+ if(get[i] == 40) chapush(40);
+ if(get[i] == 94) chapush(94);
+ if(get[i] == 41)
+ {
+ c = chapop();
+ while (c != 40)
+ {
+ get2[ii] = 32;
+ get2[ii + 1]=c;
+ ii = ii + 2;
+ c = chapop();
+ }
+ }
+ if (get[i] == 43)
+ {
+ while (chas[chat] == 43 || chas[chat] == 45 || chas[chat] == 42 || chas[chat] == 47 || chas[chat] == 37 || chas[chat] == 94)
+ {
+ if (find()==0)break;
+ }
+ chapush(43);
+ }
+ if (get[i] == 45)
+ {
+ while (chas[chat] == 43 || chas[chat] == 45 ||chas[chat] == 42 || chas[chat] == 47 || chas[chat] == 37 || chas[chat] == 94)
+ {
+ if(find()==0)break;
+ }
+ chapush(45);
+ }
+ if(get[i] == 42)
+ {
+ while (chas[chat] == 42 || chas[chat] == 47 ||chas[chat] == 37 || chas[chat] == 94)
+ {
+ if (find()==0)break;
+ }
+ chapush(42);
+ }
+ if (get[i] == 47)
+ {
+ while (chas[chat] == 42 || chas[chat] == 47 || chas[chat] == 37 || chas[chat] == 94)
+ {
+ if (find()==0)break;
+ }
+ chapush(47);
+ }
+ if (get[i] == 37)
+ {
+ while (chas[chat] == 42 || chas[chat] == 47 || chas[chat] == 37 || chas[chat] == 94)
+ {
+ if (find()==0)break;
+ }
+ chapush(37);
+ }
+ get2[ii] = 32;
+ ii = ii + 1;
+ }
+ i = i + 1;
+ }
+ while(chat > 0)
+ {
+ int c = chapop();
+ get2[ii] = 32;
+ get2[ii + 1]=c;
+ ii = ii + 2;
+ }
+ get2[ii]= 64;
+ i = 1;
+ while (get2[i] != 64)
+ {
+ if (get2[i] == 43 || get2[i] == 45 || get2[i] == 42 || get2[i] == 47 || get2[i] == 37 || get2[i] == 94)
+ {
+ int a=intpop();int b=intpop();int c;
+ if (get2[i] == 43) c = a + b;
+ if (get2[i] == 45) c = b - a;
+ if (get2[i] == 42) c = a * b;
+ if (get2[i] == 47) c = b / a;
+ if (get2[i] == 37) c = b % a;
+ if (get2[i] == 94) c = power(b,a);
+ intpush(c);
+ }
+ else
+ {
+ if(get2[i] != 32)
+ {
+ intpush(get2[i] - 48);
+ ii=1;
+ while(get2[i+ii] != 32)
+ {
+ intadd(get2[i+ii] - 48);
+ ii = ii + 1;
+ }
+ i = i + ii-1;
+ }
+ }
+ i = i + 1;
+ }
+ putint(ints[1]);
+ return 0;
+}