ref: 75b1fcc9cf71fa5e72ac5030ac8815c7e6be674e
parent: 4d5b562d78b2ac82526f634ee4800d25d1775065
author: de88 <hamidreza.soleimani88@gmail.com>
date: Sun Nov 26 07:11:19 EST 2023
add calculator with + and *, need final touches
--- a/Makefile
+++ b/Makefile
@@ -17,6 +17,7 @@
cmd/echo.o\
cmd/clear.o\
cmd/check.o\
+ cmd/cal.o\
L = \
libc/strlen.o\
--- /dev/null
+++ b/cmd/cal.c
@@ -1,0 +1,110 @@
+char*
+somegetline(int n)
+{
+ char c;
+ char *line;
+
+ int i = 0;
+
+ line = malloc(n);
+ memset(line, 0, n);
+
+ while(i < n)
+ {
+ c = ps2_getc();
+ // you may, or may not require the following
+ vga_putc(c);
+
+ if(c == '\r' || c == '\n')
+ {
+ line[i] = '\0';
+ return line;
+ }
+ else if(c != '\b')
+ {
+ line[i] = c;
+ i++;
+ }
+ else
+ {
+ /* we don't want a negative i */
+ if(i)
+ i--;
+ line[i] = 0;
+ }
+ }
+}
+
+int
+chartonum(char input[10]){
+ int i, result = 0;
+
+ for (i = 0; input[i] != '\0'; i++) {
+ result = result * 10 + (input[i] - '0');
+ }
+ return result;
+}
+
+// char*
+// numtochar(int input){
+// char result[10] = "0000000000";
+// for (int i = 9; i >= 0 ; i--) {
+// result[i] = input%10;
+// input = input/10;
+// }
+// return *result;
+// }
+
+char*
+numtochar(int input){
+ char *result = malloc(11);
+ // result = "0000000000";
+ for (int i = 9; i >= 0 ; i--) {
+ result[i] = '0' + (input % 10);
+ input = input/10;
+ }
+ return result;
+}
+
+void
+cal_main(int, char**){
+ char *input;
+ int num1;
+ int num2;
+ // char *oper;
+ // num1 = chartonum(somegetline(16));
+ // num2 = chartonum(somegetline(16));
+ // printf("%d",num1*num2);
+ printf("Welcome To Calculator!\n\n");
+ // printf("Welcome To Calculator!\n\n");
+ while (1)
+ {
+ printf("enter number 1\n");
+ input = somegetline(16);
+ if (!strcmp(input, "quit")) return;
+ num1 = chartonum(input);
+
+ printf("enter number 2\n");
+ input = somegetline(16);
+ if (!strcmp(input, "quit")) return;
+ num2 = chartonum(input);
+
+ printf("enter operation\n");
+ input = somegetline(16);
+ if (!strcmp(input, "quit")) return;
+
+ if(!strcmp(input, "+"))
+ {
+ printf("%d",num1+num2);
+ printf("\n\n");
+ }
+ else if(!strcmp(input, "*"))
+ {
+ printf("%d",num1*num2);
+ printf("\n\n");
+ }
+
+ }
+
+}
+
--- a/cmd/rc.h
+++ b/cmd/rc.h
@@ -4,6 +4,7 @@
int echo_main(int, char**);
int clear_main(int, char**);
int check_main(int, char**);
+int cal_main(int, char**);
static prog_t cmdtbl[] =
@@ -11,4 +12,5 @@
{"echo", echo_main},
{"clear", clear_main},
{"check", check_main},
+ {"cal", cal_main},
};