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;
}
相关推荐
幼稚园的山代王3 分钟前
python3基础语法梳理(一)
开发语言·python
幼稚诠释青春25 分钟前
面试实例题
java·开发语言
weixin_4576653928 分钟前
C++11新标准
开发语言·c++
阿蒙Amon29 分钟前
C#封装HttpClient:HTTP请求处理最佳实践
开发语言·http·c#
大白爱琴1 小时前
使用python进行图像处理—像素级操作与图像算术(4)
开发语言·图像处理·python
laocooon5238578861 小时前
win操作系统安装C++语言开发环境之一, vscode +MinGW ,流程
c语言
奔跑吧邓邓子1 小时前
解锁Vscode:C/C++环境配置超详细指南
c语言·c++·vscode·配置指南
虾球xz2 小时前
CppCon 2015 学习:Reactive Stream Processing in Industrial IoT using DDS and Rx
开发语言·c++·物联网·学习
aischang2 小时前
统信桌面专业版如何使用python开发平台jupyter
开发语言·python·jupyter·统信uos
狐凄2 小时前
Python实例题:Python计算概率论
开发语言·python·概率论