Saturday, April 20, 2013

C source code for simple calculator!!

Below is the complete working C source code for the simple calculator. Click here for the problem description. This programs takes the input file name as a command line argument and write the result in the output.dat in the current directory.

//
//  main.c
//  Calculator
//
//  Created by chanduthedev on 4/18/13.
//

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>


void add(int first, int second,FILE * ofp)
{
    printf("%d + %d = %d\n",first,second,first+second);
    fprintf(ofp,"%d + %d = %d\n",first,second,first+second);
    
}
void multiply(int first, int second,FILE *ofp)
{
    printf("%d * %d = %d\n",first,second,first * second);
    fprintf(ofp,"%d * %d = %d\n",first,second,first * second);

}
void substract(int first, int second, FILE *ofp)
{
    
    printf("%d - %d = %d\n",first,second,first-second);
    fprintf(ofp,"%d - %d = %d\n",first,second,first-second);

}
void divide(int first,int second, FILE * ofp)
{
    
    fprintf(ofp,"%d / %d = %d\n",first,second,first/second);
    printf("%d / %d = %d\n",first,second,first/second);
}
void toUpperCase(char ch, FILE * ofp)
{
    printf("%c ==> %c\n",ch,toupper(ch));
    fprintf(ofp,"%c ==> %c\n",ch,toupper(ch));
}
void toLowerCase(char ch,FILE * ofp)
{
    printf("%c ==> %c\n",ch,tolower(ch));
    fprintf(ofp,"%c ==> %c\n",ch,tolower(ch));

}
void printKthDigit(int val, int k,FILE * ofp)
{
    int i=1;
    int temp =val;
    while(temp >0)
    {
        if(i == k)
        {
            printf("%d (digit @ %d) ==> %d\n",val,k, temp%10);
            fprintf(ofp,"%d (digit @ %d) ==> %d\n",val,k, temp%10);

            break;
        };
        i++;
        temp = temp/10;
    }
    
}
void roundDecimal(double x, int n, FILE * ofp)
{
    char temp[32]="";
    
    double  rounded = round(x);
    
    sprintf(temp,"%.*lf",n,rounded);
    printf("%lf (precision=%d) => %s\n",x,n,temp);
    fprintf(ofp,"%lf (precision=%d) => %s\n",x,n,temp);
    
}

void separte(double x , FILE * ofp)
{
    char temp[32]="";
    char sign;
    int intPart;
    int fraction;
    sprintf(temp,"%lf",x);
    
    sscanf(temp,"%d.%d",&intPart,&fraction);
    
    if(intPart > 0)
        sign = '+';
    else
    {
        sign = '-';
        intPart = -1 * intPart;
    }
    printf("%lf ==>  sign=%c, integer=%d,fraction=%d\n",x, sign,intPart,fraction);
    fprintf(ofp,"%lf ==>  sign=%c, integer=%d,fraction=%d\n",x, sign,intPart,fraction);

}
void partitionInteger(int i, int x, FILE * ofp)
{
    int j;
    int k;
    
    j = floor(x* i/100.0);
    
    k = i-j;
    
    printf("i=%d,x=%d ==>  j=%d, k=%d \n",i,x,j,k);
    fprintf(ofp,"i=%d,x=%d ==>  j=%d, k=%d \n",i,x,j,k);
}
void help( FILE * ofp)
{
    printf("\n *** Help Menu! See below for Calculator Commands: ***\n");
    printf("\n + i j ---> Add integers i and j \n");
    printf("\n * i j ---> Multiply integers i and j \n");
    printf("\n + -i j ---> Subtract integer j from i \n");
    printf("\n /i j ---> Divide integer i by j \n");
    printf("\n C Ch ---> Change character Ch to uppercase \n");
    printf("\n c Ch ---> Change character Ch to lowercase \n");
    printf("\n P i k ---> Print out the k-th digit of integer i \n");
    printf("\n R x i ---> Round double value x to i decimal places \n");
    printf("\n S x ---> Separate out the sign, integer part and fractional part of double value x \n");
    printf("\n D i x ---> Given integers i and x, print out two integers j and k, where the sum of j and k equals i, and when you take x'percent' of i and truncate it you get j \n");
    printf("\n H ---> view all commands \n");
    printf("\n Q ---> Quit program\n");
    
}


int main(int argc, const char * argv[])
{

    if(argc != 2)
    {
        printf("Usage ");
        exit(0);
    }
    
    const char * inFile=argv[1];
 
    char str[64];
    int lineNum=1;
    int first,second;
    double firstDouble;
    char ch;
    char outFile[]="outData.dat";
    
    FILE * ofp = fopen(outFile,"w");
    
    FILE * fp = fopen(inFile,"r");
    while(fgets(str,64,fp) != NULL)
    {
        if(lineNum == 1)
        {
            printf("Initial %s",str);
        }
        else
        {
            char * temp = str;
            char cmd = temp[0];
            temp++; //ignore command
            temp++; //ignore space;
                
            printf("CMD=%c : ",cmd);
            if(cmd == '+')
            {
                sscanf(temp,"%d %d",&first,&second);
                add(first,second,ofp);
            }
            else if (cmd == '*')
            {
                sscanf(temp,"%d %d",&first,&second);
                multiply(first,second,ofp);
            }
            else if (cmd == '-')
            {
                sscanf(temp,"%d %d",&first,&second);
                substract(first,second,ofp);
            }
            else if (cmd == '/')
            {
                sscanf(temp,"%d %d",&first,&second);
                divide(first,second,ofp);
            }
            else if (cmd == 'C')
            {
                sscanf(temp,"%c",&ch);
                toUpperCase(ch,ofp);
            }
            else if (cmd == 'c')
            {
                sscanf(temp,"%c",&ch);
                toLowerCase(ch,ofp);
            }
            else if (cmd == 'P')
            {
                sscanf(temp,"%d %d",&first, &second);
                printKthDigit(first,second,ofp);
            }
            else if (cmd == 'R')
            {
                sscanf(temp, "%lf %d",&firstDouble,&second);
                roundDecimal(firstDouble, second,ofp);
            }
            else if (cmd == 'S')
            {
                sscanf(temp,"%lf",&firstDouble);
                separte(firstDouble,ofp);
            }
            else if (cmd == 'D')
            {
                sscanf(temp,"%d %d",&first,&second);
                partitionInteger(first,second,ofp);
            }
            else if (cmd == 'H')
            {
                help(ofp);
            }
            else if (cmd == 'Q')
            {
                break;//quits
            }
            else
            {
                printf("Function Z not implemented yet!");
            }
        }
        lineNum++;
    }
    
    fclose(fp);
    fclose(ofp);
    return 0;
}


Input file format:

KING
H
+ 2 5
- 10 5
* 8 9
/ 92 3
c A
C b
P 56234 3
R 3.64195 2
S -51.235
D 50 10

OutPut:
2 + 5 = 7
10 - 5 = 5
8 * 9 = 72
92 / 3 = 30
A ==> a
b ==> B
56234 (digit @ 3) ==> 2
3.641950 (precision=2) => 4.00
-51.235000 ==>  sign=-, integer=51,fraction=235000
i=50,x=10 ==>  j=5, k=45


No comments:

Popular Posts