寒假作业2

作业所属课程 2020年面向对象程序设计
作业要求 面向对象程序设计寒假作业1
作业目标 编程题:优化作业1代码

1、对作业一中的代码进行一节改进优化,把代码分解成几个模块
a.输入函数(当新一行的第一个值为‘看看’时,终止输入)


int scan(string str1[],string str2[],string str3[])
{
int i=0;
while(1)
{
cin>>str1[i];
if(str1[i]=="看看")
{
cin>>str2[i];
break;
}
else
cin>>str2[i]>>str3[i];
i++;
}
return(i);
}

b.设置汉字零到十与阿拉伯数字的一一对应关系

int character_to_num(string str)
{
	int a;
	if(str=="零")a=0;
	else if(str=="一")a=1;
	else if(str=="二")a=2;
	else if(str=="三")a=3;
	else if(str=="四")a=4;
	else if(str=="五")a=5;
	else if(str=="六")a=6;
	else if(str=="七")a=7;
	else if(str=="八")a=8;
	else if(str=="九")a=9;
	else if(str=="十")a=10;
	return a;	
} 

c.定义汉字所对应的运算法则

int operation(int sum,int num,string str2)
{
	if(str2=="增加")
	sum=sum+num;
	else if(str2=="减少")
	sum=sum-num;
	return sum; 
}

d.判断是否出现未定义的变量

int check_variable(string str12,string str1[],int i)
{
	int flag=1;
	for(int j=0;j

e.把输入的汉字数值进行转化并进行运算(这里运用到之前定义的character_to_num()和operation()函数)

int turn_to_num(string str2[],string str3[],string str14,int i)
{
	int character_to_num(string str);
	int operation(int sum,int num,string str2); 
	int sum,num;
	sum=character_to_num(str14);
	for(int j=0;j<=i;j++)
	{
		num=character_to_num(str3[j]);    //把汉字转成数字存进num中 
		sum=operation(sum,num,str2[j]);   //进行加减运算,把最终结果存进sum中 
	}
	return sum;
}

接下来两个函数配合能够把阿拉伯数字转化成汉字输出,最大单位为十万
f.把阿拉伯数字0-9转化成汉字输出

void num_to_str(int num1)
{
	switch(num1)
	{
		case 0:cout<<"零";break;
		case 1:cout<<"一";break;
		case 2:cout<<"二";break;
		case 3:cout<<"三";break;
		case 4:cout<<"四";break;
		case 5:cout<<"五";break;
		case 6:cout<<"六";break;
		case 7:cout<<"七";break;
		case 8:cout<<"八";break;
		case 9:cout<<"九";break;
	}
}

g.进行汉字单位的输出(以位数为基准)

void transfer(int j)
{
	switch(j)
	{
		case 0:break;
		case 1:cout<<"十";break;
		case 2:cout<<"百";break;
		case 3:cout<<"千";break;
		case 4:cout<<"万";break;
		case 5:cout<<"十万";break;	
	}
 } 

h.最后是主函数,把之前编写的各个不同功能的函数串联起来

int main()
{
	int scan(string str1[],string str2[],string str3[]);
	int turn_to_num(string str2[],string str3[],string str14,int i);
	int check_variable(string str12,string str1[],int i);
	void transfer(int j);
	void num_to_str(int num1);
	string str1[1000],str2[1000],str3[1000],str11,str12,str13,str14;int i,j,sum,num1[100],n;
	cin>>str11>>str12>>str13>>str14;
	i=scan(str1,str2,str3);    //实现数据输入,并把总数据行数存进i中 
	if(check_variable(str12,str1,i))
	{
	        sum=turn_to_num(str2,str3,str14,i);
		for(i=0,j=0;sum>0;i++)
		{
			num1[i]=sum%10;
			sum=sum/10;
			
		}
		for(j=0;j=n;j--)
		{
			//把阿拉伯数字零到九转成汉字的函数
			num_to_str(num1[j]);
			if(num1[j]!=0)
			//汉字十、百、千、万的输出 
			transfer(j);
		}
	}
}

以下是两个测试数据截图


2.制作编译脚本,运行脚本可以编译代码
这是我第一次接触脚本这块知识,连名词概念都是临时上网查的,所以,这次作业做得很糟糕,有一些东西实在理解不了,也没办法完成,就先把已完成的提交了,剩下的再继续学。
我从网上查到使用Windows批处理需要建立.bat文件,我按照找到的步骤,先新建一个.txt文件,然后进行重命名,把后缀名改成.bat,因为新建的文件里面是空的,我双击后电脑显示如下错误:

我以为还需要安装什么东西,就又上网查了半天,后来通过右键编辑添加一点测试代码保存后再双击发现它不会报错了,但是弹出的小黑框闪退,所以我又百度了,在代码末行添加了pause之后解决问题,然后如果保存时右下角的编码选的是utf-8的话会出现中文乱码,所以应该勾选ansi,这次作业对我来说是一步一坎啊。

参考资料http://xstarcd.github.io/wiki/windows/windows_cmd_summary_commands.html

a.先贴出脚本代码

@echo off
echo 输入要编译的cpp文件名
set /p file=%file%
g++ %file%.cpp -o %file%.exe
if exist %file%.exe echo lucky
if not exist %file%.exe echo ohno
pause

因为我要编译的文件和脚本是放在一个目录里面的,所以不需要更改目录,这是运行编译脚本以前的文件夹

运行脚本

运行后文件夹里就有1.exe文件了,编译成功。

3.进行单元测试,测试每一个函数
先贴出测试脚本

@echo off
echo 输入测试函数名
set /p index=请输入:
g++ %index%.cpp -o %index%.exe
if exist %index%.exe echo continue 输入测试数据
if not exist %index%.exe echo find nothing
%index%
pause

因为测试函数的过程都大同小异,所以我就只贴出测试其中一个简单函数的过程
一下是我要测试的函数character_to_num()的代码

  #include
using namespace std;
int main()
{
	int a;string str;
	cin>>str;
	if(str=="零")a=0;
	else if(str=="一")a=1;
	else if(str=="二")a=2;
	else if(str=="三")a=3;
	else if(str=="四")a=4;
	else if(str=="五")a=5;
	else if(str=="六")a=6;
	else if(str=="七")a=7;
	else if(str=="八")a=8;
	else if(str=="九")a=9;
	else if(str=="十")a=10;
	cout<

运行脚本,按照提示输入就可以了

以上是我现在能够完成的部分。

posted @ 2020-02-05 18:09  sl0805  阅读(140)  评论(1编辑  收藏  举报