『SWU』如何读取文件中字符串的数字并将其累加和求均值

吃饱了 2014-04-11 09:35:17
题目要求,从文件中读取一个字符串,字符串中个数不定,将其中的字符读取,数字部分便累加和求平均值,要求保留三位有效数字。我个人代码如下,其中由于字符串个数不定,不知道怎么定义数组a【】和字符串str【】,对有些文件操作也不甚熟悉,望各位看官指教,定虚心以接受指导。
代码:

1 #include<stdio.h>
2 #define MAXE_LENGTH 500
3 int main()
4 {
5 FILE*fp, *fr;
6 int i, j, count, a[MAXE_LENGTH];
7 float sum, av;
8 char str[MAXE_LENGTH];
9 fp = fopen("data1.in","r");
10 fr = fopen("data2.in","w+");
11 j = 1;
12 count = 0;
13 if(fp==NULL){
14 printf("Can't open the file.");
15 return 0;
16 }
17 for(i=0;i<j;i++){
18 if (str[i]==EOF){
19 break;
20 }
21 fscanf(fp,"%c",&str[i]);
22 count++;
23 j++;
24 }
25 for(i=0;i<count-1;i++){
26 if(str[i]<'0'||str[i]>'9'){
27 str[i]=str[i]-str[i]+32;
28 }
29 }
30 for(i=0;i<count-1;i++){
31 fputc(str[i],fr);
32 }
33 for(i=0;i<j;i++){
34 if(a[i]==EOF){
35 break;
36 }
37 fscanf(fr,"%d",a[i]);
38 count++;
39 j++;
40 }
41 sum = 0;
42 for(i=0;i<count-1;i++){
43 sum = sum+a[i];

44 }
45 av = sum/count;
46
47 printf("%f\n",sum);
48 printf("%.3f",av);
49 fclose(fp);
50 fclose(fr);
51 return 0;
52 }
...全文
120 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
astro_lee 2014-04-12
  • 打赏
  • 举报
回复

list:3->1->456->123
astro_lee 2014-04-12
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

struct num_list {
    int num;
    struct num_list *next;
};

struct num_list *num_list_append(struct num_list *list, int num)
{
    struct num_list *node = (struct num_list*)calloc(1, sizeof(struct num_list));
    node->next = list;
    node->num = num;
    return node;
}

struct num_list *scan(const char *text)
{
    struct num_list *list = 0;
    int num, pos = 0;
    while (text[pos]) {
        if (isdigit(text[pos])) {
            sscanf(text+pos, "%d", &num);
            list = num_list_append(list, num);
            while (isdigit(text[pos])) {
                ++pos;
            }
        } else {
            ++pos;
        }
    }
    return list;
}

int main(void)
{
    struct num_list *list = scan("123ab456\r\n1.3");
    int sum = 0, count = 0;
    while (list) {
        sum += list->num;
        ++count;
        list = list->next;
    }
    printf("Sum: %d\n", sum);
    printf("Average: %.3f", (double)sum / count);
    return 0;
}
读取文本文件之后,数据传入scan即可,返回一个链表,每个节点包含一个数值。之后在main里遍历链表,计算总和以及平均值。
碚雅 2014-04-12
  • 打赏
  • 举报
回复
可考虑用fgets将一行读入到一个数组中,然后判断字符是否为数字,若为数字则从该位置开始sscanf。
吃饱了 2014-04-12
  • 打赏
  • 举报
回复
追加提问: 如果,字符串中数字与字母还有符号·空格夹杂在一块,如何只读取数字,并且当例如某段字符结构为w12a,其中12算位12而不是1和2,然后 当字符串不知个数怎么定义?我在去找找看怎么用
碚雅 2014-04-11
  • 打赏
  • 举报
回复
标题请用[SWU]开头,谢谢。
碚雅 2014-04-11
  • 打赏
  • 举报
回复
题目应该给定数量的上限。若没有上限则需要动态分配内存了,显然对于现阶段来说比较难一点了。 定义数组时就按上限来定义,数组中的空间未必一定要使用完。 数组的容量应该是常数,不应该是变量。C99虽然支持变长数组,但程序设计不应该依赖于C99。真正被全体C编译器支持的,只有ANSI C。想一想,某天在某种平台上开发,没有C99岂不是不能玩了? 变长数组可参考:http://www.cnblogs.com/hazir/p/variable_length_array.html
偷不得懒 2014-04-11
  • 打赏
  • 举报
回复
1 #include <stdio.h> 2 #include <ctype.h> 3 4 #define SUCCESS 0 5 #define ERROR_FAIL_OPEN_FILE 1 6 #define ERROR_INVALID_DATA 2 7 8 #define MAX_LENGTH 500 9 #define FILENAME "data.in" 10 11 int main() 12 { 13 char a[MAX_LENGTH]; 14 int i, j, ret, num, count, sum; 15 float ave; 16 FILE *fp; 17 18 // Open file 19 fp = fopen(FILENAME, "r"); 20 if(fp==NULL){ 21 fprintf(stderr, "cant't open the file\n"); 22 return ERROR_FAIL_OPEN_FILE; 23 } 24 25 // Scan 26 num = 0; 27 for(i=0; i<MAX_LENGTH; i++){ 28 ret = fscanf(fp, "%c", &a[i]); 29 if(feof(fp)){ 30 break; 31 } 32 num++; 33 // Check for valid 34 if(ret!=1){ 35 fprintf(stderr, "data invalid\n"); 36 fclose(fp); 37 return ERROR_INVALID_DATA; 38 } 39 } 40 41 // sort 42 num--; 43 count = 0; 44 sum = 0; 45 for(i=0; i<num; i++){ 46 if(isdigit(a[i])){ 47 count++; 48 sum = sum+a[i]-48; 49 } 50 } 51 52 53 // Output sorted 54 ave = (float)sum/count; 55 printf("%.3e\n", ave); 56 57 fclose(fp); 58 return SUCCESS; 59 } 自己的算法 提出来 大家一起帮忙看看。希望对你有所帮助

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧