Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // printf("Funktioniert!\n");
- char marscalc(int argc, char *argv[]);
- int digit_laenge (char *digit);
- int alloc_laenge_1(char *argv[]);
- void alloc_cpy(char *argv[],char *digit);
- int alloc_laenge_2(char *argv[]);
- void alloc_cpy2(char *argv[],char *digit);
- char vorzeichen_schiebung(char *digit);
- char null_move (char *result);
- char null_fueller(char *digit1, char *digit2);
- char addition(char *digit1, char *digit2);
- char subtraktion(char *digit1, char *digit2);
- int main(int argc, char *argv[])
- {
- marscalc(argc,argv);
- return 0;
- }
- char marscalc(int argc, char *argv[])
- {
- (void) argc; //argc wird nie benutzt deshalb void damit der Upload funktioniert.
- int c = alloc_laenge_1(argv);
- char *digit1 = (char *)malloc(c * sizeof(char));
- alloc_cpy(argv,digit1);
- int d = alloc_laenge_2(argv);
- char *digit2 = (char *)malloc(d * sizeof(char));
- alloc_cpy2(argv,digit2);
- char op = argv[2][0];
- if(digit1[0] == '+' || digit1[0] == '-')
- vorzeichen_schiebung(digit1);
- if(digit2[0] == '+' || digit2[0] == '-')
- vorzeichen_schiebung(digit2);
- printf("Number: %s\n", digit1);
- printf("Number: %c\n", op);
- printf("Number: %s\n", digit2);
- null_move(digit1);
- null_move(digit2);
- null_fueller(digit1,digit2);
- if(op == '+')
- addition(digit1,digit2);
- if(op == '-')
- subtraktion(digit1,digit2);
- free(digit1);
- free(digit2);
- return 0;
- }
- int digit_laenge (char *digit)
- {
- int i = 0;
- for(; digit[i] != '\0'; i++){
- }
- return i;
- }
- int alloc_laenge_1(char *argv[]){
- int i = 0;
- for(i = 0;argv[1][i] != '\0'; i++ ){
- }
- return i+1;
- }
- int alloc_laenge_2(char *argv[]){
- int i = 0;
- for(i = 0;argv[3][i] != '\0'; i++ ){
- }
- return i+1;
- }
- void alloc_cpy(char *argv[],char *digit){
- int i = 0;
- for(i = 0;argv[1][i] != '\0'; i++ ){
- digit[i] = argv[1][i];
- }
- digit[i+1] = '\0';
- }
- void alloc_cpy2(char *argv[],char *digit){
- int i = 0;
- for(i = 0;argv[3][i] != '\0'; i++ ){
- digit[i] = argv[3][i];
- }
- digit[i+1] = '\0';
- }
- char vorzeichen_schiebung(char *digit)
- {
- for(int i = 0; digit[i] != '\0';i++){
- digit[i] = digit[i+1];
- }
- return *digit;
- }
- char null_move (char *result)
- {
- int laenge = digit_laenge(result);
- int vorne_ja = 0;
- int vorne_nulls = 0;
- int hinten_nulls = 0;
- int komma_ja = 0;
- int komma = 0;
- for(int i = 0; result[i] == '0' || result[i] == ',' || result[i] == '\0'; i++){
- if(result[i] == '\0'){
- result[0] = '0';
- result[1] = '\0';
- printf("result lautet in nullmove: %s\n", result);
- return *result;}
- }
- for(int i = 0; result[i] == '0'; i++){ // sobald vor dem komma nur 0en sind , komma_ja = 1
- if(result[i+1] == ','){
- komma_ja = 1;
- }
- if(i == laenge){
- break;
- }
- }
- for(int i = 0; result[i] != ',';i++){ // sobald irgendein komma gefunden wird
- if(result[i+1] == ','){
- komma = 1;
- }
- if(i == laenge){
- break;
- }
- }
- if(result[0] == '0'){ // vorne nullen zählen
- vorne_ja = 1;
- for(int i = 0; result[i] == '0'; i++){
- vorne_nulls = i+1;}
- }
- if(result[laenge-1] == '0'){ // hinten nullen zählen
- for(int i = laenge-1; result[i] == '0'; i--){
- hinten_nulls = laenge - i;}
- }
- if(komma_ja == 1){
- vorne_nulls = vorne_nulls - 1;
- }
- int zahl = laenge - vorne_nulls - hinten_nulls;
- if(komma == 0)
- zahl = laenge - vorne_nulls;
- int null_ter = zahl ;
- if (vorne_ja == 1){
- for(int i = 0; i != zahl; i++){
- result[i] = result[i+vorne_nulls];
- }
- }
- result[null_ter] = '\0';
- if (result[0] == '0' && result[1] == ',' && result[2] == '\0')
- result[1] = '\0';
- laenge = digit_laenge(result);
- if(result[laenge-1] == ',')
- result[laenge-1] = '\0';
- /* printf("komma_pos lautet in nullmove: %d\n", komma_pos);
- printf("laenge lautet in nullmove: %d\n", laenge);
- printf("zahl lautet in nullmove: %d\n", zahl);
- printf("vorne_ja lautet in nullmove: %d\n", vorne_ja);
- printf("vorne_nulls lautet in nullmove: %d\n", vorne_nulls);
- printf("hinten_nulls lautet in nullmove: %d\n", hinten_nulls);
- -0034,34237600 + -001282,1100
- */
- printf("result lautet in nullmove: %s\n", result);
- return 0;
- }
- char null_fueller(char *digit1, char *digit2)
- {
- int vorkommazahlen_digit1 = 0;
- int vorkommazahlen_digit2 = 0;
- int gros_vorkommazahl = 0;
- int gros_nachkommazahl = 0;
- int nachkommazahlen_digit1 = 0;
- int nachkommazahlen_digit2 = 0;
- int komma_ja_1 = 0;
- int komma_ja_2 = 0;
- int komma_pos_1 = 0;
- int komma_pos_2 = 0;
- int komma_end = 0;
- int speicher = 0;
- for(int i = 0; digit1[i] != ','; i++ ){ // zählt bis zum komma, +1 weil es bei 0 anfängt -34,342376 + -1282,11
- vorkommazahlen_digit1 = i+1; // +1 weil sonst eine stelle fehlt
- if(digit1[i] == '\0'){
- vorkommazahlen_digit1 -=1; // für den fall dass eine zahl ohne komma kommt
- break;} // zb. 34
- }
- for(int i = 0;digit2[i] != ','; i++ ){
- vorkommazahlen_digit2 = i+1;
- if(digit2[i] == '\0'){
- vorkommazahlen_digit2 -=1;
- break;}
- }
- for(int i = 0;digit1[i] != '\0'; i++ ){ // zählt nach dem komma
- if (komma_ja_1 == 1)
- nachkommazahlen_digit1 += 1;
- if(digit1[i] == ','){
- komma_ja_1 += 1;
- komma_pos_1 = i;}
- }
- for(int i = 0;digit2[i] != '\0'; i++ ){
- if (komma_ja_2 == 1)
- nachkommazahlen_digit2 += 1;
- if(digit2[i] == ','){
- komma_ja_2 += 1;
- komma_pos_2 = i;}
- }
- if(vorkommazahlen_digit1 >= vorkommazahlen_digit2){
- gros_vorkommazahl = vorkommazahlen_digit1;
- }else{
- gros_vorkommazahl = vorkommazahlen_digit2;}
- if(nachkommazahlen_digit1 >= nachkommazahlen_digit2){
- gros_nachkommazahl = nachkommazahlen_digit1;
- }else{
- gros_nachkommazahl = nachkommazahlen_digit2;}
- if(komma_ja_1 == 0 && komma_ja_2 == 0){
- speicher = gros_nachkommazahl + gros_vorkommazahl;}
- if(komma_ja_1 == 1 && komma_ja_2 == 0){
- speicher = gros_nachkommazahl + gros_vorkommazahl + 1;
- komma_end = speicher - gros_nachkommazahl - 1 ;}
- if(komma_ja_1 == 0 && komma_ja_2 == 1){
- speicher = gros_nachkommazahl + gros_vorkommazahl + 1;
- komma_end = speicher - gros_nachkommazahl - 1;}
- if(komma_ja_1 == 1 && komma_ja_2 == 1){
- speicher = gros_nachkommazahl + gros_vorkommazahl +1;
- komma_end = speicher - gros_nachkommazahl - 1;}
- char *digitfull_1 = (char *)malloc(speicher * sizeof(char));
- char *digitfull_2 = (char *)malloc(speicher * sizeof(char));
- int laenge_digit1 = speicher - vorkommazahlen_digit1;
- int laenge_digit2 = speicher - vorkommazahlen_digit2;
- for(int i = 0; i < speicher; i++)
- digitfull_1[i] = '0';
- digitfull_1[speicher] = '\0';
- for(int i = 0; i < speicher; i++)
- digitfull_2[i] = '0';
- digitfull_2[speicher] = '\0';
- if(komma_ja_1 == 0 && komma_ja_2 == 0){
- if(vorkommazahlen_digit1 > vorkommazahlen_digit2){
- for(int i = gros_vorkommazahl-1; i >= 0; i--){
- digitfull_1 [i] = digit1[i];
- if(digit1[i] < 48 || digit1[i] > 57)
- digitfull_1 [i] = '0';
- }
- for(int i = vorkommazahlen_digit2-1; i >= 0; i--){ // hier stand mal int i = vorkommazahlen_digit2-1
- digitfull_2 [i+laenge_digit2] = digit2[i];
- if(digit2[i-vorkommazahlen_digit2] < 48 || digit2[i] > 57)
- digitfull_2 [i-laenge_digit2] = '0';
- }
- }
- if(vorkommazahlen_digit1 < vorkommazahlen_digit2){
- for(int i = gros_vorkommazahl-1; i >= 0; i--){
- digitfull_2 [i] = digit2[i];
- if(digit2[i] < 48 || digit2[i] > 57)
- digitfull_2 [i] = '0';
- }
- for(int i = vorkommazahlen_digit1-1; i >= 0; i--){ // hier stand mal int i = vorkommazahlen_digit2-1
- digitfull_1 [i+laenge_digit1] = digit1[i];
- if(digit1[i-vorkommazahlen_digit1] < 48 || digit1[i] > 57)
- digitfull_1 [i-laenge_digit1] = '0';
- }
- }
- if(vorkommazahlen_digit1 == vorkommazahlen_digit2){
- for(int i = gros_vorkommazahl-1; i >= 0; i--){
- digitfull_1 [i] = digit1[i];
- if(digit1[i] < 48 || digit1[i] > 57)
- digitfull_1 [i] = '0';
- }
- for(int i = gros_vorkommazahl-1; i >= 0; i--){
- digitfull_2 [i] = digit2[i];
- if(digit2[i] < 48 || digit2[i] > 57)
- digitfull_2 [i] = '0';
- }
- }
- }
- if(komma_ja_1 == 1 && komma_ja_2 == 0){
- digitfull_1[komma_end] = ',';
- digitfull_2[komma_end] = ',';
- for(int i = 0; i < gros_vorkommazahl ; i++){ // füllt vordere zahlen
- digitfull_1[komma_end-1-i] = digit1[komma_pos_1-1-i];
- if(komma_pos_1-1-i < 0)
- digitfull_1[komma_end-1-i] = '0';
- }
- for(int i = 0; i < gros_nachkommazahl; i++){
- digitfull_1[komma_end+1+i] = digit1[komma_pos_1+1+i];
- }
- for(int i = 0; i < gros_vorkommazahl; i++){ // schleife für die digit2
- digitfull_2[komma_end-1-i] = digit2[vorkommazahlen_digit2-1-i];
- if(vorkommazahlen_digit2-1-i < 0)
- digitfull_2[komma_end-1-i] = '0';
- }
- }
- if(komma_ja_1 == 0 && komma_ja_2 == 1){
- digitfull_1[komma_end] = ',';
- digitfull_2[komma_end] = ',';
- for(int i = 0; i < gros_vorkommazahl; i++){ // schleife für die digit2
- digitfull_1[komma_end-1-i] = digit1[vorkommazahlen_digit1-1-i];
- if(vorkommazahlen_digit1-1-i < 0)
- digitfull_1[komma_end-1-i] = '0';
- }
- for(int i = 0; i < gros_vorkommazahl ; i++){ // füllt vordere zahlen
- digitfull_2[komma_end-1-i] = digit2[komma_pos_2-1-i];
- if(komma_pos_2-1-i < 0)
- digitfull_2[komma_end-1-i] = '0';
- }
- for(int i = 0; i < gros_nachkommazahl; i++){
- digitfull_2[komma_end+1+i] = digit2[komma_pos_2+1+i];
- }
- }
- if(komma_ja_1 == 1 && komma_ja_2 == 1){
- digitfull_1[komma_end] = ',';
- digitfull_2[komma_end] = ',';
- for(int i = 0; i < gros_vorkommazahl ; i++){ // füllt vordere zahlen
- digitfull_1[komma_end-1-i] = digit1[komma_pos_1-1-i];
- if(komma_pos_1-1-i < 0)
- digitfull_1[komma_end-1-i] = '0';
- }
- for(int i = 0; i < gros_nachkommazahl; i++){
- digitfull_1[komma_end+1+i] = digit1[komma_pos_1+1+i];
- if(komma_end+1+i > komma_end + nachkommazahlen_digit1)
- digitfull_1[komma_end+1+i] = '0';
- }
- for(int i = 0; i < gros_vorkommazahl ; i++){ // füllt vordere zahlen
- digitfull_2[komma_end-1-i] = digit2[komma_pos_2-1-i];
- if(komma_pos_2-1-i < 0)
- digitfull_2[komma_end-1-i] = '0';
- }
- for(int i = 0; i < gros_nachkommazahl; i++){
- digitfull_2[komma_end+1+i] = digit2[komma_pos_2+1+i];
- if(komma_end+1+i > komma_end + nachkommazahlen_digit2)
- digitfull_2[komma_end+1+i] = '0';
- }
- }
- /* printf("vorkommazahlen_digit1 in null_fill: %d\n", vorkommazahlen_digit1);
- printf("nachkommazahlen_digit1 in null_fill: %d\n", nachkommazahlen_digit1);
- printf("vorkommazahlen_digit2 in null_fill: %d\n", vorkommazahlen_digit2);
- printf("nachkommazahlen_digit2 in null_fill: %d\n", nachkommazahlen_digit2);
- printf("gros_vorkommazahl in null_fill: %d\n", gros_vorkommazahl);
- printf("gros_nachkommazahl in null_fill: %d\n", gros_nachkommazahl);
- printf("komma_pos_1 in null_fill: %d\n", komma_pos_1);
- printf("komma_pos_2 in null_fill: %d\n", komma_pos_2);
- printf("komma_ja_1 in null_fill: %d\n", komma_ja_1);
- printf("komma_ja_2 in null_fill: %d\n", komma_ja_2);
- printf("komma_end in null_fill: %d\n", komma_end);
- printf("digitfull_1 in null_fill: %s\n", digitfull_1);
- printf("digitfull_2 in null_fill: %s\n", digitfull_2);
- printf("speicher in null_fill: %d\n", speicher); */
- printf("digitfull_1 in null_fill nach allem: %s\n", digitfull_1);
- printf("digitfull_2 in null_fill nach allem: %s\n", digitfull_2);
- for(int i = 0; i < speicher; i++)
- digit1[i] = digitfull_1[i];
- digit1[speicher] = '\0';
- for(int i = 0; i < speicher; i++)
- digit2[i] = digitfull_2[i];
- digit2[speicher] = '\0';
- free(digitfull_1);
- free(digitfull_2);
- return 0;
- }
- char addition(char *digit1, char *digit2)
- {
- printf("digit1 in add: %s\n", digit1);
- printf("digit2 in add: %s\n", digit2);
- int speicher = digit_laenge(digit1) +1;
- int speicher2 = digit_laenge(digit1) ;
- char *result = (char *)malloc(speicher * sizeof(char));
- for(int i = 0; i < speicher; i++) // ergebnis füllen mit 0en und erste stelle ist 0 für den übertrag
- result[i] = 0;
- result[speicher] = '\0';
- result[0] = '0';
- int ia = 0;
- int ia_vorcast = 0;
- int ib = 0;
- int ib_vorcast = 0;
- int ic = 0;
- int ubertrag = 0;
- int rest = 0;
- printf("result vor allem lautet: %s\n", result);
- for(int i = 1; i <= speicher-1; i++){
- ia_vorcast = digit1[speicher - i - 1];
- ib_vorcast = digit2[speicher - i - 1];
- ia = ia_vorcast - 48;
- ib = ib_vorcast - 48;
- ubertrag = result[speicher - i];
- ic = ia + ib + ubertrag ;
- if(ic < 9 && ia_vorcast != ','){
- result[speicher - i] = 0;
- result[speicher - i] += ic + 48;
- }
- if(ic >= 9 && ia_vorcast != ','){
- rest = ic % 9;
- result[speicher - i] = 0;
- result[speicher - i] += rest + 48;
- if(digit1[speicher - i -2] == ','){
- result[speicher - i -2] += 1;}else{
- result[speicher - i -1] += 1;}
- }
- if(ia_vorcast == ',' && ib_vorcast == ','){
- result[speicher - i] = ',';}
- }
- if(result[0] == '0'){
- for(int i = 0; i < speicher; i++)
- result[i] = result[i+1];
- result[speicher2] = '\0';
- }
- printf("result lautet: %s\n", result);
- free(result);
- return 0;
- }
- char subtraktion(char *digit1, char *digit2)
- {
- int speicher = digit_laenge(digit1) +1;
- int speicher2 = digit_laenge(digit1);
- char *digitzwischenspeicher = (char *)malloc(speicher2 * sizeof(char));
- char *result = (char *)malloc(speicher * sizeof(char));
- int ia = 0;
- int ia_vorcast = 0;
- int ib = 0;
- int ib_vorcast = 0;
- int ic = 0;
- int zahl = 0;
- int digit1_gros_ja = 0;
- int digit2_gros_ja = 0;
- int ubertrag = 0;
- int ib_ubertrag = 0;
- for(int i = 0; i < speicher; i++)
- result[i] = 0;
- result[speicher] = '\0';
- result[0] = '0';
- for(int i = 0;i < speicher; i++){
- if(digit1[i] > digit2[i]){
- digit1_gros_ja = 1;
- break;}
- if(digit1[i] < digit2[i]){
- digit2_gros_ja = 1;
- break;}
- }
- if(digit2_gros_ja == 1){
- for(int i = 0; i < speicher2; i++){
- digitzwischenspeicher[i] = digit2[i];
- digitzwischenspeicher[speicher2] = '\0';
- }
- for(int i = 0; i < speicher2; i++){
- digit2[i] = digit1[i];
- digit2[speicher2] = '\0';
- }
- for(int i = 0; i < speicher2; i++){
- digit1[i] = digitzwischenspeicher[i];
- digit1[speicher2] = '\0';
- }
- }
- if(digit1_gros_ja == 0 && digit2_gros_ja == 0){
- printf("0 \n");
- }
- for(int i = 1; i <= speicher-1; i++){
- ia_vorcast = digit1[speicher - i - 1];
- ib_vorcast = digit2[speicher - i - 1];
- ia = ia_vorcast - 48;
- ib = ib_vorcast - 48;
- ubertrag = result[speicher - i];
- ib_ubertrag = ib + ubertrag;
- ic = ia - (ib + ubertrag);
- if(ib_ubertrag == 9 && ia != ',' ){
- result[speicher -i]= ia;
- result[speicher -i -1]= 1;
- }
- if(ic >= 0 && ia != ',' ){
- result[speicher-i] = ic + 48;
- }
- if(ic < 0 && ia != ','){
- zahl = (9+ia) - ib - ubertrag;
- result[speicher-i] = zahl + 48;
- result[speicher -i -1]= 1;
- if(digit1[speicher - i - 2] == ',')
- result[speicher -i -2]= 1;
- }
- if(ia_vorcast == ',' && ib_vorcast == ','){
- result[speicher - i] = ',';}
- }
- null_move(result);
- printf("%s\n", result);
- free(result);
- free(digitzwischenspeicher);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement