使用sourcemonitor发现的一些问题

使用sourcemonitor发现的一些问题

当代码中含中文注释时会影响计算结果:
(以下面代码片段为例)
情况一:

#include <iostream>
#include <vector>
using namespace std;

vector<int> arr;

int tmp = 0;

void fab(int n)
{
	if (n == 0) return 0;
	if (n <= 2) return 1;
	else return fab(n - 1) + fab(n - 2);
}

void fab1(int n)
{
	if (n == 0) return 0;
	if (n <= 2) return 1;
	else return fab(n - 1) + fab(n - 2);
}

void fab2(int n)
{
	if (n == 0) return 0;
	if (n <= 2) return 1;
	else return fab(n - 1) + fab(n - 2);
}

int binary_search(int tmp) {
	int l = 0, r = arr.size() - 1;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (arr[mid] > tmp) {
			r = mid - 1;
		} else if (arr[mid] < tmp){
			l = mid + 1;
		} else {
			return mid;
		}
	}
	return -1;
}

int main() {
	for (int i = 0; i < 10; i++) {
		arr.push_back(i);
	}
	cout << binary_search(0);
	return 0;
}

这时不包含注释,计算结果为:
在这里插入图片描述
当在代码中试图加入一些 中文注释和英文注释

void fab(int n)
{
	//this is fab();
	if (n == 0) return 0;
	if (n <= 2) return 1;
	else return fab(n - 1) + fab(n - 2);
}

void fab1(int n)
{
	if (n == 0) return 0;//这是中文注释
	if (n <= 2) return 1;//这是中文注释
	else return fab(n - 1) + fab(n - 2);
}

//加在这
void fab2(int n)
{
	if (n == 0) return 0;
	if (n <= 2) return 1;
	else return fab(n - 1) + fab(n - 2);
}

int binary_search(int tmp) {
	//二分搜索
	int l = 0, r = arr.size() - 1;
	//当l要r小于等于时
	while (l <= r) {
		//这里可以优化一下
		int mid = (l + r) >> 1;
		if (arr[mid] > tmp) {
			r = mid - 1;
		} else if (arr[mid] < tmp){
			l = mid + 1;
		} else {
			return mid;
		}
	}
	return -1;
}

这时计算结果为:
在这里插入图片描述
这里加注释后binary_search函数的复杂度发生了变化,最为严重的是竟然记录不到fab2函数了。
这是因为fab2函数的头上有一条中文注释,sourcemonitor在进行解析时,解析成了:
(sourcemonitor生成的结果中,右击对应的文件,选择View Source File可以查看源文件)
在这里插入图片描述
函数名跑到了注释一行,自然就找不到fab2了。

情况二:
情况二也是中文引起的,但是这个我找了好久才发现这个bug
如果将上面代码段加上一行#define HONE ("定向的")
比如:

#include <iostream>
#include <vector>
using namespace std;

#define HONE ("定向的")

vector<int> arr;

int tmp = 0;

//(...这里和上面一样)

int main() {
	for (int i = 0; i < 10; i++) {
		arr.push_back(i);
	}
	cout << binary_search(0);
	return 0;
}

计算结果为:
在这里插入图片描述
可以发现结果为空。
查看一下源文件:
在这里插入图片描述
这是因为sourcemonitor在解析"定向的"这个中文字符串时,只得到了一个" ,因此它会一直找另一个"与它匹配,但我们代码中恰好没有",所以找到文件结尾都没匹配成功,自然也就六亲不认,一个函数都没计算。
为了验证上述结论,可以在下面再写一行宏,这样就有两个"了,匹配后就能计算后面的函数了。
在这里插入图片描述
可以发现果真如此(虽然此时结果仍然是错的,因为代码中含中文注释):
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高二的笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值