最近做c++笔试题都快做吐了。不过感觉回顾基础是还是有收获的,以前用库时的不解有时可以想得通了。不过这段时间真的是做吐了,理论这东西还是要通过应用程序来实践的好。
atoi()函数
#include "stdafx.h" #include <iostream.h> #include <string.h> #include <math.h> int stoi(const char *str) { int len=strlen(str); int string=0; for (int i=0;i<len;i++) { if(str[i]<47||str[i]>58) { return 0; } string=string+(str[i]-48)*pow(10,(len-i-1));//VC下某某的几次方没有现成的运算符,就有了math.h下的pow函数 } return string; } int main(int argc, char* argv[]) { char *str="38"; int string=stoi(str); cout<<string<<endl; return 0; }
#include "stdafx.h" #include <iostream.h> #include <string.h> char* Reverse(char *str,int length); int main(int argc, char* argv[]) { char str[7]="woaini"; Reverse(str,strlen(str)); cout<<str<<endl; return 0; } char* Reverse(char *str,int length) { char s; for (int i=0;i<length/2;i++) { if (str[i]==str[length-1-i]) { } else { s=str[i]; str[i]=str[length-1-i]; str[length-1-i]=s; } } str[length]='\0'; return str; }
// 大小写转换.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string.h> #include <iostream.h> char *transform(char *cstr,char *str,int length); int main(int argc, char* argv[]) { char *str="woaini38"; cout<<str<<endl; char s[10]; transform(s,str,strlen(str)); cout<<s<<endl; return 0; } char *transform(char *cstr,char *str,int length) { for(int i=0;i<length+1;i++) { if (str[i]>='A'&&str[i]<='Z') { cstr[i]=str[i]+'a'-'A'; continue; } if (str[i]>='a'&&str[i]<='z') { cstr[i]=str[i]-'a'+'A'; } else { cstr[i]=str[i]; } } return cstr; }