Autor |
Beitrag |
01110111
Hält's aus hier
Beiträge: 6
|
Verfasst: So 01.10.17 16:24
Hallo Zusammen,
ich möchte eine csv Datei erstellen. Diese Datei soll in der Ersten Spalte eine gewisse Anzahl an Zuständen haben. z.B. 4374. Die zweite, dritte und vierte Spalte soll die Werte der Fakultät N = 3 (3! = 6) haben. (d.h. 012, 102, 201, 210, 021, 120). Die restlichen sechs Spalten können jeweils Werte von 0-2 annehmen (N=3).
Das heißt die Erste Spalte besitzt nun z.B. die Zustände 1-6.
1 012 000000
2 102 000000
3 201 000000
4 210 000000
5 021 000000
6 120 000000
7 012 000001
8 102 000001
9 201 000001
10 210 000001
11 021 000001
12 120 000001
usw.
Habe die letzten 9 Spalten mittels for-Schleifen geschrieben und darum eine while-Schleife gesetzt, welche die Zustände angeben soll.
Leider erhalte ich nicht die gewünschte Ausgabe. Siehe Dateianhang.
Könnte mir hier jemand vielleicht weiterhelfen?
Wäre euch sehr dankbar!
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: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60:
| #include <stdio.h>
int main() { int q = 1, x = 0, y = 0, z = 0; int a = 0, b = 0, c = 0; FILE *permutation=fopen("testn3.csv","w+"); fprintf(permutation, "Zustand; N; a; b; c;\n"); { while(q <= 162){ for(a=0; a<=3; a++) { for(b=0; b<=3; b++) { for(c=0; c<=3; c++) { for (x = 0; x <= 3; x++) { for (y = 0; y <= 3; y++) { for (z = 0; z <= 3; z++) { { fprintf(permutation, "%d %25d %d %d %30d %30d %30d\n", q,a,b,c,x,y,z); q++; } } } } } } } } } } |
Moderiert von Th69: Code-Tags hinzugefügt
Moderiert von Christian S.: Code- durch C++-Tags ersetzt
Einloggen, um Attachments anzusehen!
|
|
Horst_H
Beiträge: 1653
Erhaltene Danke: 243
WIN10,PuppyLinux
FreePascal,Lazarus
|
Verfasst: So 01.10.17 18:16
Hallo,
editiere mal deine Frage und hänge am Ende Deines Quelltextes ein "[/code]" oder "[/cs]" ohne Anführungszeichen an.
Lese ich richtig, das in die erste Spalte einfach nur die Zeilennummer kommt und dann wird anschliessend die Permutation angehängt und wenn die Permutation durch ist wird schlussendlich eine 6 stellige Zahl zur Basis 3 == (Zeilennummer-1)\(Anzahl Permutation) angehängt.
Für Permutationen gibt es ja vieles im Netz, wenn nicht hier im Forum.
Ala: stackoverflow.com/qu...ons-of-a-string-in-c
Gruß Horst
|
|
01110111
Hält's aus hier
Beiträge: 6
|
Verfasst: So 01.10.17 18:57
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: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59:
| #include <stdio.h> int main() { int q = 1, x = 0, y = 0, z = 0; int a = 0, b = 0, c = 0; FILE *permutation=fopen("testn3.csv","w+"); fprintf(permutation, "Zustand; N; a; b; c;\n"); { while(q <= 162){ for(a=0; a<=2; a++) { for(b=0; b<=2; b++) { for(c=0; c<=2; c++) { for (x = 0; x <= 2; x++) { for (y = 0; y <= 2; y++) { for (z = 0; z <= 2; z++) { { fprintf(permutation, "%d %25d %d %d %30d %30d %30d\n", q,a,b,c,x,y,z); q++; } } } } } } } } } } |
Moderiert von Christian S.: Code- durch C++-Tags ersetzt
|
|
01110111
Hält's aus hier
Beiträge: 6
|
Verfasst: So 01.10.17 19:05
Horst_H hat folgendes geschrieben : | Hallo,
editiere mal deine Frage und hänge am Ende Deines Quelltextes ein "[/code]" oder "[/cs]" ohne Anführungszeichen an.
Lese ich richtig, das in die erste Spalte einfach nur die Zeilennummer kommt und dann wird anschliessend die Permutation angehängt und wenn die Permutation durch ist wird schlussendlich eine 6 stellige Zahl zur Basis 3 == (Zeilennummer-1)\(Anzahl Permutation) angehängt.
Für Permutationen gibt es ja vieles im Netz, wenn nicht hier im Forum.
Ala: stackoverflow.com/qu...ons-of-a-string-in-c
Gruß Horst |
Diese Ausgabe soll erzeugt werden.
siehe Dateianhang
Einloggen, um Attachments anzusehen!
|
|
Horst_H
Beiträge: 1653
Erhaltene Danke: 243
WIN10,PuppyLinux
FreePascal,Lazarus
|
Verfasst: So 01.10.17 19:57
Hallo,
dann versuche doch erst einmal, die Permutation hin zu bekommen.
Denn For-Schleifen können das nicht, damit bekommt man nur Kombinationen ( Lotto 6 aus 49 aka "n über k" ) hin.
stackoverflow.com/qu...cographic-order?rq=1
Oder hardcode es.Einfach als Konstante definieren und gut ist.
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
| const char[5][4] Permutation "012\0","021\0","102\0","120\0","201\0","210\0"
func toBase3 ( Wandelt Zahl in Zeichenkette zur Basis 3 mit Länge 6 ) ....
Counter= 0 wiederhole Für PermNr von 0..5 Counter++; printf(Counter, Permutation[PermNr] , toBase3(Counter-1)/6)) bis Counter zu groß |
Gruß Horst
|
|
Symbroson
Beiträge: 382
Erhaltene Danke: 67
Raspbian, Ubuntu, Win10
C, C++, Python, JavaScript, Lazarus, Delphi7, Casio Basic
|
Verfasst: So 01.10.17 21:54
Horst_H hat folgendes geschrieben : | dann versuche doch erst einmal, die Permutation hin zu bekommen. |
ich habe vor einigen Monaten mal ein Permutations-Programm in Python geschrieben. Dieses baut (rekursiv) ein Array of Arrays auf, in dem die indizes der zu permutierenden Elemente geschrieben werden. Du (@01110111) kannst das versuchen in c zu übersetzen - sollte nicht allzu schwer sein glaube ich.
Einloggen, um Attachments anzusehen!
_________________ most good programmers do programming not because they expect to get paid or get adulation by the public, but because it's fun to program. (Linus Torvalds)
|
|
01110111
Hält's aus hier
Beiträge: 6
|
Verfasst: Mo 02.10.17 18:32
Hallo Horst,
habe mein Programm jetzt mal entsprechend umgeschrieben. Erhalte aber leider immer noch nicht die Richtige Ausgabe. Siehst du eventuell den Fehler?
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: 49: 50: 51: 52: 53: 54: 55: 56:
| #include <stdio.h>
void swap(int *x1,int *x2) { int x=*x1; *x1=*x2; *x2=x; } void per(int *arr,int st,int ls) { int i=0; if(st==ls) { int k; for(k=0;k<ls;k++) { } } else { for(i=st;i<ls;i++) { swap(arr+st,arr+i); per(arr,st+1,ls); swap(arr+st,arr+i); } } }
int main() { int arr[3]={0,1,2}; int st=0; int ls=3; per(arr,st,ls); int c0, c1, c2, c3; int n = 0; for (char c0 = '0'; c0 <= '2'; c0++) for (char c1 = '0'; c1 <= '2'; c1++) for (char c2 = '0'; c2 <= '2'; c2++) for (size_t i = 0; i < sizeof (arr) / sizeof (*arr); i++) printf ("%4d: %d %c%c%c\n", ++n ,arr[i], c0, c1, c2); } |
Moderiert von Christian S.: Code- durch C++-Tags ersetzt
|
|
Symbroson
Beiträge: 382
Erhaltene Danke: 67
Raspbian, Ubuntu, Win10
C, C++, Python, JavaScript, Lazarus, Delphi7, Casio Basic
|
Verfasst: Mo 02.10.17 20:40
Dein Code funktioniert, du hast bloß die richtige ausgabe auskommentiert. Dann werden deine Zahlen richtig permutiert ausgegeben:
(Oder habe ich dich falsch verstanden?)
C++-Quelltext 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:
| #include <stdio.h>
void swap(int *x1, int *x2) { int x = *x1; *x1 = *x2; *x2 = x; }
void per(int *arr, int st, int ls) { if (st == ls) { for (int k = 0; k < ls; k++) printf("%d ",arr[k]); printf("\n"); } else { for (int i = st; i < ls; i++) { swap(arr + st, arr + i); per(arr, st + 1, ls); swap(arr + st, arr + i); } } }
int main() { int len = 3, start = 0, arr[len] = {0, 1, 2}; per(arr, start, len); } |
_________________ most good programmers do programming not because they expect to get paid or get adulation by the public, but because it's fun to program. (Linus Torvalds)
|
|
01110111
Hält's aus hier
Beiträge: 6
|
Verfasst: Mo 02.10.17 20:50
Wenn ich die obere Zeile nicht auskommentiere, erhalte ich nicht die passende Ausgabe.
Habe zuerst die oberen Funktionen genommen und mit der Erweiterung verknüpft. Die Ausgabe soll aber entsprechend ausgegeben werden, wie ich sie in Zeile 53 habe.
|
|
Symbroson
Beiträge: 382
Erhaltene Danke: 67
Raspbian, Ubuntu, Win10
C, C++, Python, JavaScript, Lazarus, Delphi7, Casio Basic
|
Verfasst: Mo 02.10.17 21:17
Ich hab mich mal an deinen ersten Post gehalten und versicht die Ausgabe die du dort beschrieben hast umzusetzen. Entspricht das deinen Vorstellungen?
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:
| #include <stdio.h>
void swap(int *x1, int *x2) { int x = *x1; *x1 = *x2; *x2 = x; }
void per(int *arr, int st, int ls, int &col1, int col3) { if (st == ls) { printf("%3d ", (col1)++); for (int k = 0; k < ls; k++) printf("%d",arr[k]); printf(" %06d \n", col3); } else { for (int i = st; i < ls; i++) { swap(arr + st, arr + i); per(arr, st + 1, ls, col1, col3); swap(arr + st, arr + i); } } }
int main() { int len = 3, arr[len] = {0, 1, 2}, start = 0, col1 = 1; for (int i=0; i<3; i++) per(arr, start, len, col1, i); } |
Ausgabe:
Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
| 1 012 000000 2 021 000000 3 102 000000 4 120 000000 5 210 000000 6 201 000000 7 012 000001 8 021 000001 9 102 000001 10 120 000001 11 210 000001 12 201 000001 13 012 000002 14 021 000002 15 102 000002 16 120 000002 17 210 000002 18 201 000002 |
_________________ most good programmers do programming not because they expect to get paid or get adulation by the public, but because it's fun to program. (Linus Torvalds)
|
|
01110111
Hält's aus hier
Beiträge: 6
|
Verfasst: Mo 02.10.17 22:06
Ich würde gerne folgende Ausgabe erhalten.
1. Spalte 1-162
2. Spalte N=3
3. Spalte ebenfalls 3 Elemente (anstatt 6)
Vielen Dank vorab, dass du dir die Mühe gegeben hast.
Ausgabe:
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: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162:
| 1: 012 000 2: 102 000 3: 201 000 4: 210 000 5: 021 000 6: 120 000 7: 012 001 8: 102 001 9: 201 001 10: 210 001 11: 021 001 12: 120 001 13: 012 002 14: 102 002 15: 201 002 16: 210 002 17: 021 002 18: 120 002 19: 012 010 20: 102 010 21: 201 010 22: 210 010 23: 021 010 24: 120 010 25: 012 011 26: 102 011 27: 201 011 28: 210 011 29: 021 011 30: 120 011 31: 012 012 32: 102 012 33: 201 012 34: 210 012 35: 021 012 36: 120 012 37: 012 020 38: 102 020 39: 201 020 40: 210 020 41: 021 020 42: 120 020 43: 012 021 44: 102 021 45: 201 021 46: 210 021 47: 021 021 48: 120 021 49: 012 022 50: 102 022 51: 201 022 52: 210 022 53: 021 022 54: 120 022 55: 012 100 56: 102 100 57: 201 100 58: 210 100 59: 021 100 60: 120 100 61: 012 101 62: 102 101 63: 201 101 64: 210 101 65: 021 101 66: 120 101 67: 012 102 68: 102 102 69: 201 102 70: 210 102 71: 021 102 72: 120 102 73: 012 110 74: 102 110 75: 201 110 76: 210 110 77: 021 110 78: 120 110 79: 012 111 80: 102 111 81: 201 111 82: 210 111 83: 021 111 84: 120 111 85: 012 112 86: 102 112 87: 201 112 88: 210 112 89: 021 112 90: 120 112 91: 012 120 92: 102 120 93: 201 120 94: 210 120 95: 021 120 96: 120 120 97: 012 121 98: 102 121 99: 201 121 100: 210 121 101: 021 121 102: 120 121 103: 012 122 104: 102 122 105: 201 122 106: 210 122 107: 021 122 108: 120 122 109: 012 200 110: 102 200 111: 201 200 112: 210 200 113: 021 200 114: 120 200 115: 012 201 116: 102 201 117: 201 201 118: 210 201 119: 021 201 120: 120 201 121: 012 202 122: 102 202 123: 201 202 124: 210 202 125: 021 202 126: 120 202 127: 012 210 128: 102 210 129: 201 210 130: 210 210 131: 021 210 132: 120 210 133: 012 211 134: 102 211 135: 201 211 136: 210 211 137: 021 211 138: 120 211 139: 012 212 140: 102 212 141: 201 212 142: 210 212 143: 021 212 144: 120 212 145: 012 220 146: 102 220 147: 201 220 148: 210 220 149: 021 220 150: 120 220 151: 012 221 152: 102 221 153: 201 221 154: 210 221 155: 021 221 156: 120 221 157: 012 222 158: 102 222 159: 201 222 160: 210 222 161: 021 222 162: 120 222 |
Moderiert von Christian S.: Code-Tags hinzugefügt
|
|
Symbroson
Beiträge: 382
Erhaltene Danke: 67
Raspbian, Ubuntu, Win10
C, C++, Python, JavaScript, Lazarus, Delphi7, Casio Basic
|
Verfasst: Mo 02.10.17 22:20
Spalte drei ist Spalte eins ganzzahlig dividiert mit 6 im Trinärsystem (wenn Spalte eins mit 1 beginnt musst du vor der Division 1 abziehen)
das ist meines Erachtens die wohl einfachste Variante. Ich will natürlich nicht anzweifeln dass es eine effizientere Methode gibt
_________________ most good programmers do programming not because they expect to get paid or get adulation by the public, but because it's fun to program. (Linus Torvalds)
Zuletzt bearbeitet von Symbroson am Mo 02.10.17 23:28, insgesamt 1-mal bearbeitet
|
|
Symbroson
Beiträge: 382
Erhaltene Danke: 67
Raspbian, Ubuntu, Win10
C, C++, Python, JavaScript, Lazarus, Delphi7, Casio Basic
|
Verfasst: Mo 02.10.17 23:05
Diese Version dürfte jetzt endlich die Zielstellung erfüllen ^^
Beachte: wenn du eine längere Liste haben willst (also 3 Zeichen pro Spalte überschritten werden) musst du entweder die lookup Tabellen für die 3er bzw 10er Potenzen erweitern, oder direkt eine Funktion dafür schreiben die die errechnet, was um einiges langsamer wäre. Außerdem musst du len in toInt3 anpassen (oder gleich als Argument übergeben lassen
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:
| #include <stdio.h> #define uint unsigned int
void swap(int *x1, int *x2) { int x = *x1; *x1 = *x2; *x2 = x; }
int toInt3(uint num) { uint pot3[] = { 1, 3, 9 }, pot10[] = { 1, 10, 100 }, t, len = 3, ret = 0;
while (len--) { t = num/pot3[len]; ret += t * pot10[len]; num -= t * pot3[len]; } return ret; }
void per(int *arr, int st, int ls, int &col1) { if (st == ls) { printf("%3d: ", col1+1); for (int k = 0; k < ls; k++) printf("%d", arr[k]); printf(" %03d \n", toInt3(col1++/6)); } else { for (int i = st; i < ls; i++) { swap(arr + st, arr + i); per(arr, st + 1, ls, col1); swap(arr + st, arr + i); } } }
int main() { int len = 3, arr[len] = {0, 1, 2}, start = 0, col1 = 0; for (int i=0; i<27; i++) per(arr, start, len, col1); } |
Output:
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:
| 1: 012 000 2: 021 000 3: 102 000 4: 120 000 5: 210 000 6: 201 000 7: 012 001 8: 021 001 9: 102 001 10: 120 001 11: 210 001 12: 201 001 13: 012 002 14: 021 002 15: 102 002 16: 120 002 17: 210 002 18: 201 002 19: 012 010 ... 150: 201 220 151: 012 221 152: 021 221 153: 102 221 154: 120 221 155: 210 221 156: 201 221 157: 012 222 158: 021 222 159: 102 222 160: 120 222 161: 210 222 162: 201 222 |
Beste Grüße,
Symbroson
_________________ most good programmers do programming not because they expect to get paid or get adulation by the public, but because it's fun to program. (Linus Torvalds)
|
|
Fiete
Beiträge: 611
Erhaltene Danke: 347
W7
Delphi 6 pro
|
Verfasst: Di 03.10.17 14:54
Moin 01110111,
unter 123456789 to 987654321 findest Du drei Seiten Beiträge.
Gruß Fiete
_________________ Fietes Gesetz: use your brain (THINK)
|
|
|