Chinaunix首页 | 论坛 | 博客
  • 博客访问: 344788
  • 博文数量: 82
  • 博客积分: 2602
  • 博客等级: 少校
  • 技术积分: 660
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-10 08:48
文章分类

全部博文(82)

文章存档

2008年(17)

2007年(65)

分类: C/C++

2007-09-17 11:02:21

一、需求
现有一个文件,里面存放着一些算数表达式,每个表达式占一行,并且表达式都是简单的四则运算,如下所示:
1245+589
475*4661
94124-78461
要求:构造一个函数,计算上面文件中表达式的值,然后将结果写入另一文件
二、实现,见文章结尾
 
三、总结
1、好的程序,不是字母的堆砌,而是代码的有效集合,具有很好的条理性和良好的审美性
2、代码的编写是充满挑战的,只有不畏苦难、信息充足的人才能取得最后胜利
3、代码是分层的,每一层处理的过程要注意入口参数的传递和必要的出错处理
4、功夫不负有心人,菜鸟也能脱胎换骨成凤凰,诀窍就是:坚持坚持再坚持
5、缺憾:用的变量太散乱,估计除了自己,别人很难理解(这个也算作保密措施,^_^)
四、后记
fread函数:读取数据流到指定的存储单元
size_t fread(void *ptr,size_t size,size_t nmemb,FILE *fp);
要注意以下内容:
size参数是指ptr基本存储单元的大小,nmemb是指需要的基本存储单元的数目
   size参数  nmemb参数
 struct test  sizeof(struct test)  
 char buf[]  1  
int buf[]   
 
main.h

#ifndef __MAIN_H_
#define __MAIN_H_
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<ctype.h>
#define BUFFSIZE 8192

typedef struct MY_ARRAY
{
        int first_arg;
        char sign;
        int second_arg;
}my_array;
#endif

 

 

#include"main.h"

static void deal_with_array(my_array *buf,char s[])
{
        char *pstart=NULL;
        char *pend=NULL;
        char *psign=NULL;
        psign=pstart=s;
        pend=&s[strlen(s)];
        printf("pstart:%s\n",pstart);
        char tmp1[64];
        char tmp2[64];
        int i;
        for(i=0;s[i]!='\0';i++){
                if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){
                        psign=&s[i];
                        strncpy(tmp1,s,psign-pstart+1);
                        buf->first_arg=atoi(tmp1);
                        strncpy(tmp2,psign+1,pend-psign);
                        buf->second_arg=atoi(tmp2);
                        buf->sign=s[i];
                 printf("firest:%d\tsecond:%d\tsign:%c\n",buf->first_arg,buf->second_arg,buf
->sign);
                break;
                 }
        }
}static int get_result(int arg1,char sign,int arg2)
{
        int result=0;
        switch(sign){
                case '+':result=arg1+arg2;break;
                case '-':result=arg1-arg2;break;
                case '*':result=arg1*arg2;break;
                case '/':if(arg1>=arg2)result=arg1/arg2;break;
                default:break;
        }
        return result;
}static int FileToStore(char *buf,char *file)
{
        FILE *fp;
        if(NULL==file){
                fprintf(stderr,"[%s][%d]NO file!\n",__func__,__LINE__);
                return 1;
        }
        if((fp=fopen(file,"r"))==NULL){
                fprintf(stderr,"[%s][%d]fopen error!\n",__func__,__LINE__);
                return 1;
        }
        if(fread(buf,1,BUFFSIZE,fp)==0){
                fprintf(stderr,"[%s][%d]fread error!\n",__func__,__LINE__);
                return 1;
        }
        fclose(fp);
        return 0;
}
static int StoreToFile(char *buf,int len,char *file)
{
        FILE *fp;
        if(NULL==file){
                fprintf(stderr,"[%s][%d]NO file!\n",__func__,__LINE__);
                return 1;
        }
        if((fp=fopen(file,"w+"))==NULL){
                fprintf(stderr,"[%s][%d]fopen error!\n",__func__,__LINE__);
                return 1;
        }
        if(fwrite(buf,1,len,fp)==0){
                fprintf(stderr,"[%s][%d]fwrite error!\n",__func__,__LINE__);
                return 1;
        }
        fclose(fp);
       return 0;
}
int main(int argc,char **argv)
{
        char buf[BUFFSIZE];
        char read_buf[BUFFSIZE];
        int len;
        int result;
        my_array To_store;
        char pout[64];
        int tmp=0;
        char tmp_buf[64];
        char *pp[10];
        int n=1;
        if(3!=argc){
                fprintf(stderr,"Usage:\n%s destfile sourcfile\n",argv[0]);
                exit(1);
        }
        FileToStore(read_buf,argv[2]);
        int j,k;
        pp[0]=read_buf;
        for(j=0;read_buf[j]!='\0';j++){
                if('\n'==read_buf[j]){
                        pp[n]=&read_buf[j];
                        n++;
                }
        }

        int num_line=0;
        char *pbuf=read_buf;
        while(*pbuf++!='\0'){
                        if(*pbuf=='\n')
                                num_line++;
        }
        int m=0;

        for(k=0;k<num_line;k++){
                strncpy(tmp_buf,pp[m],pp[m+1]-pp[m]);
                int s;
                m++;
                deal_with_array(&To_store,tmp_buf);
                if(!To_store.sign){
                        fprintf(stderr,"deal_with_array error!\n");
                        exit(2);
                }
                len=sizeof(To_store);
                fprintf(stderr,"[%s][%d]file1_size:[%d]",__func__,__LINE__,len);
                printf("firest:%d\tsecond:%d\tsign:%c\n",To_store.first_arg,To_store.second_
arg,To_store.sign);
                if((result= get_result(To_store.first_arg,To_store.sign,To_store.second_arg)
)==0){
                        fprintf(stderr,"get_result error!\n");
                        exit(3);
                }
                printf("Result:%d\n",result);
                sprintf(pout,"%i",result);
                strcat(buf,pout);

                strcat(buf,"\n");
                memset(&To_store,0x00,sizeof(To_store));
                memset(pout,0x00,sizeof(pout));
        }
        StoreToFile(buf,strlen(buf),argv[1]);
        exit(0);
}



阅读(1061) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~