VS2019的调试功能学习(烫烫烫)
我编写了个大数减法的程序但是会出现很奇怪的报错,然后我就一路百度。。。
现在我们尝试对以下代码用VS2019进行调试修改bug:
//源文件main.cpp
#include<stdio.h>
#include<string.h>
const int maxn = 1e3 + 10;
/*
int input(char a[],char b[])
{
int j=0;
for(j=0;(a[j]=getchar())!='-';j++)
{
if(a[j]!='0'&&a[j]!='1'&&a[j]!='2'&&a[j]!='3'&&a[j]!='4'&&a[j]!='5'&&a[j]!='6'&&a[j]!='7'&&a[j]!='8'&&a[j]!='9')
return 0;
}
a[j]='\0';
scanf('%s',b);
return 1;
}
*/
void sub(char a[], char b[], char c[], int la, int lb)
{
//printf('%s\n%s\n',a,b);
memset(c, '\0', sizeof(c));
for (int i = 0; i < la / 2; i++) //进行翻转操作
{
char tmp = a[i];
a[i] = a[la - 1 - i];
a[la - 1 - i] = tmp;
}
for (int i = 0; i < lb / 2; i++)
{
char tmp = b[i];
b[i] = b[lb - 1 - i];
b[lb - 1 - i] = tmp;
}
//printf('%s\n%s\n',a,b);
for (int i = 0; i < la; i++) //减法操作
{
if (b[i] == '\0')
{
c[i] = a[i];//什么鬼
continue;
}
c[i] = a[i] - b[i] + '0';
if (c[i] < '0')
{
c[i] += 10;
a[i + 1]--;
}
}
int lc = strlen(c);
for (int i = 0; i < lc / 2; i++)
{
int tmp = c[i];
c[i] = c[lc - 1 - i];
c[lc - i - 1] = tmp;
}
//(c[0]=='0')?c[0]='\0':1;
}
int main()
{
char a[maxn], b[maxn], c[maxn];
while (1/*input(a,b)*/)
{
int j = 0;
int la = 0, lb = 0;
scanf('%s', a);
scanf('%s', b);
la = strlen(a);
lb = strlen(b);
sub(a, b, c, la, lb);
printf('%s\n', c);
memset(a, '\0', sizeof(a));
memset(b, '\0', sizeof(b));
memset(c, '\0', sizeof(c));
}
return 0;
}
编译报错:严重性代码说明项目文件行 禁止显示状态错误 C4996 scanf
首先如果在VS2019中直接运行此代码会出现如下报错:
1>------ 已启动生成: 项目: Large number subtraction, 配置: Release Win32 ------
1>main.cpp
1>C:\Users\chenlon\source\repos\Large number subtraction\main.cpp(3,22): warning C4244: “初始化”: 从“double”转换到“int”,可能丢失数据
1>C:\Users\chenlon\source\repos\Large number subtraction\main.cpp(67,1): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h(1274): message : 参见“scanf”的声明
1>C:\Users\chenlon\source\repos\Large number subtraction\main.cpp(68,1): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h(1274): message : 参见“scanf”的声明
1>已完成生成项目“Large number subtraction.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
编译报错解决
之后终于在这个文章找到了解决办法:https://blog.csdn.net/weixin_42839965/article/details/81669145
1.右键单击工程名——>属性
2.将是改为否,单击确定
运用调试功能检查BUG
一、选择断点并检查程序
1.选择需要检查或暂停运行的行,如下图红色方框前
2.点击Windows调试器
3.对程序进行对应操作,检查运行结果
运行至此处时出现问题,注意观察该赋值运算前后字符串c的变化过程:
二、“烫烫烫”乱码:
出现以上问题的原因如下:
这种乱码最常见的地方是Visual Studio里。
Visual Studio中,未初始化的栈空间用0xCC填充,而未初始化的堆空间用0xCD填充。
而0xCCCC和0xCDCD在中文GB2312编码中分别对应“烫”字和“屯”字。
如果一个字符串没有结束符'\0',输出时就会打印出未初始化的栈或堆空间的内容,这就是大名鼎鼎的“烫烫烫”乱码。
改了半天一直是这个问题,最终还是选择用int c[maxn]的数组来处理......
改完了
#include<stdio.h>
#include<string.h>
const int maxn = 1e3 + 10;
int lc=0;
int input(char a[],char b[])
{
int j=0;
for(j=0;(a[j]=getchar())!='-';j++)
{
if(a[j]!='0'&&a[j]!='1'&&a[j]!='2'&&a[j]!='3'&&a[j]!='4'&&a[j]!='5'&&a[j]!='6'&&a[j]!='7'&&a[j]!='8'&&a[j]!='9')
return 0;
}
a[j]='\0';//将a[]中的'-',去除
scanf('%s',b);
return 1;
}
void sub(char a[], char b[], int c[], int la, int lb)
{
//printf('%s\n%s\n',a,b);
for (int i = 0; i < la / 2; i++) //进行翻转操作
{
char tmp = a[i];
a[i] = a[la - 1 - i];
a[la - 1 - i] = tmp;
}
for (int i = 0; i < lb / 2; i++)
{
char tmp = b[i];
b[i] = b[lb - 1 - i];
b[lb - 1 - i] = tmp;
}
//printf('%s\n%s\n',a,b);
for (int i = 0; i < la; i++) //减法操作
{
if (b[i] == '\0')
{
c[i] = a[i] - 48;
continue;
}
c[i] = a[i] - b[i];
if (c[i] < 0)
{
c[i] += 10;
a[i + 1]--;
}
}
lc = la;
for (int i = la - 1; c[i] == 0; i--) //为了使结果开头不会有0
{
lc--;
}
for (int i = 0; i < lc / 2; i++)
{
int tmp = c[i];
c[i] = c[lc - 1 - i];
c[lc - i - 1] = tmp;
}
}
int main()
{
char a[maxn], b[maxn];
int c[maxn];
memset(a, '\0', sizeof(a));
memset(b, '\0', sizeof(b));
memset(c, 0, sizeof(c));
while (input(a,b))
{
int j = 0, type = 0, la = 0, lb = 0;
//scanf('%s', a);
//scanf('%s', b);
la = strlen(a);
lb = strlen(b);
if (la == lb)
{
if (strcmp(a, b) > 0)
{
type = 1;
sub(a, b, c, la, lb);
}
else if(strcmp(a,b)<=0)
{
sub(b, a, c, lb, la);
type = -1;
}
else
{
printf('0\n');
continue;
}
}
else
{
if (la > lb)
{
sub(a, b, c, la, lb);
type = 1;
}
else
{
sub(b, a, c, lb, la);
type = -1;
}
}
if (type == -1) printf('-');
for (int i = 0; i < lc; i++)
printf('%d', c[i]);
printf('\n');
while (getchar() != '\n'); //getchar()是在输入缓冲区顺序读入一个字符(包括空格、回车和Tab),为了去除之前的回车,在此处加此语句,牢记此使用技巧!
memset(a, '\0', sizeof(a));
memset(b, '\0', sizeof(b));
memset(c, 0, sizeof(c));//好习惯
}
return 0;
}
终于把这题过了!https://code.mi.com/problem/list/view?id=3
#include<stdio.h>
#include<string.h>
const int maxn = 1e3 + 10;
int lc=0;
int input(char a[],char b[])
{
int j=0;
for(j=0;(a[j]=getchar())!='-';j++)
{
if(a[j]!='0'&&a[j]!='1'&&a[j]!='2'&&a[j]!='3'&&a[j]!='4'&&a[j]!='5'&&a[j]!='6'&&a[j]!='7'&&a[j]!='8'&&a[j]!='9')
return 0;
}
a[j]='\0';//将a[]中的'-',去除
scanf('%s',b);
return 1;
}
void sub(char a[], char b[], int c[], int la, int lb)
{
//printf('%s\n%s\n',a,b);
for (int i = 0; i < la / 2; i++) //进行翻转操作
{
char tmp = a[i];
a[i] = a[la - 1 - i];
a[la - 1 - i] = tmp;
}
for (int i = 0; i < lb / 2; i++)
{
char tmp = b[i];
b[i] = b[lb - 1 - i];
b[lb - 1 - i] = tmp;
}
//printf('%s\n%s\n',a,b);
for (int i = 0; i < la; i++) //减法操作
{
if (b[i] == '\0')
{
c[i] = a[i] - 48;
continue;
}
c[i] = a[i] - b[i];
if (c[i] < 0)
{
c[i] += 10;
a[i + 1]--;
}
}
lc = la;
for (int i = la - 1; c[i] == 0; i--) //为了使结果开头不会有0
{
lc--;
}
for (int i = 0; i < lc / 2; i++)
{
int tmp = c[i];
c[i] = c[lc - 1 - i];
c[lc - i - 1] = tmp;
}
}
int main()
{
char a[maxn], b[maxn];
int c[maxn];
memset(a, '\0', sizeof(a));
memset(b, '\0', sizeof(b));
memset(c, 0, sizeof(c));
input(a, b);
int j = 0, type = 0, la = 0, lb = 0;
//scanf('%s', a);
//scanf('%s', b);
la = strlen(a);
lb = strlen(b);
if (la == lb)
{
if (strcmp(a, b) > 0)
{
type = 1;
sub(a, b, c, la, lb);
}
else if(strcmp(a,b)<=0)
{
sub(b, a, c, lb, la);
type = -1;
}
else
{
printf('0\n');
}
}
else
{
if (la > lb)
{
sub(a, b, c, la, lb);
type = 1;
}
else
{
sub(b, a, c, lb, la);
type = -1;
}
}
if (type == -1) printf('-');
for (int i = 0; i < lc; i++)
printf('%d', c[i]);
printf('\n');
return 0;
}
赞 (0)