VS2019的调试功能学习(烫烫烫)

我编写了个大数减法的程序但是会出现很奇怪的报错,然后我就一路百度。。。

现在我们尝试对以下代码用VS2019进行调试修改bug:

  1. //源文件main.cpp
  2. #include<stdio.h>
  3. #include<string.h>
  4. const int maxn = 1e3 + 10;
  5. /*
  6. int input(char a[],char b[])
  7. {
  8. int j=0;
  9. for(j=0;(a[j]=getchar())!='-';j++)
  10. {
  11. 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')
  12. return 0;
  13. }
  14. a[j]='\0';
  15. scanf('%s',b);
  16. return 1;
  17. }
  18. */
  19. void sub(char a[], char b[], char c[], int la, int lb)
  20. {
  21. //printf('%s\n%s\n',a,b);
  22. memset(c, '\0', sizeof(c));
  23. for (int i = 0; i < la / 2; i++) //进行翻转操作
  24. {
  25. char tmp = a[i];
  26. a[i] = a[la - 1 - i];
  27. a[la - 1 - i] = tmp;
  28. }
  29. for (int i = 0; i < lb / 2; i++)
  30. {
  31. char tmp = b[i];
  32. b[i] = b[lb - 1 - i];
  33. b[lb - 1 - i] = tmp;
  34. }
  35. //printf('%s\n%s\n',a,b);
  36. for (int i = 0; i < la; i++) //减法操作
  37. {
  38. if (b[i] == '\0')
  39. {
  40. c[i] = a[i];//什么鬼
  41. continue;
  42. }
  43. c[i] = a[i] - b[i] + '0';
  44. if (c[i] < '0')
  45. {
  46. c[i] += 10;
  47. a[i + 1]--;
  48. }
  49. }
  50. int lc = strlen(c);
  51. for (int i = 0; i < lc / 2; i++)
  52. {
  53. int tmp = c[i];
  54. c[i] = c[lc - 1 - i];
  55. c[lc - i - 1] = tmp;
  56. }
  57. //(c[0]=='0')?c[0]='\0':1;
  58. }
  59. int main()
  60. {
  61. char a[maxn], b[maxn], c[maxn];
  62. while (1/*input(a,b)*/)
  63. {
  64. int j = 0;
  65. int la = 0, lb = 0;
  66. scanf('%s', a);
  67. scanf('%s', b);
  68. la = strlen(a);
  69. lb = strlen(b);
  70. sub(a, b, c, la, lb);
  71. printf('%s\n', c);
  72. memset(a, '\0', sizeof(a));
  73. memset(b, '\0', sizeof(b));
  74. memset(c, '\0', sizeof(c));
  75. }
  76. return 0;
  77. }

编译报错:严重性代码说明项目文件行 禁止显示状态错误 C4996 scanf

首先如果在VS2019中直接运行此代码会出现如下报错:

  1. 1>------ 已启动生成: 项目: Large number subtraction, 配置: Release Win32 ------
  2. 1>main.cpp
  3. 1>C:\Users\chenlon\source\repos\Large number subtraction\main.cpp(3,22): warning C4244: “初始化”: 从“double”转换到“int”,可能丢失数据
  4. 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.
  5. 1>D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h(1274): message : 参见“scanf”的声明
  6. 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.
  7. 1>D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h(1274): message : 参见“scanf”的声明
  8. 1>已完成生成项目“Large number subtraction.vcxproj”的操作 - 失败。
  9. ========== 生成: 成功 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]的数组来处理......

改完了

  1. #include<stdio.h>
  2. #include<string.h>
  3. const int maxn = 1e3 + 10;
  4. int lc=0;
  5. int input(char a[],char b[])
  6. {
  7. int j=0;
  8. for(j=0;(a[j]=getchar())!='-';j++)
  9. {
  10. 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')
  11. return 0;
  12. }
  13. a[j]='\0';//将a[]中的'-',去除
  14. scanf('%s',b);
  15. return 1;
  16. }
  17. void sub(char a[], char b[], int c[], int la, int lb)
  18. {
  19. //printf('%s\n%s\n',a,b);
  20. for (int i = 0; i < la / 2; i++) //进行翻转操作
  21. {
  22. char tmp = a[i];
  23. a[i] = a[la - 1 - i];
  24. a[la - 1 - i] = tmp;
  25. }
  26. for (int i = 0; i < lb / 2; i++)
  27. {
  28. char tmp = b[i];
  29. b[i] = b[lb - 1 - i];
  30. b[lb - 1 - i] = tmp;
  31. }
  32. //printf('%s\n%s\n',a,b);
  33. for (int i = 0; i < la; i++) //减法操作
  34. {
  35. if (b[i] == '\0')
  36. {
  37. c[i] = a[i] - 48;
  38. continue;
  39. }
  40. c[i] = a[i] - b[i];
  41. if (c[i] < 0)
  42. {
  43. c[i] += 10;
  44. a[i + 1]--;
  45. }
  46. }
  47. lc = la;
  48. for (int i = la - 1; c[i] == 0; i--) //为了使结果开头不会有0
  49. {
  50. lc--;
  51. }
  52. for (int i = 0; i < lc / 2; i++)
  53. {
  54. int tmp = c[i];
  55. c[i] = c[lc - 1 - i];
  56. c[lc - i - 1] = tmp;
  57. }
  58. }
  59. int main()
  60. {
  61. char a[maxn], b[maxn];
  62. int c[maxn];
  63. memset(a, '\0', sizeof(a));
  64. memset(b, '\0', sizeof(b));
  65. memset(c, 0, sizeof(c));
  66. while (input(a,b))
  67. {
  68. int j = 0, type = 0, la = 0, lb = 0;
  69. //scanf('%s', a);
  70. //scanf('%s', b);
  71. la = strlen(a);
  72. lb = strlen(b);
  73. if (la == lb)
  74. {
  75. if (strcmp(a, b) > 0)
  76. {
  77. type = 1;
  78. sub(a, b, c, la, lb);
  79. }
  80. else if(strcmp(a,b)<=0)
  81. {
  82. sub(b, a, c, lb, la);
  83. type = -1;
  84. }
  85. else
  86. {
  87. printf('0\n');
  88. continue;
  89. }
  90. }
  91. else
  92. {
  93. if (la > lb)
  94. {
  95. sub(a, b, c, la, lb);
  96. type = 1;
  97. }
  98. else
  99. {
  100. sub(b, a, c, lb, la);
  101. type = -1;
  102. }
  103. }
  104. if (type == -1) printf('-');
  105. for (int i = 0; i < lc; i++)
  106. printf('%d', c[i]);
  107. printf('\n');
  108. while (getchar() != '\n'); //getchar()是在输入缓冲区顺序读入一个字符(包括空格、回车和Tab),为了去除之前的回车,在此处加此语句,牢记此使用技巧!
  109. memset(a, '\0', sizeof(a));
  110. memset(b, '\0', sizeof(b));
  111. memset(c, 0, sizeof(c));//好习惯
  112. }
  113. return 0;
  114. }

终于把这题过了!https://code.mi.com/problem/list/view?id=3

  1. #include<stdio.h>
  2. #include<string.h>
  3. const int maxn = 1e3 + 10;
  4. int lc=0;
  5. int input(char a[],char b[])
  6. {
  7. int j=0;
  8. for(j=0;(a[j]=getchar())!='-';j++)
  9. {
  10. 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')
  11. return 0;
  12. }
  13. a[j]='\0';//将a[]中的'-',去除
  14. scanf('%s',b);
  15. return 1;
  16. }
  17. void sub(char a[], char b[], int c[], int la, int lb)
  18. {
  19. //printf('%s\n%s\n',a,b);
  20. for (int i = 0; i < la / 2; i++) //进行翻转操作
  21. {
  22. char tmp = a[i];
  23. a[i] = a[la - 1 - i];
  24. a[la - 1 - i] = tmp;
  25. }
  26. for (int i = 0; i < lb / 2; i++)
  27. {
  28. char tmp = b[i];
  29. b[i] = b[lb - 1 - i];
  30. b[lb - 1 - i] = tmp;
  31. }
  32. //printf('%s\n%s\n',a,b);
  33. for (int i = 0; i < la; i++) //减法操作
  34. {
  35. if (b[i] == '\0')
  36. {
  37. c[i] = a[i] - 48;
  38. continue;
  39. }
  40. c[i] = a[i] - b[i];
  41. if (c[i] < 0)
  42. {
  43. c[i] += 10;
  44. a[i + 1]--;
  45. }
  46. }
  47. lc = la;
  48. for (int i = la - 1; c[i] == 0; i--) //为了使结果开头不会有0
  49. {
  50. lc--;
  51. }
  52. for (int i = 0; i < lc / 2; i++)
  53. {
  54. int tmp = c[i];
  55. c[i] = c[lc - 1 - i];
  56. c[lc - i - 1] = tmp;
  57. }
  58. }
  59. int main()
  60. {
  61. char a[maxn], b[maxn];
  62. int c[maxn];
  63. memset(a, '\0', sizeof(a));
  64. memset(b, '\0', sizeof(b));
  65. memset(c, 0, sizeof(c));
  66. input(a, b);
  67. int j = 0, type = 0, la = 0, lb = 0;
  68. //scanf('%s', a);
  69. //scanf('%s', b);
  70. la = strlen(a);
  71. lb = strlen(b);
  72. if (la == lb)
  73. {
  74. if (strcmp(a, b) > 0)
  75. {
  76. type = 1;
  77. sub(a, b, c, la, lb);
  78. }
  79. else if(strcmp(a,b)<=0)
  80. {
  81. sub(b, a, c, lb, la);
  82. type = -1;
  83. }
  84. else
  85. {
  86. printf('0\n');
  87. }
  88. }
  89. else
  90. {
  91. if (la > lb)
  92. {
  93. sub(a, b, c, la, lb);
  94. type = 1;
  95. }
  96. else
  97. {
  98. sub(b, a, c, lb, la);
  99. type = -1;
  100. }
  101. }
  102. if (type == -1) printf('-');
  103. for (int i = 0; i < lc; i++)
  104. printf('%d', c[i]);
  105. printf('\n');
  106. return 0;
  107. }

 

(0)

相关推荐