SoFunction
Updated on 2025-03-08

Xiaocai Programming Growth Notes (1) Interview Frustration - Is it good to have no errors in the code?) Page 1/3

Xiaocai is a senior in computer science this year. He has learned a lot of software development and has also learned to compile some small programs. He is full of confidence and is determined to find a good unit. After submitting countless resumes, I finally received an interview notice from an organization, and Xiaocai was ecstatic.
When I arrived at someone's unit, the front desk lady gave him a question, which said, "Please implement a calculator console program in C++, Java, C# or any object-oriented language, and ask to enter two numbers and operator symbols to get the result."
When Xiaocai looked at it, it was not simple. It was divided into two parts. In less than 10 minutes, Xiaocai finished writing it, and it felt like there was no mistake. After submitting the paper, the unit said that we would wait for the notice within a week. So Xiaocai had to wait patiently. But half a month has passed and there is no news. Xiaocai is very puzzled. My code has been implemented, why don’t I give me a chance?
Xiaocai found his senior brother Da Niao who had been working for three years. After asking the reasons, Da Niao asked the question and understood the details of Xiaocai's code, he laughed and said, "Xiaocai, Xiaocai, you were fooled. You didn't understand the meaning of the question in the unit, so of course you won't contact you again."
Xiaocai said: "Is my code wrong? Isn't the unit's question just to implement the code of a calculator? What's the problem with me writing like this?"
Copy the codeThe code is as follows:

class Program
{
    static void Main(string[] args)
    {
("Please enter the number A:");
        string A = ();
("Please select the operator symbol (+, -, *, /):");
        string B = ();
("Please enter the number B:");
        string C = ();
        string D = "";

        if (B == "+")
            D = ((A) + (C));
        if (B == "-")
            D = ((A) - (C));
        if (B == "*")
            D = ((A) * (C));
        if (O == "/")
            D = ((A) / (C));

("The result is: "+ D);
    }     
}

What's wrong with the code of Xiaocai?
2. Code specifications and reconstruction

Daniao said: "Let's not say what the questioner means. Just your current code, there are many shortcomings that need to be improved. For example, variable naming, your naming is ABCD, and the variable does not have any specific meaning, which is very irregular; judging branches, your writing method means that each condition must be judged, which is equivalent to the computer doing three useless work; judging the validity of data input, etc., what if the user inputs a character symbol instead of a number? What if the customer inputs 0 when the divisor is a divisor? These are the areas that can be improved."

"Oh, that's right. I've heard from the teacher before, but I've never cared about it. I'll change it immediately and let you see it after the correction."
Copy the codeThe code is as follows:

class Program
{
static void Main(string[] args)
{
try
{
("Please enter the number A:");
string strNumberA = ();
("Please select the operator symbol (+, -, *, /):");
string strOperate = ();
("Please enter the number B:");
string strNumberB = ();
string strResult = "";

switch (strOperate)
{
case "+":
strResult = ((strNumberA) + (strNumberB));
break;
case "-":
strResult = ((strNumberA) - (strNumberB));
break;
case "*":
strResult = ((strNumberA) * (strNumberB));
break;
case "/":
if (strNumberB != "0")
strResult = ((strNumberA) / (strNumberB));
else
strResult = "The divisor cannot be 0";
break;
}

("The result is: " + strResult);

();


}
catch (Exception ex)
{
("Your input is wrong: "+ );
}
}
}


Big Niao: "Hoho, good, good, is it very fast? As for the current code, there is no problem in implementing the calculator, but does the code written in this way fit the questioner's meaning?"

Shoucai: "You mean object-oriented?"

Big bird: "Ha, side dishes are not side dishes!"
123Next pageRead the full text