如何在c中使用corecursion?

如何在c中使用corecursion?,c,corecursion,C,Corecursion,我需要帮助找到在每个函数中放置print语句的位置(alpha_count和sum_digits),以便它们只打印一次(在程序结束时) 例如 字符数:8 位数总和:19 现在,每次调用函数时,它们都会打印。有什么想法吗 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> //Prototypes void count_alpha(char *

我需要帮助找到在每个函数中放置print语句的位置(alpha_count和sum_digits),以便它们只打印一次(在程序结束时)

例如
字符数:8
位数总和:19

现在,每次调用函数时,它们都会打印。有什么想法吗

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

//Prototypes
void count_alpha(char *s, int len, int index); 
void sum_digit(char *s, int len, int index);

#define SIZE 22

int main(void){
  //Declarations
  char string[SIZE];
  int length;

  //Get string from user
  printf("Enter a string of letters and numbers: ");
  scanf("%s", string);
  printf("String: %s\n", string);
  //Get length of string
  length = strlen(string);
  printf("Length: %d\n", length);
  //Get letters from string
  count_alpha(string, length, 0);

  return 0;
}

void count_alpha(char *s, int len, int index){
  static int characters = 0;
  char c = ' ';

  if (index < len){
    c = s[index];
    if(isalpha(c)){
        characters++;
        printf("char: %d\n", characters);
        index++;
        printf("index: %d\n", index);
        count_alpha(s, len, index);
    }
    else if(isdigit(c)){
        sum_digit(s, len, index);
    }
    //index++;
    //printf("index: %d\n", index);
    //printf("Number of Characters: %d\n", characters);
  }
  //else
  printf("Number of Characters: %d\n", characters);

}

void sum_digit(char *s, int len, int index){
  static int digits = 0;
  char c = ' ';

  if (index < len){
    c = s[index];
    if(isalpha(c)){
        count_alpha(s, len, index);
  }
  else if(isdigit(c)){
        printf("num is: %c", c);
        //printf("number is: %d", (atoi(&s[index])));
        //digits += atoi(&c);
        digits += c - '0';
        printf("sum: %d\n", digits);
        index++;
        printf("index: %d\n", index);
        sum_digit(s, len, index);
    }
    //index++;
    //printf("index: %d\n", index);
    //printf("Sum of digits: %d\n", digits);
  }
  //else
  printf("Sum of digits: %d\n", digits);

}
#包括
#包括
#包括
#包括
//原型
无效计数α(字符*s,整数长度,整数索引);
无效和数字(字符*s、整数长度、整数索引);
#定义尺寸22
内部主(空){
//声明
字符字符串[大小];
整数长度;
//从用户获取字符串
printf(“输入一串字母和数字:”);
scanf(“%s”,字符串);
printf(“字符串:%s\n”,字符串);
//获取字符串的长度
长度=strlen(字符串);
printf(“长度:%d\n”,长度);
//从字符串中获取字母
计数α(字符串,长度,0);
返回0;
}
无效计数α(字符*s,整数长度,整数索引){
静态整数字符=0;
字符c='';
如果(索引
全局声明
int characters=0
int digits=0
,将它们保持为全局与静态变量的用途相同,此外,它可以在任何地方访问,因此将帮助您在主功能中只打印一次所需内容

要将它们声明为全局的,只需在所有函数之外和程序开始时声明它们

.
.//header files
.
//#include <ctype.h>
int characters = 0;
int digits = 0;
编辑:使用指针不带全局变量

void count_alpha(char *s, int len, int index,int *charac,int *dig);
void sum_digit(char *s, int len, int index,int *charac,int *dig);
大体上:

int* charac=&characters;
int* dig=&digits;
count_alpha(string,length,0,charac,dig);
每当您看到函数调用时,都会传递以下指针:

count_alpha(string,length,0,charac,dig);
对于递增值,只需使用:

(*charac)++;
(*dig)++;
由于
alpha\u count()
sum\u digits()
正在有效地进行一些统计数据收集,请传入指向统计数据类型的指针。删除静态变量

typedef struct {
  unsigned characters;
  unsigned digits;
} char_digs;


void sum_digit(char *s, int len, int index, char_digs *stat) {
  // static int digits = 0;
  ...
  count_alpha(s, len, index, stat);
  ...
  stat->digits += c - '0';
  ...
  sum_digit(s, len, index, stat);
  ...
}      

void count_alpha(char *s, int len, int index, char_digs *stat) {
  // like sum_digit() above
}

int main(void) {
  ...
  char_digs stat = {0,0};
  ...
  count_alpha(..., ..., &stat);
  printf("Number of Characters: %u\n", stat.characters);
  printf("Sum of digits: %u\n", stat.digits);
}

呃。。。保留全局变量并在程序结束时打印?
digits+=atoi(&c)应该是
数字+=c-'0'
atoi
需要以空结尾的字符串,而不是单个字符的地址。@WeatherVane保留全局变量是什么意思?我应该在其中存储什么?每个函数中都有一个静态变量,用于跟踪递归的深度,并且只在所需级别上打印。建议使用
alpha\u count()
sum\u digits()
返回它们的总和。让调用函数,
main()
打印。有没有一种方法可以不使用全局变量进行打印?但是main不调用sum\u digits,它只调用alpha\u count。如何将SUMGIDEITY返回main?然后,必须通过引用main函数的两个变量传递……如果你想……我可以告诉你…引用是C++而不是C。所以,这个部分是无效的。这里,你使用了非标准的代码<代码>())/>代码和引用(C中不存在)。
typedef struct {
  unsigned characters;
  unsigned digits;
} char_digs;


void sum_digit(char *s, int len, int index, char_digs *stat) {
  // static int digits = 0;
  ...
  count_alpha(s, len, index, stat);
  ...
  stat->digits += c - '0';
  ...
  sum_digit(s, len, index, stat);
  ...
}      

void count_alpha(char *s, int len, int index, char_digs *stat) {
  // like sum_digit() above
}

int main(void) {
  ...
  char_digs stat = {0,0};
  ...
  count_alpha(..., ..., &stat);
  printf("Number of Characters: %u\n", stat.characters);
  printf("Sum of digits: %u\n", stat.digits);
}