Develop calculator applications in ten programming languages

  1. The C language
  2. C# (Windows desktop)
  3. Swift (ios app)
  4. python
  5. Dart (Flutter app, cross-platform, Android, ios, MAC, Windows, web)
  6. Java (Android App)
  7. Kotlin (Android)
  8. Js+Html+Vue (H5 application)
  9. Wechat applets
  10. Tiktok small program

C language version

The development tools

Dev-C++ (Windows) GCC (Mac)

The main code

#define MAXSIZE 100 #define END '=' #include "stdio.h" #include "stdlib.h" #include "malloc.h" #include "math.h" char ops[MAXSIZE]; // operator stack int ops_top; // double ovs[MAXSIZE]; // operand stack int ovs_top; Void push_ops(char x); Void push_ovs(double x); // stack operands char pop_ops(); // double pop_ovs(); Char gettop_ops(); Double gettop_ovs(); Void inistack_ops(); // Initialize operator stack void inistack_ovs(); // Initialize operand stack CHAR Precede(char T1,char T2); Int char_In(char c); // check whether c is the operator double Operate(double a,char theta,double b); EvaluateExpression(); // Double EvaluateExpression(); //ops[] = operand stack, ovs[] = operand stack [+] char input_p='n'; // define a global variable to cache input [+] int temp2=0; [+] int main(int argc, char* argv[]) {printf(" calculator - peanut programming \n"); Printf (" Please enter a calculation expression, such as 1+2=\n"); printf("%f\n",EvaluateExpression( )); getchar(); } void push_ops(char x) {// stack if(ops_top==MAXSIZE-1) {printf(" stack full! Overflow "); exit(1); } else { ops_top++; ops[ops_top]=x; }} void push_ovs(double x) {if(ovs_top==MAXSIZE-1) {printf(" operand stack is full! Overflow "); exit(1); } else { ovs_top++; ovs[ovs_top]=x; }} char pop_ops() {// operator out of stack char y; If (ops_top==-1) {printf(" input error "); exit(1); } else { y=ops[ops_top]; ops_top--; } return y; } double pop_ovs() {// double y; If (ovs_top==-1) {printf(" input error "); exit(1); } else { y=ovs[ovs_top]; ovs_top--; } return y; } char gettop_ops() {if (ops_top! =-1) return ops[ops_top]; Else {printf(" input error "); exit(1); }} double gettop_ovs() {if (ovs_top! =-1) return ovs[ovs_top]; Else {printf(" input error "); exit(1); }} void inistack_ops() {// initialize ops_top=-1; } void inistack_ovs() {// initialize the operand stack ovs_top=-1; } char Precede(char T1,char T2) {// determine the priorities of T1 and T2 char F; switch(t2) { case '+': case '-': if (t1=='('||t1==END) f='<'; else f='>'; break; case '*': case '/': if (t1=='*'||t1=='/'||t1==')') f='>'; else f='<'; break; Case '(': if (t1==')') {printf(" input error "); exit(1); } else f='<'; break; case ')': switch(t1) { case '(': f='='; break; Case END: printf(" input error "); exit(1); default: f='>'; } break; case END: switch(t1) { case END: f='='; break; Case '(': printf(" error "); exit(1); default: f='>'; } } return f; } int char_In(char c) {switch(c) {case '+': if((! Temp2) && (char_In (input_h) | | input_h = = 'n')) {/ / used to detect whether an input for operator "+" push_ovs (0); [+]} return 1; case '-': if((! Temp2) && (char_In (input_h) | | input_h = = 'n')) {/ / used to detect whether an input for operator "+" push_ovs (0); [+]} return 1; case '(': if((! Temp2)) {if (input_h = = ') | | (input_h > = '0' && input_h < = '9') | | input_h = = '. ') {printf (" I "); exit(0); } } return 1; case ')': if((! temp2)){ if(! ((input_h > = '0' && input_h < = '9') | | input_h = = ') ')) {printf (" I "); exit(0); } } return 1; case '*': case '/': case END: return 1; default : return 0; }} double a,char theta,double b; Switch (theta) {// Theta is the operator case '+': c=a+b; // Output the ASCII code break from 0 to 9; case '-': c=a-b; break; case '*': c=a*b; break; case '/': c=a/b; } return c; EvaluateExpression() {cache_Len=0, I,j,flag=0; [+] double a,b,curnum; [+] char stack_x,theta,input_c,buff[MAXSIZE]; // Buffs store numeric strings [+] inistack_ops(); // initialize operator stack push_ops(END); Inistack_ovs (); // initialize the operand stack input_c=getchar(); stack_x=gettop_ops(); while(input_c! =END||stack_x! If (char_In(input_c)) {// If the input character is one of the seven operators input_h=input_c; [+] for(I =0; i<flag; I ++) {// Cache string null [+] buff[I]='\0'; } if(flag) {push_ovs(curnum); } flag=0; // Precede [+] switch (Precede(STACK_x,input_c)) {case '<': temp2=0; push_ops(input_c); Input_c =getchar(); input_c=getchar(); break; case '=': temp2=0; stack_x=pop_ops(); Input_c =getchar(); input_c=getchar(); break; case '>': temp2++; theta=pop_ops(); b=pop_ovs(); a=pop_ovs(); push_ovs(Operate(a,theta,b)); break; }} else if ((input_c > = '0' && input_c < = '9') | | input_c = = '. ') {/ / input_c operand is the if (input_h = = ') ') {/ / input checking) the right not to digital printf (" I "); exit(0); } input_h=input_c; Input_c =input_c-'0'; flag++; // Flag count increment [+] buff[flag-1]=input_c; [+] curnum=(double)atof(buff); [+] input_c=getchar(); } else {printf(" input error "); exit(1); } stack_x=gettop_ops(); } return(gettop_ovs()); }Copy the code

Complete source code

Gitee.com/hspbc/calcu…

About me

Xiamen university computer professional | huawei eight years senior engineer Ten years software development experience, 5 years experience in teaching programming training Currently engaged in the teaching of programming, software development, software class graduation design guidance. All programming materials and open source projects can be found at juejin.cn/post/700279…