Software Engineering First Assignment program

Source: Internet
Author: User
Tags greatest common divisor

First, Function introduction

The program is written in C language and realizes the function:

1.30 questions each time, and give the answer

2. Arithmetic (results do not appear negative, division can be divided evenly)

3. True score arithmetic (the results are all in the simplest form)

Second, the completion of the effect

1. Start interface

2. Arithmetic Quiz

3. Show answers

4. True score Arithmetic

5. Show answers

Iii. the process of writing

The two major functions of the program are the arithmetic and the arithmetic of the true fraction, so if the main function design if branch, according to choose to use different functions.

These two main parts are controlled by generating a random number of 0-3 to control whether the problem is addition or subtraction or multiplication method, and then use for loop to complete the generation of 30 questions.

1. Preparation of the arithmetic

Generates two random operands with a value of less than 10.

If addition is added directly, then the result is stored in the array answer[30].

If it is subtraction, to compare the size of two random numbers, to avoid minuend less than the number of negative, if the meiosis is less than the meiosis, the two number of positions are swapped, and the results are stored in the array.

If multiplication is the direct multiply, the result is stored in the array.

Division uses random numbers to generate results and divisor, and then multiply to find dividend, which guarantees that the division answer is all integers.

Finally, the answer is printed with a for loop.

2. Writing a true fractional operation

Generates four random operands with a value of less than 10, respectively, as the numerator and denominator of the two operands.

The array used to save the result also has a numerator and denominator that changes to two, storing the result separately, so that the design facilitates the fractional simplification.

If it is an addition, follow least common multiple-pass and store the result.

If it is subtraction, the result is calculated first, the positive or negative, if negative, then the two-digit order is reversed and the result (negative number) is reversed and saved.

In the case of multiplication, the numerator is multiplied, the denominator is multiplied, and the greatest common divisor of the numerator denominator is obtained.

If it is division, it will be replaced by the denominator of the divisor numerator, calculated by multiplication method.

Finally, the answer is printed with a for loop.

Full code

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

void Function1 ();
void Function2 ();
int maxg (int a, int b);

int main (void)
{
int choose;
printf ("Welcome!" \ n \ nyou Choose the following actions: \ n ");
printf ("1. Integer arithmetic \n\n2. True fractional operation \ n");
printf ("Enter the corresponding option number, press ENTER to confirm \ n");
//system ("pause");
scanf ("%d", &choose);
if (choose = = 1)
Function1 ();//arithmetic
Else
if (choose = = 2)
Function2 ();//True fractional operation
Else
printf (" Illegal input! ");
return 0;
}

void Function1 ()
{
int i;
int answer[30], a, b, c,d;
for (i = 0; i <; i++)
{
A = (int) rand ()% 4;
if (a = = 0)//addition
{
b = (int) rand ()% 10;
c = (int) rand ()% 10;
printf ("title%d%d+%d= \ n", i+1,b,c);
Answer[i] = B + C;
}
Else
if (a = = 1)//subtraction
{
b = (int) rand ()% 10;
c = (int) rand ()% 10;
if (c > B)//Avoid negative numbers
{
D = b;
b = C;
c = D;
}
printf ("title%d%d-%d= \ n", i + 1, b, c);
Answer[i] = b-c;
}
Else
if (a = = 2)//multiplication
{
b = (int) rand ()% 10;
c = (int) rand ()% 10;
printf ("title%d%dx%d= \ n", i + 1, b, c);
Answer[i] = b * C;
}
Else
if (a = = 3)//Division
{
b = (int) rand ()% 10;
c = (int) rand ()% 10;
D = b*c;//guarantees that the results are all integers
printf ("title%d%d÷%d= \ n", i + 1, d, c);
Answer[i] = b;
}
}
System ("pause");//Press any key to display the answer
printf ("\ n");
for (i = 0; i <; i++)
{
printf ("title%d%d\n", i + 1, answer[i]);
}
System ("pause");
}

void Function2 ()
{
int fenzi[30],fenmu[30];
int A, B, C, D, E, I;
for (i = 0; i <; i++)
{
A = (int) rand ()% 4;
if (a = = 0)
{
b = ((int) rand ()% 9) + 1;//avoid denominator occurrence 0
c = ((int) rand ()% 9) + 1;
D = ((int) rand ()% 9) + 1;
E = ((int) rand ()% 9) + 1;
printf ("title%d (%d/%d) + (%d/%d) = \ n", i + 1, b, c,d,e);//Press least common multiple to simplify
Fenmu[i] =c*e/maxg (c,e);
Fenzi[i] = Fenmu[i]/c*b + fenmu[i]/e*d;
}
Else
if (a = = 1)
{
b = ((int) rand ()% 9) + 1;
c = ((int) rand ()% 9) + 1;
D = ((int) rand ()% 9) + 1;
E = ((int) rand ()% 9) + 1;
Fenmu[i] = C*e/maxg (c, e);//Press least common multiple to simplify
Fenzi[i] = Fenmu[i]/c*b-fenmu[i]/e*d;
if (Fenzi[i] < 0)//If there is a negative number, will be the meiosis and the replacement
{
Fenzi[i] = 0-fenzi[i];//guaranteed result is positive
printf ("title%d (%d/%d)-(%d/%d) = \ n", i + 1, D, E, B, c);
}
printf ("title%d (%d/%d)-(%d/%d) = \ n", i + 1, B, C, D, E);
}
Else
if (a = = 2)
{
b = ((int) rand ()% 9) + 1;
c = ((int) rand ()% 9) + 1;
D = ((int) rand ()% 9) + 1;
E = ((int) rand ()% 9) + 1;
printf ("title%d (%d/%d) X (%d/%d) = \ n", i + 1, B, C, D, E);
Fenzi[i] = b*d;
Fenmu[i] = c*e;
Fenzi[i] = fenzi[i]/maxg (Fenzi[i], fenmu[i]);//-pass numerator denominator divided by greatest common divisor
Fenmu[i] = fenmu[i]/maxg (Fenzi[i], fenmu[i]);
}
Else
if (a = = 3)
{
b = ((int) rand ()% 9) + 1;
c = ((int) rand ()% 9) + 1;
D = ((int) rand ()% 9) + 1;
E = ((int) rand ()% 9) + 1;
printf ("title%d (%d/%d) ÷ (%d/%d) = \ n", i + 1, B, C, D, E);
Fenzi[i] = b*e;
Fenmu[i] = c*d;
Fenzi[i] = fenzi[i]/maxg (Fenzi[i], fenmu[i]); Numerator denominator divided by greatest common divisor after-pass
Fenmu[i] = fenmu[i]/maxg (Fenzi[i], fenmu[i]);
}
}
System ("pause");//Press any key to display the answer
printf ("\ n");
for (i = 0; i <; i++)
{
if (fenzi[i]==0)
printf ("title%d%d\n", i + 1, fenzi[i]);//If the numerator is 0, print 0 as the result directly
Else
printf ("title%d%d/%d\n", i + 1, fenzi[i],fenmu[i]);
}
System ("pause");
}

int maxg (int a, int b)//Greatest common divisor
{
int C, D;
if (a < b)
{
c = A;
A = b;
b = C;
}
D = a%b;
if (d = = 0)
return b;
Else
Maxg (b, D);
}

Software Engineering first job quiz program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.