Wie teile ich das Programm auf?

Gast #1102327
Lesenswert?

Hallo,
Ich habe ein Problem und zwar weiß ich nicht, wie ich das Programm unten 
in Header, *.c und main Dateien aufteilen soll. Ich weiß wohl, dass in 
den Header die Prototypen der Funktionen rein müssen, aber was ich dann 
noch und wie im c-file stehen lassen muss und was in die main Datei 
gehört, weiß ich nicht. Habe alles mögliche rumprobiert und alles in 
einem Projekt file gesammelt aber es klappt einfach nicht. Bin wohl noch 
blutiger Anfänger aber es ist ja noch kein Meister vom Himmel 
gefallen...:-)


#include <stdio.h>
#include <stdlib.h>
#include <math.h>


struct comp
 {
   float real;
   float img;
 };


struct comp cfadd(struct comp a , struct comp b)
{
 struct comp c;
 c.real=a.real+b.real;
 c.img=a.img+ b.img;
 return c;
}

struct comp cfmul(struct comp a , struct comp b)
{
 struct comp c;
 c.real=a.real*b.real-a.img*b.img;
 c.img=a.real*b.img+ a.img*b.real;
 return c;
}

double cfabs(struct comp a)
{
 double c;
 c = sqrtf(a.real*a.real+a.img*a.img);
 return c;
}



{ struct comp a,b,c;
  printf("Bitte Realteil und Imagin\x84rteil eingeben  ");
scanf("%f%f", &a.real,&a.img);
  printf("Bitte Realteil und Imagin\x84rteil eingeben  ");
scanf("%f%f", &b.real,&b.img);

printf("a = %8.2f + %8.2fi\n", a.real, a.img);
printf("b = %8.2f + %8.2fi\n", b.real, b.img);
c = cfadd(a,b);
printf("a+b = %8.2f +%8.2fi\n", c.real, c.img);
c = cfmul(a,b);
printf("a*b = %8.2f +%8.2fi\n", c.real, c.img);
printf("a hat die L\x84nge %4:2d\n",cfabs(a));
printf("b hat die L\x84nge %4:2d\n",cfabs(b));
  system ("pause");
          }




Ich bin für jede Hilfe dankbar!!!!!
Gruß Fizzle
Persönliche Seite #1102367
Lesenswert?

Beispielsweise so:

comp.h
1
struct comp
2
{
3
  float real;
4
  float img;
5
};
6

7
struct comp cfadd(struct comp a , struct comp b);
8
struct comp cfmul(struct comp a , struct comp b);
9
double cfabs(struct comp a);

comp.c
1
#include <stdlib.h>
2
#include <math.h>
3

4
#include "comp.h"
5

6
struct comp cfadd(struct comp a , struct comp b)
7
{
8
  struct comp c;
9

10
  c.real=a.real+b.real;
11
  c.img=a.img+ b.img;
12
  return c;
13
}
14

15
struct comp cfmul(struct comp a , struct comp b)
16
{
17
  struct comp c;
18

19
  c.real=a.real*b.real-a.img*b.img;
20
  c.img=a.real*b.img+ a.img*b.real;
21
  return c;
22
}
23

24
double cfabs(struct comp a)
25
{
26
  double c;
27

28
  c = sqrtf(a.real*a.real+a.img*a.img);
29
  return c;
30
}

Und schließlich

main.c
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <math.h>
4

5
#include "comp.h"
6

7
int main(int argc, char** argv)
8
{ 
9
  struct comp a,b,c;
10
  printf("Bitte Realteil und Imagin\x84rteil eingeben  ");
11
  scanf("%f%f", &a.real,&a.img);
12
  printf("Bitte Realteil und Imagin\x84rteil eingeben  ");
13
  scanf("%f%f", &b.real,&b.img);
14

15
  printf("a = %8.2f + %8.2fi\n", a.real, a.img);
16
  printf("b = %8.2f + %8.2fi\n", b.real, b.img);
17
  c = cfadd(a,b);
18
  printf("a+b = %8.2f +%8.2fi\n", c.real, c.img);
19
  c = cfmul(a,b);
20
  printf("a*b = %8.2f +%8.2fi\n", c.real, c.img);
21
  printf("a hat die L\x84nge %4:2d\n",cfabs(a));
22
  printf("b hat die L\x84nge %4:2d\n",cfabs(b));
23
  system ("pause");
24
}

Literaturhinweis:
Brian Kernigham & Dennis Ritchie
"Programmieren in C"
Zweite Auflage, Hanser Verlag
ISBN: 3446138781
Gast #1102369
Lesenswert?

Ich würds so machen. Erklärungen gern auf Anfrage.



comp.h:
1
#ifndef COMP__H
2
#define COMP__H
3

4
struct comp
5
 {
6
   float real;
7
   float img;
8
 };
9

10

11
struct comp cfadd(struct comp a , struct comp b);
12

13
struct comp cfmul(struct comp a , struct comp b);
14

15
double cfabs(struct comp a);
16
#endif

comp.c:
1
#include "comp.h"
2

3
struct comp cfadd(struct comp a , struct comp b)
4
{
5
 struct comp c;
6
 c.real=a.real+b.real;
7
 c.img=a.img+ b.img;
8
 return c;
9
}
10

11
struct comp cfmul(struct comp a , struct comp b)
12
{
13
 struct comp c;
14
 c.real=a.real*b.real-a.img*b.img;
15
 c.img=a.real*b.img+ a.img*b.real;
16
 return c;
17
}
18

19
double cfabs(struct comp a)
20
{
21
 double c;
22
 c = sqrtf(a.real*a.real+a.img*a.img);
23
 return c;
24
}

main.c:
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <math.h>
4

5
#include "comp.h"
6

7
int main (void)
8
{ struct comp a,b,c;
9
  printf("Bitte Realteil und Imagin\x84rteil eingeben  ");
10
scanf("%f%f", &a.real,&a.img);
11
  printf("Bitte Realteil und Imagin\x84rteil eingeben  ");
12
scanf("%f%f", &b.real,&b.img);
13

14
printf("a = %8.2f + %8.2fi\n", a.real, a.img);
15
printf("b = %8.2f + %8.2fi\n", b.real, b.img);
16
c = cfadd(a,b);
17
printf("a+b = %8.2f +%8.2fi\n", c.real, c.img);
18
c = cfmul(a,b);
19
printf("a*b = %8.2f +%8.2fi\n", c.real, c.img);
20
printf("a hat die L\x84nge %4:2d\n",cfabs(a));
21
printf("b hat die L\x84nge %4:2d\n",cfabs(b));
22
  system ("pause");
23
return 0;
24
}
Gast #1102712
Lesenswert?

Aha Aha, ja so in etwa hatte ich es auch schon aufgeteilt, also mein 
Header hatte ich schon so wie ihr geschrieben hattet. Ich habe 
allerdings im *.c modul dann die Funktions-deklarationen entfernt...weil 
sie ja dann schon im Header stehen.Da sehe ich keinen Sinn...wieso 
schreibt man die Prototypen der Funktionen extra noch mal in den Header 
und lässt sie aber dennoch im *.c modul??? Na dann setz ich mich noch 
einmal dahinter....eine Frage habe ich jedoch noch.Ich arbeite mit dem 
Dev c++ Compiler. Muss ich dann, um eine vernünftige ausführbare Datei 
zu kriegen, ein Projekt-file mit den drei Komponenten erstellen, gibts 
da besondere Regeln oder muss man etwas beachten?
Vielen, vielen Dank für die Hilfe!!!!!!!!!!!1

Gruß Felix
#1102747
Lesenswert?

Fizzle wrote:
> Ich habe allerdings im *.c modul dann die Funktions-deklarationen
> entfernt...weil sie ja dann schon im Header stehen.Da sehe ich keinen
> Sinn...wieso schreibt man die Prototypen der Funktionen extra noch mal in
> den Header und lässt sie aber dennoch im *.c modul???

Die Deklarationen stehen ja auch nicht in der *.c Datei. Sie werden 
durch das
1
#include "comp.h"
in comp.c eingebunden.

In der comp.c Datei stehen die Definitionen.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren