简体   繁体   English

将C ++代码重写为Objective C

[英]Rewrite C++ code into Objective C

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. 我得到了一些C ++源代码,我想将其重写为目标C。如果有人可以为我编写此代码的头文件,这将对我有很大帮助。 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;
    transitionTable[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. 只需将iostream.h更改为stdio.h,将cout更改为printf。 Then it's a regular C program. 然后是常规的C程序。

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. 只需将这些行放入头文件中,您就应该能够包括头文件并从任何其他Objective-C或C源文件中调用函数。

(To support C++ also, you may need to put extern "C" { ... } around them, unless you are compiling everything as C++ or Objective-C++.) (为了也支持C ++,除非您将所有内容都编译为C ++或Objective-C ++,否则可能需要在它们周围加上extern "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.h (或您决定调用的任何名称),包含上面的行。
  • 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 markov.c (或您决定调用的任何文件),包含原始源文件中的其他内容,但main功能除外,应将其删除

Then, your other files that use the functions should have an #include "markov.h" line, and then you should be able to call them. 然后,使用该功能的其他文件应具有#include "markov.h"行,然后您便可以对其进行调用。

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. 如果遇到有关缺少函数的链接器错误,则意味着您没有将markov.c编译为项目的一部分,或者某些选项导致函数名称不正确匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM