如何在c中使用corecursion?

How to use corecursion in c?

我需要帮助来找到在每个函数(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);

}

全局声明 int characters = 0int digits =0,将它们保持为全局将与静态变量具有相同的用途,此外它可以在任何地方访问,因此将帮助您打印一次您想要的内容主要功能。

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

.
.//header files
.
//#include <ctype.h>
int characters = 0;
int digits = 0;

在 main() 中:打印它们

printf("Number of Characters: %d\n", characters);
printf("Sum of digits: %d\n", digits);

编辑:使用指针没有全局变量。

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_count()sum_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);
}