Beginner programming beginner 5

Posted by makeshift on Wed, 02 Feb 2022 10:37:14 +0100

BC47 judge whether it is a letter

describe

KiKi wants to judge whether the input character is a letter, please help him program it.

Enter Description:

Multiple sets of input, one character per line.

Output Description:

For each group of input, the output occupies a separate line to judge whether the input character is a letter. See the output example for the output content.

Example 1

Input:

A
6

Output:

A is an alphabet.
6 is not an alphabet.

Inner thoughts:

This problem is nothing special. Just use an isalpha() function and remember the header file. However, this question accidentally found the characteristics of multiple groups of input. Originally, I always thought that the while (CIN > > A) statement is an endless loop, but there are actually two exit methods. One is, of course, to enter data that does not belong to the data type specified by A. for example, if a is defined as int a, and then enter a, it will exit; Second, if you enter ctrl + Z, you will also exit. Note whether spaces and enter will be ignored or belong to the state of waiting for input. More than one data can be entered in the brackets of while(), such as while (CIN > > a > > b).

The code of this problem is as follows:

#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
    char a;
    while(cin>>a)
    {
        if(isalpha(a))
        {
            cout<<a<<" is an alphabet."<<endl;
        }
        else
        {
            cout<<a<<" is not an alphabet."<<endl;
        }
    }
}

BC48 letter case conversion

describe

KiKi wants to complete the case conversion of letters. If there is a character, judge whether it is an uppercase letter. If so, convert it into lowercase letters; Otherwise, it is converted to uppercase letters.

Enter Description:

Multiple groups of input, one letter per line.

Output Description:

For each group of inputs, the output occupies a separate line and outputs the corresponding form of letters.

Example 1

Input:

a
A
Z

Output:

A
a
z

Inner thoughts:

I first thought of using isupper() and toupper() functions, but I thought toupper() function directly outputs letters, but I didn't expect it to be ASCII code, so I need to convert it. The code is implemented as follows:

#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
    char ch;
    while(cin>>ch)
    {
        if(isupper(ch))
        {
            putchar(tolower(ch));
            cout<<endl;
        }
        else
        {
            putchar(toupper(ch));
            cout<<endl;
        }
    }
}

The 11th line can also be written as (char) tower (CH), Be careful not to forget to wrap.

BC53 calculation of univariate quadratic equation

describe

Input the values of a, B and C from the keyboard, program, calculate and output the univariate quadratic equation a x 2 + b x + c = 0 ax^2+bx+c=0 For the root of ax2+bx+c=0, when a = 0, output "not rectangular equation". When a ≠ 0, according to Δ = b 2 − 4 a c Δ=b^2-4ac Δ= Calculate and output the root of the equation in the three cases of b2 − 4ac.

Enter Description:

Multiple sets of inputs, one line, including three floating-point numbers a, B and C, separated by a space, represent the univariate quadratic equation a x 2 + b x + c = 0 ax^2+bx+c=0 Coefficient of ax2+bx+c=0.

Output Description:

For each group of inputs, output a row and output a quadratic equation of one variable a x 2 + b x + c = 0 ax^2+bx+c=0 The root of ax2+bx+c=0.

If a = 0, output "not rectangular equation";

If a ≠ 0, there are three cases:

Δ = 0, then the two real roots are equal, and the output form is: x1=x2 =.

Δ > 0, then the two real roots are unequal, and the output form is: x1 =; X2 =..., where x1 < = x2.

Δ < 0, then there are two virtual roots, then output: x1 = real part - imaginary part i;x2 = real part + imaginary part i, that is, the imaginary part coefficient of x1 is less than or equal to the imaginary part coefficient of x2. When the real part is 0, it can not be omitted. Real part= − b / ( 2 a ) -b / (2a) − b/(2a), imaginary part= s q r t ( − Δ ) / ( 2 a ) sqrt(-Δ) / (2a) sqrt(−Δ)/(2a)

All real numbers are required to be accurate to 2 digits after the decimal point, and there is no space between numbers and symbols.

Example 1

Input:

2.0 7.0 1.0

Output:

x1=-3.35;x2=-0.15

Example 2

Input:

0.0 3.0 3.0

Output:

Not quadratic equation

Example 3

Input:

1 2 1

Output:

x1=x2=-1.00

Example 4

Input:

2 2 5

Output:

x1=-0.50-1.50i;x2=-0.50+1.50i

Example 5

Input:

1 0 1

Output:

x1=0.00-1.00i;x2=0.00+1.00i

Inner thoughts:

Although it seems a little annoying, it's OK to mainly divide the situation. Others are relatively general. However, when I think there is no problem, I found that the expected output is x1=x2=0.00, but the output is x1=x2=-0.00. This is an interesting bug, but I can hardly find it. I only found a relevant thought:

Thoughts on - 0 and 0 in C + + (including view of variable memory)

From it, we know that - 0 and 0 are equal in judgment, but they are different in output. Just pay attention to them later.

The code implementation is as follows:

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
    double a, b, c;
    while (cin >> a >> b >> c)
    {
        if (a == 0)
        {
            cout << "Not quadratic equation" << endl;
        }
        else
        {
            cout << setprecision(2) << fixed;
            if (b * b - 4 * a * c == 0)
            {
                cout << "x1=x2=" << ((-b / (2 * a)==0)?0.00:(-b / (2 * a))) << endl;
            }
            else if (b * b - 4 * a * c > 0)
            {
                cout << "x1=" << (-b - pow((b * b - 4 * a * c), 0.5)) / (2 * a) << ";x2=" << (-b + pow((b * b - 4 * a * c), 0.5)) / (2 * a) << endl;
            }
            else
            {
                cout << "x1=" << -b / (2 * a) << "-" << sqrt(-b * b + 4 * a * c) / (2 * a) << "i;x2=" << -b / (2 * a) << "+" << sqrt(-b * b + 4 * a * c) / (2 * a) << "i" << endl;
            }
        }
    }
}

BC54 get month days

describe

KiKi wants to get how many days there are in a certain month of a certain year. Please help him program it. Enter the year and month to calculate the number of days in this month of the year.

Enter Description:

Multiple sets of inputs, with two integers in one line, representing the year and month respectively, separated by spaces.

Output Description:

For each group of input, the output is a line and an integer, indicating the number of days in this month of the year.

Example 1

Input:

2008 2

Output:

29

Inner thoughts:

This question is nothing special, but a little skill can be much more concise, otherwise it may be very complicated and repeated if... else statements. The code implementation is as follows:

#include<iostream>
using namespace std;
int main()
{
    int dayOfFeb = 0;
    int year,month;
    while(cin>>year>>month)
    {
        if(year % 4 == 0)
        {
            dayOfFeb = 29;
        }
        else
        {
            dayOfFeb = 28;
        }
        switch(month)
        {
            case 2:
                cout<<dayOfFeb<<endl;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                cout<<"30"<<endl;
                break;
            default:
                cout<<"31"<<endl;
                break;
        }
    }
}

It can be seen that it is not necessary to repeat the cout statement for each case. It can be stacked together, which looks much more comfortable and convenient.

Topics: C++ Programming