使用3.14重新激活pi

编程入门 行业动态 更新时间:2024-10-26 12:25:10
本文介绍了使用3.14重新激活pi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

用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); } }

推荐答案

这里有很多问题,从开始prt1 没有足够的空间容纳你正在连接的数据,但更重要的是,你没有使用strtok甚至接近正确。 查看文档: strtok - C ++参考 [ ^ ] - 它包含一个如何使用它的示例。扔掉那些代码,一旦你阅读完文件并了解你在做什么就重新开始,而不仅仅是猜测和希望最好! There are a load of problems here, starting with prt1 not containing enough space for the data you are concatenating into it, but more importantly, you aren't using strtok even close to correctly. Look at the documentation: strtok - C++ Reference[^] - it includes an example of how to use it. Throw that code away, and start again once you have read the documents and understand what you are doing, instead of just guessing and hoping for the best!

你为什么要使用 C - 中的字符串C ++ 代码? 尝试: Why do you use C-like string in C++ code? Try: #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

本文发布于:2023-10-26 09:23:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529753.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:pi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!