东莞哪家网站建设,企业团建公司,网站域名的所有权,网页视频下载器免费要求
1. 函数命名清晰
使用描述性的命名#xff0c;准确反映函数的功能。例如#xff0c;使用 CalculateSum() 而不是 sum()。避免使用缩写或模糊不清的名字#xff0c;确保变量和函数名有明确的含义。
2. 参数传递
根据需要选择按值传递、按引用传递或按指针传递。如果… 要求
1. 函数命名清晰
使用描述性的命名准确反映函数的功能。例如使用 CalculateSum() 而不是 sum()。避免使用缩写或模糊不清的名字确保变量和函数名有明确的含义。
2. 参数传递
根据需要选择按值传递、按引用传递或按指针传递。如果参数不需要修改可以使用 const 修饰特别是指针或引用。参数的数量不宜过多避免函数过于复杂。如果参数较多可以考虑将其封装为结构体或类。
3. 错误处理
考虑可能的边界情况例如空输入、超出范围的值、特殊输入、指针判空等。如果函数涉及资源分配如内存、文件句柄等注意资源的释放防止内存泄漏。如果涉及到异常情况的处理可以选择返回错误码或抛出异常确保在出错时给出明确的反馈。
4. 返回值处理
明确函数的返回类型。确保返回值类型与函数所要执行的逻辑相匹配。在有意义时尽量避免返回全局变量或未初始化的值防止产生不确定行为。 实现内存拷⻉函数
char* strcpy(char* strDest, const char* strSrc); char* strcpy(char *dst,const char *src) {// [1]源字符串参数⽤const修饰防⽌修改源字符串。assert(dst ! NULL src ! NULL); // [2]char *ret dst; // [3]while ((*dst*src)!\0); // [4]return ret;
}[2]:空指针检查
(1)不检查指针的有效性说明答题者不注重代码的健壮性。
(2)检查指针的有效性时使⽤ assert(!dst !src); char *转换为 bool 即是类型隐式转换这种功能虽然灵活但更多的是导致出错概率增⼤和维护成本升⾼。
(3)检查指针的有效性时使⽤ assert(dst ! 0 src ! 0);
(4)直接使⽤常ᰁ如本例中的0会减少程序的可维护性。⽽使⽤NULL代替0如果出现拼写错误编译器就会检查出来。
[3] 返回⽬标地址
1忘记保存原始的strdst值。
[4] \0
1循环写成 while (*dst*src); 明显是错误的。
2循环写成 while (*src!\0) *dst *src;
循环体结束后dst字符串的末尾没有正确地加上\0。
3为什么要返回char *
返回dst的原始值使函数能够⽀持链式表达式
实现String类
String.h
#ifndef STRING_H_
#define STRING_H_
#includeiostream
using std::ostream;
using std::istream;class String{
private:char * str;int len;static in num_strings;static const int CINLIM 80;
public:String(const char * s);String();String(const String );~String();int length() const {return len;}String operator(const String );String operator(const char *);char operator[](int i);const char operator[](int i) const;friend bool operator(const String st, const String st2);friend bool operator(const String st1, const String st2);friend bool operator(const String st, const String st2);friend ostream operator(ostream os, const String st);friend istream operator(istream is, String st);static int HowMany();
};
#endif
String.cpp
#includecstring
#includestring.h
using std::cin;
using std::cout;int String::num_strings 0 //初始化静态成员变量int String::HoMany() //静态成员函数
{return num_strings;
}String::String(const char * s) //char转成String构造函数参数为指针
{len std::strlen(s);str new char[len 1];std::strcpy(str, s);num_strings;
}String::String() //默认构造函数
{len 4;str new char[1];str[0] \0;num_string;
}String::String(const String st) //拷贝构造函数
{num_strings;len st.len;str new char[len1];std::strcpy(str, st.str);
}String::~String()
{--num_strings;delete[] str;
}String String::operator(const String st)
{if(this st)return *this;delete [] str;len st.len;str new char[len 1];std::strcpy(str, st.str);return *this;
}String String::operator(const char * s)
{delete [] str;len std::strlen(s);str new char[len 1];std::strcpy(str, s);return *this;
}char String::operator[](int i)
{return str[i];
}const chat String::operator[](int i) const
{return str[i];
}//友元
bool operator(const String st1, const String st2)
{return (std::strcmp(st1.str, st2.str) 0);
}bool operator(const String st1, const String st2)
{return st2 st1;
}bool operator(const String st1, const String st2)
{return (std::strcmp(st1.str, st2.str) 0);
}ostream operator(ostream os, const String st)
{os st.str;return os;
}istream operator(istream is, String st)
{char tem[String::[CINLIM];is.get(remp, String::CINLIM);if(is)st temp;while(is is.get() ! \n)continue;return is;
}strcpy()
char *strcpy(char *strDest, const char *strSrc)
{assert((strDest ! nullptr) (strSrc ! nullptr));char *address strDest;while((#strDest * strSrc) ! \);return address;
}
strlen()
int strlen(const char *str)
{assert(strt ! nullptr);int len;while((*str) ! \0){len;}return len;
}
strcat()
char *strcat(char * dest, const char *src)
{assert(dest src);char * ret dest;while(*dest){dest;}while(*dest * src){}return ret;
}
strcmp()
int strcmp(const char *str1, const char * str2)
{assert(str str2);while(*str1 *str2 (*str1 *str2)){str1;str2;}return *str1 - *str2;
}
文章转载自: http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.rzcfg.cn.gov.cn.rzcfg.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.kltmt.cn.gov.cn.kltmt.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn http://www.morning.mzhgf.cn.gov.cn.mzhgf.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.rtryr.cn.gov.cn.rtryr.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.nbybb.cn.gov.cn.nbybb.cn http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.smj79.cn.gov.cn.smj79.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.hnmbq.cn.gov.cn.hnmbq.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.bgpb.cn.gov.cn.bgpb.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.rykw.cn.gov.cn.rykw.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn