LUPA首页 | 资讯 | 教程 | 下载 | 求职 | 方案 | 博客 | 交易 | 英文版
LUPA论坛


 
标题: 一道程序题,测试下大家的能力
本帖已经被作者加入个人空间
tangguyan
开源主力军
Rank: 3Rank: 3



UID 28274
精华 0
积分 1671
帖子 120
LUPA币 1611 点
阅读权限 30
注册 2006-12-26
发表于 2008-3-31 12:29  资料 个人空间 短消息  QQ ICQ 状态
是可以在编程中养成一种好的习惯,就是把(n==1)这样的形式,写成(1==n)
一旦这样写的话,就算你无意中少写了一个等号,在编译的时候就能马上发现错误,(编译器能提示)
如果是n==1的话,如果少写一个等号,那么在语法上没有错误,但是在逻辑上出现了错误,这样编译器就无法提示错误了。
这个习惯我也是在一份报纸上看到的
顶部
[广告] 推荐个超酷的web2.0相册
walkerxk (walkerxk)
LUPA团队
Rank: 9Rank: 9Rank: 9
点一盏金蜡烛,为灾区人民祈福。


风雨同舟奖  
UID 980
精华 0
积分 14666
帖子 2362
LUPA币 13485 点
阅读权限 200
注册 2005-9-27
来自 LUPA
发表于 2008-3-31 13:52  资料 主页 个人空间 短消息  QQ ICQ 状态 Yahoo!


QUOTE:
原帖由 tangguyan 于 2008-3-31 12:29 发表
是可以在编程中养成一种好的习惯,就是把(n==1)这样的形式,写成(1==n)
一旦这样写的话,就算你无意中少写了一个等号,在编译的时候就能马上发现错误,(编译器能提示)
如果是n==1的话,如果少写一个等号 ...

http://man.lupaworld.com/content/develop/c&c++/c/c.htm
里面有很多很好的编程习惯。
顶部
[广告] 推荐个超酷的web2.0相册
张荣华309
关注开源
Rank: 2



UID 170181
精华 0
积分 160
帖子 18
LUPA币 151 点
阅读权限 20
注册 2008-3-30
发表于 2008-4-1 22:26  资料 个人空间 短消息 
#include "iostream.h"
void main()
{ int year=0;
  cout<<"Input the year:";
  cin>>year;
  if(year==1) cout<<"The area is 305!"<<endl;
  else if(year>1) cout<<"The area is"<<305+3*(year-1)<<endl;
        else cout<<"The input is wrong!"<<endl;
}   //C++实线了  其实不用for循环的  可以用乘法嘛

#include "stdio.h"
void main()
{ int year=0;
  printf("Input the year:");
  scanf("%d",&year);
  if(year==1) printf("The area is 305!\n");
  else if(year>1) printf("The area is %d\n!",305+3*(year-1));
        else printf("The input is wrong!\n");
}   //C实线了  其实不用for循环的  可以用乘法嘛

#include "stdio.h"
void main()
{ int year=0,i=0,count=305;
  printf("Input the year:");
  scanf("%d",&year);
  if(year==1) printf("The area is 305!\n");
  else if(year>1)
{ for(;i<year;i++)
   count=count+3;
    printf("The area is %d\n!",305+3*(year-1));
   }
     else printf("The input is wrong!\n");
}   //C实线了  用for循环的

#include "stdio.h"
void main()
{ int year=0,i=0,count=305;
  printf("Input the year:");
  scanf("%d",&year);
  if(year==1) printf("The area is 305!\n");
  else if(year>1)
{do{count=count+3;i++;}while(i>=year);
    printf("The area is %d\n!",305+3*(year-1));
   }
     else printf("The input is wrong!\n");
}   //C实线了  用while循环的
大家可以仔细比对比对  有好处的
顶部
[广告] 推荐个超酷的web2.0相册
ddddddz
初来乍到
Rank: 1



UID 172458
精华 0
积分 4
帖子 3
LUPA币 2 点
阅读权限 10
注册 2008-4-2
发表于 2008-4-2 09:49  资料 短消息 
int main(){
  int i,n,m=305;
  scanf(%d,&n);
if(n==1)
    ;
else
      for(i=2;i<=n;i++)
        m+=1;
  printf("%d",m);
}

[ 本帖最后由 ddddddz 于 2008-4-2 09:53 编辑 ]
顶部
 


当前时区 GMT+8, 现在时间是 2008-7-7 08:48
浙ICP备06002895号

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.031771 second(s), 6 queries , Gzip enabled

清除 Cookies - 联系我们 - LUPA开源社区 - Archiver - WAP