开发者

Rewrite C++ code into Objective C

开发者 https://www.devze.com 2023-02-04 17:11 出处:网络
I got some C++ Sourcecode that I would like to rewrite into Objective C. It would help me alot if someone could write me a header file for this Code. When Iget the Headerfile I would be able to rewrit

I got some C++ Sourcecode that I would like to rewrite into Objective C. It would help me alot if someone could write me a header file for this Code. When I get the Headerfile I would be able to rewrite the rest of the Sourcecode.

It would be very nice if someone could help me please.

Thanks

I will poste the sourcecode here:

#include <stdlib.h>
#include <iostream.h>

#define STATES 5

int transitionTable[STATES][STATES];

// function declarations:
double randfloat                 (void);
int    chooseNextEventFromTable  (int current, int table[STATES][STATES]);
int    chooseNextEventFromTransitionTablee (int currentState);
void   setTable                  (int value, int table[STATES][STATES]);


//////////////////////////////////////////////////////////////////////////

int main(void) { 
   int i;

   // for demo purposes:
 transitionTable[0][0] = 0;
 transitionTable[0][1] = 20;
 transitionTable[0][2] = 30;
 transitionTable[0][3] = 50;
 transitionTable[0][4] = 0;
 transitionTable[1][0] = 35;
 transitionTable[1][1] = 25;
 transitionTable[1][2] = 20;
 transitionTable[1][3] = 30;
 transitionTable[1][4] = 0;
 transitionTable[2][0] = 70;
 transitionTable[2][1] = 0;
 transitionTable[2][2] = 15;
 transitionTable[2][3] = 0;
 transitionTable[2][4] = 15;
 transitionTable[3][0] = 0;
 transitionTable[3][1] = 25;
 transitionTable[3][2] = 25;
 transitionTable[3][3] = 0;
 transitionTable[3][4] = 50;
 transitionTable[4][0] = 13;
 transitionTable[4][1] = 17;
 transitionTable[4][2] = 22;
 transitionTable[4][3] = 48;
 transitionTable[4][4] = 0;

   int currentState = 0;
 for (i=0; i<10; i++) {
    std::cout << currentState << " ";
      currentState = chooseNextEventFromTransitionTablee(currentState);
   }
   return 0;
};


//////////////////////////////////////////////////////////////////////////


//////////////////////////////
//
// chooseNextEventFromTransitionTable -- choose the next note.
//

int chooseNextEventFromTransitionTablee(int currentState) {
 int targetSum   = 0;
 int sum         = 0;
 int targetNote  = 0;
 int totalevents = 0;
 int i;

 currentState = currentState % STATES;  // remove any octave value
 for (i=0; i<STATES; i++) {
  totalevents += transitionTable[currentState][i];
 }
 targetSum   = (int)(randfloat() * totalevents + 0.5);
 while (targetNote < STATES &&
     sum+transitionTable[currentState][targetNote] < targetSum) {
  sum += transitionTable[currentState][targetNote];
  targetNote++;
 }

 return targetNote;
}





//////////////////////////////
//
// randfloat -- returns a random number between 0.0 and 1.0.
//

double randfloat(void) {
   return (double)rand()/RAND_MAX;
}



//////////////////////////////
//
// setTable -- set all values in the transition table to the given value.
//

void setTable(int value, int table[STATES][STATES]) {
   int i, j;
   for (i=0; i<STATES; i++) {
      for (j=0; j<STATES; j++) {
         table[i][j] = value;
      }
   }
}

Update

I'm not only compiling the header file there is another file i'm compiling too

SourceCode:

//
//  markovThreadsChainsViewController.m
//  markovThreadsChains
//
//  Created by Philippe Mokrzycki on 15.01.11.
//  Copyright 2011 TGM. All rights reserved.
//

#import "markovThreadsChainsViewController.h"
#import "markov.h"
//#import "markovChainOC.h"

@implementation markovThreadsChainsViewController

@synthesize mcValueLabel, threadStartGenerateButton, threadStopGenerateButton;

- (IBAction) startThreadGen:(UIButton *)sender{
    threadStopGenerateButton.hidden=NO;
    threadStartGenerateButton.hidden=YES;
    mcValueLabel.text = @"0";

    currentState=0;

//  markovChainOC *mCobc = [[markovChainOC alloc]init];

//  [mCobc setCurrentState:0];
    [NSThread detachNewThreadSelector:@selector(startThreading)  toTarget:self withObject:nil];
}

- (IBAction) stopThreadGen:(UIButton *)sender{
    threadStopGenerateButton.hidden=YES;
    threadStartGenerateButton.hidden=NO;
    [NSThread detachNewThreadSelector:@selector(stopThreading) toTarget:self withObject:nil];
}

- (void) startThreading {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

    [NSThread sleepForTimeInterval:3];
    [self performSelectorOnMainThread:@selector(markovGen) withObject:nil waitUntilDone:NO];
    [pool release];
}

- (void) stopThreading {
    [NSThread cancelPreviousPerformRequestsWithTarget:self];
}

- (void)markovGen{
    transitionTable[0][0] = 25;
    transitionTable[0][1] = 25;
    transitionTable[1][0] = 25;
    tr开发者_运维百科ansitionTable[1][1] = 25;

//  int actualValue = [mCobc getCurrentState];

    int actualValue = currentState;
    mcValueLabel.text = [NSString stringWithFormat:@"%", actualValue];
    currentState = chooseNextEventFromTransitionTablee(currentState);
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(markovGen) userInfo:nil repeats:NO];

}


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [mcValueLabel release];
    [threadStartGenerateButton release];
    [threadStopGenerateButton release];
//  [mCobc release];
    [super dealloc];
}

@end


This code has nothing object-oriented in it at all. Just change iostream.h to stdio.h, and cout to printf. Then it's a regular C program.


Just put these lines into a header file, and you should be able to include the header and call the functions from any other Objective-C or C source file.

(To support C++ also, you may need to put extern "C" { ... } around them, unless you are compiling everything as C++ or Objective-C++.)

#pragma once

#define STATES 5

int transitionTable[STATES][STATES];

// function declarations:
double randfloat                 (void);
int    chooseNextEventFromTable  (int current, int table[STATES][STATES]);
int    chooseNextEventFromTransitionTablee (int currentState);
void   setTable                  (int value, int table[STATES][STATES]);

More detail:

To use these functions from another class, you'll need the following files in your project:

  • markov.h (or whatever you decide to call it), containing the lines above.
  • markov.c (or whatever you decide to call it), containing the other stuff from your original source file, except for the main function, which you should remove

Then, your other files that use the functions should have an #include "markov.h" line, and then you should be able to call them.

If you are getting linker errors about missing functions, it means that you are not compiling markov.c as part of the project, or there are some options that are causing the function names to not match properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号