用C生成数字 [英] Generate numbers in C

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

问题描述

我发现互联网上的代码生成了我们想要的数量,增加了1个。



因此,如果我们想生成10个数字,它会产生10个数字加上一个标题。



示例。

  10  
0 1 2 3 4 5 6 7 8 9





但是我可以更改代码以开始向后生成数字并且在零之外价值?



例子

<前lang =c> 10
10 9 8 < span class =code-digit> 7 6 5 4 3 2 1





通过 Fredrik Bornander [ ^ ]:< br $> b $ b

  #include   < span class =code-keyword><   stdlib.h  >  
#include < stdio.h >
# include < string.h >

char * append_char( char * buffer, char character){
* buffer = character;
return ++ buffer;
}

char * append_number( char * buffer,< span class =code-keyword> int number, char * temp_buffer){
char * target = buffer;
char * source = temp_buffer;
char * p;
itoa(number,temp_buffer, 10 );
for (p = source; * p!= 0 ; ++ p){
* target = * p;
++目标;
}
返回目标;
}

int main( int argc, char * argv []){
// Max int是4294967295,为
添加一个 // 可能的减号,为空格添加一个分隔符
int number_max_characters = 10 + 1 + 1 ;
int count_to_generate = 1000000 ; // 这是您想要的数字数
char * temp_buffer =( char *)malloc(number_max_characters);
// 为所有数字分配空间加上标题的空格,
// 加上标题后的换行符和尾随零
// 请注意,此处缓冲区可能太大,将通过复制来修复此问题
// 只有我们需要的结果和释放缓冲区
char * buffer =( char *)malloc((count_to_generate + 1 )* number_max_characters + 2 + 1 );
char * result;
char * p = buffer;
int i;
FILE * fp;

// 追加标题和换行符
p = append_char( append_char(append_number(p,count_to_generate,temp_buffer),' \ r'),' \ n');
for (i = 0 ; i< count_to_generate; ++ i){
p = append_number(p,i,temp_buffer);
if (i< count_to_generate - 1
p = append_char(p, ' ');
}

* p = 0 ; // 空终止

// 获取缓冲区的使用大小,将其分配给结果...
result =( char *)malloc(( int )p - ( int )buffer);
// ...并复制到结果
strcpy(结果,缓冲);
free(buffer); // 我们现在已经完成了这个

printf( %s,result);

fp = fopen( test.txt w);
fprintf(fp, %s,结果);
fclose(fp);

return 0 ;
}

解决方案

查看获取循环:这是什么工作。

目前你这样做:

   (i =  0 ; i< count_to_generate; ++ i){

for loop有三个部分,用';'字符分隔。

  for (初始化;终止检查;迭代)



初始化允许您设置起始值:在您的情况下为零。

终止检查让你说当循环停止运行时:在你的情况下 i 不再低于你的最大值。

迭代以免你指定每次循环时会有什么变化:在你的情况下,你将 i 增加一个。



所以到下去,更改迭代:我 - 将减去一个而不是添加。

T帽子意味着需要更改终止检查: i> 0 将在你达到零时结束。

然后你需要改变开始,所以它可以从你的最大值开始:

 i = count_to_generate 



哪个给你:

  for (i = count_to_generate; i>  0 ; i--){



简单,是吗?


这就是你所需要的:

  int  count_to_generate = 百万;  //  这是您想要的数字数 
int i;
FILE * fp;

// 追加标题和换行符
fp = fopen ( test.txt w);
fprintf(fp, %d \ n,count_to_generate);
for (i = 0 ; i< count_to_generate; ++ i){
fprintf(fp, %d,i);
printf( %d,i);
}
fprintf(fp, \ n);
printf( \ n);
fclose(fp);


I found thatc code on the internet tha generate as many numbers as we want, increaced bu 1.

So if we want to generate 10 number it would generate 10 numbers plus one for the header.

Example.

10
0 1 2 3 4 5 6 7 8 9



But can I change the code to start generate the number backwards and weithout the zero value?

Example

10
10 9 8 7 6 5 4 3 2 1



Heres the code by Fredrik Bornander[^]:

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

char* append_char(char* buffer, char character) {
    *buffer = character;
    return ++buffer;
}
 
char* append_number(char* buffer, int number, char* temp_buffer) {
    char* target = buffer;
    char* source = temp_buffer;
    char* p;
    itoa(number, temp_buffer, 10);
    for (p = source; *p != 0; ++p) {
        *target = *p;
        ++target;
    }
    return target;
}
 
int main(int argc, char* argv[]) {
    // Max int is 4294967295, add one for a 
    // possible minus sign, add one for the space delimiter
    int number_max_characters = 10 + 1 + 1;
    int count_to_generate = 1000000; // This the number of numbers you want
    char* temp_buffer = (char*)malloc(number_max_characters);
    // Allocate space for all the numbers plus one for the "header",
    // plus space for the line-break after the header and a trailing zero
    // Note that buffer is potentially too big here, will fix that by copying 
    // only what we need into result, and freeing buffer
    char* buffer = (char*)malloc((count_to_generate + 1) * number_max_characters + 2 + 1);
    char* result;
    char* p = buffer;
    int i;
    FILE* fp;
 
    // Append header and line break
    p = append_char(append_char(append_number(p, count_to_generate, temp_buffer), '\r'), '\n');
    for (i = 0; i < count_to_generate; ++i) {
        p = append_number(p, i, temp_buffer);
        if (i < count_to_generate - 1)
            p = append_char(p, ' ');
    }
 
    *p = 0; // Null terminate

    // Take the used size of buffer, allocate that to result...
    result = (char*)malloc((int)p - (int)buffer);
    // ...and copy over to result
    strcpy(result, buffer);
    free(buffer); // We're done with this one now

    printf("%s", result);
 
    fp = fopen("test.txt", "w");
    fprintf(fp, "%s", result);
    fclose(fp);
 
    return 0;
	}

解决方案

Look at your for loop: it's what does the work.
At the moment you do this:

for (i = 0; i < count_to_generate; ++i) {

and a for loop has three parts, separated by ';' characters.

for (initialization ; termination check ; iterate)


"Initialization" lets you set a starting value: in your case zero.
"Termination check" lets you say when the loop will stop running: in your case when i is no longer less than your maximum value.
"Iterate" lest you specify what changes each time you go round the loop: in your case you increase i by one.

So to go down, change "Iterate": i-- will subtract one instead of adding.
That means the "Termination check" needs to be changed: i > 0 will end when you reach zero.
And then you need to change the start, so it works from your maximum:

i = count_to_generate


Which gives you:

for (i = count_to_generate; i > 0; i--) {


Easy, yes?


This is all you need:

int count_to_generate = 1000000; // This the number of numbers you want
int i;
FILE* fp;

// Append header and line break
fp = fopen("test.txt", "w");
fprintf(fp, "%d\n", count_to_generate);
for (i = 0; i < count_to_generate; ++i) {
    fprintf(fp, "%d ", i);
    printf("%d ", i);
}
fprintf(fp, "\n");
printf("\n");
fclose(fp);


这篇关于用C生成数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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