一17 2021-04-06 15:56 采纳率: 100%
浏览 44
已采纳

《C prime plus 第六版》程序清单14.16

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define LEN 81
char* s_gets(char* st, int n);
char showmenu(void);
void eatline(void);
void show(void (*fp)(char*), char* str);
void ToUpper(char*);
void ToLower(char*);
void Transpose(char*);
void Dummy(char*);

int main(void)
{
	char line[LEN];
	char copy[LEN];
	char choice;
	void (*pfun)(char*);

	puts("Enter a string (empty line to quit:)");
	while (s_gets(line, LEN) != NULL && line[0] != '\0')
	{
		while ((choice = showmenu()) != 'n')
		{
			switch (choice)
			{ 
				case 'u': 
				pfun = ToUpper;     
				break;
				case 'l': 
				pfun = ToLower;   
				break;
				case 't': 
			    pfun = Transpose;  
				break;
				case 'o': 
				pfun = Dummy;   
				break;
			}
			strcpy_s(copy,LEN,line);
			show(pfun, copy);
		}
		puts("Enter a string (empty line to quit):");
	}
	puts("Bye!");

	return 0;
}

char showmenu(void)
{
	char ans;
	puts("Enter menu choice:");
	puts("u) uppercase          l)lowercase");
	puts("t) transposed case    o)original case");
	puts("n) next string");
	ans = getchar();
	ans = tolower(ans);
	eatline();
	while (strchr("ulton", ans) == NULL)
	{
		puts("Please enter a u,l,t,o, or n:");
		ans = tolower(getchar());
		eatline();
	}

	return ans;
}

void eatline(void)
{
	while (getchar() != '\n')
		continue;
}

void ToUpper(char* str)
{
	while (*str)
	{
		*str = toupper(*str);
		str++;
	}
}

void ToLower(char* str)
{
	while (*str)
	{
		*str = tolower(*str);
		str++;
	}
}

void Transpose(char* str)
{
	while (*str)
	{
		if (islower(*str))
			*str = toupper(*str);
		else if (isupper(*str))
			*str = tolower(*str);
		str++;
	}
}

void Dummy(char* str)
{

}

void show(void(*fp)(char*), char* str)
{
	(*fp)(str);
	puts(str);
}

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 = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return ret_val;
}

为什么报错报错43行未初始化局部变量pfun?原因是什么?

 

  • 写回答

5条回答 默认 最新

  • 星星之友 2021-04-07 02:08
    关注

    第19行:void (*pfun)(char*) = NULL;

    第41行:strncpy(copy,line, LEN);

    第112-116行:show函数

    
    void show(void(*fp)(char*), char* str)
    {
        if (fp != NULL)
        {
            (*fp)(str);
            puts(str);
        }
        else
            puts("fp is NULL\n");
    }

    完整代码

    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    #define LEN 81
    char* s_gets(char* st, int n);
    char showmenu(void);
    void eatline(void);
    void show(void (*fp)(char*), char* str);
    void ToUpper(char*);
    void ToLower(char*);
    void Transpose(char*);
    void Dummy(char*);
     
    int main(void)
    {
    	char line[LEN];
    	char copy[LEN];
    	char choice;
    	void (*pfun)(char*) = NULL;
     
    	puts("Enter a string (empty line to quit:)");
    	while (s_gets(line, LEN) != NULL && line[0] != '\0')
    	{
    		while ((choice = showmenu()) != 'n')
    		{
    			switch (choice)
    			{ 
    				case 'u': 
    				pfun = ToUpper;     
    				break;
    				case 'l': 
    				pfun = ToLower;   
    				break;
    				case 't': 
    			    pfun = Transpose;  
    				break;
    				case 'o': 
    				pfun = Dummy;   
    				break;
    			}
    			strncpy(copy,line, LEN);
    			show(pfun, copy);
    		}
    		puts("Enter a string (empty line to quit):");
    	}
    	puts("Bye!");
     
    	return 0;
    }
     
    char showmenu(void)
    {
    	char ans;
    	puts("Enter menu choice:");
    	puts("u) uppercase          l)lowercase");
    	puts("t) transposed case    o)original case");
    	puts("n) next string");
    	ans = getchar();
    	ans = tolower(ans);
    	eatline();
    	while (strchr("ulton", ans) == NULL)
    	{
    		puts("Please enter a u,l,t,o, or n:");
    		ans = tolower(getchar());
    		eatline();
    	}
     
    	return ans;
    }
     
    void eatline(void)
    {
    	while (getchar() != '\n')
    		continue;
    }
     
    void ToUpper(char* str)
    {
    	while (*str)
    	{
    		*str = toupper(*str);
    		str++;
    	}
    }
     
    void ToLower(char* str)
    {
    	while (*str)
    	{
    		*str = tolower(*str);
    		str++;
    	}
    }
     
    void Transpose(char* str)
    {
    	while (*str)
    	{
    		if (islower(*str))
    			*str = toupper(*str);
    		else if (isupper(*str))
    			*str = tolower(*str);
    		str++;
    	}
    }
     
    void Dummy(char* str)
    {
     
    }
     
    void show(void(*fp)(char*), char* str)
    {
        if (fp != NULL)
        {
            (*fp)(str);
            puts(str);
        }
        else
            puts("fp is NULL\n");
    }
     
    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 = '\0';
    		else
    			while (getchar() != '\n')
    				continue;
    	}
    	return ret_val;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答