Sunday 3 February 2013


Smart Calculator:

/**
* author Akhilesh Dhar Dubey
**/

#include<stdio.h>
#include<conio.h>
#include<STDLIB.H>
#include<STRING.H>
main(){
int operand1,operand2,i,j,count,calc,flag,ans,more_opr;
char x[20],opr,y[10],z[10];
do{
operand1=0;
operand2=0;
i=0;
j=0;
count=0;
calc=0;
flag=1;
ans=0;
fflush(stdin);
// clrscr();
printf("Enter:\n");
scanf("%[^\n]s",&x);
fflush(stdin);
// more_opr=0;

for(i=0;x[i]!='\0';){

for(j=0;x[i] =='0' || x[i] =='1' || x[i] =='2' || x[i] =='3' || x[i] =='4' || x[i] =='5' || x[i] =='6' || x[i] =='7' || x[i] =='8' || x[i] =='9';i++,j++){
strcpy((y+j),(x+i));//y[j]=x[i];
}
// strcpy(y,x); //we can write this also bcoz x contain value before any opr accur.
//printf("\t\t\t %d \t\t\t\n",atoi(y));
operand1=atoi(y);
printf("operand1 = %d\t",operand1);

if(x[i] =='+'){
opr=x[i];
i++;
more_opr++;
}
else if(x[i] =='-'){
opr=x[i];
i++;
more_opr++;
}
else if(x[i] =='*'){
opr=x[i];
i++;
more_opr++;
}
else if(x[i] =='/'){
opr=x[i];
i++;
more_opr++;
}

/* if(more_opr >=2){
printf("wrong operation performed...");
break;
}
*/
for(j=0;x[i] =='0' || x[i] =='1' || x[i] =='2' || x[i] =='3' || x[i] =='4' || x[i] =='5' || x[i] =='6' || x[i] =='7' || x[i] =='8' || x[i] =='9';i++,j++){
strcpy((z+j),(x+i));
}
//printf("\t\t\t %d \t\t\t\n",atoi(z));
operand2=atoi(z);
printf("operand2 = %d\t",operand2);

if(opr =='+'){
if(flag){
calc=operand1+operand2;
flag=0;
}
else
calc+=operand2;
printf("opr = %c\n",opr);
}
else if(opr =='-'){
if(flag){
calc=operand1-operand2;
flag=0;
}
else
calc-=operand2;
printf("opr = %c\n",opr);
}
else if(opr =='*'){
if(flag){
calc=operand1*operand2;
flag=0;
}
else
calc*=operand2;
printf("opr = %c\n",opr);
}
else if(opr =='/'){
if(flag){
calc=operand1/operand2;
flag=0;
}
else
calc/=operand2;
printf("opr = %c\n",opr);
}
}
printf("\ncalc = %d",calc);

printf("\n\nDo you want to calculate more:(1/0)");
scanf("%d",&ans);
}while(ans==1);
}

No comments:

Post a Comment