1. 程式人生 > >程式設計小錯誤總結帖(時常更新)

程式設計小錯誤總結帖(時常更新)

一個月以來犯錯較多且難以發現錯誤的:

1,迴圈體內i,j,k.混用:

for example:

for(i=0;i<n;i++){
	for(i=0;i<m;i++){
		//statement;
	}
}
或:

for(i=0;i<n;i++){
	for(j=0;j<m;i++){
		//statement;
	}
}

或者在迴圈體中不小心改變了迴圈變數的值等等。

2,更新多個變數犯錯。

int func(int x,int y);
//ues func() to update p,q;
int p,q;
p=func(p,q);
q=func(p,q);//note the p has been changed 

3,處理字串時,忽略在結尾處新增'\0'

3.2處理字串中“數字”時,沒有減去'0',或只考慮一位數的情況。

int len=strlen(str);
int temp=0;
for(j=5;j<len;j++){	<pre name="code" class="cpp">temp=temp*10+str[j]
//should be :temp=temp*10+str[j]-'0';}S[top]=temp;//S[top]=str[5]-'0'只考慮了一位情況

 
 

4,型別定義錯誤。

//int instead of char
int m[50];
scanf("%s",m);
printf(m);
//or char instead of int;
char n[50];
if(statement){
	n++;
}

5,陣列越界,執行時未報錯。

//這個最後居然把x.size置為0了。。
struct bigint{
	int digit[200];
	int size;
};
void func(bigint x){
	int i;
	for(i=0;i<201;i++){
		x.digit[i]=0;
	}
}

6,邊界錯誤。

如,求a以內連續奇數素數對的個數。(如3,5)

for(i=2;i<a;i++)
	if(isPrime(i) && isPrime(i+2))
		count++;
以上雖然i<a,但是卻在迴圈中出現了i+2.

7,沒有注意遞迴出口

void preorder(int root){
	if(root!=0)
		printf("%d ",tree[root].x);
	preorder(tree[root].left);
	preorder(tree[root].right);
}

注意上述程式碼在root=0時會無限執行preorder(tree[root].left);

應該為:

void preorder(int root){
	if(root==0)
                return;
	printf("%d ",tree[root].x);
	preorder(tree[root].left);
	preorder(tree[root].right);
}

另外,樹裡遞迴出口很多都是if(root==0)。

比如,求樹的高度

int height(int root){
	if(root==0)return 0;
	int lefth=0,righth=0;
	
	lefth=height(tree[root].left);
	righth=height(tree[root].right);
	
	tree[root].h=max(lefth,righth)+1;
	tree[root].delth=lefth-righth;//高度差

	return tree[root].h;
}

8,在條件中的"++"運算子

while(str++<p){
		if(str[0]!='A'){
			puts("1");
			printf("a,b,c,%d,%d,%d\n",a,b,c);
			return false;	
		}
		a++;
	}
注意在迴圈中用到了str[0],但是此時的str已經不再是迴圈判定中的str了,是自增之後的str;

我這裡本來是想判定從str開始到p結束(不包括p)的字元,顯然起始位沒判斷,又多判斷了p位.

應該是下面這樣:

while(str<p){
		if(str[0]!='A'){
			puts("1");
			printf("a,b,c,%d,%d,%d\n",a,b,c);
			return false;	
		}
		str++;
		a++;
	}

9,遞迴出口錯誤。如DFS想在res為true時推出迴圈,

應該為下面:

//DFS,注意遞迴出口是從某鱷魚可以跳到陸地
bool DFS(int x){
	//DFS遞迴出口易錯
	bool res=false;
	int i;
	visited[x]=true;
	if(issaved(point[x])){
		return true;
	}
	for(i=0;i<number;i++){
		if(!visited[i]&&dist(point[x],point[i])<=step){
			//絕對不可以寫
			//return DFS(i);
			//相當於for(i=0;i<n;i++)return i;
			//除了i為0時會return出來,其他的不會return
			//事實上出口是return true。當res為false時,還要繼續
			res=DFS(i);
			if(res)return true;
		}
	}
	return false;
}
以及BFS易錯點:。。。

//注意兩個visited[]位置。
//一開始我只寫一個visited,在print後,也就是列印相當於訪問
//如下
/*
void BFS(int x){
	while(!Q.empty())Q.pop();
	Q.push(x);
	int i;
	while(!Q.empty()){
		int t=Q.front();
		Q.pop();
		printf(" %d",t);
		visited[i]=true;//這樣在列印前可能入佇列很多次。。。。
		//建議可以在列印前判斷是否是訪問過。只不過會浪費一些佇列位置
		for(i=0;i<n;i++){
			if(!visited[i] && map[i][t]==1){
				//printf("\nt,i:%d,%d",t,i);
				Q.push(i);
			}
		}
	}
}
*/

void BFS(int x){
	while(!Q.empty())Q.pop();
	Q.push(x);
	int i;
	visited[x]=true;///
	while(!Q.empty()){
		int t=Q.front();
		Q.pop();
		printf(" %d",t);
		for(i=0;i<n;i++){
			if(!visited[i] && map[i][t]==1){
				//printf("\nt,i:%d,%d",t,i);
				Q.push(i);
				visited[i]=true;
			}
		}
	}
}