编程珠玑第三章第6题。

学校留的一个小作业,在CSDN上找答案没找到,尤其是某个答案把人骗进去了,答案写个略。个人表示强烈谴责和鄙视。那我自己写个小代码放到这给大家参考一下。我的答案不是很完美,完全是套用老师给的template和处理老师的数据。

放代码
格式信函生成器

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

#define INPUT_LENGTH 128
#define FIELD_LENGTH 30
#define NUM_FIELDS   9

int main( int argc, char *argv[] )
{
  FILE *template = NULL;
  FILE *data = NULL;

  char input[INPUT_LENGTH];                     // a string for reading from a file
  char customerData[NUM_FIELDS][FIELD_LENGTH];  // an array of strings for storing the results of a "split"
  

  // yes, we're hard-coding file names... note that this is ***unacceptable***, unless we explicitly tell you to do it
  template = fopen( "template.txt", "r" );
  if ( template != NULL )
  {
    // read in the customers until we're done
    data = fopen( "data.txt", "r" );
    if ( data != NULL )
    {
      while ( fgets( input, INPUT_LENGTH, data ) != NULL )
      {
        // INSERT PARSING CODE HERE! 
        // You're tokenizing the line and putting the data into the customerData array ala split in Java
		char *p;
    	int j=0;
    	p = strtok(input, "|");
   		while(p)
		   {
	   		strcpy(customerData[j],p);
//	    	printf("%s\n",customerData[j]);
    		j++;
    		p = strtok(NULL, "|"); 
				}
        // generate the output by reading and parsing the template
        // instead of reading it into a buffer it just re-reads the file each time
        rewind( template );
        while ( fgets( input, INPUT_LENGTH, template ) != NULL )
        {
          // INSERT PARSING CODE HERE!
          
          int cnt=0;
          for(;input[cnt];cnt++)
		  {
		  	 if(input[cnt]=='$')
            {
                cnt++;
                if(input[cnt]>='0'&&input[cnt]<='9')
                    printf("%s",customerData[input[cnt]-'0']);
            }
            else
          	printf("%c",input[cnt]);
		  }
          // Process the input one character at a time looking for variables to replace with customerData
        }
      }
      
      fclose( data );
    }

    fclose( template );
  }

  return EXIT_SUCCESS;
}

最后,说一说这个代码的不足。
没法解决**“$$1”**这样的问题。但是也很简单,再加个if判断就好了。我就是懒,完成作业即可。希望可以帮助到大家。

结束之前还是要再鄙视一下那个把我骗进去然后告诉我略的那个答案!

这几天总觉得哪里不对?原来是少放了俩文件。
第一个 data.txt

Public|Jane|Q|Ms.|600|Maple Street|Your Town|Iowa|12345
Penner|Fred|R|Mr.|123|that Street|Winnipeg|MB|R3T 2N2
Gardner|Mark|E|Mr.|76|The Avenue|Toronto|ON|M5W 1E6
Acton|Hilda|P|Mrs.|66|What Blvd|Winnipeg|MB|R3T 2N2

第二个 template.txt

Welcome back, $1!
We hope that you and all the members
of the $0 family are constantly
reminding your neighbours there
on $5 to shop with us.
As usual, we will ship your order to
$3 $1 $2. $0
$4 $5
$6, $7 $8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值