c primer plus---例程程序纠正

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdbool.h>

char * s_gets(char* st, int n);

enum spectrum { red, orange, yellow, green, blue, violet };
const char* colors[] = { "red","orange","yellow","green","blue","violet"};

#define LEN 30

int main(void)
{
	char choice[LEN];
	enum spectrum color;
	bool color_is_found = false;

	puts("enter a color (empty line to quit):");
	while (s_gets(choice,LEN) != NULL && choice[0] != '\0')
	{
		//printf("");
		for (color = red; color <= violet; color++)
		{
		printf("%s\n", colors[color]);
		printf("%s\n", choice);
		printf("%d\n", (strcmp(choice, colors[color])));

        if (strcmp(choice, colors[color]) == 0)
             //strcmp()函数比较两个字符串是否一样大
			{
				color_is_found = true;
				break;
			}
		}
		//printf("%d\n", color_is_found);
		if (color_is_found)
		{
			switch (color)
			{
			case red:puts("roses are red.");
				break;
			case orange:puts("poppies are orange.");
				break;
			case yellow:puts("sunflowers are yellow.");
				break;
			case green:puts("grass is green.");
				break;
			case blue:puts("bluebells are blue.");
				break;
			case violet:puts("violets are violet.");
				break;
			}
		}
		else 
		    {
			printf("I don't know about the color %s.\n", choice);
		    }
		color_is_found = false;
		puts("next color,please (empty line to quit):");
	}
	puts("godbye!");
	return 0;
}

char* s_gets(char* st, int n)
{
	char* ret_val;
	char* find;
	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find= strchr(st, '\n');
		if (find)
		{
			*find = '\n';
		}
		else 
			while (getchar() != '\n')
			{
				continue;
			}
	}
	return ret_val;
}

c primer plus中14.15的例程程序,有错,不能显示正确程序效果。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdbool.h>

char * s_gets(char* st, int n);

enum spectrum { red, orange, yellow, green, blue, violet };
const char* colors[] = { "red","orange","yellow","green","blue","violet"};

#define LEN 30

int main(void)
{
	char choice[LEN];
	enum spectrum color;
	bool color_is_found = false;

	puts("enter a color (empty line to quit):");
	while (s_gets(choice,LEN) != NULL && choice[0] != '\0')
	{
		//printf("");
		for (color = red; color <= violet; color++)
		{
		printf("%s\n", colors[color]);
		printf("%s\n", choice);
		printf("%d\n", (strcmp(choice, colors[color])));

        if (strcmp(choice, colors[color]) == 1)
             //strcmp()函数比较两个字符串是否一样大
			{
				color_is_found = true;
				break;
			}
		}
		//printf("%d\n", color_is_found);
		if (color_is_found)
		{
			switch (color)
			{
			case red:puts("roses are red.");
				break;
			case orange:puts("poppies are orange.");
				break;
			case yellow:puts("sunflowers are yellow.");
				break;
			case green:puts("grass is green.");
				break;
			case blue:puts("bluebells are blue.");
				break;
			case violet:puts("violets are violet.");
				break;
			}
		}
		else 
		    {
			printf("I don't know about the color %s.\n", choice);
		    }
		color_is_found = false;
		puts("next color,please (empty line to quit):");
	}
	puts("godbye!");
	return 0;
}

char* s_gets(char* st, int n)
{
	char* ret_val;
	char* find;
	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find= strchr(st, '\n');
		if (find)
		{
			*find = '\n';
		}
		else 
			while (getchar() != '\n')
			{
				continue;
			}
	}
	return ret_val;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值