Lectures 3 and 4 of C++mooc in Tongji University

Keywords: calculator

Lectures 3 and 4 of C++moooc in Tongji University

Lecture 3

Topic 1

The function of the following program is to divide two non-zero integers by a quotient and a remainder.There are several errors in the program, try to find out which ones to modify, and verify the results on the computer.

Code slice.

// #include "iostream"                 
using namespace std;
int main()
{
 int x, y, r1, r2;
    cin >> x >> y;
    if (x == 0||y == 0)
        cout << "input error" << endl;
    else
 
    {
        if (x > y)
 
        {
            r1 = x / y;
            r2 = x % y;
        }
 
        else
        { 
 
            r1 = y / x;
            r2 = y % x;
}
    }
 
    cout <<"merchant = " << r1 << " Remainder = " << r2 << endl;
 
    system("pause");
 
    return 0;
}

Question 2

When shopping in a mall, if the value x of the selected item is within the following range, the actual amount y is paid at the following discount:

Implement known x-seeking y with switch statement

Code slice.

// #include "iostream"                 
using namespace std;
int main()
{
int x;
    double y;
    cout << "Please enter the commodity value:" << endl;
    cin >> x;
    switch (x/1000)
    {
    case 0:y = x; break;
    case 1:y = 0.9*x; break;
    case 2:y = 0.8*x; break;
    default:y = 0.7*x; break;
    }
    cout << "Payment amount is:" << y << endl;
    system("pause");
 
    return 0;
}

Question 3

Write a complete program to simulate the pocket calculator. The results are shown in the diagram.Requirements: Enter two operands and one operator, which determines the operation to be performed.

Code slice.

// #include "iostream"                 
#include "iostream"                 
using namespace std;
int main()
{
double x,y,z;
    char ch;
        cout << "Please enter operand 1, operator op,Operand 2:" << endl;
        cin >> x >> ch >> y;
        switch(ch)
        {
        case '+':z = x + y; break;
        case '-':z = x- y; break;
        case '*':z = x * y; break;
        case '/':z = x / y; break;
        }
        cout << x << ch << y << "=" << z << endl;
system("pause");
return 0;

} 

Question 4

Enter three numbers, x, y, z, to display in order from smallest to largest.Display Form: *x<*x<*x*

Code slice.

// #include "iostream"                 
#include "iostream"                 
using namespace std;
int main()
{
 double t, A[3];
    int i, j;
    cout << "Please enter what you want to compare x,y,z: " << endl;
    for (i = 0; i < 3; i++)
        cin >> A[i];
    for (i =1; i < 3; i++)
    {
        for (j =0; j <i; j++)
        {
            if (A[i]< A[j])
            {
                t = A[j];
                A[j] = A[i];
                A[i] = t;
                 
            }
        }
    }
        cout << A[0] << "<" << A[1] << "<" << A[2] << endl;
        system("pause");
        return 0;

} 

Lecture 4

Topic 1

The following program sums odd numbers up to 20.There are several errors in the program, try to find out which ones to modify, and verify the results on the computer.

Code slice.

//     #include  "iostream"
    #define PI 3.14
    using namespace std;
    int main()
     
    {
            int n, sum=0;
    for (n = 1; ; n += 2)
    {
        if (n>=20) break; 
    else
    sum = sum + n;
     
    }
    cout << "sum=" << sum << endl;
    system("pause");
    return 0;
}

Question 2

Write a program to output a decimal integer in reverse order.That is, if input 156, output 651.

Code slice.

// #include "iostream"                 
using namespace std;
int main()
{
   int a, i=10,j=1,b ;
    cin >> a ;
    while(a / j % i>=0)
    {
        if(a / j % i >0)
        {
        b = a / j % i;
        cout << b; 
        }
        if (a / j % i == 0)
        { 
            b = a / j;
            if (b > 1)
                cout << '0';
            else break;
        }
        j = 10 * j;
    }
      
    system("pause");
    return 0;

}

Question 3

Program to show all the daffodils.The so-called number of daffodils refers to a three-digit number with each digit cube equal to the number itself.

Code slice.

// #include "iostream"                 
#include "iostream"                 
using namespace std;
int main()
{
     int i, k, m, t;
    for (i = 100; i < 1000; i++)
    {
        t = 0; k = i;
        while (k > 0)
        {
            m = k % 10;
            t += m * m * m;
            k /= 10;
        }
        if (t == i)
            printf("%d\n", t);
    }
 
    system("pause");  
    return 0;
} 
 

Question 4

Code slice.

// #include "iostream"                 
#include "iostream"                 
using namespace std;
int main()
{
     int n, a, temp=0,i,s=0;
    a = rand()%10;
    n = 4 + rand() % 6;
    for (i = 1; i<= n; i++)
    {
         
        temp = temp * 10 + a;
        s = temp + s;
    }
    cout << "n="<<n<<" a="<<a << endl;
    cout <<s<< endl;
    system("pause");  
    return 0;
} 
 

Posted by Democreous on Sat, 20 Jun 2020 18:27:41 -0700