OOP programming that perfect 1.0

Topics are as follows:

The programming problem (use C or C ++ language, complete the following topic) :( 5 basis points)

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


Header files and definitions

#include<iostream>
#include<string.h>
using namespace std;
int sum;//总额 
char var[10],ini[10],oper[100];//变量,初始化 operation 运算

Handler

① transfer function:

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

② calculation functions: taking into account the operational requirements of this optimized architecture, functions as far as possible within the line 15, then the calculation function into the addition and subtraction functionsRunoff in the past

Add function:

void deal_add(char str[])
{
    if(strcmp(" 增加 一",str)==0)sum+=1;
    if(strcmp(" 增加 二",str)==0)sum+=2;
    if(strcmp(" 增加 三",str)==0)sum+=3;
    if(strcmp(" 增加 四",str)==0)sum+=4;
    if(strcmp(" 增加 五",str)==0)sum+=5;
    if(strcmp(" 增加 六",str)==0)sum+=6;
    if(strcmp(" 增加 七",str)==0)sum+=7;
    if(strcmp(" 增加 八",str)==0)sum+=8;
    if(strcmp(" 增加 九",str)==0)sum+=9;
    if(strcmp(" 增加 十",str)==0)sum+=10;
}
Strcmp function using the input string is determined, and further calculates variables.

Decreasing function:

void deal_minus(char str[])
{
    if(strcmp(" 减少 一",str)==0)sum-=1;
    if(strcmp(" 减少 二",str)==0)sum-=2;
    if(strcmp(" 减少 三",str)==0)sum-=3;
    if(strcmp(" 减少 四",str)==0)sum-=4;
    if(strcmp(" 减少 五",str)==0)sum-=5;
    if(strcmp(" 减少 六",str)==0)sum-=6;
    if(strcmp(" 减少 七",str)==0)sum-=7;
    if(strcmp(" 减少 八",str)==0)sum-=8;
    if(strcmp(" 减少 九",str)==0)sum-=9;
    if(strcmp(" 减少 十",str)==0)sum-=10;
}
Reason as above.

Output function:

void output(int x)
{
    if(x==0)cout<<"零";  
    if(x==1)cout<<"一";  
    if(x==2)cout<<"二";  
    if(x==3)cout<<"三";  
    if(x==4)cout<<"四";  
    if(x==5)cout<<"五";  
    if(x==6)cout<<"六";  
    if(x==7)cout<<"七";  
    if(x==8)cout<<"八";  
    if(x==9)cout<<"九";  
    if(x==10)cout<<"十"; 
} 
This function is designed for a single output digits, and for the total amount is greater than 10, it will be processed in the main function.

The main function:

int main()
{
    for(int i=0;i<2;i++)cin>>var;//读入变量名 
    for(int i=0;i<2;i++)cin>>ini;//读入初始值    
    sum=trans(ini);//将初始值转换为数字,赋值给总额。 
    for(;;)
    {
        cin>>var;//通过cin读入变量名,遇到空格停止 
        gets(oper);//通过gets读入计算操作,但会读入前面的一个空格,所以上方的加减函数字符串前面有空格 
        deal_add(oper);//加
        deal_minus(oper);//减 
        if(strcmp("看看",var)==0)break;//当读入“看看”时退出 
    }
    if(sum<=10)output(sum);
    if(sum>10)
    {
        int ten_position=sum/10;
        if(ten_position==1)cout<<"十";
        else{
            output(ten_position);
            cout<<"十";
        }
        output(sum%10);
    }
    return 0;
}

According to the meaning of the questions, read the format of the first line four words, key phrases for the second - the variable name, and the fourth - the initial value, and therefore, byOpportunisticA word is read twice, after reading the assignment to a variable, the variable name and read the initial value.
See note read in other ways.
After exiting, to calculate the total. Total points is greater than 10 and less than or equal to 10 considered.

<= 10: Chinese direct digital output corresponding to
greater than 10:

If ten is 1 then directly outputs "ten", then the output of single-digit numbers corresponding to the characters
ten is not 1, the first output of ten corresponding to characters, then individual output a Chinese character "ten", the final output bits correspond to characters


The following test data:

Input Example 1:

Integer equal to five purse
wallet increased by 4
purse reduced by four
to see the wallet

Input Example 2:

Integer equal to five bags
bags increased by 10
bags increased by 10
bags increased by 10
bags increased by 10
to see the bag

Input Example 3:

I integer equal to nine
I added four
I cut five
I cut eight
to see me

Input Example 4:

Integer equal to ten Rubik
Cube increased to half a
cube reduce three
cube increase six
to see Cube

The current version of the improvements:

Can initialize variables, variable names can change it. While the total is greater than ten less than one hundred can be output correctly.

The current version there are still problems with:

Initialization, input a Chinese character input is calculated only (zero ~ ten).

Whenever upgrade about it

Complete code:

#include<iostream>
#include<string.h>
using namespace std;
int sum;//总额 
char var[10],ini[10],oper[100];//变量,初始化 operation 运算
int trans(char str[])
{
    if(strcmp("零",str)==0)return 0;
    if(strcmp("一",str)==0)return 1;
    if(strcmp("二",str)==0)return 2;
    if(strcmp("三",str)==0)return 3;
    if(strcmp("四",str)==0)return 4;
    if(strcmp("五",str)==0)return 5;
    if(strcmp("六",str)==0)return 6;
    if(strcmp("七",str)==0)return 7;
    if(strcmp("八",str)==0)return 8;
    if(strcmp("九",str)==0)return 9;
    if(strcmp("十",str)==0)return 10;
} 
void deal_add(char str[])
{
    if(strcmp(" 增加 一",str)==0)sum+=1;
    if(strcmp(" 增加 二",str)==0)sum+=2;
    if(strcmp(" 增加 三",str)==0)sum+=3;
    if(strcmp(" 增加 四",str)==0)sum+=4;
    if(strcmp(" 增加 五",str)==0)sum+=5;
    if(strcmp(" 增加 六",str)==0)sum+=6;
    if(strcmp(" 增加 七",str)==0)sum+=7;
    if(strcmp(" 增加 八",str)==0)sum+=8;
    if(strcmp(" 增加 九",str)==0)sum+=9;
    if(strcmp(" 增加 十",str)==0)sum+=10;
}
void deal_minus(char str[])
{
    if(strcmp(" 减少 一",str)==0)sum-=1;
    if(strcmp(" 减少 二",str)==0)sum-=2;
    if(strcmp(" 减少 三",str)==0)sum-=3;
    if(strcmp(" 减少 四",str)==0)sum-=4;
    if(strcmp(" 减少 五",str)==0)sum-=5;
    if(strcmp(" 减少 六",str)==0)sum-=6;
    if(strcmp(" 减少 七",str)==0)sum-=7;
    if(strcmp(" 减少 八",str)==0)sum-=8;
    if(strcmp(" 减少 九",str)==0)sum-=9;
    if(strcmp(" 减少 十",str)==0)sum-=10;
}
void output(int x)
{
    if(x==0)cout<<"零";  
    if(x==1)cout<<"一";  
    if(x==2)cout<<"二";  
    if(x==3)cout<<"三";  
    if(x==4)cout<<"四";  
    if(x==5)cout<<"五";  
    if(x==6)cout<<"六";  
    if(x==7)cout<<"七";  
    if(x==8)cout<<"八";  
    if(x==9)cout<<"九";  
    if(x==10)cout<<"十"; 
} 

int main()
{
    for(int i=0;i<2;i++)cin>>var;//读入变量名 
    for(int i=0;i<2;i++)cin>>ini;//读入初始值    
    sum=trans(ini);//将初始值转换为数字,赋值给总额。 
    for(;;)
    {
        cin>>var;//通过cin读入变量名,遇到空格停止 
        gets(oper);//通过gets读入计算操作,但会读入前面的一个空格,所以上方的加减函数字符串前面有空格 
        deal_add(oper);//加
        deal_minus(oper);//减 
        if(strcmp("看看",var)==0)break;//当读入“看看”时退出 
    }
    if(sum<=10)output(sum);
    if(sum>10)
    {
        int ten_position=sum/10;
        if(ten_position==1)cout<<"十";
        else{
            output(ten_position);
            cout<<"十";
        }
        output(sum%10);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/LiangYC1021/p/12235959.html