• 四则运算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;
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    C# 获取枚举集合的其中两种方式
    UITextField限制字数的方法
    iOS
    iOS
    iOS
    iOS 获取已连接的wifi信息
    AFNetWorking 的简单使用
    CoreData 基本操作方法封装
    在Ios里UIWebView参入js
    AFNetworking教程
  • 原文地址:https://www.cnblogs.com/hulidanxiang/p/5271757.html
Copyright © 2020-2023  润新知