小白入门:TCPL习题1-6 —— 1-22

  • 1-6:验证表达式getchar() != EOF 的值是0 还是 1;
#include <iostream>
#include <stdio.h>
using namespace std;
int main() 
{
	int c ;
	while(c = getchar() != EOF)
	{
		printf("%d\n" , c);
	}
	printf("%d at EOF\n" , c); 		//当文件结束时,跳出while循环输出C
	system("pause");
	return 0;
}

疑问:为什么输入1、2、3的时候c不打印
输入1,getchar返回值不是-1,getchar( ) != EOF 为真
那c不是应该等于1吗,执行while语句
matlab写习惯了,while多了个;)
在这里插入图片描述

  • 1-7:编写一个打印EOF的程序
#include <iostream>
#include <stdio.h>
using namespace std;
int main() 
{
	int c ;
	c = EOF;				//将EOF赋值给变量c;
	printf("%d\n" , c);
	system("pause");
	return 0;
}

  • 1-8:编写一个统计空格、制表符与换行符个数的程序
#include <iostream>
#include <stdio.h>
using namespace std;
int main() 
{
	int c , count1 = 0 ; //空格;
	int count2 = 0 ; //制表符
	int count3 = 0 ; //换行符;
	while((c = getchar()) != EOF)
	{
		if(c == ' ') 	count1++;	
	    if (c == '\t')	count2++;	
		if (c == '\n')	count3++;	
	}
	printf("%d\t%d\t%d\n" , count1 , count2 , count3);
	system("pause");
	return 0;
}
  • 1-9 编写一个将输入复制到输出的函数,并将其中连续的多个空格用一个空格代替
#include <iostream>
#include <stdio.h>
using namespace std;
int main() 
{
	int CBefore , c ;	//cbefore为前一个字符 , t判断输入的字符是不是第一个

		c = getchar();	//输入第一个字符
		putchar(c);		//输出第一个字符
		CBefore = c;	//将第一个字符赋给CBfore
		while ((c = getchar()) != EOF)
		{
			if(c == ' ' && CBefore == ' ') continue;	//新输入字符与上一个都空格,跳过
			else 
			{
				putchar(c);
				CBefore = c;
			}

		}

	system("pause");
	return 0;
}

代码优化

#include <iostream>
#include <stdio.h>
using namespace std;
#define NON 'a'
int main() 
{
	int CBefore , c ;	//cbefore为前一个字符 , t判断输入的字符是不是第一个

	CBefore = NON;		//CBfore初始化为非空格字符
	while ((c = getchar()) != EOF)
	{
		if(c != ' ' || CBefore != ' ')	//若c是一行空格串里的第一个空格
		putchar(c);
		CBefore = c;
	}
		
	system("pause");
	return 0;
}

因为第一个字符不管是不是空格都会输出,所以将CBfore初始化为非空格即可。

  • 1-10:编写一个将输入复制到输出的函数,将制表符替换为\t,回退符替换为\b,反斜杠换为\。
#include <iostream>
#include <stdio.h>
using namespace std;
int main() 
{
	while ((c = getchar()) != EOF)
	{
		if (c == '\t')
			printf("\\t");		//printf("\\")-->输出 \ ;
		else if (c == '\b')		
			printf("\\b");
		else if (c == '\\')
			printf("\\\\");
		else
			putchar(c);
	}
	system("pause");
	return 0;
}
  • 1-12:编写一个程序,以每行一个词的形式打印其输入
#include <iostream>
#include <stdio.h>
using namespace std;
//以每行一个词的形式打印其输入
#define in 1	//在单词内
#define out 0	//在单词外
int main() 
{
	int c , state ;
	state = out ;  
	while ((c = getchar()) != EOF)
	{
		if(c == ' ' || c == '\t'  || c == '\n')	//判断是不是单词分隔符
		{
			if(state = in)
			{
				printf("\n");		//输出换行符,并将state初始化
				state = out;
			}
		}
			else if(state = out)
			{
				putchar(c);			//输出单词的第一个字母
				state = in;
			}
			else
			{
				putchar(c);			//输出单词的其他字母
			}
		
	}
	
	system("pause");
	return 0;
}
  • 1-13:编写一个程序,打印输入中单词长度的直方图。

水平方向

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//编写一个程序,打印输入中单词长度的直方图(水平)
int main() 
{
	int number[20] , i = 0 , c , state;
	memset(number , 0 ,sizeof(number));		//数组初始化
	while ((c = getchar()) != EOF)
	{
		if(c != ' ' && c != '\n')
			++i;
		else
		{
			number[i - 1] = number[i - 1] + 1;	//记录频率
			i = 0;
		}
	}
	printf("\t水平直方图\n");
	for(i = 0 ; i < 20 ; i++)
	{
		printf("%-3d" , i + 1); //打印长度
		for(int j = 0 ; j < number[i] ; j++)
		{
			printf("*");
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

竖直方向

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//编写一个程序,打印输入中单词长度的直方图(竖直)
int main() 
{
	int number[20] , i = 0 , c , state;
	memset(number , 0 ,sizeof(number));		//数组初始化
	while ((c = getchar()) != EOF)
	{
		if(c != ' ' && c != '\n')
			++i;
		else
		{
			number[i - 1] = number[i - 1] + 1;	//记录频率
			i = 0;
		}
	}
	printf("\t水平直方图\n");
	for(i = 20 ; i > 0 ; i--)
	{
		for(int j = 0 ; j < 20 ; j++)	//每行每行检测并打印
		{
			if(number[j] == i)
			{
				printf("  *");
				number[j]--;
			}
			else
			{
				printf("   ");
			}
		}
		printf("\n");
	}
	for(i = 0 ; i < 20 ; i++)
	{
		printf("%3d" , i + 1);			//输出横坐标
	}
	printf(" ");
	system("pause");
	return 0;
}
  • 1-15:使用函数数显温度准换
#include <iostream>
#include <stdio.h>
using namespace std;
//使用函数实现温度转换计算
float celsius_to_fahr (float x)	//摄氏度转化为华氏度
{
	return x*(9.0/5.0) + 32;
}
float fahr_to_celsius (float x)	//华氏度转化为摄氏度
{
	return (x - 32)*(5.0/9.0);
}
int main() 
{
	float celsius , fahr;
	while (cin >> celsius >> fahr;)
	{
		printf("%.3f\n" , celsius_to_fahr(celsius)) ;	
		printf("%.3f\n" , fahr_to_celsius(fahr)) ;
	}
	
	system("pause");
	return 0;
}
  • 1-16:可以打印任意长度的输入行的长度,并尽可能多的打印文本
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int getline(char s[]);
int main()
{
	 int c , i ;
	 char line[100010];
	 while ((c = getline(line)) > 0)
	 {
		printf("%d\t%s\n" , c , line);
	 }
	 system("pause");
	 return 0 ;
}
//getline函数:储存字符串并返回其长度;
int getline(char s[])
{
	int c , i;
	for(i=0 ; (c = getchar()) != EOF && c != '\n' ; ++i)
		s[i] = c;
		if(c == '\n')
		{
			s[i] = c;
			++i;
		}
		s[i] = '\0';
		return i ; 
}
  • 1-17:打印长度大于80的所有输入行
while ((c = getline(line)) > 0)
	 {
		printf("%d\t%s\n" , c , line);
	 }

替换为

while ((c = getline(line)) > 0)
	 {
		if(c > 80) printf("%-5d%s\n" , c , line);
	 }
  • 1-18:删除每个输入行末尾的空格和制表符,并删除完全是空格的行;
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int getline(char s[]);
int chick(char s[] , int length);
int main()
{
	int c , length , i;
	char line[150+10];
	while((length = getline(line)) > 0)
	{
		i = chick(line , length);
		if(i == 0) continue;
		else
		{
			printf("%-5d%s\n" , i , line);

		}
		
	}
}
//getline函数:储存字符串并返回其长度;
int getline(char s[])
{
	int c , i;
	for(i=0 ; (c = getchar()) != EOF && c != '\n' ; ++i)
		s[i] = c;
		if(c == '\n')
		{
			s[i] = c;
			++i;
		}
		s[i] = '\0';
		return i ; 
}
//chick函数:删除末尾的空格和\t,返回删除后的长度
int chick(char s[] , int length)
{
	int i , flag ; 
	i = length-2;//i初始化;
	while (s[i] == ' ' || s[i] == '\t')//从末尾查询,找到不是空格和\t的字符;
	{
		--i;
	}
	flag = i;
	s[flag+1] = '\0';
	return i+1;
}
  • 1-19:编写revers(s),将字符串s的顺序颠倒过来
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int getline(char s[]);
void reverse(char from[]);
int main()
{
	int c , length , i;
	char line[150+10] , NewLine[150+10];
	while((length = getline(line)) > 0)
	{
		reverse(line);
		printf("%s" , line);
	}
}
//getline函数:储存字符串并返回其长度;
int getline(char s[])
{
	int c , i;
	for(i=0 ; (c = getchar()) != EOF && c != '\n' ; ++i)
		s[i] = c;
		if(c == '\n')
		{
			s[i] = c;
			++i;
		}
		s[i] = '\0';
		return i ; 
}
//reverse函数:将字符串s顺序颠倒过来
void reverse(char s[])
{
	int i , j;
	char temp;
	i = 0;
	while(s[i] != '\0')
		i++;
	i--;
	if(s[i] == '\n')
		i--;
	j = 0;
	while(j < i)
	{
		temp = s[i] ;
		s[i] = s[j] ;
		s[j] = temp ;
		j++ ;
		i-- ;
	}
}
  • 1-20:将输入中的制表符替换成适当数目的空格 ,使空格充满到下一个制表符止位的地方。假设制表符终止位的位置是固定的,比如每个n列就会出现一个制表符终止位。n应该作为变量还是符号常量呢?
    (花了好久才看懂题目)
例如定Tab空格数为3
输入 "hellow\tHHH"
一般是输出"hellow        HHH"//8个空格
但按题目的意思是输出"hellow   HHH"
因为
"hellow---HHH"
 ^  ^  ^  ^
 -----------------------------------------------------------------
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define TABINC 3
int main()
{
	int c , nb , pos;
	//necessary blank , position of character in line;
	pos = 0;
	while ((c = getchar()) != EOF)
	{
		if(c == '\t')
		{
			nb = TABINC - (pos % TABINC);
			while (nb > 0)
			{
				printf("-");
				nb--;
				pos++;
			}

		}
		else if(c == '\n') 
		{
			putchar(c);
			pos = 0;
		}
		else
		{
			putchar(c);
			pos++;
		}
	}
	system("pause");
	return 0;
}
}//宏定义不用加‘;’

  • 1-21:题目读不懂
  • 1-22:把较长的输入行“折成”短一些的两行或多行(只写了一个条件)
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int getline(char s[]);
void change(char s1[] , char s2[] , int changepos);
int main()
{
	int c , changepos , length;
	char line[100] , line2[100]; 
	printf("清输入changepos\t");
	scanf("%d" , &changepos); // the position of changing;
	getchar();

	while ((length = getline(line)) > 0)
	{
		change(line , line2 , changepos);
		printf("%s\n%s" , line , line2);
	}
	system("pause");
	return 0;
}
//getline函数:存储字符串(包含换行符)
int getline(char s[])
{
	int i , c;
	i = 0;
	for(i=0 ; (c = getchar()) != EOF && c != '\n' ; i++)
		s[i] = c;
	if(c == '\n')
		s[i] = '\n';
	i++;
	s[i] = '\0';
	return i;
}
//change函数:对字符串进行“折”处理
void change(char s1[] , char s2[] , int changepos )
{
	int flag , i , j , pos;
	i = 0;
	while(s1[i] != '\0')
		i++; //确定字符串的长度
	flag = changepos;
	while(s1[flag - 1] == ' ')
	{
		-- flag;
	}
	pos = flag ;
	for(j=0 ; flag<=i ; j++)
	{
		s2[j] = s1[flag];
		flag ++;
	}
	s1[pos] = '\0' ; //分行
	s2[flag] = '\0';
	
}

23 24题蛮麻烦的,就先不啃了😂

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值