Thread: Basic Calculator

  1. #16
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You might also want to get a better IDE. Notepad isn't particularly that good for writing code.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  2. #17
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    The one-and-only Notepad -- comes with all versions of W*ndows by default; small footprint, easily adaptable to various languages and development environments. A great code editor in all respects. Far superior to Dev-C++ and MSVC.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #18
    Registered User
    Join Date
    Nov 2006
    Location
    Rockdale, IL
    Posts
    11
    Okay, we have very good news. The calculator has came to life! YAY!

    Code:
    /* C-Calculator by Matthew Nicola */
    /* Mid-Term project of M. Fagan's C Programming Class */
    
    #include <stdio.h>
    #include <conio.h>
    
    /*This is where the coding takes place:*/
    
    #pragma argsused
    int
    main(void)
    
    
    {
        double mainval,        /* The starting value for the program */
               memory,         /* Holds memory value for memory functions */
               newval,         /* This modifies the main value by operation */
               exit1;          /* Prevents breaking of loop */
        
        char   opr;            /* Term for basic functions */
        
        exit1 = 0;
        mainval = 0;
        memory = 0;
    
     /* Calculator Introduction */
     
     printf("Welcome to Matt Nicola's C Calculator!\n");
     printf("Enter an operator and then enter the value\n");
     printf("to correspond with the operator. Enjoy!\n\n");
     printf("Enter H in operator for basic commands.\n\n");
     
     while (exit1 == 0) {
           
            printf("\nMEMORY: %lf", memory);
            printf("\nThe current value is %lf", mainval);
        
            printf("\n\nEnter the operator to begin: ");
            scanf("%c", &opr);
     
     switch (opr) {
    
    	case '+':        /* Addition */
    
    		printf("\nEnter a value to add to the main value. ");
    		scanf("%lf", &newval);
    		mainval = mainval + newval;
    		break;
    
    	case '-':        /* Subtraction */
    
    		printf("\nEnter a value to subtract from the main value. ");
    		scanf("%lf", &newval);
    		mainval = mainval - newval;
    		break;
    
    	case '*':        /* Multiplication */
    
    		printf("\nEnter a value to multiply the main value. ");
    		scanf("%lf", &newval);
    		mainval = mainval * newval;
    		break;
    
    	case '/':        /* Division */
    
    		printf("\nEnter a value to divide the main value. ");
    		scanf("%lf", &newval);
    		mainval = mainval / newval;
    		break;
    
    	case 'C':        /* Clear */
    	case 'c':
    
    		mainval = 0.0;
    		printf("\nThe main value has been cleared.\n");
    		break;
    
    	case 'A':       /* Add to memory */
    	case 'a':
    
    		printf("\nEnter a value to add to the memory. ");
    		scanf("%lf", &newval);
    		memory = memory + newval;
    		break;
    
    	case 'D':       /* Subtract from memory */
    	case 'd':
    
    		printf("\nEnter a value to deduct from memory. ");
    		scanf("%lf", &newval);
    		memory = memory - newval;
    		break;
    
    	case 'R':        /* Return the memory */
    	case 'r':
    
    		mainval = memory;
    		printf("\nMemory has returned to the main value.\n");
    		break;
    
    	case 'H':        /* Gives basic commands to user */
    	case 'h':
    
    		printf("\nHELP CONTENTS\n\n");
    		printf("Basic operations comply to their sign.\n");
    		printf("c = Clear the main value.\n");
    		printf("a = Add value to memory.\n");
    		printf("d = Subtract a value from memory.\n");
    		printf("r = Place the memory value into the main value.\n");
    		printf("x = Clear the value of the memory.\n");
    		printf("e = Exit the program.\n\n");
    		break;
    
    	case 'X':        /* Clear the memory */
    	case 'x':
    
    		memory = 0.0;
    		printf("\nThe memory has been cleared.\n");
    		break;
    
    	case 'E':        /* Exit program */
    	case 'e':
    
    		printf("\nThank you for using the calculator. Enter any key");
    		printf("\nand press ENTER to exit the program.");
    		scanf(" ");
    		exit1 = 1;
    		break;
    
    	default:
    
    		printf("\nThat case does not comply to the operator.\n");
    		break;
    
    		}	/* End of Switch statement. */
    		}	/* End of While statement. */
    
    	return (0);
    }
    There's just two things to take care of with this.
    - Reduce the value to its hundredths place (o.oo)
    - Take care of the bug that's making my program repeat something once everytime I enter a value.

    At least it still works, but I just have one bug to take care of.

  4. #19
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You may want to look into your use of scanf. scanf is nasty. I bet it's filling your buffer with newlines that you probably don't want.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Calculator I made check it out if you want...
    By Sshakey6791 in forum C++ Programming
    Replies: 8
    Last Post: 01-08-2009, 12:20 AM
  2. Basic calculator program in C++
    By linkofazeroth in forum C++ Programming
    Replies: 70
    Last Post: 08-28-2005, 04:23 PM
  3. need help with my basic c++ calculator
    By iCouch_Potato in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2004, 06:23 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM