通过数字打印在C位的一个数字 [英] Print a number in c digit by digit

查看:195
本文介绍了通过数字打印在C位的一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 5678

我要显示号码如下

  5678
678
78
8

我怎么能在C办?

我做到了这一点。

  INT N = 5678;的for(int i = N; I> = 1; --i)
{
    对于(INT J = 1; J< = I; ++ j)条
  {       打印(%D,J);  }   打印(\\ n)
}


解决方案

首先的字符串的整型,那么你可以使用一个字符串指针这样的:

  INT的main()
{
    INT N = 5678;
    字符* str_p,海峡[10]; //声明一个字符指针,以及10个字符数组
    sprintf的(STR,%D,N); //初始化字符串str的{'5','6','7','8','\\ 0'}
    str_p =安培;海峡[0]; //初始化指针开始字符串,str_p =海峡;工作,太
    而(* str_p!='\\ 0')
    {//当指针不指向结束的字符串:
        的printf(%S \\ n,str_p); //打印字符串str_p点
        str_p ++; //移指针,指向字符串中的下一个字符
    }
    返回0;
}

输出结果,你可以看到在这个codePAD 是:

  5678
678
78
8

很容易,真的。

诀窍是 str_p ++ 。起初,当我学习指点一下,我发现这相当混乱。但它确实是相当简单的。想想一个指针作为一个日历,红色的塑料方的事情:你将它滑过的任何日期恰好是这一天:

  MAY:
_____________________________
| | | | | | ___ | |
| 1 | 2 | 3 | 4 | 5 || 6 || 7 |
| ___ | ___ | ___ | ___ | ___ | --- | ___ |

这意味着它是5月的第六位。没错,如果我们翻译这个的指针+阵列使用C,我们会碰到这样的:

  INT可[31] = {1,2,3,4,5,6,7,8,9,...};
为int *今天=放大器;可[5]; //零索引

滑动红色正方形的东西,第二天的行动(今天+ 1 ==明日)写入,从逻辑上讲,是这样的:

 今天++; //还是今天+ = 1

这就是说:

 今天+ 1:[] => //却将红方啄
_____________________________
| | | | | | | ___ |
| 1 | 2 | 3 | 4 | 5 | 6 || 7 ||
| ___ | ___ | ___ | ___ | ___ | ___ | --- |

所以,如果我再分别写:

  today--;

同样的逻辑也适用,今天点回到6 ...去商店,买这些日历之一,如果你觉得有必要想象这...

假如你想改变正在由一个指针指向的价值?好了,坚持做同一比喻(滑块超过一定价值的),你不得不用你的一方面认为,滑盖的地方,并用另一只手,然后你可以什么是它下面的工作。在您的code这是由inderection * 运营商反映。把它看成是一个针,以保持指针地方,而你不管它指向工作:

 (*今天)++; //或*今天++ ;,无论是运营商具有相同的precedence

这真的,真的,真的就是这么简单......唉,不,这不是:)但你使用指针9/10倍,思考的作品这样

BTW:使用 str_p = STR; ,我认为更符合逻辑,一点点更有效的,但是编译器可能优化了声明,同样的事情。尽管如此这里还有一个codePAD,证明都做同样的事情

而现在,到年底,更通用的方法,那将具有可变长度的数字,动态分配字符串所需的内存,然后再释放它的工作:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
    INT I,LEN,N = 5678;
    字符* str_p = NULL;
    INT digitCount(int类型);
    LEN = digitCount(N); //多少个字符我们需要分配?
    str_p =释放calloc(LEN,sizeof的(炭)); //分配字符串,释放calloc inits内存为0
    LEN - ; //避免在最后打印空字符串
    sprintf的(str_p,%D,N); //在内存中设置字符串
    对于(i = 0; I< LEN ++ I)的printf(%S \\ n,str_p + I); //打印字符串,用i递增指针
    免费(str_p); //解除分配字符串内存
    返回0;
}INT digitCount(int类型)
{
    诠释计数= 1; // 1 0终止符
    而(在)
    {//只要/ 10(即一个int,这将最终成为0 ==>假)
        在/ = 10;
        算上++; //增加1位来算
    }
    返回计数;
}

i have number 5678

i want display number as below

5678
678
78
8

how can i do in c??

i done this

int n = 5678;

for(int i=n; i >= 1; --i)
{
    for(int j=1; j <= i; ++j)
  {

       print("%d",j);

  }

   print("\n")
}

解决方案

First "stringify" the int, then you can use a string pointer for this:

int main()
{
    int n = 5678;
    char *str_p, str[10];//declare a char pointer, and an array of 10 chars
    sprintf(str, "%d", n);//initializes str to {'5','6','7','8','\0'}
    str_p = &str[0];//init pointer to start of string, str_p = str; works, too
    while(*str_p != '\0')
    {//while pointer doesn't point to end of string:
        printf("%s\n", str_p);//print string str_p points to
        str_p++;//shift pointer, to point to next char in string
    }
    return 0;
}

The resulting output, as you can see in this codepad is:

5678
678
78
8

Quite easy, really.
The trick is str_p++. At first, when I was learning about pointers, I found this quite confusing. But it really is quite simple. Think of a pointer as that red plastic square thing on a calendar: you slide it over whatever date it happens to be that day:

MAY:
_____________________________
|   |   |   |   |   |___|   |
| 1 | 2 | 3 | 4 | 5 ||6|| 7 |
|___|___|___|___|___|---|___|

This means it's the sixth of may. Right, if we translate this to a pointer + array in C we'd have something like:

int may[31] = {1,2,3,4,5,6,7,8,9,...};
int *today = &may[5];//zero indexed

The action of sliding the red square thing to the next day (today + 1 == tomorrow) is written, logically, like this:

today++;//or today += 1

This is to say:

          today + 1: [] =>[]//shift the red square-thingy
_____________________________
|   |   |   |   |   |   |___|
| 1 | 2 | 3 | 4 | 5 | 6 ||7||
|___|___|___|___|___|___|---|

So if I then were to write:

today--;

the same logic applies, and today points back to 6... go to the store, buy one of these calendars if you feel the need to visualize this...
Suppose you want to change the value that is being pointed to by a pointer? Well, sticking to the same analogy (of a slider over some value), you'd have to use your one hand to hold that slider in place, and with the other hand, you can then work on what's underneath it. In your code this is reflected by the inderection * operator. Think of it as a pin, to hold the pointer in place while you work on whatever it points to:

(*today)++;//or *today++;, both operators have the same precedence

It really, really, really is that simple... Well, no it's not :) but 9/10 times you're using pointers, this way of thinking works

BTW: using str_p = str; is, I think more logical, and a tad more efficient, but the compiler probably optimizes both statements to the same thing. Still Here's another codepad, to prove both do the same thing

And now, to end, a more generic approach, that'll work with numbers of variable length, dynamically allocating memory required for the string, and freeing it again:

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

int main()
{
    int i, len, n = 5678;
    char *str_p = NULL;
    int digitCount(int in);
    len = digitCount(n);//how many chars do we need to allocate?
    str_p = calloc(len, sizeof(char));//allocate string, calloc inits memory to 0
    len--;//avoid printing empty string at the end
    sprintf(str_p, "%d", n);//set string in memory
    for(i=0;i<len;++i) printf("%s\n", str_p + i);//print string, increment pointer by i
    free(str_p);//deallocate string memory
    return 0;
}

int digitCount(int in)
{
    int count = 1;//1 for 0 terminating char
    while(in)
    {//as long as in/10 (being an int, this will eventually become 0 ==> false)
        in /= 10;
        ++count;//add 1 digit to count
    }
    return count;
}

这篇关于通过数字打印在C位的一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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