zoukankan      html  css  js  c++  java
  • 计算机与软件工程 第二次作业

    作业要求

    作业一

    预习

    用软件写代码要保证正确性,有效性。
    一个错误会导致漏洞。低效能运行速度会减慢。
    所以要考虑WordFreq算法的时间复杂度,能否优化,加以验证。

    代码

    #include<iostream.h>
    #include<stdlib.h>
    #include<time.h>
    
    void function1()
    {
    	int a,b,d;
    	char A[2]={'+','-'};
    	float D[30];
    	int n=0;
    	for(int i=0;n<30;i++)
    	{
    		a=rand()%10;
    		b=rand()%10;
    		d=rand()%2+1;
    		cout<<a<<A[d]<<b<<"="<<endl;
    		if(d=1)
    		{
    			D[n]=a+b;
    			n++;
    			}
    		else
    		{
    			D[n]=a-b;
    			n++;
    		}
    	}
    	cout<<"********************分割线*********************"<<endl;
    	cout<<"******************以下是答案*******************"<<endl;
    	for(int i=0;i<=30;i++)
    	{
    		cout<<D[i]<<endl;
    	}
    }
    
    void function2()
    {
    	int a,b,d;
    	char A[2]={'+','-'};
    	float D[30];
    	int n=0;
    	for(int i=0;n<30;i++)
    	{
    		a=rand()%10;
    		b=rand()%10;
    		d=rand()%2+1;
    		cout<<a<<A[d]<<b<<"="<<endl;
    		if(d=1)
    		{
    			D[n]=a+b;
    			n++;
    			}
    		else
    		{
    			D[n]=a-b;
    			n++;
    		}
    	}
    	cout<<"********************分割线*********************"<<endl;
    	cout<<"******************以下是答案*******************"<<endl;
    	for(int i=0;i<=30;i++)
    	{
    		cout<<D[i]<<endl;
    	}
    }
    
    void function3()
    {
    	int a,b,c,d,r;
    	char A[2]={'+','-','*','/'};
    	char B[2]={'+','-','*','/'};
    	float D[30];
    	int n=0;
    	for(int i=0;n<30;i++)
    	{
    		a=rand()%20;
    		b=rand()%20;
    		c=rand()%20;
    		d=rand()%4+1;
    		r=rand()%4+1;
    		cout<<a<<A[d]<<b<<B[r]<<c<<"="<<endl;
    		if(d=1)
    		{
    			if(r=1)
    			{
    				D[n]=a+b+c;
    				n++;
    				}
    			else if(r=2)
    			{
    				D[n]=a+b-c;
    				n++;
    			}
    			else if(r=3)
    			{
    				D[n]=a+b*c;
    				n++;
    				}
    			else
    			{
    				D[n]=a+b/c;
    				n++;
    			}
    		}
    		else if(d=2)
    		{
    			if(r=1)
    			{
    				D[n]=a-b+c;
    				n++;
    				}
    			else if(r=2)
    			{
    				D[n]=a-b-c;
    				n++;
    			}
    			else if(r=3)
    			{
    				D[n]=a-b*c;
    				n++;
    			}
    			else
    			{
    				D[n]=a-b/c;
    				n++;
    			}
    		}
    		else if(d=3)
    		{
    			if(r=1)
    			{
    				D[n]=a*b+c;
    				n++;
    			}
    			else if(r=2)
    			{
    				D[n]=a*b-c;
    				n++;
    			}
    			else if(r=3)
    			{
    				D[n]=a*b*c;
    				n++;
    			}
    			else
    			{
    				D[n]=a*b/c;
    				n++;
    			}
    		}
    		else(d=4)
    		{
    			if(r=1)
    			{
    				D[n]=a/b+c;
    				n++;
    			}
    			else if(r=2)
    			{
    				D[n]=a/b-c;
    				n++;
    			}
    			else if(r=3)
    			{
    				D[n]=a/b*c;
    				n++;
    			}
    			else
    			{
    				D[n]=a/b/c;
    				n++;
    			}
    		}
    	}
    	cout<<"********************分割线*********************"<<endl;
    	cout<<"******************以下是答案*******************"<<endl;
    	for(int i=0;i<=30;i++)
    	{
    		cout<<D[i]<<endl;
    	}
    }
    
    void function4()
    {
    	int a,b,c,d,r;
    	char A[2]={'+','-','*','/'};
    	char B[2]={'+','-','*','/'};
    	float D[30];
    	int n=0;
    	for(int i=0;n<30;i++)
    	{
    		a=rand()%100;
    		b=rand()%100;
    		c=rand()%100;
    		d=rand()%4+1;
    		r=rand()%4+1;
    		cout<<a<<A[d]<<b<<B[r]<<c<<"="<<endl;
    		if(d=1)
    		{
    			if(r=1)
    			{
    				D[n]=a+b+c;
    				n++;
    				}
    			else if(r=2)
    			{
    				D[n]=a+b-c;
    				n++;
    			}
    			else if(r=3)
    			{
    				D[n]=a+b*c;
    				n++;
    				}
    			else
    			{
    				D[n]=a+b/c;
    				n++;
    			}
    		}
    		else if(d=2)
    		{
    			if(r=1)
    			{
    				D[n]=a-b+c;
    				n++;
    				}
    			else if(r=2)
    			{
    				D[n]=a-b-c;
    				n++;
    			}
    			else if(r=3)
    			{
    				D[n]=a-b*c;
    				n++;
    			}
    			else
    			{
    				D[n]=a-b/c;
    				n++;
    			}
    		}
    		else if(d=3)
    		{
    			if(r=1)
    			{
    				D[n]=a*b+c;
    				n++;
    			}
    			else if(r=2)
    			{
    				D[n]=a*b-c;
    				n++;
    			}
    			else if(r=3)
    			{
    				D[n]=a*b*c;
    				n++;
    			}
    			else
    			{
    				D[n]=a*b/c;
    				n++;
    			}
    		}
    		else(d=4)
    		{
    			if(r=1)
    			{
    				D[n]=a/b+c;
    				n++;
    			}
    			else if(r=2)
    			{
    				D[n]=a/b-c;
    				n++;
    			}
    			else if(r=3)
    			{
    				D[n]=a/b*c;
    				n++;
    			}
    			else
    			{
    				D[n]=a/b/c;
    				n++;
    			}
    		}
    	}
    	cout<<"********************分割线*********************"<<endl;
    	cout<<"******************以下是答案*******************"<<endl;
    	for(int i=0;i<=30;i++)
    	{
    		cout<<D[i]<<endl;
    	}
    }
    
    void function5()
    {
    	int a,b,c,d,r,s;
    	char A[2]={'+','-','*','/'};
    	char B[2]={'+','-','*','/'};
    	float D[30];
    	int n=0;
    	for(int i=0;n<30;i++)
    	{
    		a=rand()%100;
    		b=rand()%100;
    		c=rand()%100;
    		d=rand()%4+1;
    		r=rand()%4+1;
    		s=rand()%2+1;
    		if(s==1){
    			cout<<"("<<a<<A[d]<<b<<")"<<B[r]<<c<<"="<<endl;
    			if(d=1)
    			{
    				if(r=1)
    				{
    					D[n]=(a+b)+c;
    					n++;
    				}
    				else if(r=2)
    				{
    					D[n]=(a+b)-c;
    					n++;
    				}
    				else if(r=3)
    				{
    					D[n]=(a+b)*c;
    					n++;
    				}
    				else
    				{
    					D[n]=(a+b)/c;
    					n++;
    				}
    			}
    			else if(d=2)
    			{
    				if(r=1)
    				{
    					D[n]=(a-b)+c;
    					n++;
    				}
    				else if(r=2)
    				{
    					D[n]=(a-b)-c;
    					n++;
    				}
    				else if(r=3)
    				{
    					D[n]=(a-b)*c;
    					n++;
    				}
    				else
    				{
    					D[n]=(a-b)/c;
    					n++;
    				}
    			}
    			else if(d=3)
    			{
    				if(r=1)
    				{
    					D[n]=(a*b)+c;
    					n++;
    				}
    				else if(r=2)
    				{
    					D[n]=(a*b)-c;
    					n++;
    				}
    				else if(r=3)
    				{
    					D[n]=(a*b)*c;
    					n++;
    				}
    				else
    				{
    					D[n]=(a*b)/c;
    					n++;
    				}
    			}
    			else(d=4)
    			{
    				if(r=1)
    				{
    					D[n]=(a/b)+c;
    					n++;
    				}
    				else if(r=2)
    				{
    					D[n]=(a/b)-c;
    					n++;
    				}
    				else if(r=3)
    				{
    					D[n]=(a/b)*c;
    					n++;
    				}
    				else
    				{
    					D[n]=(a/b)/c;
    					n++;
    				}
    			}
    		}
    		if(s==1){
    			cout<<a<<A[d]<<"("<<b<<B[r]<<c<<")="<<endl;
    			if(d=1)
    			{
    				if(r=1)
    				{
    					D[n]=a+(b+c);
    					n++;
    				}
    				else if(r=2)
    				{
    
    					D[n]=a+(b-c);
    					n++;
    				}
    				else if(r=3)
    				{
    					D[n]=a+(b*c);
    					n++;
    				}
    				else
    				{
    					D[n]=a+(b/c);
    					n++;
    				}
    			}
    			else if(d=2)
    			{
    				if(r=1)
    				{
    					D[n]=a-(b+c);
    					n++;
    				}
    				else if(r=2)
    				{
    					D[n]=a-(b-c);
    					n++;
    				}
    				else if(r=3)
    				{
    					D[n]=a-(b*c);
    					n++;
    				}
    				else
    				{
    					D[n]=a-(b/c);
    					n++;
    				}
    			}
    			else if(d=3)
    			{
    				if(r=1)
    				{
    					D[n]=a*(b+c);
    					n++;
    				}
    				else if(r=2)
    				{
    					D[n]=a*(b-c);
    					n++;
    				}
    				else if(r=3)
    				{
    					D[n]=a*(b*c);
    					n++;
    				}
    				else
    				{
    					D[n]=a*(b/c);
    					n++;
    				}
    			}
    			else(d=4)
    			{
    				if(r=1)
    				{
    					D[n]=a/(b+c);
    					n++;
    				}
    				else if(r=2)
    				{
    					D[n]=a/(b-c);
    					n++;
    				}
    				else if(r=3)
    				{
    					D[n]=a/(b*c);
    					n++;
    				}
    				else
    				{
    					D[n]=a/(b/c);
    					n++;
    				}
    			}
    		}
    	cout<<"********************分割线*********************"<<endl;
    	cout<<"******************以下是答案*******************"<<endl;
    	for(int i=0;i<=30;i++)
    	{
    		cout<<D[i]<<endl;
    	}
    }
    int main()  //随机产生四则运算
    {
    	int m;
    	cout<<"请选择年级"<<endl;
    	cout<<"输入1,一年级计算题(20以内两个数字的加减法)"<<endl;
    	cout<<"输入2,二年级计算题(10以内两个数字的四则运算"<<endl;
    	cout<<"输入3,三年级计算题(20以内的三个数字的四则运算)"<<endl;
    	cout<<"输入4,四年级计算题(100以内的三个数字的四则运算)"<<endl;
    	cout<<"输入5,五年级计算题(100以内的三个数字加括号的四则运算)"<<endl;
    	cout<<"请输入:"<<endl;
    	cin>>m;
    	if(m==1)
    		function1();
    	else if(m==2)
    		function2();
    	else if(m==3)
    		function3();
    	else if(m==4)
    		function4();
    	else
    		function5();
    	return 0;
    }
    

    问题

    电脑上的Microsoft Visual Studio出现错误,所以没运行。代码是自己写的,没写出分数形式的计算题。

    作业二

    我的gitee仓库

  • 相关阅读:
    【Linux】iptables相关实践,原理及参数解释
    【Linux】解决Linux服务器内存不足问题
    【原】Linux环境下Shell调用MySQL并实现定时任务
    Nginx配置,413 Request Entity Too Large错误解决
    【MAC】Mac下部分常用的小工具
    好久不见
    HashMap工作原理(转载)
    Java中long和Long有什么区别 (转载)
    Explain in detail the steps/processes that occur from the moment you type a URL in a browser and hit enter
    Find Minimum in Rotated Sorted Array leetcode java
  • 原文地址:https://www.cnblogs.com/shixiaomao12138/p/12404101.html
Copyright © 2011-2022 走看看