473,388 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

return from my function is nan

Dear all,

I tried sth easy(actually this was an exercise) but I tried to use the
standard lib. heavily for this problem(as far as I can). There was one
point I could not figure out. The problem is :

../a.out 1.3+3.2+.1+40/3*8/7-4*5-32

The program will parse the argument and find the result of the above
expression. I have two versions(the 2nd is working , not perfect ;-}),
but the first have a return problem I guess because if I use that
version I get "nan" as the result at line 37. What is my problem?

Sorry that there are some comments and cout statement in around the
code. But I guess it could be easier to figure out.

My Best,

1: this version returns nan from the "performer function"
--------------
3 #include <iostream>
4 #include <string>
5 #include <vector>
6 #include <sstream>
7 #include <algorithm>
8 #include <cstdlib>
9 using namespace std;
10
11 template <typename T>
12 T str_To_num (const std::string & s)
13 {
14 T result;
15 std::istringstream stream (s);
16 if (stream >result)
17 return result;
18 else{
19 cerr << "error in conversion" <<endl;
20 return EXIT_FAILURE;
21 }
22 }
23 void parse_string(const
string&str,vector<double>&string_num_vec,vector<ch ar>&operators_vec){
24 string str_temp=str;
25 string operators("+-*/");
26 string value;
27 string::size_type index;
28 while((index=str_temp.find_first_of(operators))!
=string::npos){
29 operators_vec.push_back(str_temp[index]);
30 value=str_temp.substr(0,index);
31
string_num_vec.push_back(str_To_num<double>(value) );
32 str_temp=str_temp.substr(index+1);
33
if(str_temp.find_first_of(operators)==string::npos )
34
string_num_vec.push_back(str_To_num<double>(str_te mp));
35 }
36 }
37 double performer(vector<double>&vec_d,vector<char>&vec_ch ){
38 double result_temp;
39 string md("*/");
40 string pm("+-");
41 vector<char>::iterator iter;
42 vector<int>::size_type index;
43
if((iter=find_first_of(vec_ch.begin(),vec_ch.end() ,md.begin(),md.end())
)!=vec_ch.end())
44 ;
45 else
if((iter=find_first_of(vec_ch.begin(),vec_ch.end() ,pm.begin(),pm.end())
)!=vec_ch.end())
46 ;
47 else
48 ;
49 switch (*iter){
50 case '*':
51 index = iter-vec_ch.begin();
52 result_temp=vec_d[index]*vec_d[index+1];
53
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
54 vec_ch.erase(iter);
55 break;
56 case '/':
57 index = iter-vec_ch.begin();
58 result_temp=vec_d[index]/vec_d[index+1];
59
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
60 vec_ch.erase(iter);
61 break;
62 case '+':
63 index = iter-vec_ch.begin();
64 result_temp=vec_d[index]+vec_d[index+1];
65
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
66 vec_ch.erase(iter);
67 break;
68 case '-':
69 index = iter-vec_ch.begin();
70 result_temp=vec_d[index]-vec_d[index+1];
71
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
72 vec_ch.erase(iter);
73 break;
74 default:
75 ;
76 }
77 if(/*vec_d.size()!=1*/vec_ch.size()!=0){
78 for(vector<double>::const_iterator
i=vec_d.begin();i!=vec_d.end();++i)
79 cout << *i << endl;
80 cout << "------------"<<endl;
81 performer(vec_d,vec_ch);
82 }
83 else
84 return vec_d[0];
85 }
86 int main(int argc, char *argv[]){
87 if(argc!=2){
88 cout << "Usage error should be: <program_name
<mathematical expression" << endl;
89 cout << "Example: operator 1.3+3.2+.
1+40/3*8/7-4*5-32" << endl;
90 return EXIT_FAILURE;
91 }
92 // convert c-style string argument to a c++ string
93 string arg(argv[1]);
94 //cout << arg << endl;
95 vector<doublenums;
96 vector<charops;
97 parse_string(arg,nums,ops);
98 cout << performer(nums,ops) << endl;
99 return 0;
100}

2: on this version, at the end vector includes only one element that
is the result
-------------------------

1 // C++ way of parsing: heavy use of standard library, string parsing
functions
2 //
================================================== =====================
=====
3 #include <iostream>
4 #include <string>
5 #include <vector>
6 #include <sstream>
7 #include <algorithm>
8 #include <cstdlib>
9 using namespace std;
10
11 template <typename T>
12 T str_To_num (const std::string & s)
13 {
14 T result;
15 std::istringstream stream (s);
16 if (stream >result)
17 return result;
18 else{
19 cerr << "error in conversion" <<endl;
20 return EXIT_FAILURE;
21 }
22 }
23 void parse_string(const
string&str,vector<double>&string_num_vec,vector<ch ar>&operators_vec){
24 string str_temp=str;
25 string operators("+-*/");
26 string value;
27 string::size_type index;
28 while((index=str_temp.find_first_of(operators))!
=string::npos){
29 operators_vec.push_back(str_temp[index]);
30 value=str_temp.substr(0,index);
31
string_num_vec.push_back(str_To_num<double>(value) );
32 str_temp=str_temp.substr(index+1);
33
if(str_temp.find_first_of(operators)==string::npos )
34
string_num_vec.push_back(str_To_num<double>(str_te mp));
35 }
36 }
37 void performer(vector<double>&vec_d,vector<char>&vec_ch ){
38 double result_temp;
39 string md("*/");
40 string pm("+-");
41 vector<char>::iterator iter;
42 vector<int>::size_type index;
43
if((iter=find_first_of(vec_ch.begin(),vec_ch.end() ,md.begin(),md.end())
)!=vec_ch.end())
44 ;
45 else
if((iter=find_first_of(vec_ch.begin(),vec_ch.end() ,pm.begin(),pm.end())
)!=vec_ch.end())
46 ;
47 else
48 ;
49 switch (*iter){
50 case '*':
51 index = iter-vec_ch.begin();
52 result_temp=vec_d[index]*vec_d[index+1];
53
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
54 vec_ch.erase(iter);
55 break;
56 case '/':
57 index = iter-vec_ch.begin();
58 result_temp=vec_d[index]/vec_d[index+1];
59
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
60 vec_ch.erase(iter);
61 break;
62 case '+':
63 index = iter-vec_ch.begin();
64 result_temp=vec_d[index]+vec_d[index+1];
65
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
66 vec_ch.erase(iter);
67 break;
68 case '-':
69 index = iter-vec_ch.begin();
70 result_temp=vec_d[index]-vec_d[index+1];
71
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
72 vec_ch.erase(iter);
73 break;
74 default:
75 ;
76 }
77 if(/*vec_d.size()!=1*/vec_ch.size()!=0){
78 performer(vec_d,vec_ch);
79 }
80 //else
81 // return vec_d[0];
82 }
83 int main(int argc, char *argv[]){
84 if(argc!=2){
85 cout << "Usage error should be: <program_name
<mathematical expression" << endl;
86 cout << "Example: operator 1.3+3.2+.
1+40/3*8/7-4*5-32" << endl;
87 return EXIT_FAILURE;
88 }
89 // convert c-style string argument to a c++ string
90 string arg(argv[1]);
91 //cout << arg << endl;
92 vector<doublenums;
93 vector<charops;
94 parse_string(arg,nums,ops);
95 //cout << performer(nums,ops) << endl;
96 performer(nums,ops);
97 for(vector<double>::const_iterator iter=nums.begin();iter!
=nums.end();++iter)
98 cout << *iter << endl;
99 //for(vector<char>::const_iterator iter=ops.begin();iter!
=ops.end();++iter)
100 // cout << *iter << endl;
101 return 0;
102 }

Jan 28 '07 #1
2 2447
On Sun, 28 Jan 2007 08:28:26 -0800, utab wrote:
Dear all,

I tried sth easy(actually this was an exercise) but I tried to use the
standard lib. heavily for this problem(as far as I can). There was one
point I could not figure out. The problem is :

./a.out 1.3+3.2+.1+40/3*8/7-4*5-32

The program will parse the argument and find the result of the above
expression. I have two versions(the 2nd is working , not perfect ;-}), but
the first have a return problem I guess because if I use that version I
get "nan" as the result at line 37. What is my problem?

Sorry that there are some comments and cout statement in around the code.
But I guess it could be easier to figure out.
ummm....

I was going to suggest that you read the "dragon book" but upon closer
examination consider this. Since you are calling a.out remember that UNIX
shells usually expand '*' characters to wildcards. Quote your expression
then see what happens.
Jan 29 '07 #2
37 double performer(vector<double>&vec_d,vector<char>&vec_ch ){
38 double result_temp;
39 string md("*/");
40 string pm("+-");
41 vector<char>::iterator iter;
42 vector<int>::size_type index;
43
if((iter=find_first_of(vec_ch.begin(),vec_ch.end() ,md.begin(),md.end())
)!=vec_ch.end())
44 ;
45 else
if((iter=find_first_of(vec_ch.begin(),vec_ch.end() ,pm.begin(),pm.end())
)!=vec_ch.end())
46 ;
47 else
48 ;
49 switch (*iter){
50 case '*':
51 index = iter-vec_ch.begin();
52 result_temp=vec_d[index]*vec_d[index+1];
53
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
54 vec_ch.erase(iter);
55 break;
56 case '/':
57 index = iter-vec_ch.begin();
58 result_temp=vec_d[index]/vec_d[index+1];
59
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
60 vec_ch.erase(iter);
61 break;
62 case '+':
63 index = iter-vec_ch.begin();
64 result_temp=vec_d[index]+vec_d[index+1];
65
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
66 vec_ch.erase(iter);
67 break;
68 case '-':
69 index = iter-vec_ch.begin();
70 result_temp=vec_d[index]-vec_d[index+1];
71
vec_d.insert(vec_d.erase(vec_d.erase(vec_d.begin()
+index)),result_temp);
72 vec_ch.erase(iter);
73 break;
74 default:
75 ;
76 }
77 if(/*vec_d.size()!=1*/vec_ch.size()!=0){
78 for(vector<double>::const_iterator
i=vec_d.begin();i!=vec_d.end();++i)
79 cout << *i << endl;
80 cout << "------------"<<endl;
81 performer(vec_d,vec_ch);
Here is undefined return value.
82 }
83 else
84 return vec_d[0];
85 }
You should try to compile it with all warnings turned on. Then you
will see, that in function perform is sometimes undefined returned
value, because it simply does not return any value.

Jan 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Colin Graham | last post by:
Im working on some asp code using vbscript but i cant seem to get the following code to work. I can get the result from the database but im having to joy returning the value to the html code in...
17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
2
by: Bryan Parkoff | last post by:
I set Function Pointer variable to private so unauthorized users cannot access it, but they are allowed to use Get_ and Set_ functions to modify Function Pointer variable. Look at my example. ...
1
by: XYZ | last post by:
dear group. I have a table with a few columns and I have a form to add/modify/ delete records now whenever a new record is added, i want one column "logged By" to automatically get the...
3
by: poojagupta | last post by:
Hi,I am confused and need the answer of the question mentioned below soon. return(x,y) is valid? Can a return() function return more than one value within one statement. eg: #include<stdio.h>...
2
by: Jeff | last post by:
I had this little bit of code: function parseTemplateData($file){ $file2=preg_replace_callback( "/<edit(.*?)>/s", "parseEditTag", $file); echo $file2;
13
by: Steve | last post by:
On page 392 of "Javascript the definitive guide" a function is called like this:- <form action="processform.cgi" onsubmit="return validateForm();"> Why, in this instance, is the return...
7
by: vimal | last post by:
Hi guys, <input class="btn" type="submit" name="terminate" value="Terminate" onclick="return check_proc_sel()" /> i have returned false from check_proc_sel() but still the form's submit event...
33
by: zamaam0728 | last post by:
I am at the end of my first semester of C++, and I'm not sure what I should do to make this program meet the requested specifications. The assignment is as follows: Write a function called...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.