如何编写一个计算文件中有多少个数字的程序

时间:2018-12-27 10:27:03

标签: c

我需要编写一个C程序来计算一个文件中有多少个数字,假设下面有一个给定的文件

文件:

 Lorem ipsum dolor sit amet 103 consectetur adipiscing elit. 103.55 
 Phasellus nec neque posuere 103.55e-67 nulla sagittis efficitur.

输出:

There are 3 numbers in file.

分别是103、103.55和103.55e-67。

我了解在C语言中,通过迭代到EOF,我可以使用fgetc()逐字符读取字符。如上输出如何获得数字序列。

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

int main()
{
  FILE * file;
  char path[100];

  char ch;
  int numbers[200];
  int number_count;



  printf("Enter source file path: ");
  scanf("%s", path);


  file = fopen(path, "r");



  if (file == NULL)
  {
    printf("\nUnable to open file.\n");
    printf("Please check if file exists and you have read privilege.\n");

    exit(EXIT_FAILURE);
  }



  /* Finding and counting numbers */

  while ((ch = fgetc(file)) != EOF){

    // What logic do i write here??

  } 


  printf("The file has %d numbers.\n", number_count);

  return 0;
}

3 个答案:

答案 0 :(得分:0)

尝试一下:

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

int main()
{
    FILE * file = stdin;
    int number_count = 0;

    for (;;) {
        // scan a long double number
        long double tmp;
        if (fscanf(file, "%lf", &tmp) == 1) {
            number_count++;
            // find two numbers one after another
            continue;
        }
        // discard data until whitespace
        // this will also break on end of file
        if (fscanf(file, "%*s") == EOF) {
            break;
        }
    }

    printf("The file has %d numbers.\n", number_count);
    return 0;
}
  • 我将fscanf"%lf" scanf修饰符一起使用以扫描单个数字
  • 如果成功扫描了号码,则计数器会增加,并会重复发出提示音
  • 如果未扫描任何数字,我们将跳过一个字符串(*中的"%*s"会丢弃该字符串)
  • 如果scanf返回EOF,则表示遇到了文件结尾,我们应该进行计数

答案 1 :(得分:0)

我需要的是这种实现方式。

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

void count_numbers(){

    FILE * file;  
    char c, str[1000], path[100]; 

    int number_count = 0, i=0, check=0, state = 12;

    printf("Enter source file path: ");
    scanf("%s", path);

    /* Open source file in 'r' mode */
    file = stdin;
    file = fopen(path, "r");

    /* Check if file opened successfully */
    if (file == NULL)
    {
        printf("\nUnable to open file.\n");
        printf("Please check if file exists and you have read privilege.\n");

        exit(EXIT_FAILURE);
    }

    /* Read file content, store characters to a pointer str, and counting numbers */
    fread(str, 1000, 1000 ,file);
    int leng = strlen(str);

    while(check==0){
        if(i==(leng+1))
            check = 1;

        switch (state){
            case 12: c = str[i];
                if(isdigit(c)) { state = 13; i++; }
                else { state = 12; i++; }
                break;
            case 13: c = str[i];
                if(isdigit(c)) { state = 13; i++; }
                else if(c == '.') { state = 14; i++; }
                else if (c == 'E' || c == 'e') { state = 16; i++; }
                else {state = 20; number_count++; i++; }
                break;
            case 14: c = str[i];
                if(isdigit(c)) { state = 15; i++; }
                else{ state = 0; number_count++; i++; }
                break;
            case 15: c = str[i];
                if(isdigit(c)) { state = 15; i++; }
                else if (c == 'E' || c == 'e') { state = 16; i++; }
                else {state = 21; number_count++; i++; }
                break;
            case 16: c = str[i];
                if (c == '+' || c == '-') { state = 17; i++; }
                else if(isdigit(c)) { state = 18; i++; }
                break;            
            case 17: c = str[i];
                if(isdigit(c)) { state = 18; i++; }
                break;
            case 18: c = str[i];
                if(isdigit(c)) { state = 18; i++; }
                else { state = 19; number_count++; i++; }
                break;
            case 19: state = 12;
                break;        
            case 20: state = 12;
                break;       
            case 21: state = 12;
                break; 
        }
    }

    /* Close source file */
    fclose(file);

    /* Print the count value of numbers obtained from the source file */
    printf("\n Number of numbers is %d \n", number_count);
}

int main(){
    count_numbers();
    return 0;
}

答案 2 :(得分:0)

只需检查您从文件中读取的字符是否在数字的ascii值范围内即可;即; 0 = 48,9 = 57(ASCII格式)。如果是,则只需增加num count的值

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

int main()
{
  FILE * file;
  char path[100];

  char ch;
  int numbers[200];
  int number_count;



  printf("Enter source file path: ");
  scanf("%s", path);


  file = fopen(path, "r");



  if (file == NULL)
  {
    printf("\nUnable to open file.\n");
    printf("Please check if file exists and you have read privilege.\n");

    exit(EXIT_FAILURE);
  }



  /* Finding and counting numbers */
  number_count = 0;
  while ((ch = fgetc(file)) != EOF){

    if(ch >= 48 && ch <=  57)
    {
       number_count++;
     }

  }     



  printf("The file has %d numbers.\n", number_count);

  return 0;
}