2020寒假作业-1

这个作业属于哪个课程 2020年面向对象程序设计
这个作业要求在哪里 面向对象程序设计寒假作业1
这个作业的目标 1.安装C++开发环境 2.完成问答题、实践题及编程题 3.发布博客
作业正文 https://www.cnblogs.com/ying-hua/p/12222477.html
其他参考文献 https://jingyan.baidu.com/article/b2c186c81e79a6c46ef6ffd1.html

一.问答题

1.c语言的缺陷:

我不了解c++,学习c语言到现在的确遇到一些不顺手的地方。例如:输入或输出不同类型的变量时要用不同的格式符,比如%d %lf。有时整个程序都要修改,比较麻烦。最烦恼的地方就是在程序某些地方写错的时候不会编译错误,比如把%d写成%n,或者忘记加地址符&,这类错误就难以找到。

2.c语言的编译过程:

(1)进行编译预处理:对程序中的伪指令和特殊符号进行处理。
(2)编译:将经过预处理之后的程序转换成特定汇编代码。
(3)汇编:将汇编代码转化为机器码,产生目标文件。
(4)连接:连接多个目标文件和库文件,产生可执行文件。

二.实践题

1.查看c++编译器版本

打开devc++,就在右上角,如图2020寒假作业-1_第1张图片

2.使用命令行编译一份c语言代码

由于不懂什么事命令行,我搜索到了如下网址,并按他的方法操作
https://jingyan.baidu.com/article/b2c186c81e79a6c46ef6ffd1.html2020寒假作业-1_第2张图片
然而里面有错误,无论如何我都找不到vsvarsall.bat 后来看到图片中是vcvarsall.bat
于是我就找到vcvarsall.bat的目录,模仿图片中的代码写,居然成功了
2020寒假作业-1_第3张图片
然后继续模仿图片代码,成功生成exe文件2020寒假作业-1_第4张图片
但是我输入数据按了回车后程序就退出了,我还没来得及看清答案QAQ
于是我就在命令行中直接运行程序,终于成功了
2020寒假作业-1_第5张图片

三.编程题(c语言)

问题分解

1.如何辨别汉字?

汉字本质上是字符串,经过查阅资料,了解c语言中的strcmp函数可以比较字符串,于是所有的汉字我都用以下的方法辨别。
strcmp(str,"整数")==0

2.如何让计算机知道需要定义、计算还是输出?

我用了类似模拟题的方法做的:
程序每行第一个字符串只能是“整数”、“看看”或一个变量

  • 如果第一个字符串是关键字或数字对应的汉字则输入不合法
  • 如果字符串是“整数”,就再读取三个字符串并判断合法性
  • 如果字符串是“看看”,就再读取一个字符串并判断合法性
  • 如果不是以上三种情况,就将字符串作为一个变量名

3.将汉字变量与英文变量一一对应

输入的汉字变量名不能作为程序的变量名,那么如何使输入的汉字变量对应程序中的一个变量呢?
这里我用了结构体数据类型,定义了一个结构体,并用如下方式初始化

struct Var{
        int engv;          //英文变量 
        char chi[20];      //汉字变量 
    };
struct Var var[100]={0,""};

4.汉字与数字相互转换

我定义了两个如下的函数来实现该功能

int to_num(char num[]) //汉字转数字
{
    if(strcmp(num,"零")==0)return 0;
    if(strcmp(num,"一")==0)return 1;
}
void print_chi(int engv) //输出汉字
{
    switch(engv){
    case 0:printf("零\n");break;
    case 1:printf("一\n");break;
    }
}

5.合法性判断

用户输入的数据可能不合法,有如下几种情况

  • 将关键字或数字对应的汉字作为变量名
  • 关键字打错
  • 输入的数字不合法(二十一写成二一就不行)
  • 重复定义变量
  • 还未定义变量就做运算或输出
  • 做增加运算时超过99
  • 做减少运算时小于0
    遇到以上几种情况时输出错误提示

6.如何定义变量、运算和输出?

定义

定义变量时将汉字变量名存入结构体数组中,并对其对应的英文变量赋值

运算

读取到“增加”时将对应英文变量加上汉字对应的数值
读取到“减少”时将对应英文变量减去汉字对应的数值

输出

读取到“看看”时输出数值对应的汉字

完整代码(v1.0)

#include
#include
#include
#include
struct Var{
        int engv;          //英文变量 
        char chi[20];      //汉字变量 
    };
struct Var var[100]={0,""};
void error()
{
    printf("你写错啦!!!");
}
int op_legal(char op[])
{
    if(strcmp(op,"等于")==0||strcmp(op,"增加")==0||strcmp(op,"减少")==0||
    strcmp(op,"零")==0||strcmp(op,"一")==0||strcmp(op,"二")==0||
    strcmp(op,"三")==0||strcmp(op,"四")==0||strcmp(op,"五")==0||
    strcmp(op,"六")==0||strcmp(op,"七")==0||strcmp(op,"八")==0||
    strcmp(op,"九")==0||strcmp(op,"十")==0||strcmp(op,"十一")==0||strcmp(op,"十二")==0||
    strcmp(op,"十三")==0||strcmp(op,"十四")==0||strcmp(op,"十五")==0||
    strcmp(op,"十六")==0||strcmp(op,"十七")==0||strcmp(op,"十八")==0||
    strcmp(op,"十九")==0||strcmp(op,"二十")==0||strcmp(op,"二十一")==0||
    strcmp(op,"二十二")==0||strcmp(op,"二十三")==0||strcmp(op,"二十四")==0||
    strcmp(op,"二十五")==0||strcmp(op,"二十六")==0||strcmp(op,"二十七")==0||
    strcmp(op,"二十八")==0||strcmp(op,"二十九")==0||strcmp(op,"三十")==0||strcmp(op,"三十一")==0||
    strcmp(op,"三十二")==0||strcmp(op,"三十三")==0||strcmp(op,"三十四")==0||
    strcmp(op,"三十五")==0||strcmp(op,"三十六")==0||strcmp(op,"三十七")==0||
    strcmp(op,"三十八")==0||strcmp(op,"三十九")==0||strcmp(op,"四十")==0||strcmp(op,"四十一")==0||
    strcmp(op,"四十二")==0||strcmp(op,"四十三")==0||strcmp(op,"四十四")==0||
    strcmp(op,"四十五")==0||strcmp(op,"四十六")==0||strcmp(op,"四十七")==0||
    strcmp(op,"四十八")==0||strcmp(op,"四十九")==0||strcmp(op,"五十")==0||strcmp(op,"五十一")==0||
    strcmp(op,"五十二")==0||strcmp(op,"五十三")==0||strcmp(op,"五十四")==0||
    strcmp(op,"五十五")==0||strcmp(op,"五十六")==0||strcmp(op,"五十七")==0||
    strcmp(op,"五十八")==0||strcmp(op,"五十九")==0||strcmp(op,"六十")==0||strcmp(op,"六十一")==0||
    strcmp(op,"六十二")==0||strcmp(op,"六十三")==0||strcmp(op,"六十四")==0||
    strcmp(op,"六十五")==0||strcmp(op,"六十六")==0||strcmp(op,"六十七")==0||
    strcmp(op,"六十八")==0||strcmp(op,"六十九")==0||strcmp(op,"七十")==0||strcmp(op,"七十一")==0||
    strcmp(op,"七十二")==0||strcmp(op,"七十三")==0||strcmp(op,"七十四")==0||
    strcmp(op,"七十五")==0||strcmp(op,"七十六")==0||strcmp(op,"七十七")==0||
    strcmp(op,"七十八")==0||strcmp(op,"七十九")==0||strcmp(op,"八十")==0||strcmp(op,"八十一")==0||
    strcmp(op,"八十二")==0||strcmp(op,"八十三")==0||strcmp(op,"八十四")==0||
    strcmp(op,"八十五")==0||strcmp(op,"八十六")==0||strcmp(op,"八十七")==0||
    strcmp(op,"八十八")==0||strcmp(op,"八十九")==0||strcmp(op,"九十")==0||strcmp(op,"九十一")==0||
    strcmp(op,"九十二")==0||strcmp(op,"九十三")==0||strcmp(op,"九十四")==0||
    strcmp(op,"九十五")==0||strcmp(op,"九十六")==0||strcmp(op,"九十七")==0||
    strcmp(op,"九十八")==0||strcmp(op,"九十九")==0)
        return 0;
    return 1;
}
int var_legal(char vari[])
{
    int i;
    if(strcmp(vari,"整数")==0||strcmp(vari,"等于")==0||
    strcmp(vari,"增加")==0||strcmp(vari,"减少")==0||strcmp(vari,"看看")==0)
        return 0;
    for(i=0;strlen(var[i].chi)!=0;i++)
        if(strcmp(vari,var[i].chi)==0)  //变量已被定义过 
            return 0;
    return 1;
}
int num_legal(char num[])
{
    if(strcmp(num,"零")==0||strcmp(num,"一")==0||strcmp(num,"二")==0||
    strcmp(num,"三")==0||strcmp(num,"四")==0||strcmp(num,"五")==0||
    strcmp(num,"六")==0||strcmp(num,"七")==0||strcmp(num,"八")==0||
    strcmp(num,"九")==0||strcmp(num,"十")==0||strcmp(num,"十一")==0||strcmp(num,"十二")==0||
    strcmp(num,"十三")==0||strcmp(num,"十四")==0||strcmp(num,"十五")==0||
    strcmp(num,"十六")==0||strcmp(num,"十七")==0||strcmp(num,"十八")==0||
    strcmp(num,"十九")==0||strcmp(num,"二十")==0||strcmp(num,"二十一")==0||
    strcmp(num,"二十二")==0||strcmp(num,"二十三")==0||strcmp(num,"二十四")==0||
    strcmp(num,"二十五")==0||strcmp(num,"二十六")==0||strcmp(num,"二十七")==0||
    strcmp(num,"二十八")==0||strcmp(num,"二十九")==0||strcmp(num,"三十")==0||strcmp(num,"三十一")==0||
    strcmp(num,"三十二")==0||strcmp(num,"三十三")==0||strcmp(num,"三十四")==0||
    strcmp(num,"三十五")==0||strcmp(num,"三十六")==0||strcmp(num,"三十七")==0||
    strcmp(num,"三十八")==0||strcmp(num,"三十九")==0||strcmp(num,"四十")==0||strcmp(num,"四十一")==0||
    strcmp(num,"四十二")==0||strcmp(num,"四十三")==0||strcmp(num,"四十四")==0||
    strcmp(num,"四十五")==0||strcmp(num,"四十六")==0||strcmp(num,"四十七")==0||
    strcmp(num,"四十八")==0||strcmp(num,"四十九")==0||strcmp(num,"五十")==0||strcmp(num,"五十一")==0||
    strcmp(num,"五十二")==0||strcmp(num,"五十三")==0||strcmp(num,"五十四")==0||
    strcmp(num,"五十五")==0||strcmp(num,"五十六")==0||strcmp(num,"五十七")==0||
    strcmp(num,"五十八")==0||strcmp(num,"五十九")==0||strcmp(num,"六十")==0||strcmp(num,"六十一")==0||
    strcmp(num,"六十二")==0||strcmp(num,"六十三")==0||strcmp(num,"六十四")==0||
    strcmp(num,"六十五")==0||strcmp(num,"六十六")==0||strcmp(num,"六十七")==0||
    strcmp(num,"六十八")==0||strcmp(num,"六十九")==0||strcmp(num,"七十")==0||strcmp(num,"七十一")==0||
    strcmp(num,"七十二")==0||strcmp(num,"七十三")==0||strcmp(num,"七十四")==0||
    strcmp(num,"七十五")==0||strcmp(num,"七十六")==0||strcmp(num,"七十七")==0||
    strcmp(num,"七十八")==0||strcmp(num,"七十九")==0||strcmp(num,"八十")==0||strcmp(num,"八十一")==0||
    strcmp(num,"八十二")==0||strcmp(num,"八十三")==0||strcmp(num,"八十四")==0||
    strcmp(num,"八十五")==0||strcmp(num,"八十六")==0||strcmp(num,"八十七")==0||
    strcmp(num,"八十八")==0||strcmp(num,"八十九")==0||strcmp(num,"九十")==0||strcmp(num,"九十一")==0||
    strcmp(num,"九十二")==0||strcmp(num,"九十三")==0||strcmp(num,"九十四")==0||
    strcmp(num,"九十五")==0||strcmp(num,"九十六")==0||strcmp(num,"九十七")==0||
    strcmp(num,"九十八")==0||strcmp(num,"九十九")==0)
        return 1;
    return 0;
}
int equal_legal(char equal[])
{
    if(strcmp(equal,"等于")==0)
        return 1;
    return 0;
}
int to_num(char num[])
{
    if(strcmp(num,"零")==0)return 0;
    if(strcmp(num,"一")==0)return 1;
    if(strcmp(num,"二")==0)return 2;
    if(strcmp(num,"三")==0)return 3;
    if(strcmp(num,"四")==0)return 4;
    if(strcmp(num,"五")==0)return 5;
    if(strcmp(num,"六")==0)return 6;
    if(strcmp(num,"七")==0)return 7;
    if(strcmp(num,"八")==0)return 8;
    if(strcmp(num,"九")==0)return 9;
    if(strcmp(num,"十")==0)return 10;
    if(strcmp(num,"十一")==0)return 11;
    if(strcmp(num,"十二")==0)return 12;
    if(strcmp(num,"十三")==0)return 13;
    if(strcmp(num,"十四")==0)return 14;
    if(strcmp(num,"十五")==0)return 15;
    if(strcmp(num,"十六")==0)return 16;
    if(strcmp(num,"十七")==0)return 17;
    if(strcmp(num,"十八")==0)return 18;
    if(strcmp(num,"十九")==0)return 19;
    if(strcmp(num,"二十")==0)return 20;
    if(strcmp(num,"二十一")==0)return 21;
    if(strcmp(num,"二十二")==0)return 22;
    if(strcmp(num,"二十三")==0)return 23;
    if(strcmp(num,"二十四")==0)return 24;
    if(strcmp(num,"二十五")==0)return 25;
    if(strcmp(num,"二十六")==0)return 26;
    if(strcmp(num,"二十七")==0)return 27;
    if(strcmp(num,"二十八")==0)return 28;
    if(strcmp(num,"二十九")==0)return 29;
    if(strcmp(num,"三十")==0)return 30;
    if(strcmp(num,"三十一")==0)return 31;
    if(strcmp(num,"三十二")==0)return 32;
    if(strcmp(num,"三十三")==0)return 33;
    if(strcmp(num,"三十四")==0)return 34;
    if(strcmp(num,"三十五")==0)return 35;
    if(strcmp(num,"三十六")==0)return 36;
    if(strcmp(num,"三十七")==0)return 37;
    if(strcmp(num,"三十八")==0)return 38;
    if(strcmp(num,"三十九")==0)return 39;
    if(strcmp(num,"四十")==0)return 40;
    if(strcmp(num,"四十一")==0)return 41;
    if(strcmp(num,"四十二")==0)return 42;
    if(strcmp(num,"四十三")==0)return 43;
    if(strcmp(num,"四十四")==0)return 44;
    if(strcmp(num,"四十五")==0)return 45;
    if(strcmp(num,"四十六")==0)return 46;
    if(strcmp(num,"四十七")==0)return 47;
    if(strcmp(num,"四十八")==0)return 48;
    if(strcmp(num,"四十九")==0)return 49;
    if(strcmp(num,"五十")==0)return 50;
    if(strcmp(num,"五十一")==0)return 51;
    if(strcmp(num,"五十二")==0)return 52;
    if(strcmp(num,"五十三")==0)return 53;
    if(strcmp(num,"五十四")==0)return 54;
    if(strcmp(num,"五十五")==0)return 55;
    if(strcmp(num,"五十六")==0)return 56;
    if(strcmp(num,"五十七")==0)return 57;
    if(strcmp(num,"五十八")==0)return 58;
    if(strcmp(num,"五十九")==0)return 59;
    if(strcmp(num,"六十")==0)return 60;
    if(strcmp(num,"六十一")==0)return 61;
    if(strcmp(num,"六十二")==0)return 62;
    if(strcmp(num,"六十三")==0)return 63;
    if(strcmp(num,"六十四")==0)return 64;
    if(strcmp(num,"六十五")==0)return 65;
    if(strcmp(num,"六十六")==0)return 66;
    if(strcmp(num,"六十七")==0)return 67;
    if(strcmp(num,"六十八")==0)return 68;
    if(strcmp(num,"六十九")==0)return 69;
    if(strcmp(num,"七十")==0)return 70;
    if(strcmp(num,"七十一")==0)return 71;
    if(strcmp(num,"七十二")==0)return 72;
    if(strcmp(num,"七十三")==0)return 73;
    if(strcmp(num,"七十四")==0)return 74;
    if(strcmp(num,"七十五")==0)return 75;
    if(strcmp(num,"七十六")==0)return 76;
    if(strcmp(num,"七十七")==0)return 77;
    if(strcmp(num,"七十八")==0)return 78;
    if(strcmp(num,"七十九")==0)return 79;
    if(strcmp(num,"八十")==0)return 80;
    if(strcmp(num,"八十一")==0)return 81;
    if(strcmp(num,"八十二")==0)return 82;
    if(strcmp(num,"八十三")==0)return 83;
    if(strcmp(num,"八十四")==0)return 84;
    if(strcmp(num,"八十五")==0)return 85;
    if(strcmp(num,"八十六")==0)return 86;
    if(strcmp(num,"八十七")==0)return 87;
    if(strcmp(num,"八十八")==0)return 88;
    if(strcmp(num,"八十九")==0)return 89;
    if(strcmp(num,"九十")==0)return 90;
    if(strcmp(num,"九十一")==0)return 91;
    if(strcmp(num,"九十二")==0)return 92;
    if(strcmp(num,"九十三")==0)return 93;
    if(strcmp(num,"九十四")==0)return 94;
    if(strcmp(num,"九十五")==0)return 95;
    if(strcmp(num,"九十六")==0)return 96;
    if(strcmp(num,"九十七")==0)return 97;
    if(strcmp(num,"九十八")==0)return 98;
    if(strcmp(num,"九十九")==0)return 99;
}
int is_defined(char vari[])
{
    int i;
    for(i=0;strlen(var[i].chi)!=0;i++)
        if(strcmp(vari,var[i].chi)==0)
            return 1;
    return 0;
}
void print_chi(int engv)
{
    switch(engv){
        case 0:printf("零\n");break;
        case 1:printf("一\n");break;
        case 2:printf("二\n");break;
        case 3:printf("三\n");break;
        case 4:printf("四\n");break;
        case 5:printf("五\n");break;
        case 6:printf("六\n");break;
        case 7:printf("七\n");break;
        case 8:printf("八\n");break;
        case 9:printf("九\n");break;
        case 10:printf("十\n");break;
        case 11:printf("十一\n");break;
        case 12:printf("十二\n");break;
        case 13:printf("十三\n");break;
        case 14:printf("十四\n");break;
        case 15:printf("十五\n");break;
        case 16:printf("十六\n");break;
        case 17:printf("十七\n");break;
        case 18:printf("十八\n");break;
        case 19:printf("十九\n");break;
        case 20:printf("二十\n");break;
        case 21:printf("二十一\n");break;
        case 22:printf("二十二\n");break;
        case 23:printf("二十三\n");break;
        case 24:printf("二十四\n");break;
        case 25:printf("二十五\n");break;
        case 26:printf("二十六\n");break;
        case 27:printf("二十七\n");break;
        case 28:printf("二十八\n");break;
        case 29:printf("二十九\n");break;
        case 30:printf("三十\n");break;
        case 31:printf("三十一\n");break;
        case 32:printf("三十二\n");break;
        case 33:printf("三十三\n");break;
        case 34:printf("三十四\n");break;
        case 35:printf("三十五\n");break;
        case 36:printf("三十六\n");break;
        case 37:printf("三十七\n");break;
        case 38:printf("三十八\n");break;
        case 39:printf("三十九\n");break;
        case 40:printf("四十\n");break;
        case 41:printf("四十一\n");break;
        case 42:printf("四十二\n");break;
        case 43:printf("四十三\n");break;
        case 44:printf("四十四\n");break;
        case 45:printf("四十五\n");break;
        case 46:printf("四十六\n");break;
        case 47:printf("四十七\n");break;
        case 48:printf("四十八\n");break;
        case 49:printf("四十九\n");break;
        case 50:printf("五十\n");break;
        case 51:printf("五十一\n");break;
        case 52:printf("五十二\n");break;
        case 53:printf("五十三\n");break;
        case 54:printf("五十四\n");break;
        case 55:printf("五十五\n");break;
        case 56:printf("五十六\n");break;
        case 57:printf("五十七\n");break;
        case 58:printf("五十八\n");break;
        case 59:printf("五十九\n");break;
        case 60:printf("六十\n");break;
        case 61:printf("六十一\n");break;
        case 62:printf("六十二\n");break;
        case 63:printf("六十三\n");break;
        case 64:printf("六十四\n");break;
        case 65:printf("六十五\n");break;
        case 66:printf("六十六\n");break;
        case 67:printf("六十七\n");break;
        case 68:printf("六十八\n");break;
        case 69:printf("六十九\n");break;
        case 70:printf("七十\n");break;
        case 71:printf("七十一\n");break;
        case 72:printf("七十二\n");break;
        case 73:printf("七十三\n");break;
        case 74:printf("七十四\n");break;
        case 75:printf("七十五\n");break;
        case 76:printf("七十六\n");break;
        case 77:printf("七十七\n");break;
        case 78:printf("七十八\n");break;
        case 79:printf("七十九\n");break;
        case 80:printf("八十\n");break;
        case 81:printf("八十一\n");break;
        case 82:printf("八十二\n");break;
        case 83:printf("八十三\n");break;
        case 84:printf("八十四\n");break;
        case 85:printf("八十五\n");break;
        case 86:printf("八十六\n");break;
        case 87:printf("八十七\n");break;
        case 88:printf("八十八\n");break;
        case 89:printf("八十九\n");break;
        case 90:printf("九十\n");break;
        case 91:printf("九十一\n");break;
        case 92:printf("九十二\n");break;
        case 93:printf("九十三\n");break;
        case 94:printf("九十四\n");break;
        case 95:printf("九十五\n");break;
        case 96:printf("九十六\n");break;
        case 97:printf("九十七\n");break;
        case 98:printf("九十八\n");break;
        case 99:printf("九十九\n");break;
    }
}
void look(char vari[])
{
    int i;
    for(i=0;strlen(var[i].chi)!=0;i++)
        if(strcmp(vari,var[i].chi)==0){     
            print_chi(var[i].engv);
            break;
        }
}
int compute_legal(char compute[])
{
    if(strcmp(compute,"增加")==0||strcmp(compute,"减少")==0)
        return 1;
    return 0;
}
void computef(char vari[],char com[],char num[])
{
    int i;
    if(strcmp(com,"增加")==0){
        for(i=0;strlen(var[i].chi)!=0;i++)
            if(strcmp(vari,var[i].chi)==0){
                if((var[i].engv+to_num(num))>=100)
                    printf("增加的数值过大\n");
                else 
                    var[i].engv+=to_num(num);
                break;
            }
    }
    else{
        for(i=0;strlen(var[i].chi)!=0;i++)
            if(strcmp(vari,var[i].chi)==0){
                if((var[i].engv-to_num(num))<0)
                    printf("减少的数值过大\n");
                else 
                    var[i].engv-=to_num(num);
                break;
            }
    }
}
int main()
{
    int i=0,j;
    char op[10],vari[10],num[10],compute[10],equal[10];
    while(1){
        scanf("%s",op);
        if(op_legal(op)==0)
            error(); 
        else{
            if(strcmp(op,"整数")==0){
                scanf("%s%s%s",vari,equal,num);
                if(var_legal(vari)==0||num_legal(num)==0||equal_legal(equal)==0)
                    error();
                else{
                    var[i].engv=to_num(num);
                    strcpy(var[i].chi,vari);
                    i++;
                }
            }
            else if(strcmp(op,"看看")==0){
                scanf("%s",vari);
                if(is_defined(vari)==0)
                    error();
                else
                    look(vari);
            }
            else{       //计算 
                if(is_defined(op)==0)
                    error(); 
                else{
                    scanf("%s%s",compute,num);
                    if(compute_legal(compute)==0||num_legal(num)==0)
                        error();
                    else{
                        computef(op,compute,num);
                    }
                }
            }
        }
    }
    return 0;
}

原谅我的代码过于冗长QAQ
修改后的代码见作业二

你可能感兴趣的:(2020寒假作业-1)