C ++ first winter work

This work belongs courses 2020 Object-Oriented Programming
Where the job requires The first winter operational requirements
The target job 1) understand some of the advantages and disadvantages of C / C ++, and related compilation process, install their own development environment; 2) learn to use the command line to check your compiler version and compile the source file; 3) the use of the vernacular programming
Text of the job Questions and Answers practice topic programming problem
Other references Baidu encyclopedia compiled using Visual Studio comes with MSVC Windows, the command-line compiler C / C ++ program

Questions and Answers

The question: What do you think C language defects (do you think the use is not easily)?

Defects: (1) C language high risk. For example, the C language can be a floating point data type is assigned to an integer variable, it will lose precision, and the compiler will warn, does not complain. I had learned a little bit of Java, also tried it on these practices Idea IDE, compiler found the error directly. Another example pointers and arrays using the C language, if the programmer is not correct and timely manner to the pointer variable assignment, or visit the bounds of an array, the compiler does not complain, and these actions could cause the program to not be able to achieve our It intended function. So it seems, with the C programming language will have more freedom, but there may be some more dangerous not even in line with our expectations expression is not found or disabled compiler. (2) in conjunction with the use of a large number of C language program operators will be written easily confusing. In C, there are a number of operators can be used. In my personal experience, in reading the many operators that combine the use of C code together, which I need to clarify a number of operator precedence, associativity, so C code for me it would be more difficult to read .

Second problem: describe briefly the compilation process C language / C ++ is.

(1) pre-processing the source code is divided into specific cells or - preprocessing token (preprocessing token) to support the language characteristics, expand the header / macro place / remove comments / conditional compilation (pretreatment is generally different from the source code can the execution environment is easily modified or compiled)
(2) check the compiled syntax (grammar and lexical analysis), generating an intermediate code syntax analysis found error message is given.
(3) The intermediate code converting assembler machine language code (object code).
(4) linked by the linker (a separate program) will target code you write, the system boot code and library code (precompiled libraries) these three parts are combined into one file, you can execute the file.

Practice questions

Task one: Check your C ++ compiler version.

I view separate version Dev-C ++ compiler and gcc compiler cl.exe of the vs2019.

· I open the DOS interface with Win + R type cmd, and using gcc -v command to check the version of gcc

· I find vs2019 own direct command line at the beginning of the page, use the cl command to check the version cl

Task two: using the command line to compile a C language / C ++ code.

I tried to compile with gcc and cl are almost complete, always appear as the last command error, unable to open the source files (card in the form of source file is compiled into exe files that step)
gcc

cl

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

编写一个程序,输入满足以下语法要求的一段文字,输出运行后的结果。
变量定义:整数 钱包 等于 零
运算(加法):钱包 增加 四
运算(减法):钱包 减少 四
输出:看看 钱包

样例

输入:

整数 钱包 等于 零
钱包 增加 四
钱包 减少 三
看看 钱包

输出:

注意:

输入输出用例均为GBK编码,推荐使用vscode把文本切换为GBK编码。
数字只会出现以下 零一二三四五六七八九十 。

思考过程:

一.阅读题目,理解编程要求

由题可知需要对“整数”、“钱包”、“等于”、“增加”,“减少”、“看看”等关键字定义相关操作。比如,“整数”用来限定输入的中文数额(零、一、......)为整数;“钱包”相当于是一个储存数据的变量;“等于”相当于给“钱包”赋值;“增加”“减少”则代表了需要对钱包储存的值进行相应的加法和减法操作;“看看”则代表需要对钱包中储存的值做输出操作。

二.转化(汉字数字转阿拉伯数字和阿拉伯数字转汉字数字可以各自写一个函数)

1.需要考虑将中文数字转化为阿拉伯数字。

中文数字(零到十)可直接用if语句转化为对应的阿拉伯数字(0~10)。汉字需用字符数组来储存(每个汉字占两个字节,两个ASCII码可以组成一个GBK码)。因为题目提示输入的数字只会出现零到十,这些汉字数字输入时转化为阿拉伯数字不难。
这时我们可以定义一个变量,将得到的阿拉伯数字累加储存到这个变量中,以此代表钱包中的数额。

2.接下来要考虑的是,将得到的钱包中的数额(此时为阿拉伯数字)转化为汉字数字。

1) 易得0到10可直接使用switch语句完成转化,11到19和20到99需要额外考虑。
2) 先来看20到99,这一组数据最终需转换成二十到九十九。以20和21为例,20应转化为二十(两个汉字),21转化为二十一(三个汉字),比较后易得两者都有十位的2(可直接对应首个汉字“二”(%s)(“十”是20到99的转化成的汉字数字中必然出现的)),而汉字数字是由两个还是三个汉字组成,需要看个位是否为0,若为0,则不用输出第三个数,若不为零,还需要将个位数处理成相应的汉字。而要完成这些操作,前提就是需要将这些数的十位和个位拆开。我想到的是利用数组,然后逆序用其中的元素。数组开两个,不用管个位有无0,这个问题可以直接利用switch语句中的default来解决。
3) 然后就是要考虑11到19了。此时只要考虑个位数字即可,若个位为1,输出设置为(十%s)即可。同样也是用switch来解决。

三.操作指令

看看:使用while语句,先判断该字符串是不是“看看”,如果不是看看,进入循环,设置scanf("%s",store),吸收“钱包”。
增加减少):即加法(减法)操作,用store来储存,后面用if语句判断是何种操作指令
等于:处理等于后面的数字(赋初值)。“等于”前面的“钱包”可以开一个较大的数组储存。
整数:当输入数字不在零到十范围内时报错。
下面是代码展示

汉字数字转阿拉伯数字

处理方式比较暴力,直接if-else语句完成转换,目前还没想到更好的算法。

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;
}

阿拉伯数字转汉字数字

在这里也是处理得比较直接粗暴,0到10之间的转换方法(基础),至于11到19的转换方法和20到99的转换方法,都可以借助0到10的转换方法,只不过需要对阿拉伯数字得提前进行处理。对于11到19,只需要提前处理得到个位数字。而对于20到99,可提前处理得到十位数字和个位数字。

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;
        }
    }
}

完整代码

这份代码还有一些部分我觉得还不太满意,例如,在用户输入不符合要求的数据时,有些部分相应的反馈还没法完成。

#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;
        }
    }
}

测试样例
输入
整数 钱包 等于 一
钱包 增加 二
钱包 增加 四
钱包 减少 五
看看 钱包

输出

输入
整数 银行卡 等于 十
银行卡 增加 十
银行卡 减少 八
看看 银行卡

输出
十二

输入
整数 银行卡 等于 九
银行卡 增加 十
银行卡 减少 三
看看 钱包

输出
“钱包”为未定义变量,请重新输入正确的变量:

Guess you like

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