Autor Beitrag
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 21.12.04 15:39 
Hi :)!

Über das "Taschenrechner"-Niveau bin ich in Delphi zwar schon einige Zeit hinaus, aber nach 8 Übungseinheiten einen Taschenrechner in C zu programmieren, der mit der polnischen Notation arbeitet...
Folgendes Problem: ich hab den prinzipiellen Code soweit fertig - was mit fehlt ist der Part, wo "[still missing]" steht - da soll der letzte verwendete Operator bzw. Zahl aus dem String gelöscht werden, also zB

1 2 + 4 *

2 + 4 *

Zur polnischen Notation:

Normal (1 + 2)*(3 + 4)
Polnisch 1 2 + 3 4 + *

Hier der Code:

main.c
ausblenden volle Höhe 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:
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:
/*
 * file: main.c [Taschenrechner mit polnischer Notation]
 *
 * date: 2004-12-17
 *
 * author: Andreas Unterweger
 * email: aunterweger.tks2004@fh-sbg.ac.at
 *
 * Salzburg University of Applied Sciences and Technologies
 * Telecommunications Engeneering
 * INF1-LB/a, exercise 1
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "stack.h"

int main() {
  
  char rechnung[100], dummy[100]; //to save input; dummy variable for current number/operator
  int i; //dummy variable for for-loop
  int position; //to save position of last character number/operator 
  
  int ergebnis = 0; //to save (temporary) result of calculation
  int temp; //temporary 
  
  printf("Taschenrechner mit polnischer Notation\n");
  printf("======================================\n\n");
  
  printf("Bitte geben Sie eine Rechnung ein (z.B. '1 2 - 4 5 + *')\n");
  
  fgets(rechnung, 100, stdin);
  
  while (rechnung[0] != '\0') { //while string not empty
    
    for (i = 0; rechnung[i] != '\0'; i++) { //find space in nonempty string
      
      if (rechnung[i] == ' ') {
        
        position = i - 1; //save position before space
        
        break;
        
        }
      
      }

    for (i = 0; i <= position; i++) { //save current number/operator to dummy string
      
      dummy[i] = rechnung[i];
      
      }  
      
    dummy[i + 1] = '\0'; //terminate string
    
      //delete currently used number/operator in "rechnung" [still missing!!!]
      
    rechnung[i + position + 1] = '\0'; //terminate string

    printf("%s\n", rechnung);
    
    switch (dummy[0]) {
      
      case '+': ergebnis = Pop() + Pop(); Push(ergebnis); break;
      
      case '-': ergebnis = Pop() - Pop(); Push(ergebnis); break;
      
      case '*': ergebnis = Pop() * Pop(); Push(ergebnis); break;
      
      case '/': ergebnis = Pop() / Pop(); Push(ergebnis); break;
      
      default:
      
        temp = atoi(dummy);
      
        Push(temp); //put read number onto stack
      
      }
    
    }
    
  printf("= %d", ergebnis);
  
  return 0;
  
  }


Und falls es jemanden interessiert:

stack.c
ausblenden volle Höhe 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:
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:
/*
 * file: stack.c
 *
 * date: 2004-12-17
 *
 * author: Andreas Unterweger
 * email: aunterweger.tks2004@fh-sbg.ac.at
 *
 * Salzburg University of Applied Sciences and Technologies
 * Telecommunications Engeneering
 * INF1-LB/a, exercise 1
 *
 */

int Stack[10];
int Pos = -1; //ammount of elements - 1 currently put on stack (~ index)

/**********************************
 * Push
 **********************************/
/* Put value onto stack
   Parameters: Value to put on stack
   Returns -1 if stack is full
*/
int Push(int Value) {

  int i;

  if (Pos == 9) { //maximal number of elements reached

    return -1;

    }

  Pos++;

  for (i = Pos; i >= 0; i--) {

    Stack[i + 1] = Stack[i]; //move elements on stack backwards

    }

  Stack[0] = Value; //put Value as first element onto stack

  return Value;

  }

/**********************************
 * Pop
 **********************************/
/* Get first value from stack
   Parameters: none
   Returns -1 if stack is empty
*/
int Pop() {

  int i, temp; //temp to memorize variable

  if (Pos == -1) {

    return -1;

    }

  temp = Stack[0]; //remember first value on stack

  for (i = 0; i <= Pos; i++) {

    Stack[i] = Stack[i + 1]; //move elements on stack upwards

    }

  Pos--;

  return temp;

  }


Ineffizient, aber egal; es geht ja nur ums Prinzip. stack.h dürfte klar sein:

ausblenden Quelltext
1:
2:
int Push(int);
int Pop();


Danke im Voraus für eure Hilfe
AXMD