本文介绍了使用3.14重新激活pi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 用3.14替换pi字。它显示输出但强迫我关闭程序,任何关于此的帮助将不胜感激。 什么我试过了:replacing pi word with 3.14 .it is showing the output but forcing me to close the program,any help regarding this will be appreciated.What I have tried:#include <iostream>#include <vector>#include <string>#include<cstring>#include <sstream>using namespace std;void swa(char b[]);int main(){ char str[]="pi day is celebrated in march, pi day is on 14 march"; swa(str);}void swa(char b[]){ int i; char * str=new char(); str=strtok(b," "); char *ptr1=new char(); strcat(ptr1,str); strcat(ptr1," "); if(strcmp(str,"pi")==0) { ptr1="3.14 "; // cout<<ptr1<<" "; } if(str!=NULL) { cout<<ptr1; swa(b+strlen(str)+1); }}推荐答案#include <iostream>#include <string>using namespace std;int main(){ string s = "pi day is celebrated in march, pi day is on 14 march"; size_t pos = 0; while ( (pos = s.find("pi", pos)) != string::npos ) { s.replace(pos,2, "3.14"); } cout << s << endl;} 这篇关于使用3.14重新激活pi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-31 06:11