Lab 4-3: 計算機輸入檢查及例外處理 (20%)

  • 輸入:
    1. 要執行的計算功能,分別如下:
      1. 加 (+)
      2. 減 (-)
      3. 乘 (*)
      4. 除 (/)
      5. 取餘數 (%)
    2. 兩個以 10 為底的實數,以科學記號格式輸入。
      • 實數的正負號 (sign),正負號輸入大於等於 0 為正數,小於 0 為負數。
      • 實數的尾數 (mantissa),尾數部分固定 9 位數字,第一位不為 0,數值為輸入數字乘以\( \frac{1}{100,000,000} \),。
      • 實數的指數部分 (exponent),指數部分為 -38 至 +38。
      • 若為取餘數運算,兩數皆須為正整數。
  • 輸出:所選擇的運算子字元 (i.e. +, -, *, /, %)及兩實數的運算結果,以科學記號表示,有效位數為 15 位。若輸入不合法的數字則需顯示 Invalid input 並結束程式。
  • 檔名:lab4_3_<學號>.cpp (e.g. lab4_3_106062802.cpp)

程式需提示使用者輸入需要的運算功能,輸出該運算功能的運算子字元。 程式需檢查使用者輸入的是否符合題目要求,不合法的輸入需在當下的輸入完成後顯示 Invalid input 並結束程式。 不合法輸出格式範例請參考以下範例。 使用者會在每次輸入時輸入任意實數,請考慮所有合法 double 型態的數字輸入。

Simple scientific calculator
1) plus (+)
2) minus (-)
3) multiplication (*)
4) division (/)
5) modulation (%)
Please select the operator: <1-5>
You selected: <+, -, *, /, %>
Input real number (a), sign: <sign a>
Input real number (a), mantissa: <mantissa a>
Input real number (a), exponent: <exponent a>
Input real number (b), sign: <sign b>
Input real number (b), mantissa: <mantissa b>
Input real number (b), exponent: <exponent b>
The real number (a) is: <output real number a>
The real number (b) is: <output real number b>
(a) <+, -, *, /, %> (b) = <result>

Example:

$ ./a.out
Simple scientific calculator
1) plus (+)
2) minus (-)
3) multiplication (*)
4) division (/)
5) modulation (%)
Please select the operator: 3
You selected: *
Input real number (a), sign: -100
Input real number (a), mantissa: 123456789
Input real number (a), exponent: 10
Input real number (b), sign: -10
Input real number (b), mantissa: 123456788
Input real number (b), exponent: 10
The real number (a) is: -1.23456789e+10
The real number (b) is: -1.23456788e+10
(a) * (b) = 1.52415786267337e+20

$ ./a.out
Simple scientific calculator
1) plus (+)
2) minus (-)
3) multiplication (*)
4) division (/)
5) modulation (%)
Please select the operator: 6
Invalid input

$ ./a.out
Simple scientific calculator
1) plus (+)
2) minus (-)
3) multiplication (*)
4) division (/)
5) modulation (%)
Please select the operator: 4
You selected: /
Input real number (a), sign: 0
Input real number (a), mantissa: 12345678912
Invalid input

$ ./a.out
Simple scientific calculator
1) plus (+)
2) minus (-)
3) multiplication (*)
4) division (/)
5) modulation (%)
Please select the operator: 4
You selected: /
Input real number (a), sign: -100
Input real number (a), mantissa: 123456789
Input real number (a), exponent: 10
Input real number (b), sign: -10
Input real number (b), mantissa: 0
Invalid input

Reference Code:

Credit: 郭羽芹 (110021104)

#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
    int x;
    cout << "Simple scientific calculator" << endl;
    cout << "1) plus (+)" << endl;
    cout << "2) minus (-)" << endl;
    cout << "3) multiplication (*)" << endl;
    cout << "4) division (/)" << endl;
    cout << "5) modulation (%)" << endl;
    cout << "Please select the operator: ";
    cin >> x;
    if (x == 1)
        cout << "You selected: +" << endl;
    else if (x == 2)
        cout << "You selected: -" << endl;
    else if (x == 3)
        cout << "You selected: *" << endl;
    else if (x == 4)
        cout << "You selected: /" << endl;
    else if (x == 5)
        cout << "You selected: %" << endl;
    else
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    double s1, s2, e1, e2;
    double m1, m2;
    cout << "Input real number (a), sign: ";
    cin >> s1;
    cout << "Input real number (a), mantissa: ";
    if (x == 5)
        if (s1 < 1)
        {
            cout << "Invalid input" << endl;
            return 0;
        }
    cin >> m1;
    if (m1 >= 1000000000)
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    else if (m1 < 100000000)
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    cout << "Input real number (a), exponent: ";
    cin >> e1;
    if (e1 < -38 || e1 > 38)
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    int e11 = e1;
    if (e11 != e1)
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    if (s1 >= 0)
        s1 = 1;
    else
        s1 = -1;
    while (m1 >= 10)
        m1 /= 10;
    for (int i = 0; i < e1; i++)
        m1 *= 10;
    m1 = m1 * s1;
    if (x == 5)
    {
        if (e1 < 8)
        {
            cout << "Invalid input" << endl;
            return 0;
        }
    }
    cout << "Input real number (b), sign: ";
    cin >> s2;
    if (x == 5)
        if (s2 < 0)
        {
            cout << "Invalid input" << endl;
            return 0;
        }
    cout << "Input real number (b), mantissa: ";
    cin >> m2;
    if (m2 >= 1000000000)
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    else if (m2 < 100000000)
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    cout << "Input real number (b), exponent: ";
    cin >> e2;
    if (e2 < -38 || e2 > 38)
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    int e22 = e2;
    if (e22 != e2)
    {
        cout << "Invalid input" << endl;
        return 0;
    }
    if (s2 >= 0)
        s2 = 1;
    else
        s2 = -1;
    while (m2 >= 10)
        m2 /= 10;
    for (int i = 0; i < e2; i++)
        m2 *= 10;
    m2 = m2 * s2;
    int m22 = m2;
    if (x == 5)
    {
        if (e2 < 8)
        {
            cout << "Invalid input" << endl;
            return 0;
        }
    }
    cout << "The real number (a) is: " << setprecision(8) << scientific << m1 << endl;
    cout << "The real number (b) is: " << setprecision(8) << scientific << m2 << endl;
    if (x == 1)
        cout << "(a) + (b) = " << setprecision(14) << scientific << m1 + m2 << endl;
    else if (x == 2)
        cout << "(a) - (b) = " << setprecision(14) << scientific << m1 - m2 << endl;
    else if (x == 3)
        cout << "(a) * (b) = " << setprecision(14) << scientific << m1 * m2 << endl;
    else if (x == 4)
        cout << "(a) / (b) = " << setprecision(14) << scientific << m1 / m2 << endl;
    else
    {
        long long a = m1, b = m2;
        cout << "(a) % (b) = " << setprecision(14) << scientific << a % b << endl;
    }
    return 0;
}

Credit: 林元鴻 (110021120)

#include <iostream>
#include <iomanip>
#include <cmath>
int main()
{
    int a = 0, t = 0;
    long long i = 0, a_mod = 0, b_mod = 0, n8 = 100000000;
    double exp_a = 0, exp_b = 0, num_a = 0, num_b = 0, sign_a = 0, sign_b = 0;

    std::cout << "Simple scientific calculator"
              << "\n"
              << "1) plus (+)"
              << "\n"
              << "2) minus (-)"
              << "\n"
              << "3) multiplication (*)"
              << "\n"
              << "4) division (/)"
              << "\n"
              << "5) modulation (%)"
              << "\n"
              << "Please select the operator: ";
    std::cin >> a;
    switch (a)
    {
    case 1:
        std::cout << "You selected: +" << std::endl;
        break;
    case 2:
        std::cout << "You selected: -" << std::endl;
        break;
    case 3:
        std::cout << "You selected: *" << std::endl;
        break;
    case 4:
        std::cout << "You selected: /" << std::endl;
        break;
    case 5:
        std::cout << "You selected: %" << std::endl;
        break;
    default:
        std::cout << "Invalid input" << std::endl;
        return 0;
    }

    std::cout << "Input real number (a), sign: ";
    std::cin >> sign_a;
    if (a == 5 && sign_a < 0)
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    std::cout << "Input real number (a), mantissa: ";
    std::cin >> num_a;
    if (num_a >= 1000000000 || num_a < 100000000)
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    if (a == 5 && ((long long)num_a != num_a || num_a <= 0))
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    for (i = 1; i <= 10; i += 1)
    {
        num_a = (num_a >= 10 ? num_a / 10 : num_a);
    }
    if (sign_a < 0)
        num_a = num_a * (-1);
    std::cout << "Input real number (a), exponent: ";
    std::cin >> exp_a;
    while (((long)(num_a * n8)) % (long)(pow(10, t)) == 0)
    {
        t++;
    }
    t--;
    if (a == 5 && exp_a < (8 - t))
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    if (exp_a > 38 || exp_a < -38 || (long)exp_a != exp_a)
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    std::cout << "Input real number (b), sign: ";
    std::cin >> sign_b;
    if (a == 5 && sign_b < 0)
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    std::cout << "Input real number (b), mantissa: ";
    std::cin >> num_b;
    if (num_b >= 1000000000 || num_b < 100000000)
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    if (a == 5 && ((long long)num_b != num_b || num_b <= 0))
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    for (i = 1; i <= 10; i += 1)
    {
        num_b = (num_b >= 10 ? num_b / 10 : num_b);
    }
    if (sign_b < 0)
        num_b = num_b * (-1);
    std::cout << "Input real number (b), exponent: ";
    std::cin >> exp_b;
    t = 0;
    while (((long)(num_b * n8)) % (long)(pow(10, t)) == 0)
    {
        t++;
    }
    t--;
    if (a == 5 && exp_b < (8 - t))
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }
    if (exp_b > 38 || exp_b < -38 || (long)exp_b != exp_b)
    {
        std::cout << "Invalid input" << std::endl;
        return 0;
    }

    a_mod = num_a * pow(10, exp_a);
    b_mod = num_b * pow(10, exp_b);

    std::cout << "The real number (a) is: " << std::scientific << std::setprecision(8) << num_a * pow(10, exp_a) << std::endl;
    std::cout << "The real number (b) is: " << std::scientific << std::setprecision(8) << num_b * pow(10, exp_b) << std::endl;
    if (a == 1)
        std::cout << "(a) + (b) = " << std::scientific << std::setprecision(14) << num_a * pow(10, exp_a) + num_b * pow(10, exp_b) << std::endl;
    if (a == 2)
        std::cout << "(a) - (b) = " << std::scientific << std::setprecision(14) << num_a * pow(10, exp_a) - num_b * pow(10, exp_b) << std::endl;
    if (a == 3)
        std::cout << "(a) * (b) = " << std::scientific << std::setprecision(14) << (num_a * pow(10, exp_a)) * (num_b * pow(10, exp_b)) << std::endl;
    if (a == 4)
        std::cout << "(a) / (b) = " << std::scientific << std::setprecision(14) << (num_a * pow(10, exp_a)) / (num_b * pow(10, exp_b)) << std::endl;
    if (a == 5)
        std::cout << "(a) % (b) = " << std::scientific << std::setprecision(14) << (double)(a_mod % b_mod) << std::endl;
    return 0;
}