全球前十强,年薪15万的面试题
程序风格的作用主要是使代码容易阅读。代码应该时清楚的和简单的-------具有直截了当的逻辑、自然的表达式、通行的语言使用方式、有意义的名字和有帮助作用的注释等,应该避免耍小聪明,绝不使用非正规的结构。
下面的代码具有很多缺陷,请从风格的角度评论并改正下面各个代码片断:
/* return SUCCESS */
return SUCCESS;
----------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id > = unblocks))
-----------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}
-------------------------------------------------------
i= 0;
while(i <= n-1)
array[i++] = 1.0;
----------------------计算n的阶乘---------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= n;
return fac;
}
-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j)
-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) a > b? a:b
[解决办法]
/* return SUCCESS */
return SUCCESS; 不需要注释
----------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id > = unblocks))
-----------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}
应该把==换成equals
-------------------------------------------------------
i= 0;
while(i <= n-1)
array[i++] = 1.0;
循环放的不好
----------------------计算n的阶乘---------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= n; 循环放的不好,语句没加括号
return fac;
}
-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j)
-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) a > b? a:b
[解决办法]
ctrl + shift + f
[解决办法]
-----------C函数,但是Java开发人员也可以做---------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j)
后面少分号了:
if (i > j)
printf(“%d is greater than %d.\n”, i, j);
else
printf(“%d is smaller than %d.\n”, i, j);
-------------------------------------- C语言--------------------------------------
#define MAX(a, b) a > b? a:b
应该这样写:
#define MAX((a),(b)) (a)> (b)? (a):(b)
因为a,b可以是表达式
不行!其他的看不出啊
[解决办法]
明显逻辑错误,不是大于就是小于,那等于呢。。。
-----------C函数,但是Java开发人员也可以做---------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j)
后面少分号了:
if (i > j)
printf(“%d is greater than %d.\n”, i, j);
else
printf(“%d is smaller than %d.\n”, i, j);
-------------------------------------- C语言--------------------------------------
[解决办法]
第二题,最好不要用那个三元的表达式,不好懂~~
[解决办法]
/*
*return SUCCESS
*/
return SUCCESS;
----------------------------------------------------
if (
!(block_id < actblks)
&
brvbar;
&
brvbar; !(block_id > = unblocks)
)
-----------------------------------------------------
int count = 0;
while (count < total)
{
count++;
if (this.getName(count) == nametable.userName())
{
return true;
}
}
-------------------------------------------------------
i=0;
while(i <= (n-1))
{
i++
array[i] = 1.0;
}
----------------------计算n的阶乘---------------------------------
int factorial(int n)
{
int fac = 1;
while(n--)
{
fac *= n;
}
return fac;
}
-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
{
printf(“%d is greater than %d.\n”, i, j) ;
}
else
{
printf(“%d is smaller than %d.\n”, i, j) ;
}
-------------------------------------- C语言--------------------------------------------
#define MAX((a),(b)) (a)> (b)? (a):(b);
[解决办法]
kary给打个分,我的格式
[解决办法]
在Java中允许使用count做变量吗
[解决办法]
不好意思我错了 是const
[解决办法]
/* return SUCCESS */
return SUCCESS;
这个注释是多余的
----------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id > = unblocks))
if没有这种格式
-----------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}
在Java中比较相等应该用equals()方法
-------------------------------------------------------
i= 0;
while(i <= n-1)
array[i++] = 1.0;
应该用Arrays.fill()
----------------------计算n的阶乘---------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= n;
return fac;
}
错误,少乘一个n,并且Java中应该明确判断n是否大于零,如:
while( n > 0 ) {
fac *= n --;
}
-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j)
在printf(...)后缺了;
-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) a > b? a:b
最好如下定义:
#define MAX(a,b) ((a) > (b))?a:b
并且在C中用宏造成错误的可能性较大,尽量不用
[解决办法]
printf( "%d is %s than %d ", (i> j)? "greater ": "smaller ");
[解决办法]
刚建立了一个软件开发技术和休闲交流的QQ群44540509,java,.net,c/c++,php,linux,unix、软件测试等技术、休闲讨论区,欢迎大家加入!
[解决办法]
printf( "%d is %s than %d ", i, (i> j)? "greater ": "smaller ", j);
[解决办法]
return SUCCESS;
----------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id > = unblocks))
错误语法
-----------------------------------------------------
for ( int i = 0; i <total; i ++ )
if (this.getName(i).equals(nametable.userName())) {
return true;
}
}
-------------------------------------------------------
i= 0;
while(i < n)
array[i++] = 1.0;
----------------------计算n的阶乘---------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= ++fac;
return fac;
}
-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j);
else if ( i < j)
printf(“%d is smaller than %d.\n”, i, j);
else
printf(“%d is equal with %d.\n”, i, j);
-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) (a) > (b)? a:b
[解决办法]
/* return SUCCESS */
return SUCCESS; 不需要注释
----------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id > = unblocks))
-----------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}
应该把==换成equals
-------------------------------------------------------
i= 0;
while(i <= n-1)
array[i++] = 1.0;
用for循环简单明了
----------------------计算n的阶乘---------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= n; 循环用的不好,for循环即可
return fac; 没有考虑数据溢出问题, 应 该使用数组或者字符串存储阶乘的结果
}
-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j) //没有考虑相等
-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) a > b? a:b //a,b可以为表达式,且结果外再加个括号,否则用在表
//达式可能会出错,#define MAX(a,b) ((a)> (b)?(a):(b))
[解决办法]
if ((block_id > = actblks) || (block_id < unblocks))
[解决办法]
15万就把你吹的?SB
[解决办法]
return SUCCESS;
----------------------------------------------------
if ((block_id > = actblks) && (block_id < unblocks))
------------------Java语言-----------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) equals nametable.userName()) {
return true;
}
}
----------------------初始化一个数组,数组大小为n---------------------------------
int i= 0;
while(i <= (n-1))
array[i++] = 1.0;
----------------------计算n的阶乘---------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while ((n-1) > 0){
fac *= n;
n--;
}
return fac;
}
----------------------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i , j);
else if( i < j)
printf(“%d is smaller than %d.\n”, i, j);
-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) (a) > (b)? (a):(b)
