MacRumors Forums Kochan 2.0 Exercise 10-6
 User Name Remember Me? Password

 Thread Tools Search this Thread Display Modes
 Jan 25, 2009, 09:53 AM #1 mdeh macrumors 6502   Join Date: Jan 2009 Kochan 2.0 Exercise 10-6 Here is the exercise in full: Based on the following definitions float f = 1.00; short int i = 100; long int l = 500L; double d = 15.00; and the seven steps outlined in this chapter for the conversion of operands in expressions, determine the type and value of the following expressions: f + i l / d i / l + f l * i f / 2 i / (d + f) l / (i * 2.0) l + i / (double) l >>>>>>>>>>>>>>>>> Here is what I **thought** I would try and do. someGenericType result; result = f + i; if (SomeTestToDetermineResult == float) NSLOG(@"Result of f + i is a float"); But quickly ran into problems trying to find a "someGenericType". I **think** what the author means is using the "grammatical rules of syntax, what would you expect to find, as opposed to a program that does what I am trying to do, viz use a generic return data type, then test that type and declare what it is. Any ideas? 0
 Jan 25, 2009, 11:39 AM #2 lee1210 macrumors 68040     Join Date: Jan 2005 Location: Dallas, TX I could not come up with a way to do this in C or Objective-C (take an expression and get its type). The typeof gcc extension would allow you to define a new type with the type of an expression, but that still doesn't help. Using the sizes you can narrow it down, but it's not an "answer": Code: ```#include const char *type_string(int); int main(int argc, char *argv[]) { float f = 1.00; short int i = 100; long int l = 500L; double d = 15.00; printf("Size of f + i: %d\n",sizeof(f + i)); printf("Size of l / d: %d\n",sizeof(l / d)); printf("Size of i / l + f: %d\n",sizeof(i / l +f)); printf("Size of l * i: %d\n",sizeof(l * i)); printf("Size of f / 2: %d\n",sizeof(f / 2)); printf("Size of i / (d + f): %d\n",sizeof(i / (d + f))); printf("Size of l / (i * 2.0): %d\n",sizeof(l / (i * 2.0))); printf("Size of l + i / (double) l: %d\n",sizeof(l + i / (double) l)); printf("Type of f + i: %s\n",type_string(sizeof(f + i))); printf("Type of l / d: %s\n",type_string(sizeof(l / d))); printf("Type of i / l + f: %s\n",type_string(sizeof(i / l +f))); printf("Type of l * i: %s\n",type_string(sizeof(l * i))); printf("Type of f / 2: %s\n",type_string(sizeof(f / 2))); printf("Type of i / (d + f): %s\n",type_string(sizeof(i / (d + f)))); printf("Type of l / (i * 2.0): %s\n",type_string(sizeof(l / (i * 2.0)))); printf("Type of l + i / (double) l: %s\n",type_string(sizeof(l + i / (double) l))); return 0; } const char *type_string(int in) { switch(in) { default: return "unknown type"; break; case 1: return "char"; break; case 2: return "short int"; break; case 4: return "float or int"; break; case 8: return "double or long int"; break; case 12: case 16: return "long double"; break; } }``` Note that i am only posting code because i don't think you are supposed to write code for this, but follow the implicit casting/promotion rules to solve this. The results of this program are ambiguous enough that it doesn't give it away for you. Since C++ is more type-safe, and allows for overloading functions, you can do this there... I honestly can't say if the rules are the same in C and C++. There is enough else that is different between the two that i wouldn't bet my life on it, but i am pretty sure they are the same in this case. Anyhow, don't run this code unless you want the answers given away: Code: ```#include #include std::string myType(char); std::string myType(short int); std::string myType(int); std::string myType(long int); std::string myType(float); std::string myType(double); std::string myType(long double); int main(int argc, char *argv) { float f = 1.00; short int i = 100; long int l = 500L; double d = 15.00; std::cout <<"Type of f + i: " << myType(f + i) << std::endl; std::cout <<"Type of l / d: "<
Jan 25, 2009, 11:46 AM   #3
mdeh
macrumors 6502

Join Date: Jan 2009
Quote:
 Originally Posted by lee1210 I could not come up with a way to do this in C or Objective-C (take an expression and get its type). The typeof gcc extension would allow you to define a new type with the type of an expression, but that still doesn't help. ......snip.... Someone may be more clever than I am and be able to figure out a way to handle this in plain C or Objective-C, but it is definitely impractical, and once more not likely to be what the author was hoping for. -Lee
Bravo!!! Great effort...I am impressed.
I think the conclusion you reached is probably correct. With the author's directive that answers only use the material covered to that point, there is no way to do this other than using the grammatical syntax rules provided earlier in the chapter.
But...thanks again for the effort.
0

 MacRumors Forums

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Linear Mode

 Similar Threads thread Thread Starter Forum Replies Last Post afousek iPhone/iPad Programming 4 Apr 26, 2014 01:45 PM