Arithmetic C language Program

Source: Internet
Author: User
Tags rand

Chiu has a son on the second grade of primary school, the teacher daily let parents give children 30 add subtraction problem, although not much, but every day to do is also a burden, Chiu as a veteran programmer of course want to use the computer to solve this small problem, at present for this problem for any language is not a problem

Title Requirements:

    • can automatically generate primary school arithmetic topic (note is for the pupils, if the results appear negative, they will be confused!) )
    • In addition to integers, support the arithmetic of true fractions

Title analysis: 1. Because it is for elementary school students, so the need to calculate the time can not have negative numbers, therefore, the subtraction operation, the preceding number must be greater than the number of subsequent.

2. The rule for the division operation, the denominator cannot be 0.

3. The topic requires support for true scores, known by the concept of true scores, and molecules cannot be greater than the number of denominators.

4. Since the title requirement is a random 30 question, the random generation number function Srand () and Rand () are used.

The source code is as follows:

#include <stdio.h>
#include <Windows.h>
#include <time.h>
void Main ()
{
int A, B, C, d,i,m,n;
float p, q;
Srand (Unsigned (Time (NULL)));
for (i = 0; i <; i++)
{
A = rand ()% 100;
b = rand ()% 100;
c = rand ()% 5;
Switch (c)
{
Case 0:printf ("%d +%d =\n", A, b);
Break
Case 1:if (A < b)
{
D = A;
A = b;
b = D;
}
printf ("%d-%d =\n", A, b);
Break
Case 2:printf ("%d *%d =\n", A, b);
Break
Case 3:
while (b = = 0)
{
b = rand ()% 100;
}
printf ("%d/%d =\n", A, b);
Break
Case 4:
A = rand ()% 100;
b = rand ()% 100;
m = rand ()% 100;
n = rand ()% 100;
c = rand ()%4;
while (b = = 0)
{
b = rand ()% 100;
}
while (n = = 0)
{
n = rand ()% 100;
}
if (a > B)
{
D = A;
A = b;
b = D;
}
if (M > N)
{
d = m;
m = n;
n = d;
}
Switch (c)
{
Case 0:printf ("%d/%d +%d/%d =\n", A, B, M, n);
Break
Case 1:
p = (float) A/(float) b;
Q = (float) m/(float) n;
if (P < q)
{
D = A;
A = m;
m = D;
D = b;
b = n;
n = d;
}
printf ("%d/%d-%d/%d =\n", p, Q);
Break
Case 2:printf ("%d/%d *%d/%d =\n", A, B, M, n);
Break
Case 3:
A = rand ()% 100;
b = rand ()% 100;
m = rand ()% 100;
n = rand ()% 100;
c = rand ()% 4;
while (M = = 0)
{
m = rand ()% 100;
}
printf ("%d/%d/%d/%d =\n", A, B, M, n);
Break
}
}
}
System ("pause");
}

Program run:

Summarize:

Because only the C language and C + +, and C + + is not too familiar, so the choice of C language to complete the job, programming with the time of the morning, because before the random generation function and Include<time.h>, so in the class with the help of classmates, successfully completed the work.

This assignment not only improved my proficiency in C programming, but also learned the random generation number function. Look forward to the next homework exercise

Yes, that's it!

Arithmetic C language 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.