zoukankan      html  css  js  c++  java
  • 使用预处理器进行调试

    一、预处理指令

    问题:输入一些WORD,判断有没有首字母大写的两个相同的词

     1 #include<iostream>
     2 #include<string>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     string currWord, preWord;
     9     cout << "Enter some words:(ctrl+z to end)" << endl;
    10     while (cin >> currWord)
    11     {
    12 #ifndef NDEBUG
    13         cout << endl << "调试:" << currWord << endl;  //这是调试用的
    14 #endif
    15         if (!isupper(currWord[0]))
    16             continue;
    17         if (currWord == preWord)
    18             break;
    19         else preWord = currWord;
    20     }
    21     if (currWord == preWord&&!currWord.empty())
    22     {
    23         cout << "The repeated word:" << currWord << endl;
    24     }
    25     else
    26         cout << "没有重复的!" << endl;
    27     system("pause");
    28 }

    关闭预处理:

    解决资源方案--demo--右键“属性”--c++--命令行----附加选项:/DNDEBUG

    结果:

    二、预处理常量

     1 #include<iostream>
     2 #include<string>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     cout << "文件:" << __FILE__ << endl<<"行:" << __LINE__ << endl<<"日期:" << __DATE__ << endl<<"时间:" << __TIME__ << endl;
     9     system("pause");
    10 }

    三、assert断言

     1 #include<iostream>
     2 #include<assert.h>
     3 
     4 using namespace std;
     5 
     6 int add(int x, int y)
     7 {
     8     return x * y;
     9 }
    10 int main()
    11 {
    12     int result;
    13     result = add(1, 2);
    14     assert(result == 3);
    15     cout << result << endl;
    16     system("pause");
    17 }

    断言也用来调试,与预定义一样,受附加选项/DNDEBUG控制

    坚持比努力更重要
  • 相关阅读:
    Linux 下判断磁盘是ssd还是hdd
    Ceph rgw COR测试
    nfs 挂载选项
    【Linux命令】dmsetup--device mapper 管理工具(更底层的管理工具)
    Device Mapper 存储介绍
    easyui combotree 默认 初始化时就选中
    EasyUI 添加tab页(iframe方式)(转)
    EasyUI DataGrid 配置参数
    EasyUI 后台接受DataGrid传来的参数
    (转)combogrid的代码实例
  • 原文地址:https://www.cnblogs.com/dameidi/p/9284160.html
Copyright © 2011-2022 走看看