The programming problem (2020 Object-Oriented Programming first job)

This work belongs courses 2020 Chunfu large object-oriented programming
Where the job requires 1 winter operations
The target job Achieve vernacular programming, recording the learning process, publish blog
Text of the job Programming problem
Other references Kanji deposit with an array of characters

Read the questions

Chinese culture is profound, starting from Changjei creation, Chinese characters has been passed down to this day. When we lamented the long history of Chinese characters, but also can not help feeling, why not a programming language using Chinese characters?
Chinese characters can not really programming? Recently classical programming a fire, I have a number. Said III. Name, called "armor." This unpretentious variable definition is certainly not thousands of years of Chinese culture in the development of a wonderful work.
Today, Wang students think, classical vernacular that can be programmed to do? He found you, let you help him.
Programming requires
the preparation of a program, the result of input to meet the following syntax requirements of a piece of text output operation.
Variable Definition: an integer equal to zero wallet
arithmetic (adder): Purse increased by 4
operation (subtraction): Purse reduced by four
outputs: a look wallet
Sample
input:
an integer equal to zero wallet
purse increased by 4
wallet decrease three
look wallet
output:
a

And thinking process dividing module

We can see from the title
integer purses are used to define the initial value
is equal to the assignment
increase decrease respectively the addition and subtraction
to see the output result for

Programs need to be divided by function into the following sections

  • The Chinese character input is converted to digital
  • The calculation results are output in the form of characters
  • Exclude faulty input
  • The "increase" and "decrease" are calculated

Write modules and test case

Baidu that in the Chinese windows operating system, the default is to use BGK Chinese character set, each character occupies two bytes. Thus char array of characters can be saved, and strcmp (a, b) in a, b returns a value of 0 is identical, it can be judged by strcmp function. Now we can write a function to determine the initial value.

int change(char shu[])
{
    if(strcmp(shu,"一")==0) 
        return 1;
    if(strcmp(shu,"二")==0) 
        return 2;
    if(strcmp(shu,"三")==0) 
        return 3;
    if(strcmp(shu,"一")==0) 
        return 1;
    if(strcmp(shu,"四")==0) 
        return 4;
    if(strcmp(shu,"五")==0) 
        return 5;
    if(strcmp(shu,"六")==0) 
        return 6;
    if(strcmp(shu,"七")==0) 
        return 7;
    if(strcmp(shu,"八")==0) 
        return 8;
    if(strcmp(shu,"九")==0) 
        return 9;
    if(strcmp(shu,"零")==0) 
        return 0;
    
}

Next we need to write a function to output the results is quite good (write only function in a single digital output, data guarantee from 0-99 digit case it can be placed in the main function determination)

void change1(int num)
{
    if(num==0) printf("零");
    if(num==1) printf("一");
    if(num==2) printf("二");
    if(num==3) printf("三");
    if(num==4) printf("四");
    if(num==5) printf("五");
    if(num==6) printf("六");
    if(num==7) printf("七");
    if(num==8) printf("八");
    if(num==9) printf("九");
}

Consider first how the first line of input. Four can be kept short character array are four words, and an error input provided Laid judgment. As the name of the variable it is not necessarily a "wallet", so the first line of the input time if the variable name is not "wallet" is not input error, and we can initialize the data. Here is the main function for inputting sentence of the first line and check.

scanf("%s %s %s %s",a,b,c,d);
 if(strcmp(a,"整数")!=0||strcmp(c,"等于")!=0)
    {printf("输入错误\n");
     return 0;
    }
 num=change(d);

As the "look" the need outputs, so when the input "look" is no longer enter the calculation.
If the calculated variable name input lines with variable names different from the first row, also belong to input errors.
After removing this case, then the input value and the next calculation type.
If the calculated and the type "increase" or "decrease" are the same, respectively, into different operations, if a "plus", "minus the" no such input into the operation.

while(1)
    {scanf("%s",y);
     if(strcmp(y,"看看")==0) break;
     if(strcmp(y,b)!=0)
        {printf("输入错误\n");
         return 0;
        }
     scanf("%s",z);
     scanf("%s",x);
     if(strcmp(z,"增加")==0)
        num=num+change(x);
     else if(strcmp(z,"减少")==0)
        num=num-change(x);
     else 
        {printf("输入错误\n");
         return 0;
        } 
    }

The original output section only write one sentence

change1(num);

Which can meet the input and output sample.

But then I think Although the numbers zero through nine only, but many times greater than ten operands can be obtained, so the calculation results of the classification.
Can be divided into three categories, the first category is a digit, the second is the integer ten, the third category is a two-digit number, but not the entire ten.

 if(num/10==0) 
    change1(num);
 else if(num%10==0) 
    {change1(num/10);
     printf("十"); 
    }
 else
    {change1(num/10);
     printf("十");
     change1(num%10);
    }

This complies outputs the calculation result is greater than ten.

Here is a variable name can not be detected sample "wallet"

Areas for improvement

If the input is not a single-digit numbers, so I can not judge the program and calculate

The complete code

#include<stdio.h>
#include<string.h>
int change(char shu[])
{
    if(strcmp(shu,"一")==0) 
        return 1;
    if(strcmp(shu,"二")==0) 
        return 2;
    if(strcmp(shu,"三")==0) 
        return 3;
    if(strcmp(shu,"一")==0) 
        return 1;
    if(strcmp(shu,"四")==0) 
        return 4;
    if(strcmp(shu,"五")==0) 
        return 5;
    if(strcmp(shu,"六")==0) 
        return 6;
    if(strcmp(shu,"七")==0) 
        return 7;
    if(strcmp(shu,"八")==0) 
        return 8;
    if(strcmp(shu,"九")==0) 
        return 9;
    if(strcmp(shu,"零")==0) 
        return 0;
    
}
void change1(int num)
{
    if(num==0) printf("零");
    if(num==1) printf("一");
    if(num==2) printf("二");
    if(num==3) printf("三");
    if(num==4) printf("四");
    if(num==5) printf("五");
    if(num==6) printf("六");
    if(num==7) printf("七");
    if(num==8) printf("八");
    if(num==9) printf("九");
}
int main()
{char a[10],b[10],c[10],d[10],y[10],z[10],x[10],m[10];
 int num;
 scanf("%s %s %s %s",a,b,c,d);
 if(strcmp(a,"整数")!=0||strcmp(c,"等于")!=0)
    {printf("输入错误\n");
     return 0;
    }
 num=change(d);
 while(1)
    {scanf("%s",y);
     if(strcmp(y,"看看")==0) break;
     if(strcmp(y,b)!=0)
        {printf("输入错误\n");
         return 0;
        }
     scanf("%s",z);
     scanf("%s",x);
     if(strcmp(z,"增加")==0)
        num=num+change(x);
     else if(strcmp(z,"减少")==0)
        num=num-change(x);
     else 
        {printf("输入错误\n");
         return 0;
        } 
    }
 scanf("%s",m);
 if(num/10==0) 
    change1(num);
 else if(num%10==0) 
    {change1(num/10);
     printf("十"); 
    }
 else
    {change1(num/10);
     printf("十");
     change1(num%10);
    }
 return 0;
}

Guess you like

Origin www.cnblogs.com/CCchaos/p/12233453.html