C语言程序设计第一章部分习题

作为一个程序员一直不会C程序设计语言,觉得缺点什么,所以最近在学习C ,会定期分享学习心得,最近刚看完第一章,部分习题供大家参考,欢迎指正。

 C程序设计语言 下载地址

提取码: nbnb

#include "Temperature.h"


Temperature::Temperature(void)
{
}


Temperature::~Temperature(void)
{
}

void statisticsEmptyLineTablesNum(){
	int emptyChar = 0;
	int changeLineChar = 0;
	int tableChar = 0;
	int c;
	bool slash = false;
	while ((c = getchar())!=EOF){

		if (c == 10){
			printf("emptyChar %d changeLineChar %d tableChar %d", emptyChar, changeLineChar, tableChar);
			return;
		} else if(c == ' '){
			emptyChar ++;
		}else if(slash == 1) {
			switch(c){
			case 'n':
				changeLineChar ++;
				break;
			case 't':
				tableChar ++;
				break;
			}
		}
		slash = c == '\\';
	}
}


/*
 *只显示一个空格
 */
void practice1_9_only_one_empty(){
	printf("输入的多个空格字符只会保留一个\n");
	bool lastEmpty = false;
	int c;

	while((c = getchar()) != EOF){
		bool charIsEmpty = c == ' ';
		if(!lastEmpty || !charIsEmpty){
			putchar(c);
		}
		lastEmpty = charIsEmpty;
	}
}

void practice1_10(){
	printf("输入的多个制表符用\\t替换\n");
	bool lastEmpty = false;
	char s;
	int c;

	while((c = getchar()) != EOF){
		char cha;
		if(c == '\t'){
			putchar('\\');
			c = 't';
		}
		putchar(c);
		
	}
}

/*
 * 一行打印一个单词
 */
void practice1_12(){
	printf("输入的多个单词将逐行打印且一行只会有一个单词\n");
	bool isLastEnd = false; // 最后一个字符是否是结束字符
	int lastc;
	while ((lastc = getchar()) != EOF) {
		if (lastc == ' ' || lastc == '\n' || lastc =='\t'){
			if (!isLastEnd) 
				printf("\n");	
			isLastEnd = true;
		} else {
			isLastEnd = false;
			putchar(lastc);
		}
	}
	printf("\n End print");
}

void printVertical(bool isPrintFromat, int countPosition, int countArray[]){
	if(isPrintFromat)
		printf("\n打印格式\n");
	else {
		printf("\n不打印格式\n");
	}
	int maxCount = 0;
	for (int i = 0; i < countPosition; i ++)
	{
		if (maxCount < countArray[i])
			maxCount = countArray[i];
	}
	for (int i = 0; i < countPosition; i ++) {
		printf("%2d", countPosition - i);
		int showNum = countArray[i]; // 输出白色背景个数
		int showStart = (maxCount - showNum) / 2; // 左边输出的空格
		for (int j = 0; j < maxCount; j ++) {
			if (j > showStart -1 && showNum > 0){
				printf("C");
				showNum --;
			} else if(showNum > 0 && isPrintFromat) {
				printf(" ");
			} else if( showNum == 0) {
				break;
			}

		}
		
		printf("\n");
	}
	printf("  ");
	for (int i = 0; i < maxCount; i ++)
	{
		printf("%d", i + 1);
	}
}

void practice1_13() {
	printf("start printf\n");
	bool isLastEnd = false;
	int count, countPosition;
	int lastc;
	int countArray[10];
	count = countPosition = 0;
	for (int i =0; i < 10; i ++) 
		countArray[i] = 0;
	
	while ((lastc = getchar()) != EOF && lastc != 'E') 
	{
		if (lastc == ' ' || lastc == '\n' || lastc =='\t') {
			if (!isLastEnd) {
				countArray[countPosition] = count;
				printf("\n%d", count);
				count = 0;
				countPosition ++;
			}					
			isLastEnd = true;
		} else {
			isLastEnd = false;
			// putchar(lastc);
			count ++;
		}
	}
	printf("\n");
	printVertical(false, countPosition, countArray);
	printVertical(true, countPosition, countArray);
	printf("end printf");
}

void practice1_17();
void practice1_18();
void practice1_19();
void reverse(char string[], int len);
int getline(char string[]);

void main()
#define UPPER 300
#define LOWER 0
#define STEP 20
#define MAXLINE 500
{
	printf("start print\n");
	practice1_19();
	printf("\nend print");
}
void practice1_17(){
	char contents[MAXLINE];
	int len = 0;
	while((len = getline(contents)) > 80){
		printf("\n支付串长度%4d", len);
		printf("\n输入的字符串%s", contents);
		printf("\n%s\t%4d", contents, len);
	}
}
void practice1_18(){
	char contents[MAXLINE];

	int len = 0;
	
	len = getline(contents);
	len --;
	printf("\n输入的字符%s\t数量%4d", contents, len);
	char lastCha;
	int emptyChar, tableChar;
	emptyChar = tableChar = 0;
	for (; len > 0; len --)
	{
		lastCha = contents[len-1];
		if(lastCha == ' '){
			emptyChar ++;
			contents[len] = '\0';
		}else if (lastCha == '\t')
		{
			tableChar ++;
			contents[len] = '\0';
		}else {
			break;
		}
	}
	printf("\n末尾空格数量%3d\t末尾制表符数量", emptyChar, tableChar);
	
	printf("\n过滤后的字符%s\t数量%4d", contents, len);

}

void practice1_19(){
	int c;
	char string[MAXLINE];
	int index = 0;
	while((c = getchar()) != EOF && c != '\n'){
		string[index] = c;
		index ++;
	}
	index --;
	reverse(string, index);
}

void reverse(char string[], int len) {
	for (; len >= 0; len --)
		printf("%c", string[len]);
}

int getline(char string[]){
	int c, i;
	for (i = 0; (c = getchar()) != EOF && c != '\n'; i ++)
		string[i] = c;
	if (c == '\n'){
		string[i] = c;
		i++;
	}
	string[i] = '\0';
	return i;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值