【Linux C】作业:文本相加/格子中输出

 文本相加

实现在text1.txt和text2.txt文件中除去首行和末尾对应的数据,要求三个文本内容如下:

   text1                         text2                        text3
   begin                        begin                       begin
   10 11 12                  15 16 17                   25 27 29
   20 21 22                  25 26 27                   45 47 49
   30 31 32                  35 36 37                   65 67 69
   end                          end                           end
 

#include <stdio.h>
#include <errno.h>

FILE* fopen_text (const char *, const char *);//打开文件
int fputc_text(const int ,FILE*); //写入字符


int main()
{
    FILE *fp_text1 = fopen_text ("text1.txt", "r");
    FILE *fp_text2 = fopen_text ("text2.txt", "r");
    FILE *fp_text3 = fopen_text ("text3.txt", "w+");

    char ch1;
    char ch2;
    char ch3;

	while(1)
	{
		ch1 = fgetc(fp_text1);
		ch2 = fgetc(fp_text2);

		if(ch1 == EOF || ch2 == EOF)
		{
			break;
		}
		
		if (ch1 >= '0' && ch1 <= '9')       
        {  
            ch3 = ch1 + ch2 - '0';  
            fputc_text(ch3,fp_text3);  
        }  
        else 
        {  
            fputc_text(ch1,fp_text3);  
        }   
	}
		
    fclose(fp_text1);
    fclose(fp_text2);
    fclose(fp_text3);

    return 0;
}

FILE* fopen_text (const char *path, const char *mode)
{
    FILE* fp = fopen (path, mode);
    if (NULL == fp)
    {
        perror ("fopen fail:");
		return;
    }

    return fp;
} 

int fputc_text(const int ch,FILE* fp)
{
	int ret = fputc(ch,fp);
	if(ret == EOF)
	{
		perror ("fputc fail:");
		return;
	}
	return ret;
}

 步骤:

  1. 定义3个FILE*指针,fp_text1和fp_text2分别用于打开text1.txt和text2.txt文件
  2. 定义3个char变量,读入的字符ch1,ch2和相加后存入的字符ch3
  3. 定义了两个函数,其中fopen_text()函数用于打开文件,并判断是否打开成功。fputc_text()函数写入字符,并判断是否写入成功。(本来还定义了一个fgetc_text()函数用于读入字符,但是循环中需要判断是否读到文件尾,再封装成一个函数的话比较麻烦,就删掉了)
  4. while(1)死循环,让循环体不断的进行操作,直到读到文件尾为止
  5. 判断文件中的字符是否为数字'0' ~'9',是则进行相加操作,并将ch3写入text3.txt中;不是则原样写入text3.txt中
  6. 关闭3个文件

格子中输出 (作业/来源于:2015年蓝桥杯B组)

/*
题目:格子中输出

StringInGrid函数会在一个指定大小的格子中打印指定的字符串。
要求字符串在水平、垂直两个方向上都居中。
如果字符串太长,就截断。
如果不能恰好居中,可以稍稍偏左或者偏上一点。

下面的程序实现这个逻辑,请填写划线部分缺少的代码。
*/

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

void StringInGrid(int width, int height, const char* s)
{
	int i,k;
	char buf[1000];
	strcpy(buf, s);
	if(strlen(s)>width-2) buf[width-2]=0;
	
	printf("+");
	for(i=0;i<width-2;i++) printf("-");
	printf("+\n");
	
	for(k=1; k<(height-1)/2;k++){
		printf("|");
		for(i=0;i<width-2;i++) printf(" ");
		printf("|\n");
	}
	
	printf("|");
	
	printf("%*s%s%*s",_____________________________________________);  //填空
	          
	printf("|\n");
	
	for(k=(height-1)/2+1; k<height-1; k++){
		printf("|");
		for(i=0;i<width-2;i++) printf(" ");
		printf("|\n");
	}	
	
	printf("+");
	for(i=0;i<width-2;i++) printf("-");
	printf("+\n");	
}

int main()
{
	StringInGrid(20,6,"abcd1234");
	return 0;
}


/*
对于题目中数据,应该输出:
+------------------+
|                  |
|     abcd1234     |
|                  |
|                  |
+------------------+
*/

填空的地方就是输出字符串的地方

而“%*s”:在scanf里用*修饰符,是起到过滤读入的作用;在printf中的含义完全不同,是代表设置域宽的意思,* 修饰符用来灵活的控制域宽

由题意可知要字符串要居中,去掉首尾的“|”再分左边和右边,域宽即为“(width - strlen(s) ) / 2 - 1”


猜你喜欢

转载自blog.csdn.net/qq_42417182/article/details/88970108