第一次作业

面向对象程序设计2020第一次作业

这个作业属于哪个课程 2020面向对象程序设计张栋班
这个作业要求在哪里 面向对象程序设计寒假作业1
这个作业的目标 对c语言与c++的思考,安装开发环境,了解编译的过程,学习文字的编程,通过思考与学习提高基础能力并记录博客
作业正文 第一次作业
其它参考文献 c语言编译过程 命令行编译c

一.问答题

1.(1)复杂
c语言具有较多的运算符,不容易记住,而且对于那么多的优先级难以搞清楚,容易混乱而出错。
c语言在实现某些功能时需要写的很多很复杂。
(2)不安全
c语言的数组越界,程序不会报错,容易破环程序。
c语言对于浮点变量赋给整型只会提示warning,即有歧义的程序也可以通过,容易攻击。

2.编译过程:编译预处理>编译阶段>优化阶段>汇编过程>链接程序
(1)对伪指令以及特殊符号进行处理。伪指令即以#开头的指令,包括宏定义指令,条件编译指令,头文件包含指令等,而特殊符号可以被预编译程序识别。于是最终生成一个没有宏定义、没有条件编译指令、没有特殊符号的输出文件,完成整个编译预处理过程。
(2)这是一个将高级语言转化为机械语言的过程。通过语法以及词法分析翻译成等价代码或者中间代码。
(3)优化阶段分为两部分。一部分是对中间代码进行优化,另一部分是对目标代码的生成而进行的。经过优化得到的汇编代码必须经过汇编程序的汇编转换成相应的机器指令,方可能被机器执行。
(4)汇编将程序翻译成了机械指令,从而得到了最终的文件。
(5)链接过程是将相关的文件相互连接成为一个整体。最终程序就被转换成了可执行文件。

二.实践题

1.查看编译器的版本。我安装的是visual studio。在visual studio tools里输入cl.exe即可查看编译器的版本。

2.自己打一段代码,在文件里用cmd命令打开,输入要编译的文件即gcc+文件名,出现新的文件a,重新输入a就可以运行了。
不过在使用gcc前需要配置环境变量,下载一个mingW,在电脑的高级设置中path里配置bin即可。

三.编程题

编写一个程序,输入满足以下语法要求的一段文字,输出运行后的结果。
变量定义:整数 钱包 等于 零
运算(加法):钱包 增加 四
运算(减法):钱包 减少 四
输出:看看 钱包
1.首先先分析题目,由题目以及样例可以分析清楚
整数 钱包是定义变量
增加 减少是做加减法
看看是输出结果
等于是赋值
题目要求是数字只会出现以下 零一二三四五六七八九十,那么就会出现两位数的数字。
2.对于白话文数字,我的想法是先将汉字转化为对应的数字,然后进行加减法,最后将结果转换回来进行输出。
(1)
这个是我首先想到的将对应的汉字转化为数字,因为汉字占两个字节所以用字符数组进行储存,通过strcmp函数进行转换。

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

(2)
第二个函数是将进行加减法后得到的结果转化为对应的汉字进行输出。

void switch2(int shu1)
{
	switch (shu1)
	{
	     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;
	}
}

(3)
接下来是主函数部分。
同样的我选择用strcmp函数来进行比较。
考虑到有几个问题:格式定义整数变量,如果是其他的不符合要求就输出Error。
变量不一定是钱包,可能是别的,但是我实在不会搞,于是我选择放弃。
对于等于,增加,减少这种运算可能有别的输入时不符合要求,那这样也要输出Error。
数字题目说是在0-10以内,那我就不管其他数字的可能了。
然后“看看”作为结果的输出,需要重新来判断。
最后我遗漏了对于结果的输出是一位数的自然直接输出,但是对于二位数,分为十的倍数和其他二位数比如“二十一”这样,我们需要另行输出,我的想法是还需要单独输出一个“十”,减少麻烦。

int main()
{
	char a[50], b[50], c[50], d[50];
	int n,s,i;
	char e[50], f[50], g[50],h[50];
	scanf("%s %s %s %s", a, b, c, d);
	if (strcmp("整数", a) == 0)
	{
		if (strcmp("等于", c) == 0)
		{
			n = switch1(d);
	while(1)
	{
		scanf("%s",e);
		if(strcmp("看看",e)==0)
		break;
		scanf("%s %s",f,g);
		if(strcmp("增加", f) != 0&&strcmp("减少",f) != 0)
		{
			printf("Error!");
			break;
		}
		if (strcmp("增加", f) == 0)
		{
			n += switch1(g);
		}
		if (strcmp("减少",f) == 0)
		{
			n -= switch1(g);
		}
	}
	scanf("%s",h);
	if (strcmp("看看", e) == 0)
	{
		if (n <= 10)
		{
			switch2(n);
		}
		if (n > 10)
		{
			if (n % 10 == 0)
			{
				s = n / 10;
				switch2(s);
				printf("十");
			}
			else
			{
				s = n / 10;
				i = n % 10;
				switch2(s);
				printf("十");
				switch2(i);
			}
		}
	}
	} 
	else
	printf("Error!");
	} 
	else
	printf("Error!");
	return 0;
}

(4)
最后是完整的代码。还有一些输出以及想法的不足我还没想到方法改进。

#include<stdio.h>
#include<string.h>
int switch1(char shu[]);
void switch2(int shu1);
int main()
{
	char a[50], b[50], c[50], d[50];
	int n,s,i;
	char e[50], f[50], g[50],h[50];
	scanf("%s %s %s %s", a, b, c, d);
	if (strcmp("整数", a) == 0)
	{
		if (strcmp("等于", c) == 0)
		{
			n = switch1(d);
	while(1)
	{
		scanf("%s",e);
		if(strcmp("看看",e)==0)
		break;
		scanf("%s %s",f,g);
		if(strcmp("增加", f) != 0&&strcmp("减少",f) != 0)
		{
			printf("Error!");
			break;
		}
		if (strcmp("增加", f) == 0)
		{
			n += switch1(g);
		}
		if (strcmp("减少",f) == 0)
		{
			n -= switch1(g);
		}
	}
	scanf("%s",h);
	if (strcmp("看看", e) == 0)
	{
		if (n <= 10)
		{
			switch2(n);
		}
		if (n > 10)
		{
			if (n % 10 == 0)
			{
				s = n / 10;
				switch2(s);
				printf("十");
			}
			else
			{
				s = n / 10;
				i = n % 10;
				switch2(s);
				printf("十");
				switch2(i);
			}
		}
	}
	} 
	else
	printf("Error!");
	} 
	else
	printf("Error!");
	return 0;
}
int switch1(char shu[])
{
	if (strcmp("一", shu) == 0)
		return 1;
	else if (strcmp("二", shu) == 0)
		return 2;
	else if (strcmp("三", shu) == 0)
		return 3;
	else if (strcmp("四", shu) == 0)
		return 4;
	else if (strcmp("五", shu) == 0)
		return 5;
	else if (strcmp("六", shu) == 0)
		return 6;
	else if (strcmp("七", shu) == 0)
		return 7;
	else if (strcmp("八", shu) == 0)
		return 8;
	else if (strcmp("九", shu) == 0)
		return 9;
	else if (strcmp("十", shu) == 0)
		return 10;
	else if (strcmp("零", shu) == 0)
		return 0;
}
void switch2(int shu1)
{
	switch (shu1)
	{
	     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;
	}
}

输出样例






做出新的修改

我重新将我的转换switch函数用循环进行简化
先设一个全局变量,储存汉字

char hanzi[11][10]={"零","一","二","三","四","五","六","七","八","九","十"};

然后两个函数如下,进行简化就不用写那么麻烦了。

int switch1(char shu[])
{
    int i;
    for(i=0;i<=10;i++)
    {
    	if(strcmp(hanzi[i],shu)==0)
    	return i;
	}
}
void switch2(int shu1)
{
    int i;
    for(i=0;i<=10;i++)
    {
    	if(shu1==i)
    	printf("%s",hanzi[i]);
	}
}

完整代码如下

#include<stdio.h>
#include<string.h>
int switch1(char shu[]);
void switch2(int shu1);
char hanzi[11][10]={"零","一","二","三","四","五","六","七","八","九","十"};
int main()
{
    char a[50], b[50], c[50], d[50];
    int n,s,i;
    char e[50], f[50], g[50],h[50];
    scanf("%s %s %s %s", a, b, c, d);
    if (strcmp("整数", a) == 0)
    {
        if (strcmp("等于", c) == 0)
        {
            n = switch1(d);
    while(1)
    {
        scanf("%s",e);
        if(strcmp("看看",e)==0)
        break;
        scanf("%s %s",f,g);
        if(strcmp("增加", f) != 0&&strcmp("减少",f) != 0)
        {
            printf("Error!");
            break;
        }
        if (strcmp("增加", f) == 0)
        {
            n += switch1(g);
        }
        if (strcmp("减少",f) == 0)
        {
            n -= switch1(g);
        }
    }
    scanf("%s",h);
    if (strcmp("看看", e) == 0)
    {
        if (n <= 10)
        {
            switch2(n);
        }
        if (n > 10)
        {
            if (n % 10 == 0)
            {
                s = n / 10;
                switch2(s);
                printf("十");
            }
            else
            {
                s = n / 10;
                i = n % 10;
                switch2(s);
                printf("十");
                switch2(i);
            }
        }
    }
    } 
    else
    printf("Error!");
    } 
    else
    printf("Error!");
    return 0;
}
int switch1(char shu[])
{
    int i;
    for(i=0;i<=10;i++)
    {
    	if(strcmp(hanzi[i],shu)==0)
    	return i;
	}
}
void switch2(int shu1)
{
    int i;
    for(i=0;i<=10;i++)
    {
    	if(shu1==i)
    	printf("%s",hanzi[i]);
	}
}
原文地址:https://www.cnblogs.com/Misanthropel/p/12230724.html