Advertisement
89yoyos

Source Code: Math Helper Main.m

Feb 11th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.32 KB | None | 0 0
  1. #import <UIKit/UIKit.h>
  2. #import <QuartzCore/QuartzCore.h>
  3.  
  4. #include <sys/socket.h>
  5. #include <arpa/inet.h>
  6. #include <netinet/in.h>
  7. #include <libgen.h>
  8.  
  9.  
  10. // the following definition sends all debug messages and NSLog macros to a
  11. // specific file in the app's Documents directory. Useful for debugging.
  12. #define DEBUG_LOG_TO_FILE "debug.log"
  13.  
  14. // the following definition sends all debug messages and NSLog macros to a
  15. // remote debug console over TCP/IP. Don't release your app with this enabled!
  16. //#define DEBUG_LOG_TO_REMOTE_CONSOLE "192.168.1.105"
  17.  
  18.  
  19. // interface for our application class. We derive it from UIApplication.
  20. @interface TestProjectApp : UIApplication
  21. {
  22.     UIWindow *window; // our application's main window
  23.     UITextField *sample_text; // some sample text field
  24.     UITextField *Hex;
  25.     UITextField *pentLabel;
  26.     UITextField *Pent;
  27.     UITextField *quadLabel;
  28.     UITextField *Quad;
  29.     UITextField *triLabel;
  30.     UITextField *Tri;
  31.     UITextField *diLabel;
  32.     UITextField *Di;
  33.     UITextField *unLabel;
  34.     UITextField *Un;
  35.     UITextField *Con;
  36.     UIButton *sample_button; // some sample button
  37.     UIButton *NavButton;
  38.     UINavigationBar *myNavBar;
  39.     UITextView *popup;
  40. }
  41.  
  42. - (void) applicationDidFinishLaunching: (UIApplication *) application;
  43. - (void) sampleButtonPressed;
  44. - (void) NavTouch;
  45.  
  46. @end
  47.  
  48.  
  49. // implementation of our application class, as defined in its interface.
  50. @implementation TestProjectApp
  51.  
  52. - (void) applicationDidFinishLaunching: (UIApplication *) application
  53. {
  54.     // this message is sent when the Cocoa framework has just finished starting
  55.     // our application and is passing control to us. By implementing it we can
  56.     // call our own initialization functions. Also, since we don't use any .nib
  57.     // file, we can create our application windows, buttons and Cocoa controls
  58.     // ourselves here. Later on, when actions will happen on them, Cocoa will
  59.     // send their respective handling messages to our code, provided we
  60.     // implement them.
  61.  
  62.     // place your own startup code here.
  63.  
  64.     // create a full-screen window with a black background. Do it manually
  65.     // since we don't rely on a .nib file to do it for us.
  66.     window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
  67.     [window setBackgroundColor: [UIColor blackColor]];
  68.    
  69.  
  70.     //declare width as a variable equalling the screen's width
  71.     CGFloat width = [UIScreen mainScreen].bounds.size.width;
  72.     CGFloat height = [UIScreen mainScreen].bounds.size.height;
  73.  
  74.  
  75.     // create a sample text field window covering the specified rectangle and
  76.     // add this window as a child window of the main window
  77.  
  78.     //Label for x^6
  79.     sample_text = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 75, 65, 30)];
  80.     sample_text.font = [UIFont boldSystemFontOfSize: 18.0];
  81.     sample_text.text = @"X^6";
  82.     sample_text.backgroundColor = [UIColor whiteColor];
  83.     sample_text.userInteractionEnabled = NO;
  84.     [window addSubview: sample_text];
  85.  
  86.     //Input for X^6
  87.     Hex = [[UITextField alloc] initWithFrame: CGRectMake(20, 75, width-110, 30)];
  88.     Hex.font = [UIFont boldSystemFontOfSize: 18.0];
  89.     Hex.text = @"0";
  90.     //Commenting the next line of code out so I'll have it for refrence in the future.
  91.     //Sets the type of keyboard to numerice keypad
  92.  
  93.     //Hex.keyboardType = UIKeyboardTypeNumberPad;
  94.  
  95.     //Doesn't work well with this app because there's no negative button.
  96.     Hex.backgroundColor = [UIColor whiteColor];
  97.     [window addSubview: Hex];
  98.  
  99.     //Label for x^5
  100.     pentLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 125, 65, 30)];
  101.     pentLabel.font = [UIFont boldSystemFontOfSize: 18.0];
  102.     pentLabel.text = @"X^5";
  103.     pentLabel.backgroundColor = [UIColor whiteColor];
  104.     pentLabel.userInteractionEnabled = NO;
  105.     [window addSubview: pentLabel];
  106.  
  107.     //Input for X^5
  108.     Pent = [[UITextField alloc] initWithFrame: CGRectMake(20, 125, width-110, 30)];
  109.     Pent.font = [UIFont boldSystemFontOfSize: 18.0];
  110.     Pent.text = @"0";
  111.     Pent.backgroundColor = [UIColor whiteColor];
  112.     [window addSubview: Pent];
  113.  
  114.  
  115.     //Label for x^4
  116.     quadLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 175, 65, 30)];
  117.     quadLabel.font = [UIFont boldSystemFontOfSize: 18.0];
  118.     quadLabel.text = @"X^4";
  119.     quadLabel.backgroundColor = [UIColor whiteColor];
  120.     quadLabel.userInteractionEnabled = NO;
  121.     [window addSubview: quadLabel];
  122.  
  123.     //Input for X^4
  124.     Quad = [[UITextField alloc] initWithFrame: CGRectMake(20, 175, width-110, 30)];
  125.     Quad.font = [UIFont boldSystemFontOfSize: 18.0];
  126.     Quad.text = @"0";
  127.     Quad.backgroundColor = [UIColor whiteColor];
  128.     [window addSubview: Quad];
  129.  
  130.  
  131.     //Label for x^3
  132.     triLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 225, 65, 30)];
  133.     triLabel.font = [UIFont boldSystemFontOfSize: 18.0];
  134.     triLabel.text = @"X^3";
  135.     triLabel.backgroundColor = [UIColor whiteColor];
  136.     triLabel.userInteractionEnabled = NO;
  137.     [window addSubview: triLabel];
  138.  
  139.  
  140.     //Input for X^3
  141.     Tri = [[UITextField alloc] initWithFrame: CGRectMake(20, 225, width-110, 30)];
  142.     Tri.font = [UIFont boldSystemFontOfSize: 18.0];
  143.     Tri.text = @"0";
  144.     Tri.backgroundColor = [UIColor whiteColor];
  145.     [window addSubview: Tri];
  146.  
  147.     //Label for x^2
  148.     diLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 275, 65, 30)];
  149.     diLabel.font = [UIFont boldSystemFontOfSize: 18.0];
  150.     diLabel.text = @"X^2";
  151.     diLabel.backgroundColor = [UIColor whiteColor];
  152.     diLabel.userInteractionEnabled = NO;
  153.     [window addSubview: diLabel];
  154.  
  155.     //Input for X^2
  156.     Di = [[UITextField alloc] initWithFrame: CGRectMake(20, 275, width-110, 30)];
  157.     Di.font = [UIFont boldSystemFontOfSize: 18.0];
  158.     Di.text = @"0";
  159.     Di.backgroundColor = [UIColor whiteColor];
  160.     [window addSubview: Di];
  161.  
  162.  
  163.     //Label for x
  164.     unLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 325, 65, 30)];
  165.     unLabel.font = [UIFont boldSystemFontOfSize: 18.0];
  166.     unLabel.text = @"X";
  167.     unLabel.backgroundColor = [UIColor whiteColor];
  168.     unLabel.userInteractionEnabled = NO;
  169.     [window addSubview: unLabel];
  170.  
  171.  
  172.     //Input for X
  173.     Un = [[UITextField alloc] initWithFrame: CGRectMake(20, 325, width-110, 30)];
  174.     Un.font = [UIFont boldSystemFontOfSize: 18.0];
  175.     Un.text = @"0";
  176.     Un.backgroundColor = [UIColor whiteColor];
  177.     [window addSubview: Un];
  178.  
  179.     //Input for Constant
  180.     Con = [[UITextField alloc] initWithFrame: CGRectMake(20, 375, width-45, 30)];
  181.     Con.font = [UIFont boldSystemFontOfSize: 18.0];
  182.     Con.text = @"0";
  183.     Con.backgroundColor = [UIColor whiteColor];
  184.     [window addSubview: Con];
  185.  
  186.  
  187.  
  188.  
  189.     //Create the NavBar
  190.     myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 20, width, 45)];
  191.     myNavBar.barStyle=UIBarStyleBlackTranslucent;
  192.     [window addSubview: myNavBar];
  193.     //Add a title
  194.     UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"Math Helper"];
  195.     [myNavBar pushNavigationItem: navItem];
  196.  
  197.     //Set the status bar to be a nifty black color
  198.     [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
  199.  
  200.  
  201.     // create a sample button with the rounded style, set its frame position and
  202.     // size, its default caption and make it call a sampleButtonPressed function
  203.     // in our own implementation when it's released (touched up inside). Then
  204.     // set the button to be a child window of our main window.
  205.  
  206.     sample_button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  207.     sample_button.frame = CGRectMake ((width/2)-75, 425, 150, 50);
  208.     [sample_button setTitle: @"Solve Problem" forState: UIControlStateNormal];
  209.     [sample_button addTarget: self action: @selector(sampleButtonPressed) forControlEvents: UIControlEventTouchUpInside];
  210.     [window addSubview: sample_button];
  211.  
  212.  
  213.     //overlay an invisible button on the navbar to handle touch events
  214.  
  215.     NavButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 20, width, 45)];
  216.     [NavButton setTitle: @"" forState: UIControlStateNormal];
  217.     [NavButton addTarget: self action: @selector(NavTouch) forControlEvents: UIControlEventTouchUpInside];
  218.     NavButton.backgroundColor = [UIColor clearColor];
  219.  
  220.  
  221.     [window addSubview: NavButton];
  222.  
  223.     // now show the main window and its children
  224.     [window makeKeyAndVisible];
  225.  
  226.     //Pop-up for later:
  227.     popup = [[UITextView alloc] initWithFrame: CGRectMake(0, 65, width, height-65)];
  228.     popup.font = [UIFont boldSystemFontOfSize: 18.0];
  229.     popup.backgroundColor = [UIColor whiteColor];
  230.     popup.editable = NO;
  231.     [window addSubview: popup];
  232.     [popup setHidden:( ! [popup isHidden])];
  233.  
  234. }
  235.  
  236. - (void)NavTouch{
  237.  
  238.     //Hide Keyboard on touch of the navbar
  239.  
  240.   [Hex resignFirstResponder];
  241.   [Pent resignFirstResponder];
  242.   [Quad resignFirstResponder];
  243.   [Tri resignFirstResponder];
  244.   [Di resignFirstResponder];
  245.   [Un resignFirstResponder];
  246.   [Con resignFirstResponder];
  247.  
  248.   //also hides the popup
  249.  
  250.     popup.hidden = true;
  251. }
  252.  
  253. - (void) sampleButtonPressed
  254. {
  255.     //Parse the textboxs' strings into doubles, and then Ints
  256.     double x1 = [Un.text doubleValue];
  257.     int xx1 = (int)(x1 + (x1>0 ? 0.5 : -0.5));
  258.     double x2 = [Di.text doubleValue];
  259.     int xx2 = (int)(x2 + (x2>0 ? 0.5 : -0.5));
  260.     double x3 = [Tri.text doubleValue];
  261.     int xx3 = (int)(x3 + (x3>0 ? 0.5 : -0.5));
  262.     double x4 = [Quad.text doubleValue];
  263.     int xx4 = (int)(x4 + (x4>0 ? 0.5 : -0.5));
  264.     double x5 = [Pent.text doubleValue];
  265.     int xx5 = (int)(x5 + (x5>0 ? 0.5 : -0.5));
  266.     double x6 = [Hex.text doubleValue];
  267.     int xx6 = (int)(x6 + (x6>0 ? 0.5 : -0.5));
  268.     double con = [Con.text doubleValue];
  269.     int xCon = (int)(con + (con>0 ? 0.5 : -0.5));
  270.  
  271.     int ind = 1;
  272.     int FirstNonZero;
  273.  
  274.     NSMutableArray *bot = [[NSMutableArray alloc] init];
  275.     NSMutableArray *top = [[NSMutableArray alloc] init];
  276.  
  277.     //Tests all the text boxes to get the first one that isn't = 0
  278.  
  279.     if (xx6==0){
  280.     if (xx5==0){
  281.     if (xx4==0){
  282.     if (xx3==0){
  283.     if (xx2==0){
  284.     if (xx1==0){
  285.     if (xCon==0){
  286.  
  287.     }else{
  288.     FirstNonZero = xCon;
  289.     }
  290.     }else{
  291.     FirstNonZero = xx1;
  292.     }
  293.     }else{
  294.     FirstNonZero = xx2;
  295.     }
  296.     }else{
  297.     FirstNonZero = xx3;
  298.     }
  299.     }else{
  300.     FirstNonZero = xx4;
  301.     }
  302.     }else{
  303.     FirstNonZero = xx5;
  304.     }
  305.     }else{
  306.     FirstNonZero = xx6;
  307.     }
  308.  
  309.  
  310.     //Tests all the number up to the number to see which are evenly divisible
  311.     //Uses "FirstNonZero" to see the degree of the problem and to ensure that,
  312.     //if a spot is = 0, that it'll choose the next one automatically.
  313.        
  314.     while (ind<=abs(FirstNonZero)){
  315.         if (FirstNonZero%ind==0){
  316.         NSString* i = [NSString stringWithFormat:@"%d", ind];
  317.          [bot addObject:i];
  318.         }
  319.         ind++;
  320.     }
  321.  
  322.  
  323.     //Set ind (the index of the loops) back to 1
  324.     //Uses 1 instead of zero because this involves dividing by this number.
  325.    
  326.     ind = 1;
  327.  
  328.  
  329.  
  330.     while (ind<=abs(xCon)){
  331.         if (xCon%ind==0){
  332.         NSString* i = [NSString stringWithFormat:@"%d", ind];
  333.          [top addObject:i];
  334.         }
  335.         ind++;
  336.     }
  337.  
  338.    
  339.  
  340.  
  341.  
  342.  
  343.    
  344.     //Create a list of all Possible roots, then test them to see if they work.
  345.     //If they do, add them to the list of actual roots.
  346.    
  347.     NSMutableArray *AllRealRoots = [[NSMutableArray alloc] init];
  348.     NSMutableArray *AllPossibleRealRoots = [[NSMutableArray alloc] init];
  349.  
  350.     for (NSString *dividend in top){
  351.         for (NSString *divisor in bot){
  352.                
  353.                 NSString *fraction1 = [dividend stringByAppendingString:@"/"];
  354.                 NSString *fraction2 = [fraction1 stringByAppendingString:divisor];
  355.                 [AllPossibleRealRoots addObject:fraction2];
  356.                 double p1 = [dividend doubleValue];
  357.                 double p2 = [divisor doubleValue];
  358.                 double res = p1/p2;
  359.                 if ((xx1*res)+(xx2*res*res)+(xx3*res*res*res)+(xx4*res*res*res*res)+(xx5*res*res*res*res*res)+(xx6*res*res*res*res*res*res)+xCon==0){
  360.                
  361.                 [AllRealRoots addObject:fraction2];
  362.  
  363.                 }
  364.                 res = 0-res;
  365.                 if ((xx1*res)+(xx2*res*res)+(xx3*res*res*res)+(xx4*res*res*res*res)+(xx5*res*res*res*res*res)+(xx6*res*res*res*res*res*res)+xCon==0){
  366.                
  367.                 NSString *fraction3 = [@"-" stringByAppendingString:fraction2];
  368.                 [AllRealRoots addObject:fraction3];
  369.  
  370.                 }
  371.         }
  372.     }
  373.  
  374.     //Create a string to display the data to the user.
  375.  
  376.     NSString *AllAsOne = @"All Possible Real Roots:\n(+ or -)";
  377.     for (NSString *Ans in AllPossibleRealRoots){
  378.         AllAsOne = [AllAsOne stringByAppendingString:Ans];
  379.         AllAsOne = [AllAsOne stringByAppendingString:@"\n(+ or -)"];
  380.     }
  381.         AllAsOne = [AllAsOne stringByAppendingString:@"\n\n\nThe Actual Roots Are:\n"];
  382.  
  383.     for (NSString *Ans in AllRealRoots){
  384.         AllAsOne = [AllAsOne stringByAppendingString:Ans];
  385.         AllAsOne = [AllAsOne stringByAppendingString:@"\n"];
  386.     }
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.     //Hide Keyboard
  394.  
  395.   [Hex resignFirstResponder];
  396.   [Pent resignFirstResponder];
  397.   [Quad resignFirstResponder];
  398.   [Tri resignFirstResponder];
  399.   [Di resignFirstResponder];
  400.   [Un resignFirstResponder];
  401.   [Con resignFirstResponder];
  402.  
  403.  
  404.   //Bring up the results
  405.  
  406.     popup.text = AllAsOne;
  407.     [popup setHidden:( ! [popup isHidden])];
  408.  
  409.     // pop up a sample alert box with a caption, text and button and show it
  410.     /*
  411.     UIAlertView *sample_alert;
  412.     sample_alert = [[UIAlertView alloc] initWithTitle: @"Answer" message: AllAsOne delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
  413.     [sample_alert show];
  414.     */
  415. }
  416.  
  417. @end
  418.  
  419.  
  420. int main (int argc, char **argv)
  421. {
  422.     // program entrypoint.
  423.  
  424.     NSAutoreleasePool *pool;
  425.     int return_value;
  426.  
  427. #if defined DEBUG_LOG_TO_FILE
  428.  
  429.     // redirect the standard error stream to a log file
  430.     char logfile_path[128];
  431.     sprintf (logfile_path, "%s/Documents/" DEBUG_LOG_TO_FILE, dirname (dirname (argv[0])));
  432.     if (freopen (logfile_path, "w", stderr) == NULL)
  433.     {
  434.         strcpy (logfile_path, "/var/mobile/" DEBUG_LOG_TO_FILE);
  435.         freopen (logfile_path, "w", stderr); // fallback
  436.     }
  437.  
  438. #elif defined DEBUG_LOG_TO_REMOTE_CONSOLE
  439.  
  440.     // redirect the standard error stream to a remote console
  441.     int debug_socket;
  442.     struct sockaddr_in debugserver_sockaddr;
  443.     memset (&debugserver_sockaddr, 0, sizeof (debugserver_sockaddr));
  444.     debugserver_sockaddr.sin_family = AF_INET;
  445.     debugserver_sockaddr.sin_addr.s_addr = inet_addr (DEBUG_LOG_TO_REMOTE_CONSOLE);
  446.     debugserver_sockaddr.sin_port = htons (5000);
  447.     if (((debug_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) && (connect (debug_socket, (struct sockaddr *) &debugserver_sockaddr, sizeof (debugserver_sockaddr)) != -1))
  448.         dup2 (debug_socket, STDERR_FILENO);
  449.  
  450. #endif
  451.  
  452.     // allocate an autorelease memory pool and start Cocoa. Tell Cocoa about
  453.     // the name of our application's main class, and the name of the delegate
  454.     // class to which send event messages. We wouldn't need to specify them
  455.     // here if we were using a .nib file, like they do in XCode.
  456.     pool = [[NSAutoreleasePool alloc] init];
  457.     return_value = UIApplicationMain (argc, argv, @"TestProjectApp", @"TestProjectApp");
  458.     [pool release]; // release the pool when Cocoa returns
  459.  
  460.     return (return_value); // and exit application
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement