zoukankan      html  css  js  c++  java
  • 编写四则运算程序之心得

    第一周老师要求我们编写了简单的30道加减法题,第二周老师提高了对我们的要求,要求我们编写一个四则运算的程序。参考了其他的同学的想法,我的想法是先随机生成一个数,然后把这个数进行拆分,生成运算式。代码如下:

    // second.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "stdlib.h"
    #include "time.h"
    
    typedef unsigned char Operator;
    #define Judge(OP1,OP2)	(((Operator)(OP1)>>1<(Operator)(OP2)>>1)?true:false)
    
    void Split(Operator OP,int &a,int &b,int num)
    {
    	switch(OP)
    	{
    	case 0:
    		{
    			if(num)
    			{
    				a=rand()%(num+1);
    				b=num-a;
    			}
    			else a=b=0;
    		}break;
    	case 1:
    		{
    			if(num)
    			{
    				a=num+rand()%100;
    				b=a-num;
    			}
    			else a=b=rand()%100;
    		}break;
    	case 2:
    		{
    			if(num)
    			{
    				int i;
    				for(i=1;i<num;i++)
    					if(!(num%i))
    						if(rand()%2)
    							break;
    				a=i,b=num/i;
    			}
    			else
    			{
    				if(rand()%2)
    					a=rand()%100,b=0;
    				else
    					b=rand()%100,a=0;
    			}
    		}break;
    	case 3:
    		{
    			if(num)
    			{
    				a=num*(1+rand()%10+rand()%10);
    				b=a/num;
    			}
    			else
    				a=0,b=rand()%100+1;
    		}
    	}
    }
    
    void Printf_OP(Operator OP)
    {
    	switch(OP)
    	{
    	case 0: printf("+");break;
    	case 1: printf("-");break;
    	case 2: printf("*");break;
    	case 3: printf("/");break;
    	}
    }
    
    void Printf_equation(Operator Parent,int num,int OP_num)
    {
    	if(!OP_num) 
    	{
    		printf("%d",num);
    		return;
    	}
    	Operator here=rand()%4;
    	int a,b,OP_n1,OP_n2;
    	OP_num--;
    	Split(here,a,b,num);
    	Split(0,OP_n1,OP_n2,OP_num);
    	if(Judge(here,Parent))
    	{
    		printf("(");
    		Printf_equation(here,a,OP_n1);
    		Printf_OP(here);
    		Printf_equation(here,b,OP_n2);
    		printf(")");
    	}
    	else
    	{
    		Printf_equation(here,a,OP_n1);
    		Printf_OP(here);
    		Printf_equation(here,b,OP_n2);
    	}
    
    }
    
    int main(int argc, char* argv[])
    {
    	int x[100],y[100];
    	srand((unsigned)time(NULL));
    	for(int i=1;i<=30;i++)
    	{
    		int num=rand()%100;
    		x[i]=num;
    		printf("%d  ",i);
    		Printf_equation(0,num,1+i/8);
    		printf("=?
    ");
    	}
    	for(i=1;i<=30;i++)
    	{
    		printf("请按题号答题:
    ");
    		printf("%d
    ",i);
    		scanf("%d",&y[i]);
    		if(y[i]==x[i])
    		{
    			printf("恭喜你!回答正确!
    ");
    		}
    				
    		else
    		{
    			printf("答案错误!正确答案为%d
    ",x[i]);
    			
    		}
    	}
    	return 0;
    }
    

      这次编程让我知道了编程的方式有很多种,编程的思路也是千变万化的,每一种思路都是可行的,只是看你个人的习惯,各种方法要巧妙运用,以后我要加大编写的代码量,只有编写的代码多了,才能熟能生巧,以后碰到各种难题都能迎刃而解。

  • 相关阅读:
    js中(function(){…})()立即执行函数写法理解
    JS 立即执行的函数表达式(function)写法
    javascript中call,apply,bind的用法对比分析
    C++成员函数指针的应用
    typeid详解
    dynamic_cast
    C++标准转换运算符dynamic_cast
    继承的构造函数
    考虑写一个不抛出异常的swap函数
    布隆过滤器(转)
  • 原文地址:https://www.cnblogs.com/zhumf/p/5275899.html
Copyright © 2011-2022 走看看