虽然程序还有一些问题,但是最重要的部分终于弄明白了,参考了一下Mr.Wint的博客http://www.cnblogs.com/duasonir/p/5269335.html#3380168,还有些差距,但是我没有放弃,最终功夫不负有心人,结果正确了。
后面的结果有些凌乱,仔细检查程序之后,感觉错误应该是s1.str()这个函数输出时候自带回车,有时间在检查一下。
~~找到了问题所在,原来是使用流的时候多赋值了一个endl,当时居然没检查出来。然后加了个小程序,使减法和除法的结果更可靠,不再为0;运行之后又检查出错误了— —!
// xiaoxue.cpp : 定义控制台应用程序的入口点。 //
#include "stdafx.h" #include "iostream" #include "string" #include "time.h" #include "cstdio" #include<sstream> using namespace std; static int n=1; static string str=""; static int sum=0; void grow() { ostringstream s1; if(n==1) { int b=rand()%100+1; sum = sum + b; s1 <<b<< endl;//原来错误在这里!!已改正~ str = str + s1.str(); } else { int a=rand()%100+1; int or=rand()%2; int c=rand()%4+1; if(or)// you { if(n<=4) { if(c==1) { sum = sum + a; s1 << a << endl; str = "(" + str + "+" + s1.str() + ")";//这个函数输出自带回车,还没有解决。 } else if(c==2) { sum = sum - a; s1 << a << endl; str = "(" + str + "-" + s1.str() + ")"; } else if (c==3) { sum = sum * a; s1 << a << endl; str = str + "*" + s1.str(); } else if (c==4) { sum = sum / a; s1 << a << endl; str = str + "/" + s1.str(); } } else { if(c==1) { sum = sum + a; s1 << a << endl; str = str + "+" + s1.str(); } else if(c==2) { sum = sum - a; s1 << a << endl; str = str + "-" + s1.str(); } else if (c==3) { sum = sum * a; s1 << a << endl; str = str + "*" + s1.str(); } else if (c==4) { sum = sum / a; s1 << a << endl; str = str + "/" + s1.str(); } } } else //zuo { if(n<=4){ if(c==1) { sum = a + sum; s1 << a << endl; str = "(" + s1.str() + "+" + str+ ")"; } else if(c==2) { sum = a - sum; s1 << a << endl; str = "(" + s1.str() + "-" + str + ")"; } else if (c==3) { sum = a * sum; s1 << a << endl; str = s1.str() + "*" + str; } else { if(sum==0) { sum = rand()%100+1; } sum = a / sum; s1 << a << endl; str = s1.str() + "/" + str; } } else if(c==1) { sum = a + sum; s1 << a << endl; str = s1.str() + "+" + str; } else if(c==2) { sum = a - sum; s1 << a << endl; str = s1.str() + "-" + str; } else if (c==3) { sum = a * sum; s1 << a << endl; str = s1.str() + "*" + str; } else { if(sum==0) { sum = rand()%100+1; } sum = a / sum; s1 << a << endl; str = s1.str() + "/" + str; } } } n++; if(n<6) { grow(); } } void born() { n=1; str=""; sum=0; ostringstream s1(""); grow(); cout<<str<<"="<<sum<<endl; } int _tmain(int argc, _TCHAR* argv[]) { srand((unsigned)time(NULL)); for(int i=0;i<30;i++) { cout<<i+1<<"."; born(); } getchar(); return 0; }
这个程序快把我的F10,F11按烂了,一次一次的报错感觉天昏地暗。结果带来的不光是喜悦,更多的是自信。相信自己会做的更好。
下面是最初的版本
// xiaoxue.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "iostream" #include "string" #include "time.h" #include "cstdio" #include<sstream> using namespace std; static int n=1; static int j = 0; static string str=""; static int sum=0; void grow() { ostringstream s1; if(n==1) { int b=rand()%100+1; sum = sum + b; s1 <<b<< endl; str = str + s1.str(); } else { int a=rand()%100+1; int or=rand()%2; int c=rand()%4+1; if(or)// you { if(j<3) { if(c==1) { sum = sum + a; s1 << a << endl; str = "(" + str + "+" + s1.str() + ")"; } else if(c==2) { sum = sum - a; s1 << a << endl; str = "(" + str + "-" + s1.str() + ")"; } } else { if(j<3) { if(c==1) { sum = sum + a; s1 << a << endl; str = str + "+" + s1.str(); } else if(c==2) { sum = sum - a; s1 << a << endl; str = str + "-" + s1.str(); } } else { if(c==1) { sum = sum + a; s1 << a << endl; str = str + "+" + s1.str(); } else if(c==2) { sum = sum - a; s1 << a << endl; str = str + "-" + s1.str(); } else if (c==3) { sum = sum * a; s1 << a << endl; str = str + "*" + s1.str(); } else { sum = sum / a; s1 << a << endl; str = str + "/" + s1.str(); } } } } else //zuo { if(c==1) { sum = a + sum; s1 << a << endl; str = s1.str() + "+" + str; } else if(c==2) { sum = a - sum; s1 << a << endl; str = s1.str() + "-" + str; } else if (c==3) { sum = a * sum; s1 << a << endl; str = s1.str() + "*" + str; } else { sum = a / sum; s1 << a << endl; str = s1.str() + "/" + str; } } } n++; j++; if(n<=6) { grow(); } } void born() { n=1; str=""; grow(); ostringstream s1(""); cout<<str<<"="<<sum<<endl; } int _tmain(int argc, _TCHAR* argv[]) { srand((unsigned)time(NULL)); for(int i=0;i<30;i++) { cout<<i+1<<"."; born(); } getchar(); return 0; }
错误挺多的,最后都改正了。