关于c->c++过渡的一些小细节

本文介绍了C++编程的基础知识,包括头文件的引入方式及其区别,以及三种常用的调试方法:预处理宏定义、自定义打印宏和assert断言。通过这些技巧,可以帮助开发者更有效地进行代码调试。

1头文件

传统的c,#include<stdio.h>

当然c++可以继续采用,但是以示区别#include<cstdio> using namespace std;

对于c++特有的#include<iostream.h>  也可以写成#include<iostream> using namespace std;

如果没有.h,则后面一定要添加using namespace std;名字空间,它使得c++定义集被封装在独立的名字空间中,及时有同名的情况也不会发生冲突。

using 这个关键字 使得c++标准库都封装在一个名字空间中,,而后续的std,打开std名字空间,使它所有的名字都可用。

2关于调式。

第一种,预处理方式

在代码前。如果是多个文件,.h中声明

#define _DEBUG


在需要调式的代码中

#ifdef _DEBUG

  printf();

#endif


如果不需要这段调式代码,可以修改宏#define 为#undef _DEBUG   这里有个禁区,不要使用NDEBUG这是c的保留字


第二种,我们采用的仍然是宏定义

#define p(x) cout<<#x"="<<x<<"\n";

这里的#会把x当做字符数组处理,类似于cout<<"x="<<x<<"\n";


第三种,我们使用assert()宏

这里需要添加#include<cassert>

int i=100;

assert(i!=100); 里面的语句是i!=100,但是我们明明i=100,因此会提示有错误。程序执行到该句会停止下来。

如果不需要这个功能,记住在#include<cassert>之前加入#define NDEBUG 这里可以使用NDEBUG



void ModelNewmodel::run(UByte dim,State *s) {        ConstitutiveModel::run(dim,s);        if (s->modulus_reduction_factor_>0.0) {            Double shear_new = shear_ * s->modulus_reduction_factor_;            e1_ = bulk_ + shear_new * d4d3;            e2_ = bulk_ - shear_new * d2d3;            g2_ = 2.0 * shear_new;            Double ra = e1_ - rnps_ * e2_;            Double rb = e2_ - rnps_ * e1_;            Double rd = ra - rb * nph_;            sc1_  = ra / rd;            sc3_  = rb / rd;            sc2_  = e2_ * (1.0 - rnps_) / rd;            e21_  = e2_ / e1_;        }        // plasticity indicator:        // store 'now' info. as 'past' and turn 'now' info off         if (s->state_ & shear_now) s->state_ |= shear_past;        s->state_ &= ~shear_now;        if (s->state_ & tension_now) s->state_ |= tension_past;        s->state_ &= ~tension_now;        int plas = 0;         /* --- trial elastic stresses --- */        Double e11 = s->stnE_.s11();//strain tensors normal components        Double e22 = s->stnE_.s22();        Double e33 = s->stnE_.s33();        s->stnS_.rs11() += e11 * e1_ + (e22 + e33) * e2_;        s->stnS_.rs22() += (e11 + e33) * e2_ + e22 * e1_;        s->stnS_.rs33() += (e11 + e22) * e2_ + e33 * e1_;         s->stnS_.rs12() += s->stnE_.s12() * g2_;        s->stnS_.rs13() += s->stnE_.s13() * g2_;        s->stnS_.rs23() += s->stnE_.s23() * g2_;         // default settings, altered below if found to be failing        s->viscous_ = true;  // Allow stiffness-damping terms         if (canFail()) {            // Calculate principal stresses            SymTensorInfo info;            DVect3 prin = s->stnS_.getEigenInfo(&info);             /* --- Mohr-Coulomb failure criterion --- */            Double fsurf = prin.x() - nph_ * prin.z() + csn_;             /* --- Tensile failure criteria --- */            Double tsurf = tension_ - prin.z();            Double pdiv = -tsurf + (prin.x() - nph_ * tension_ + csn_) * bisc_;             /* --- tests for failure */             if (fsurf < 0.0 && pdiv <= 0.0) {                 plas = 1;                /* --- shear failure: correction to principal stresses ---*/                s->state_ |= shear_now;                prin.rx() -= fsurf * sc1_;                prin.ry() -= fsurf * sc2_;                prin.rz() -= fsurf * sc3_;            }             else if (tsurf < 0.0 && pdiv > 0.0) {                plas = 2;                /* --- tension failure: correction to principal stresses ---*/                s->state_ |= tension_now;                Double tco = e21_ * tsurf;                prin.rx() += tco;                prin.ry() += tco;                prin.rz()  = tension_;            }             if (plas) {                s->stnS_ = info.resolve(prin);// transform back to refrence frame                s->viscous_ = false; // Inhibit stiffness-damping terms            }         }    }结合这个来看呢
11-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值