zoukankan      html  css  js  c++  java
  • 四则运算1规范代码

    //Arithmetic program, supportting proper fraction arithmetic 
    //2016,03,04
    
    #include<iostream>
    #include<stdlib.h>
    #include<time.h>
    using namespace std;
    
    void main()
    {
    	srand((int)time(NULL));  //Produce seeds
    
    	int array_A[30];
    	int array_B[30];
    	for (int i = 0; i < 30; i++)   //Produce 30 random number in the two arrays respectively
    	{
    		array_A[i] = 1+rand() % 99;
    		array_B[i] = 1+rand() % 99;
    	}
    	for (int i = 0; i < 30; i++)
    	{
    		if (array_A[i] > array_B[i])
    		{
    			int suanfu = rand() % 4;
    			switch (suanfu)
    			{
    			case 0:
    				cout << array_A[i] << " + " << array_B[i] << "=" << endl; break;
    			case 1:
    				cout << array_A[i] << " - " << array_B[i] << "=" << endl; break;
    			case 2:
    				cout << array_A[i] << " x " << array_B[i] << "=" << endl; break;
    			case 3:
    				cout << array_A[i] << " ÷ " << array_B[i] << "=" << endl; break;
    			}
    		}
    		else 
    		{
    			int copyA = array_A[i];    //Retain the original value of A;
    			int copyB = array_B[i];
    			int beichushuA = 100;      //The denominator is set to 100
    			int beichushuB = 100;
    			int firstA = beichushuA % copyA;
    			int firstB = beichushuB % copyB;
    			while (firstA != 0)       //Get the greatest common divisor of A and 100 to get true score reduction
    			{
    				int temp = copyA;
    				copyA = beichushuA%copyA;
    				beichushuA = temp;
    				firstA = beichushuA%copyA;  //copyA is becoming the Greatest common divisor in the end
    			}
    			while (firstB != 0)
    			{
    				int temp = copyB;
    				copyB = beichushuB%copyB;
    				beichushuB = temp;
    				firstB = beichushuB%copyB;
    			}
    			int suanfu = rand() % 4;
    			switch (suanfu)
    			{
    				// Output fractional in the form of the minimalist
    			case 0:
    				cout << array_A[i] / copyA << "/" << 100 / copyA << " + " << array_B[i] / copyB << "/" << 100 / copyB << "=" << endl; break;
    			case 1:
    				cout << array_B[i] / copyB << "/" << 100 / copyB << " - " << array_A[i] / copyA << "/" << 100 / copyA << "=" << endl; break;
    			case 2:
    				cout << array_A[i] / copyA << "/" << 100 / copyA << " x " << array_B[i] / copyB << "/" << 100 / copyB << "=" << endl; break;
    			case 3:
    				cout << array_A[i] / copyA << "/" << 100 / copyA << " ÷ " << array_B[i] / copyB << "/" << 100 / copyB << "=" << endl; break;
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    python
    python中xrange和range的区别
    python
    shell用if
    shell调用shell
    An unhandled exception of type 'System.TypeInitializationException' occurred in System.ServiceModel.dll
    C# 获取存在DataTable1不存在DataTable2的数据的快速方法
    textbox自动提示
    全面理解面向对象的 JavaScript(转载)
    C#中文乱码转换
  • 原文地址:https://www.cnblogs.com/hulidanxiang/p/5271757.html
Copyright © 2011-2022 走看看