为什么指针变量没有显示100个数字? [英] why pointer variable not showing 100 digits?

查看:169
本文介绍了为什么指针变量没有显示100个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我对精度的要求,我需要显示100位或至少50个数字

aim: i had a requirement for accuracy i need 100 digits to be displayed or atleast 50 digits

1尝试:第一次我用了整型变量则仅显示10个数字

try 1: first i had used integer variable then it displayed only 10 digits

2尝试:我的朋友的帮助下我使用的指针概念,它已成功地输入50至100位,但指针变量只显示10个数字

try 2: with the help of my friend i used pointers concept it successfully had input 50 to 100 digits but pointer variable display only 10 digits

我写的程序是

#include <stdio.h>
#include <string.h>

main() 
 {
    int *p;
    p=(int*)malloc(100*sizeof(int));
    *p=1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890;
    printf("%d",*p); 
 }

上执行上面的过程我得到一个垃圾值

on executing the above procedure i'm getting a garbage value

在此先感谢

推荐答案

您需要使用的 BIGNUM (任意precision大的数字)图书馆,因为本地 INT 有由C语言实现限制一个precision - 即硬件(通常是32或64位;另见&LT; stdint.h&GT; 标题和 int32_t &放大器; 的int64_t 类型)。我建议使用 GMPlib ;你需要精通C到使用这些库。阅读GMLib 的文档。

You need to use a bignum (arbitrary precision big numbers) library, since native int or long have a precision limited by the C implementation - ie the hardware (often 32 or 64 bits; see also <stdint.h> header and int32_t & int64_t types). I suggest using GMPlib; you need to be fluent in C to use such libraries. Read documentation of GMLib.

不要试图code bignumber算术自己。你会使用低效率的算法。对于大数高效的算法是很难理解和创造。你仍然可以得到一个博士学位。因此,使用一些的现有的BIGNUM库。

Don't try to code bignumber arithmetic yourself. You'll use inefficient algorithms. Efficient algorithms for bignums are difficult to understand and to invent. You can still get a PhD on that. So use some existing bignum library.

所以,你可能code类似以下137乘以你的BIGNUM并打印出结果。

So you might code something like the following to multiply your bignum by 137 and print the result

mpz_t bign;
mpz_t bigr;
// initialize bign from a literal string
mpz_init_set_str (bign,
                 "12345678901234567890123456789012345678901234567890"
                 "12345678901234567890123456789012345678901234567890",
                 10);
// initialize bigr
mpz_init(bigr);
/// compute bigr = bign * 137 (137 is a long, not a bignum)
mpz_mul_si(bigr, bign, 137L);
/// print bigr on stdout in base 10
mpz_out_str (stdout, 10, bigr);
/// clear all the numbers
mpz_clear(bign);
mpz_clear(bigr);

在Linux系统上安装了GMP,你可以编译这样的code。使用( yoursource.c 中):

gcc -Wall -Wextra -g yoursource.c -lgmp -o yourprog

在其他系统可能需要一些 -I -L 参数 GCC 。当你调试你的程序,要求编译器与 -O2

On other systems you might need some -I and -L arguments to gcc. When you have debugged your program, ask the compiler to optimize it with -O2

注意,有些语言(Common Lisp的,计划,Python)的有内置的大数。

Notice that some languages (Common Lisp, Scheme, Python) have built-in bignums.

顺便说一句,你的函数的错误的定义。它应该定义为 INT主(INT ARGC,字符** argv的) ...

BTW, your main function is incorrectly defined. It should be defined as int main(int argc, char**argv) ...

这篇关于为什么指针变量没有显示100个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆