[ create a new paste ] login | about

Link: http://codepad.org/T2ypXaY3    [ raw code | output | fork ]

C, pasted on May 5:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10

int cal(int, int, int, int);

main()
{
	//初期化フェーズ
	unsigned int a, b, c, num;
	int i, n;
	//処理フェーズ
	srand((unsigned int)time(NULL));
	printf("3つの変数で四則演算します\n");
	
		for (n = 1; n <= 100; n++) {
			num = (rand() % 6);
			a = rand() % SIZE;
			b = rand() % SIZE;
			c = rand() % SIZE;
			i = cal(a, b, c, num);
			printf("%d %d %d %d\n", a, b, c, num);

			while (i == 999) {
				num = rand() % 6;
				a = rand() % SIZE;
				b = rand() % SIZE;
				c = rand() % SIZE;
				i = cal(a, b, c, num);
			   printf("%d %d %d %d\n", a, b, c, num);
		   }
			if (num == 0)
				printf("%d * %d + %d = ", a, b, c);
		   else if (num == 1)
				printf("%d + %d * %d = ", a, b, c);
		   else if (num == 2)
				printf("%d * %d - %d = ", a, b, c);
		   else if (num == 3)
				printf("%d - %d * %d = ", a, b, c);
		   else if (num == 4)
				printf("%d / %d - %d = ", a, b, c);
		   else if (num == 5)
				printf("%d - %d / %d = ", a, b, c);
			else
				printf("エラー\n");

		printf("%d %d\n", i, n);
		//終了フェーズ
		}
		return 0;	
	}

int cal (int x, int y, int z, int num1)
{
	if (num1 == 0)
		return x * y + z;
	else if (num1 == 1)
		return x + y * z;
	else if (num1 == 2)
		return x * y - z;
	else if (num1 == 3)
		return x - y * z;
	else if (num1 == 4)
		if (x % y == 0)
			return x / y - z;
		else
			return 999;
	else if (num1 == 5)
		if (y % z == 0)
			return x - y / z;
		else
			return 999;
	else
		return 999;
}


Output:
3つの変数で四則演算します
0 0 7 2
0 * 0 - 7 = -7 1
7 9 4 1
7 + 9 * 4 = 43 2
7 1 1 3
7 - 1 * 1 = 6 3
6 0 0 1
6 + 0 * 0 = 6 4
6 3 5 1
6 + 3 * 5 = 21 5
9 5 5 2
9 * 5 - 5 = 40 6
8 5 8 4
2 6 9 4
6 8 7 0
6 * 8 + 7 = 55 7
0 3 9 2
0 * 3 - 9 = -9 8
5 2 4 3
5 - 2 * 4 = -3 9
4 4 8 1
4 + 4 * 8 = 36 10
9 4 1 5
9 - 4 / 1 = 5 11
1 7 4 4
2 5 4 2
2 * 5 - 4 = 6 12
1 5 0 4
6 9 7 5
4 6 7 0
4 * 6 + 7 = 31 13
8 1 5 3
8 - 1 * 5 = 3 14
7 5 6 1
7 + 5 * 6 = 37 15
9 7 7 3
9 - 7 * 7 = -40 16
6 2 3 1
6 + 2 * 3 = 12 17
9 9 6 0
9 * 9 + 6 = 87 18
5 8 1 0
5 * 8 + 1 = 41 19
9 9 1 3
9 - 9 * 1 = 0 20
7 8 7 4
1 4 3 0
1 * 4 + 3 = 7 21
0 1 7 4
0 / 1 - 7 = -7 22
8 5 4 1
8 + 5 * 4 = 28 23
9 7 7 1
9 + 7 * 7 = 58 24
7 6 0 2
7 * 6 - 0 = 42 25
6 3 7 4
6 / 3 - 7 = -5 26
2 8 2 4
8 9 0 4
6 3 7 5
4 7 7 2
4 * 7 - 7 = 21 27
2 2 3 0
2 * 2 + 3 = 7 28
1 1 9 3
1 - 1 * 9 = -8 29
9 1 4 4
9 / 1 - 4 = 5 30
4 3 1 5
4 - 3 / 1 = 1 31
3 5 1 3
3 - 5 * 1 = -2 32
7 1 2 5
6 2 9 5
9 8 6 0
9 * 8 + 6 = 78 33
0 0 4 5
0 - 0 / 4 = 0 34
3 5 2 0
3 * 5 + 2 = 17 35
6 9 7 2
Floating point exception


Create a new paste based on this one


Comments: