怎么在循环中精简代码——将循环测试和更新循环放在一起~

先来看一个代码

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
int main(void)
{
	char ch;
	ch = getchar();
	while (ch != '\n')
	{
		if (ch == ' ')
			putchar(ch);
		else
			putchar(ch + 1);
		ch = getchar();
	}
	putchar(ch);
	system("pause");
	return 0;
}

     
     
    
    
   
   

再来看这个精简代码

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
int main(void)
{
	char ch;
	while ((ch=getchar()) != '\n')
	{
		if (ch == ' ')
			putchar(ch);
		else
			putchar(ch + 1);
	}
	putchar(ch);
	system("pause");
	return 0;
}
     
     
    
    
   
   


可以发现这个精简方法是——将循环测试与更新循环放在一起

/*感觉写代码最不舒服的就是下面的这个情况

#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
int square_sum(int a, int b);
int main(void)
{
	int lower, upper;
	printf("请输入下限整数:");
	scanf("%d", &lower);
	printf("请输入上限整数:");
	scanf("%d", &upper);
	while (lower < upper)
	{
		printf("%d\n", square_sum(lower, upper));
		printf("请输入下限整数:");
		scanf("%d", &lower);
		printf("请输入上限整数:");
		scanf("%d", &upper);
	}
	printf("Done\n");
	system("pause");
	return 0;
}
int square_sum(int a, int b)
{
	int sum = 0;
	for (; a <= b; a++)
	{
		sum += a*a;
	}
	return sum;
}
      
      
     
     
    
    

真的感觉为之难受了一个学期。。。就是明明循环内要写这个语句,但是循环外又必须写,没有办法的难受。。。

但是今天,突然有一个代码给我启发:
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
int main(void)
{
	char ch;
	while (
			(ch = getchar())
					!= '\n')
	{
	
		if (ch == ' ')
			putchar(ch);
		else
			putchar(ch + 1);
	}
	putchar(ch);
	system("pause");
	return 0;
}
       
       
      
      
     
     

*********C对代码的格式要求宽松,这样写让其中的每个行为更加清晰

那么根据上面的这个格式的启发,我突然想到精简上面的那个难受的代码可以这么干:

#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
int square_sum(int a, int b);
int main(void)
{
	int lower, upper;
	while (printf("请输入下限整数:"),
			scanf("%d", &lower),
		   printf("请输入上限整数:"),
			scanf("%d", &upper),
    		lower < upper)
	{
		printf("%d\n", square_sum(lower, upper));
	}
	printf("Done");
	system("pause");
	return 0;
}
int square_sum(int a, int b)
{
	int sum = 0;
	for (; a <= b; a++)
	{
		sum += a*a;
	}
	return sum;
}
       
       
      
      
     
     

核心思想还是精简方法是——将循环测试与更新循环放在一起
所以下次再面对这种难受的情况的时候,就像上面的那个代码那样干哦~~


精简代码第二弹
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
int main(void)
{
	bool isPrime;//像这种只用取两个值,只用占一个字节的变量就可以设置成bool类型
	long num;//为了扩大num可以取值的范围,故使用long型的变量
	while (isPrime=true,scanf("%ld",&num)==1)//一定要记得,long型变量的转换说明对应的不应该是%l而应该是%ld  !!!!!!!!
	{
		for (long i = 2; (i*i) <= num; i++)
		{
			if (num%i == 0)
			{
				isPrime = false;
				if ((i*i) == num)
				{
					printf("%ld is %ld的因数\n", i, num);
				}
				else
					printf("%ld and %ld is %ld 的因数\n", i, num / i, num);
			}
		}
		if (isPrime)
		{
			printf("%ld is prime\n", num);
		}
		printf("press a digit to continue or press a nonnumeric to quit:");
	}
	printf("bey~\n");
	system("pause");
	return 0;
}
       
       
      
      
     
     
    
    

核心思想还是精简方法是——将循环测试与更新循环放在一起

这里利用的是scanf函数的返回值,将其作为测试循环的条件~

核心思想还是精简方法是——将循环测试与更新循环放在一起
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值