Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <UIKit/UIKit.h>
- #import <QuartzCore/QuartzCore.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <libgen.h>
- // the following definition sends all debug messages and NSLog macros to a
- // specific file in the app's Documents directory. Useful for debugging.
- #define DEBUG_LOG_TO_FILE "debug.log"
- // the following definition sends all debug messages and NSLog macros to a
- // remote debug console over TCP/IP. Don't release your app with this enabled!
- //#define DEBUG_LOG_TO_REMOTE_CONSOLE "192.168.1.105"
- // interface for our application class. We derive it from UIApplication.
- @interface TestProjectApp : UIApplication
- {
- UIWindow *window; // our application's main window
- UITextField *sample_text; // some sample text field
- UITextField *Hex;
- UITextField *pentLabel;
- UITextField *Pent;
- UITextField *quadLabel;
- UITextField *Quad;
- UITextField *triLabel;
- UITextField *Tri;
- UITextField *diLabel;
- UITextField *Di;
- UITextField *unLabel;
- UITextField *Un;
- UITextField *Con;
- UIButton *sample_button; // some sample button
- UIButton *NavButton;
- UINavigationBar *myNavBar;
- UITextView *popup;
- }
- - (void) applicationDidFinishLaunching: (UIApplication *) application;
- - (void) sampleButtonPressed;
- - (void) NavTouch;
- @end
- // implementation of our application class, as defined in its interface.
- @implementation TestProjectApp
- - (void) applicationDidFinishLaunching: (UIApplication *) application
- {
- // this message is sent when the Cocoa framework has just finished starting
- // our application and is passing control to us. By implementing it we can
- // call our own initialization functions. Also, since we don't use any .nib
- // file, we can create our application windows, buttons and Cocoa controls
- // ourselves here. Later on, when actions will happen on them, Cocoa will
- // send their respective handling messages to our code, provided we
- // implement them.
- // place your own startup code here.
- // create a full-screen window with a black background. Do it manually
- // since we don't rely on a .nib file to do it for us.
- window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
- [window setBackgroundColor: [UIColor blackColor]];
- //declare width as a variable equalling the screen's width
- CGFloat width = [UIScreen mainScreen].bounds.size.width;
- CGFloat height = [UIScreen mainScreen].bounds.size.height;
- // create a sample text field window covering the specified rectangle and
- // add this window as a child window of the main window
- //Label for x^6
- sample_text = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 75, 65, 30)];
- sample_text.font = [UIFont boldSystemFontOfSize: 18.0];
- sample_text.text = @"X^6";
- sample_text.backgroundColor = [UIColor whiteColor];
- sample_text.userInteractionEnabled = NO;
- [window addSubview: sample_text];
- //Input for X^6
- Hex = [[UITextField alloc] initWithFrame: CGRectMake(20, 75, width-110, 30)];
- Hex.font = [UIFont boldSystemFontOfSize: 18.0];
- Hex.text = @"0";
- //Commenting the next line of code out so I'll have it for refrence in the future.
- //Sets the type of keyboard to numerice keypad
- //Hex.keyboardType = UIKeyboardTypeNumberPad;
- //Doesn't work well with this app because there's no negative button.
- Hex.backgroundColor = [UIColor whiteColor];
- [window addSubview: Hex];
- //Label for x^5
- pentLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 125, 65, 30)];
- pentLabel.font = [UIFont boldSystemFontOfSize: 18.0];
- pentLabel.text = @"X^5";
- pentLabel.backgroundColor = [UIColor whiteColor];
- pentLabel.userInteractionEnabled = NO;
- [window addSubview: pentLabel];
- //Input for X^5
- Pent = [[UITextField alloc] initWithFrame: CGRectMake(20, 125, width-110, 30)];
- Pent.font = [UIFont boldSystemFontOfSize: 18.0];
- Pent.text = @"0";
- Pent.backgroundColor = [UIColor whiteColor];
- [window addSubview: Pent];
- //Label for x^4
- quadLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 175, 65, 30)];
- quadLabel.font = [UIFont boldSystemFontOfSize: 18.0];
- quadLabel.text = @"X^4";
- quadLabel.backgroundColor = [UIColor whiteColor];
- quadLabel.userInteractionEnabled = NO;
- [window addSubview: quadLabel];
- //Input for X^4
- Quad = [[UITextField alloc] initWithFrame: CGRectMake(20, 175, width-110, 30)];
- Quad.font = [UIFont boldSystemFontOfSize: 18.0];
- Quad.text = @"0";
- Quad.backgroundColor = [UIColor whiteColor];
- [window addSubview: Quad];
- //Label for x^3
- triLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 225, 65, 30)];
- triLabel.font = [UIFont boldSystemFontOfSize: 18.0];
- triLabel.text = @"X^3";
- triLabel.backgroundColor = [UIColor whiteColor];
- triLabel.userInteractionEnabled = NO;
- [window addSubview: triLabel];
- //Input for X^3
- Tri = [[UITextField alloc] initWithFrame: CGRectMake(20, 225, width-110, 30)];
- Tri.font = [UIFont boldSystemFontOfSize: 18.0];
- Tri.text = @"0";
- Tri.backgroundColor = [UIColor whiteColor];
- [window addSubview: Tri];
- //Label for x^2
- diLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 275, 65, 30)];
- diLabel.font = [UIFont boldSystemFontOfSize: 18.0];
- diLabel.text = @"X^2";
- diLabel.backgroundColor = [UIColor whiteColor];
- diLabel.userInteractionEnabled = NO;
- [window addSubview: diLabel];
- //Input for X^2
- Di = [[UITextField alloc] initWithFrame: CGRectMake(20, 275, width-110, 30)];
- Di.font = [UIFont boldSystemFontOfSize: 18.0];
- Di.text = @"0";
- Di.backgroundColor = [UIColor whiteColor];
- [window addSubview: Di];
- //Label for x
- unLabel = [[UITextField alloc] initWithFrame: CGRectMake(width-90, 325, 65, 30)];
- unLabel.font = [UIFont boldSystemFontOfSize: 18.0];
- unLabel.text = @"X";
- unLabel.backgroundColor = [UIColor whiteColor];
- unLabel.userInteractionEnabled = NO;
- [window addSubview: unLabel];
- //Input for X
- Un = [[UITextField alloc] initWithFrame: CGRectMake(20, 325, width-110, 30)];
- Un.font = [UIFont boldSystemFontOfSize: 18.0];
- Un.text = @"0";
- Un.backgroundColor = [UIColor whiteColor];
- [window addSubview: Un];
- //Input for Constant
- Con = [[UITextField alloc] initWithFrame: CGRectMake(20, 375, width-45, 30)];
- Con.font = [UIFont boldSystemFontOfSize: 18.0];
- Con.text = @"0";
- Con.backgroundColor = [UIColor whiteColor];
- [window addSubview: Con];
- //Create the NavBar
- myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 20, width, 45)];
- myNavBar.barStyle=UIBarStyleBlackTranslucent;
- [window addSubview: myNavBar];
- //Add a title
- UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"Math Helper"];
- [myNavBar pushNavigationItem: navItem];
- //Set the status bar to be a nifty black color
- [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
- // create a sample button with the rounded style, set its frame position and
- // size, its default caption and make it call a sampleButtonPressed function
- // in our own implementation when it's released (touched up inside). Then
- // set the button to be a child window of our main window.
- sample_button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- sample_button.frame = CGRectMake ((width/2)-75, 425, 150, 50);
- [sample_button setTitle: @"Solve Problem" forState: UIControlStateNormal];
- [sample_button addTarget: self action: @selector(sampleButtonPressed) forControlEvents: UIControlEventTouchUpInside];
- [window addSubview: sample_button];
- //overlay an invisible button on the navbar to handle touch events
- NavButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 20, width, 45)];
- [NavButton setTitle: @"" forState: UIControlStateNormal];
- [NavButton addTarget: self action: @selector(NavTouch) forControlEvents: UIControlEventTouchUpInside];
- NavButton.backgroundColor = [UIColor clearColor];
- [window addSubview: NavButton];
- // now show the main window and its children
- [window makeKeyAndVisible];
- //Pop-up for later:
- popup = [[UITextView alloc] initWithFrame: CGRectMake(0, 65, width, height-65)];
- popup.font = [UIFont boldSystemFontOfSize: 18.0];
- popup.backgroundColor = [UIColor whiteColor];
- popup.editable = NO;
- [window addSubview: popup];
- [popup setHidden:( ! [popup isHidden])];
- }
- - (void)NavTouch{
- //Hide Keyboard on touch of the navbar
- [Hex resignFirstResponder];
- [Pent resignFirstResponder];
- [Quad resignFirstResponder];
- [Tri resignFirstResponder];
- [Di resignFirstResponder];
- [Un resignFirstResponder];
- [Con resignFirstResponder];
- //also hides the popup
- popup.hidden = true;
- }
- - (void) sampleButtonPressed
- {
- //Parse the textboxs' strings into doubles, and then Ints
- double x1 = [Un.text doubleValue];
- int xx1 = (int)(x1 + (x1>0 ? 0.5 : -0.5));
- double x2 = [Di.text doubleValue];
- int xx2 = (int)(x2 + (x2>0 ? 0.5 : -0.5));
- double x3 = [Tri.text doubleValue];
- int xx3 = (int)(x3 + (x3>0 ? 0.5 : -0.5));
- double x4 = [Quad.text doubleValue];
- int xx4 = (int)(x4 + (x4>0 ? 0.5 : -0.5));
- double x5 = [Pent.text doubleValue];
- int xx5 = (int)(x5 + (x5>0 ? 0.5 : -0.5));
- double x6 = [Hex.text doubleValue];
- int xx6 = (int)(x6 + (x6>0 ? 0.5 : -0.5));
- double con = [Con.text doubleValue];
- int xCon = (int)(con + (con>0 ? 0.5 : -0.5));
- int ind = 1;
- int FirstNonZero;
- NSMutableArray *bot = [[NSMutableArray alloc] init];
- NSMutableArray *top = [[NSMutableArray alloc] init];
- //Tests all the text boxes to get the first one that isn't = 0
- if (xx6==0){
- if (xx5==0){
- if (xx4==0){
- if (xx3==0){
- if (xx2==0){
- if (xx1==0){
- if (xCon==0){
- }else{
- FirstNonZero = xCon;
- }
- }else{
- FirstNonZero = xx1;
- }
- }else{
- FirstNonZero = xx2;
- }
- }else{
- FirstNonZero = xx3;
- }
- }else{
- FirstNonZero = xx4;
- }
- }else{
- FirstNonZero = xx5;
- }
- }else{
- FirstNonZero = xx6;
- }
- //Tests all the number up to the number to see which are evenly divisible
- //Uses "FirstNonZero" to see the degree of the problem and to ensure that,
- //if a spot is = 0, that it'll choose the next one automatically.
- while (ind<=abs(FirstNonZero)){
- if (FirstNonZero%ind==0){
- NSString* i = [NSString stringWithFormat:@"%d", ind];
- [bot addObject:i];
- }
- ind++;
- }
- //Set ind (the index of the loops) back to 1
- //Uses 1 instead of zero because this involves dividing by this number.
- ind = 1;
- while (ind<=abs(xCon)){
- if (xCon%ind==0){
- NSString* i = [NSString stringWithFormat:@"%d", ind];
- [top addObject:i];
- }
- ind++;
- }
- //Create a list of all Possible roots, then test them to see if they work.
- //If they do, add them to the list of actual roots.
- NSMutableArray *AllRealRoots = [[NSMutableArray alloc] init];
- NSMutableArray *AllPossibleRealRoots = [[NSMutableArray alloc] init];
- for (NSString *dividend in top){
- for (NSString *divisor in bot){
- NSString *fraction1 = [dividend stringByAppendingString:@"/"];
- NSString *fraction2 = [fraction1 stringByAppendingString:divisor];
- [AllPossibleRealRoots addObject:fraction2];
- double p1 = [dividend doubleValue];
- double p2 = [divisor doubleValue];
- double res = p1/p2;
- 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){
- [AllRealRoots addObject:fraction2];
- }
- res = 0-res;
- 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){
- NSString *fraction3 = [@"-" stringByAppendingString:fraction2];
- [AllRealRoots addObject:fraction3];
- }
- }
- }
- //Create a string to display the data to the user.
- NSString *AllAsOne = @"All Possible Real Roots:\n(+ or -)";
- for (NSString *Ans in AllPossibleRealRoots){
- AllAsOne = [AllAsOne stringByAppendingString:Ans];
- AllAsOne = [AllAsOne stringByAppendingString:@"\n(+ or -)"];
- }
- AllAsOne = [AllAsOne stringByAppendingString:@"\n\n\nThe Actual Roots Are:\n"];
- for (NSString *Ans in AllRealRoots){
- AllAsOne = [AllAsOne stringByAppendingString:Ans];
- AllAsOne = [AllAsOne stringByAppendingString:@"\n"];
- }
- //Hide Keyboard
- [Hex resignFirstResponder];
- [Pent resignFirstResponder];
- [Quad resignFirstResponder];
- [Tri resignFirstResponder];
- [Di resignFirstResponder];
- [Un resignFirstResponder];
- [Con resignFirstResponder];
- //Bring up the results
- popup.text = AllAsOne;
- [popup setHidden:( ! [popup isHidden])];
- // pop up a sample alert box with a caption, text and button and show it
- /*
- UIAlertView *sample_alert;
- sample_alert = [[UIAlertView alloc] initWithTitle: @"Answer" message: AllAsOne delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
- [sample_alert show];
- */
- }
- @end
- int main (int argc, char **argv)
- {
- // program entrypoint.
- NSAutoreleasePool *pool;
- int return_value;
- #if defined DEBUG_LOG_TO_FILE
- // redirect the standard error stream to a log file
- char logfile_path[128];
- sprintf (logfile_path, "%s/Documents/" DEBUG_LOG_TO_FILE, dirname (dirname (argv[0])));
- if (freopen (logfile_path, "w", stderr) == NULL)
- {
- strcpy (logfile_path, "/var/mobile/" DEBUG_LOG_TO_FILE);
- freopen (logfile_path, "w", stderr); // fallback
- }
- #elif defined DEBUG_LOG_TO_REMOTE_CONSOLE
- // redirect the standard error stream to a remote console
- int debug_socket;
- struct sockaddr_in debugserver_sockaddr;
- memset (&debugserver_sockaddr, 0, sizeof (debugserver_sockaddr));
- debugserver_sockaddr.sin_family = AF_INET;
- debugserver_sockaddr.sin_addr.s_addr = inet_addr (DEBUG_LOG_TO_REMOTE_CONSOLE);
- debugserver_sockaddr.sin_port = htons (5000);
- if (((debug_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) && (connect (debug_socket, (struct sockaddr *) &debugserver_sockaddr, sizeof (debugserver_sockaddr)) != -1))
- dup2 (debug_socket, STDERR_FILENO);
- #endif
- // allocate an autorelease memory pool and start Cocoa. Tell Cocoa about
- // the name of our application's main class, and the name of the delegate
- // class to which send event messages. We wouldn't need to specify them
- // here if we were using a .nib file, like they do in XCode.
- pool = [[NSAutoreleasePool alloc] init];
- return_value = UIApplicationMain (argc, argv, @"TestProjectApp", @"TestProjectApp");
- [pool release]; // release the pool when Cocoa returns
- return (return_value); // and exit application
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement