You are not logged in.

  • Login

Dear visitor, welcome to Coder Forum. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

fabfaban

Unregistered

1

Monday, June 20th 2011, 12:29pm

Probleme beim Umschreiben von C in C++ Code

Mein Vorhaben ist, wie in der Ueberschrift erwaehnt, meinen C Code in C++ Code umzuschreiben. Ist eine einmalige Sache, aber mein Unwissen in C++ hilft mir da nicht gerade. Koennte vielleicht jemand so freundlich sein und mir ein wenig helfen?
Hier mal einen der Codeabschnitte:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>


#include "date.h"

mdate create_d(unsigned day, unsigned month, unsigned year)
{
	//mdate* date =   (mdate*)malloc(sizeof(mdate));
	mdate date;
	date.day=day;
	date.month=month;
	date.year=year;
	return date;
}

mdate copy_d(mdate d)
{
	return create_d(d.day,d.month,d.year);
}

int datecmp( const mdate *d1, const mdate *d2)
{
	if (d1->year < d2->year)
			return -1;
		if (d1->year > d2->year)
			return 1;
		//year is equal
		if (d1->month < d2->month)
			return -1;
		if (d1->month > d2->month)
			return 1;
		//year and month are equal
		if (d1->day < d2->day)
			return -1;

		if (d1->day > d2->day)
					return 1;
		//the dates are equal
		return 0;
}



void print_d(mdate d)
{
	printf("%u.%u.%u",d.day,d.month,d.year);
}


und hier noch eine Headerdatei:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef __DATE_H__
#define __DATE_H__

/* a struct to represent a date! of course there is an existing implementation in the C standard :)
 * */
struct date_{
	unsigned year;
	unsigned month;
	unsigned day;
} ;

typedef struct date_ mdate;

/* creates a date, currently their is no check, whether the given date is valid; 31.02.2000 would make no sense :)
 * */
mdate create_d(unsigned day, unsigned month, unsigned year);

mdate copy_d(mdate d);

/*allows to compare two dates, the function returns
 * 0 if the dates are equal
 * 1 if d1 > d2
 * -1 otherwise
 * */
int datecmp(const mdate *d1, const mdate *d2);

/*prints the date to stdout day.month.year*/
void print_d(mdate d);

#endif

2

Monday, June 20th 2011, 1:43pm

Davon abgesehen, dass ich den Sinn von copy_d() nicht so ganz verstehe ... Inwiefern willst du das in C++ "umschreiben"? Willst du eine Klasse schreiben?

fabfaban

Unregistered

3

Monday, June 20th 2011, 1:51pm

Mein urspruengliches Programm speichert und loescht daten in/aus einem array, eine sortierfunktion nach namen und Geburtjahr wurde implementiert. Ich dachte ich poste erstmal nur diesen Abschnitt. Ich will das Programm so umschreiben, dass ich es in C++ in identischer weise nutzen kann. I

4

Monday, June 20th 2011, 5:56pm

Normalerweisse solltest du denn Code so wie er ist auch in C++ benutzen können.

Welchen Compiler benutzt du und/oder welche Entwicklungsumgebung hast du?
Was spuckt der compiler aus?

mfg Rushh0ur

5

Monday, June 20th 2011, 8:48pm

Umschreiben von C nach C++
reicht es, wenn der Compiler das ohne Schluckauf frisst?
Sollen minimale weitergehende Änderungen gemacht werden? Beispiel: Header, oder in dem Quelltext -> printf()
Oder soll noch weiter C++ Stilmittel genutzt werden? Für Letzteres denke ich ist dein gezeigter Quelltext nicht ausreichend.

Zeig uns mal wie du das angehst und wo deine Schwierigkeiten sind - d.h. wir wollen Quelltext der schon bearbeitet ist sehen :!:

MfG bcc-fan

Social bookmarks