C/C++ Programming: Lab 1. Programming principles on C

Part 1 should be completed in the lab period, part 2 in your own time and submitted at the START of your next lab period.

After the first lab period you should be

        Familiar with the basic operation of the Borland C/C++ 4.5 Compiler
        Familiar with standard variables type "int" and "double".       
        Familiar with standard variables type "int" and "double".       
        Familiar with for, while, switch, if commands in C..


Part 1: The following program implements a cheap calculator that reads in integers and operations from the keyboard, such as 7 * 3 + 6 = print the answer, and exit. Just as on a cheap calculator, addition and multiplication have the same precedence, and evaluation is strictly left to right. For example, 4+5*6 = prints 54 because 4+5=9 is computed before the multiplication.


#include < stdio.h >

int main()
       int first, Second; //declare new variables
        char Operation;
                            
        cin >> first >> Operation;
        while (operation != '=')
        {    cin >> second;
             switch(operation)
             {  case '+': first = first + second; 
                case '-': first = first - second; break;     /* good 
                case '*': first = first * second; break;      program*/
                case '/': first = first / second; break;
                default:  cout << "Input error. \n"; return 1
             }
             cin >> operation;
        }    
        cout  "\n"
        cout << first << "\n";
        return 0
}



Part 2. Modify the program written for part 1 to handle the following modifications.

                                            1, if b=0
         a power b =  a=       a, if b =1                                    
                                            square(ab/2), if b is even and b > 1
                                            a*square(ab/2), if b is odd and b > 1

YOU WILL BE REQUIRED TO SUBMIT THIS PROGRAM AT THE START OF YOUR SECOND LAB SESSION.


Comments.

In the lab you will be using C/C++ Borland 4.5 compiler. You can run this compiler by using: Start->Miscellaneous Software -> Programming Tools -> C & C++ -> Borland C & C++ 4.5

Type your program into the text edit window. Remember to save it to your home directory, give it a sensible name: File->Save As

Compile your program. Project->Compile

Highlight the error in the message window and the line in error in the text edit window is displayed. Recompile until there are no errors.

See the Debug menu for run and debug options.