第八章习题

习题2:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main(void)
{
char ch;
int num = 0;

while ((ch = getchar()) != EOF)
{
	num++;
	if (ch < ' ')
	{
		if (ch == '\n')
		{
			putchar('\\');
			printf("n:%d\n", ch);
			num = 0;
		}
		else if (ch == '\t')
		{
			putchar('\\');
			printf("t:%d ", ch);
		}
		else
		{
			putchar('^');
			putchar(ch + 64);
			printf(":%d ", ch);
		}
	}
	else
	{
		putchar(ch);
		printf(":%d ", ch);
	}
	if ((num % 10 == 0) && num != 0)
		putchar('\n');
}

return 0;

}
疑问:
(printf(“n:%d**\n**”, ch);
num = 0;) 书上有这样一句话(除每次遇到换行符打印新的一行外,每行打印10对值) 我理解的意思:如果没遇到换行符,直接打印10对值,一旦遇到‘\n’,就立马打印下一行,不管上一行有没有打印10对值(也许就打印了3对值,遇到了换行符,这一行就打印3对值了)。所以我加了一个\n和num = 0;如果不加,感觉达不到这意思了。不知道我讲的对不对 ,楼主看下 谢谢

习题四、
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
int main(void)
{
char ch;
int n = 0;
int m = 0;
double val;
bool inword = false;

while ((ch = getchar()) != EOF)
{
	if (isalpha(ch))
		n++;
	if ((!ispunct(ch) && !isspace(ch)) && !inword)//确保统计从字母开始(空格和符号开始都不行)
	{
		m++;
		inword = true;
	}
	if ((ispunct(ch) || isspace(ch)) && inword)//确保这个单词从空格或者符号(例如逗号)处结束
		inword = false;
}
val = (double)n / (double)m;
printf("n=%d m=%d val=%.2f\n", n,m,val);

return 0;

}

问题2:我这样写可不可以?

习题八

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
char get_first(void);
void add(void);
void subtract(void);
void multiply(void);
void divide(void);
float first_num(void);
float second_num(void);
int main(void)
{
char ch;

while ((ch = get_first()) != 'q')
{
	switch (ch)
	{
	case 'a':add();
		break;
	case 's':subtract();
		break;
	case 'm':multiply();
		break;
	case 'd':divide();
		break;
	case 'q':
		return;
	default:
		printf("Please enter the choice a or s or m or d or q.\n");
		continue;
	}

}
return 0;

}

char get_first(void)
{
char ch;

printf("Enter the operation of your choice : \n");
printf("a.add                 s.subtract\n");
printf("m.multiply            d.divide\n");
printf("q.quit\n");

ch = getchar();
while (getchar() != '\n')
	continue;

return ch;

}

void add(void)
{
float first_number, second_number, result_number;

first_number = first_num();
second_number = second_num();
result_number = first_number + second_number;
printf("%.1f+%.1f=%.1f\n", first_number, second_number, result_number);

}

void subtract(void)
{
float first_number, second_number, result_number;

first_number = first_num();
second_number = second_num();
result_number = first_number - second_number;
printf("%.1f-%.1f=%.1f\n", first_number, second_number, result_number);

}

void multiply(void)
{
float first_number, second_number, result_number;

first_number = first_num();
second_number = second_num();
result_number = first_number * second_number;
printf("%.1f*%.1f=%.1f\n", first_number, second_number, result_number);

}

void divide(void)
{
float first_number, second_number, result_number;

first_number = first_num();
second_number = second_num();
if (second_number == 0)
{
	printf("Enter a number other than 0: ");
	second_number = second_num();
}
result_number = first_number / second_number;
printf("%.1f/%.1f=%.1f\n", first_number, second_number, result_number);

}

float first_num(void)
{
float first_number;
char ch;

printf("Enter first number:");
while (scanf("%f", &first_number) != 1)
{
	while ((ch = getchar()) != '\n')
		putchar(ch);
	printf(" is not an number.\n");
	printf("Please enter a number, such as 2.5,-1.78E8,or 3: ");
}
while (getchar() != '\n')
	continue;

return first_number;

}

float second_num(void)
{
float second_number;
char ch;

printf("Enter second number:");
while (scanf("%f", &second_number) != 1)
{
	while((ch=getchar())!='\n')
		putchar(ch);
	printf(" is not an number.\n");
	printf("Please enter a number, such as 2.5,-1.78E8,or 3: ");
}
while (getchar() != '\n')
	continue;

return second_number;

}
问题,楼主你写的我看不怎么懂 ,这个也是我在网上找的答案,我感觉这个比较好,模块化,很容易懂,但是有个问题,就是最后输入second——number=0的时候,和书上显示出来就不一样了,多了一行提示,楼主能把这个给解决下么

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值