文件操作(升级)

计算字符串“25 + 32 = ”

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

int  calc_string(char *s)
{
    char buf1[100] = {0};
    char oper = 0;
    char buf2[100] = {0};

    int len =  strlen(s);
    int i;
    for(i  = 0; i < len; i++)
    {
        if( '+' == s[i] || '-' == s[i] || '*' == s[i] || '/' == s[i] )
        {
            strncpy(buf1,s,i);
            oper = s[i];
            break;
        }
    }

    int start = i + 1;
    for(; i < len; i++)
    {
        if(s[i] == '=')
        {
            strncpy(buf2,&s[start ],i - start);
        }
    }

    printf("buf1 = %s,oper = %c,buf2 = %s
",buf1,oper,buf2);

    int a = atoi(buf1);
    int b = atoi(buf2);

    switch(oper)
    {
        case '+':
            return  a+b ;
        case '-':
            return a-b;
        case '*':
            return a*b;
        case '/':
            if(a)
                return a/b;
            else    
                return 0;
    }
        
}

int main()
{
    char *s = "25 + 46 = ";
    printf("%d
",calc_string(s));
    
    return 0;
}

计算文本文件中的计算:

/***
a.txt
***/
23 + 45 = 
45 * 12 =
56 / 2 = 
45- 12 =
/***
calc.c
***/
#include<stdio.h>
#include<string.h>

int  calc_string(char *s)
{
    char buf1[100] = {0};
    char oper = 0;
    char buf2[100] = {0};

    int len =  strlen(s);
    int i;
    for(i  = 0; i < len; i++)
    {
        if( '+' == s[i] || '-' == s[i] || '*' == s[i] || '/' == s[i] )
        {
            strncpy(buf1,s,i);
            oper = s[i];
            break;
        }
    }

    int start = i + 1;
    for(; i < len; i++)
    {
        if(s[i] == '=')
        {
            strncpy(buf2,&s[start ],i - start);
        }
    }

    //printf("buf1 = %s,oper = %c,buf2 = %s
",buf1,oper,buf2);

    int a = atoi(buf1);
    int b = atoi(buf2);

    switch(oper)
    {
        case '+':
            return  a+b ;
        case '-':
            return a-b;
        case '*':
            return a*b;
        case '/':
            if(a)
                return a/b;
            else    
                return 0;
    }
        
}

void cuterrturn(char *s)
{
    int len = strlen(s);
    if('
' == s[len - 1])
    {
        s[len - 1] = 0;
    }
}

int main()
{
    FILE *p = fopen("./a.txt","r");
    FILE *p1 = fopen("./b.txt","w");

    char buf[1024];
    char buf1[1024];
    while(!feof(p))
    {
        memset(buf,0,sizeof(buf));
        fgets(buf,sizeof(buf),p);  //从文件中读一行记录,字符串最后是以’
’结尾
        cuterrturn(buf);            //吃掉从文件每行读出来的换行符
        int value = calc_string(buf);
        memset(buf1,0,sizeof(buf));
        sprintf(buf1,"%s%d
",buf,value);
        fputs(buf1,p1);
    }
    fclose(p);
    fclose(p1);
    
    return 0;
}

运行结果;

运行结果;
b.txt
23 + 45 = 68
45 * 12 =540
56 / 2 = 28
45- 12 =33
0

分析:最后面多一个0是因为用feof判断是否到达文件结尾会多循环一次。

strncpy()函数:将指定长度的字符串复制到字符数组中

语法:

char *strncpy(char *destinin, char *source, int maxlen);

参数:

destinin:表示复制的目标字符数组;

source:表示复制的源字符数组;

maxlen:表示复制的字符串长度。

原文地址:https://www.cnblogs.com/wanghao-boke/p/11209356.html