Calculator in C++

This is my calculator in C++ with in-line assembler:

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main( void )
{
	int nZahl1 = 0, nZahl2 = 0, nVar3 = 0;string strOp="";
do{
	
   	cout << "calculate (+,-,*,/) :" ;
   	cin >> strOp;

   	cout << "number  1: " ;
   	cin >> nZahl1;
	
   	cout << "number  2: " ;
   	cin >> nZahl2;

	if("+"==strOp){
	asm("ADD %%edx, %%eax \n":"=a"(nZahl1):"d"(nZahl2),"a"(nZahl1));
	cout <<" = "<<(nZahl1)<<endl;
	}else if("-"==strOp){
	asm("SUB %%edx, %%eax \n":"=a"(nZahl1):"d"(nZahl2),"a"(nZahl1));
	cout <<" = "<<(nZahl1)<<endl;
	}else if("*"==strOp){
	asm("MOV %%edx, %%eax \n":"=a"(nVar3):"d"(nZahl1),"a"(nVar3));
	for(int i = 2 ;i<=nZahl2;i++)
	asm("ADD %%edx, %%eax \n":"=a"(nZahl1):"d"(nVar3),"a"(nZahl1));
	cout <<" = "<<(nZahl1)<<endl;
	}else if("/"==strOp){
	asm("MOV %%edx, %%eax \n XOR  %%edx,%%edx \n":"=a"(nVar3),"=d"(nZahl1):"d"(nZahl1),"a"(nVar3));
	for(int i = nZahl2 ;nVar3>=i;nZahl1++)
	asm("SUB %%edx, %%eax \n":"=a"(nVar3):"d"(nZahl2),"a"(nVar3));
	cout <<" = "<<(nZahl1)<<",";
	for(int i = 0 ;i<=10;i++){
	nVar3*=10;nZahl1=0;
	for(int i = nZahl2 ;nVar3>=i;nZahl1++)
	asm("SUB %%edx, %%eax \n":"=a"(nVar3):"d"(nZahl2),"a"(nVar3));
	cout <<(nZahl1);
	}
	cout << endl;
	}

}while(1);

return(0);
}

Compile the code:

g++ -o main test.cpp
./main

output:

calculate (+,-,*,/) :+ 
number  1: 4           
number  2: 5           
 = 9                   
calculate (+,-,*,/) :- 
number  1: 9           
number  2:             
4                      
 = 5                   
calculate (+,-,*,/) :^C
sh-4.3# main           
calculate (+,-,*,/) :+ 
number  1: 4           
number  2: 5           
 = 9                   
calculate (+,-,*,/) :- 
number  1: 9           
number  2: 4           
 = 5                   
calculate (+,-,*,/) :* 
number  1: 4           
number  2: 5           
 = 20                  
calculate (+,-,*,/) :/ 
number  1: 20          
number  2: 4           
 = 5,00000000000

One thought on “Calculator in C++”

  1. Piece of writing writing is also a fun, if you know after that you
    can write or else it is difficult to write.

Leave a Reply

Your email address will not be published.

sixteen + nine =