-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmpz_input.c
More file actions
76 lines (64 loc) · 2.05 KB
/
mpz_input.c
File metadata and controls
76 lines (64 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//******************************************************************************
// mpz_input.c : Sample code of multiple precision integer
// Copyright (C) 2019 Tomonori Kouya
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation, either version 3 of the License or any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//******************************************************************************
#include <stdio.h>
#include "gmp.h"
int main(void)
{
mpz_t a, b, c;
unsigned char str_a[1024], str_b[1024];
// mpz_init_set_str(a, "123456789012345678901234567890", 10);
// mpz_init_set_ui(b, 9876543210);
mpz_init(a);
mpz_init(b);
mpz_init(c);
// input
printf("a = ");
#ifdef USE_STR_INPUT
// string to mpz
while(scanf("%s", str_a) < 1);
printf("str_a = %s\n", str_a);
mpz_set_str(a, str_a, 10);
#else // USE_STR_INPUT
gmp_scanf("%Zd", a);
#endif // USE_STR_INPUT
gmp_printf("a = %Zd\n", a);
printf("b = ");
#ifdef USE_STR_INPUT
// string to mpz
while(scanf("%s", str_b) < 1);
printf("str_b = %s\n", str_a);
mpz_set_str(b, str_b, 10);
#else // USE_STR_INPUT
gmp_scanf("%Zd", b);
#endif // USE_STR_INPUT
mpz_add(c, a, b);
gmp_printf("%Zd + %Zd = %Zd\n", a, b, c);
mpz_sub(c, a, b);
gmp_printf("%Zd - %Zd = %Zd\n", a, b, c);
mpz_mul(c, a, b);
gmp_printf("%Zd * %Zd = %Zd\n", a, b, c);
mpz_div(c, a, b);
gmp_printf("%Zd / %Zd = %Zd\n", a, b, c);
mpz_mod(c, a, b);
gmp_printf("%Zd %% %Zd = %Zd\n", a, b, c);
mpz_clear(a);
mpz_clear(b);
mpz_clear(c);
return 0;
}