C ++ Programming winter for the first time the title of the job

This work belongs courses 2020 Object-Oriented Programming
Where the job requires The first winter operational requirements
The target job To use the vernacular programming
Text of the job as follows
Other references no

Programming problem

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 requirements

Write a program, enter the following syntax requirements of a text, the result of 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:

Integer equal to zero wallet
purse increased by 4
purse decrease three
to see the wallet

Output:

One

note:

Example input and output are GBK encoding, the text is recommended vscode switched GBK encoding.
The following figures will only be zero 12,345,678 ninety.

Thought process:

A. Read the questions and understand the programming requirements

The need to "be seen by the title integer ", " wallet ", " equal ", " increase ", " decrease ", " look at the definition of related operations" and other keyword. For example, " integer " used to define the amount of Chinese inputs (zero, one, ......) are integers; " wallet " is a variable corresponding to the data storage; " equal " is equivalent to " wallet " Assignment; " increase "" decrease "represents the need for purse value stored corresponding addition and subtraction operations;" look at "the value stored in the wallet needs to do represents the output operation.

II. Conversion (digital-to-kanji Arabic characters and Arabic numerals turn may each write a digital function)

1. The need to consider the Chinese numbers into Arabic numbers.

Chinese number (zero to ten) may be directly converted to Arabic numerals (0 to 10) corresponding with the if statement. Required to store Kanji character array (two bytes for each character, ASCII code can be composed of a two GBK code). Because the task presenting digital input will only occur zero to ten, translated into Arabic numerals is not difficult when these characters digital input.
Then we can define a variable, and the resulting cumulative Arabic numerals stored in the variable, as a proxy for the amount wallet.

2. The next thing to consider is the amount obtained wallet (in this case Arabic numerals) converted into digital characters.

1) easy to get from 0 to 10 may be used as a switch statement to complete the conversion, 11-19 and 20-99 require additional considerations.
2) First look at 20-99, the set of data will ultimately be converted into twenty to ninety-nine. Example 20 and 21, 20 should be converted to twenty (two characters), 21 is converted to twenty-one (three characters), after comparison both readily available ten 2 (corresponding to the first Chinese characters can be directly "two" (% s) ( "ten" is the conversion of 20 to 99 characters into the inevitable numbers)), while the number of Chinese characters is composed of two or three characters, it is necessary to see if the bit is 0, if is 0, then do not output a third number, if not zero, the number of bits also need to be treated to a corresponding Chinese characters. And to complete these operations, the premise is the need for the tens and ones of these numbers apart. I am thinking of using the array, and then in reverse order with its elements. An array of two open, do not control whether the bit 0, this problem can directly use the switch statement default to resolve.
3) then it is to consider the 11-19. At this time, as far as the digit to, if the bit is 1, the output is set to (X% s) can be. It is also a switch to solve.

Three operation instructions

See : Using the while statement determines whether the first character string is not " look ", if not to see , into the circulation, provided scanf ( "% s", store ), absorption "wallet."
Increase ( decrease ): i.e., addition (subtraction) operation, a store to store, if statement is used later operation instruction which
is equal to : equal number after treatment (initial value). "Equal" in front of "wallet" can start a large storage array.
Integer : when the error is not within the range of zero to ten digital input.
Here is the code shows .

Digital-to-digital Arabic characters

Compare violence treatment, to complete the conversion directly if-else statements, yet to think of a better algorithm.

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

Arabic characters turn digital

Here the comparison is handled directly rude conversion method between 0 to 10 (basic), as the conversion method 11-19 and 20-99 of the conversion method, a method can be converted by means of from 0 to 10, but the need for Arabic numerals must be processed in advance. For 11-19, only we need to deal with to get a single-digit advance. For 20 to 99, may be processed to obtain the tens digit and ones digit advance.

void exc2(int sum)
{
    int i,j=10;
    if(sum<=10){
        switch(sum){
            case 0:printf("零");break;
            case 1:printf("一");break;
            case 2:printf("二");break;
            case 3:printf("三");break;
            case 4:printf("四");break;
            case 5:printf("五");break;
            case 6:printf("六");break;
            case 7:printf("七");break;
            case 8:printf("八");break;
            case 9:printf("九");break;
            case 10:printf("十");break; 
        }
    }
    else if(11<=sum&&sum<=19){
        switch(sum%10){
            case 1:printf("十一");break;
            case 2:printf("十二");break;
            case 3:printf("十三");break;
            case 4:printf("十四");break;
            case 5:printf("十五");break;
            case 6:printf("十六");break;
            case 7:printf("十七");break;
            case 8:printf("十八");break;
            case 9:printf("十九");break;
        }
    }
}

The complete code

There are some parts of this code is still not satisfied, I think, for example, the user input data does not meet the requirements, some parts of the law have not completed the appropriate feedback.

#include<stdio.h>
#include<string.h>
int exc1(char store[10]);
void exc2(int sum);
int main()
{
    int sum,temp;
    char s0[10],store[10]; //store[10]用来暂存汉字
    store[0]='\0'; 
    scanf("%s",store);
    scanf("%s",s0);//用于储存代表变量的汉字,如钱包,银行卡之类的 
    while(strcmp(store,"等于")!=0) scanf("%s",store);//遇到“等于”,准备进行赋值操作 
    scanf("%s",store);
    sum=exc1(store);//赋初值,exc1函数用于汉字转为阿拉伯数字 
    scanf("%s",store);
    while(strcmp(store,"看看")!=0){//判断是否进入运算操作 
        scanf("%s",store);
        if(strcmp(store,"增加")==0){
            scanf("%s",store);
            temp=exc1(store);
            while(temp==-1){
            printf("您输入的数字无效,请重新输入:"); 
            scanf("%s",store);
            temp=exc1(store);
          }
            sum=sum+temp;
        }
        else{
            scanf("%s",store);
            temp=exc1(store);
            while(temp==-1){
            printf("您输入的数字无效,请重新输入:"); 
            scanf("%s",store);
            temp=exc1(store);
          }
            sum=sum-temp;
        }
        scanf("%s",store);
    }
    scanf("%s",store);//输入钱包+回车 
    if(strcmp(store,s0)!=0){
        printf("“%s”为未定义变量,请重新输入正确的变量: ",store);
        scanf("%s",store);
    } 
    if(strcmp(store,s0)==0){
        if(sum<20) exc2(sum);
        else{//大于等于20的数我分成两部分处理:十位和个位,中间输出一个“十”即可 
            exc2(sum/10);
            printf("十");
            if(sum%10!=0) exc2(sum%10); 
        }
    }
    return 0;
}
int exc1(char store[10])
{
    if(strcmp(store,"零")==0) return 0;
    else if(strcmp(store,"一")==0) return 1;
    else if(strcmp(store,"二")==0) return 2;
    else if(strcmp(store,"三")==0) return 3;
    else if(strcmp(store,"四")==0) return 4;
    else if(strcmp(store,"五")==0) return 5;
    else if(strcmp(store,"六")==0) return 6;
    else if(strcmp(store,"七")==0) return 7;
    else if(strcmp(store,"八")==0) return 8;
    else if(strcmp(store,"九")==0) return 9;
    else if(strcmp(store,"十")==0) return 10;
    else return -1;
}
void exc2(int sum)
{
    int i,j=10;
    if(sum<=10){
        switch(sum){
            case 0:printf("零");break;
            case 1:printf("一");break;
            case 2:printf("二");break;
            case 3:printf("三");break;
            case 4:printf("四");break;
            case 5:printf("五");break;
            case 6:printf("六");break;
            case 7:printf("七");break;
            case 8:printf("八");break;
            case 9:printf("九");break;
            case 10:printf("十");break; 
        }
    }
    else if(11<=sum&&sum<=19){
        switch(sum%10){
            case 1:printf("十一");break;
            case 2:printf("十二");break;
            case 3:printf("十三");break;
            case 4:printf("十四");break;
            case 5:printf("十五");break;
            case 6:printf("十六");break;
            case 7:printf("十七");break;
            case 8:printf("十八");break;
            case 9:printf("十九");break;
        }
    }
}

Test case

Entry

Integer equal to a wallet
wallet add two
purses increased by 4
purse decrease five
look wallet

Export

two

Entry

Integer equal to ten bank card
bank cards increased by 10
bank cards to reduce eight
to see the bank card

Export

twelve

Entry

Integer equal to nine bank card
bank cards increased by 10
bank cards to reduce the three
to see the wallet

Export

"Wallet" undefined variables, please re-enter the correct variables:

Guess you like

Origin www.cnblogs.com/031902522ycy/p/12235245.html