C Primer Plus 第五章

这次写这段代码,刚开始不是很顺利,主要是卡在了输入过滤上。后来和同学讨论之后,找到了解决办法。用scanf输入的类型不同返回值不同,从而决定是否继续输入。

while(scanf("%d",&num)==1)

{

……

}

scanf读取和自己类型匹配的字符会返回1,否则返回0.

下面是要求:用户输入一个华氏温度,程序以double形式读入,再转换为三种形式输出。


/**********************************
Function:		P114_8_1()
Description: 静态数组实现输入内容读取,转换为三种温度.
				   函数名称为页号(P114),加题号或例题号(_8),加方法类型(_2)
Calls:	           void Tempperatures(double Fah);
                   atof(str);
				   strlen(str);
Input:           flag:程序异常标志位。
				   a:将要被处理的气温。
				   i:  数组位数标记。
OutPut:       无。
Return:		   无。
**********************************/
void P114_8_1()
{
	puts("Input the temperature !\n");
	int i=0,flag=1;
	double a;
	while(1)
	{
		a=0;
		flag=input_num(&a,flag);
		if(flag==1)
		{
			Tempperatures(a);
		}
		else
		{
			break;
		}
	}
}
/**********************************
Function:		Tempperatures
Description: 将转化后的数据,输出为三种温度.
Calls:	           无。
Input:          celsius华氏温度,Kelvin绝对(开氏)温度;
OutPut:       a。被转换之后的温度值
Return:		   flag。返回标志位状态
**********************************/
void Tempperatures(double Fah)
{
	const double  firist =1.8,second=32.0,third=273.16;
	double celsius,Kelvin;
	celsius=firist *Fah+second;
	Kelvin=celsius+third;
	printf("%.2f\t",celsius);
	printf("%.2f\n\n",Kelvin);
	puts("Input the temperature !\n");
}
/**********************************
Function:		nput_num
Description: getchar()函数实现输入内容读取,转换为三种温度.
Calls:	           getchar()
Input:           flag:程序异常标志位。
				   a:将要被处理的气温。
				   i:  数组位数标记。
OutPut:       a。被转换之后的温度值
Return:		   flag。返回标志位状态
**********************************/
int input_num(double *a,int flag)
{
	char Fah;
	do{
		Fah=getchar();
		if(Fah>='0'&&Fah<='9')
		{
			(*a)=(Fah-'0')+(*a)*10;
		}
		else if (Fah!='\n')
		{
			while(getchar()!='\n');
			puts("Input Error!\n");
			flag=0;
			break;
		}	
	}while(Fah!='\n');
	return flag;
}
/**********************************
Function:		P114_8_2()
Description: 静态数组实现输入内容读取,转换为三种温度.
				   函数名称为页号(P114),加题号或例题号(_8),加方法类型(_2)
Calls:	           void Tempperatures(double Fah);
                   atof(str);
				   strlen(str);
Input:          length:预计输入字符串的长度。
				   flag:程序异常标志位。
				   a:将要被处理的气温。
				   i:  数组位数标记。
				   str:输入的字符串,将被转换为气温。
OutPut:       无。
Return:		   无。
**********************************/
void P114_8_2()
{
	puts("Input the temperature !\n");
	int length,i=0,flag=0;
	char str[100]={0};
	double a;
	while(1)
	{
		scanf_s("%s",str,10);
		length=strlen(str);
		while(i<length)
		{
			if(str[i]>='0'&&str[i]<='9')
			{
				i++;
			}
			else
			{
				puts("Input Error!\nProgram Exit!\n");
				flag=1;
				break;
			}
		}
		a=atof(str);
		if(flag==0)
		{
			Tempperatures(a);
			a=0;
		}
		else
		{
			break;
		}
	}
}
/**********************************
Function:		P114_8_3()
Description: 动态数组实现输入内容读取,转换为三种温度.
				   函数名称为页号(P114),加题号或例题号(_8),加方法类型(_3)
Calls:	           void Tempperatures(double Fah);
Input:          ture_length:输入字符串的真实长度。
				   length:预计输入字符串的长度。
				   flag:程序异常标志位。
				   Fah:将要被处理的气温。
				   str:输入的字符串,将被转换为气温。
OutPut:       无。
Return:		   无。
**********************************/
//用动态数组实现
void P114_8_3()
{
	int ture_length;
	int i=0,flag=0,length=0;
	double Fah;
	char *str;
	while(1)
	{
			puts("请输入动态数组的长度:\n");
			printf("length=");
			scanf_s("%d",&length);
			if((str=(char *)malloc(length*sizeof(char *)))==NULL)
			{
				printf("内存空间分配失败!\n");
				return ;
			}
			puts("请输入字符串!\n");
			printf("str=");
			scanf_s("%s",str,length);
			ture_length=strlen(str);
			if(ture_length>length)	
			{
				puts("输入字符串过长!\n");
				return ;
			}
			while(i<ture_length)
			{
				if(str[i]>='0'&&str[i]<='9')
				{
					i++;
				}
				else
				{
					puts("输入错误!包含非法字符!\n");
					flag=1;
					break;
				}
			}
			if(flag==0)
			{
				i=0;
				Fah=atof(str);
			}
			else
			{
				puts("运行结束!\n");
				break;
			}
			Tempperatures(Fah);
			free(str);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值