Ich habe teilweise Code geschrieben, der in einer Datei voller mathematischer Ausdrücke in Postfix scannt und die Nummer auf dem Bildschirm anzeigt.Wie schreibe ich eine while-Schleife, so dass eine ganze Datei gelesen wird, anstatt nur eine Zeile?
Während die Berechnung ausgeführt wird, wird nur in einer Zeile gelesen und dann beendet. Wie ändere ich den Code, um alle arithmetischen Ausdrücke in der Textdatei zu testen?
Ich habe versucht, den Code zu beenden, wenn EOF
, NULL
und '\0'
erreicht sind, aber sie alle auf einer Linie verlassen.
Hier ist mein Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
int top = -1;
float stack[500];
/* push the given data into the stack */
void push (int data) {
stack[++top] = data;
}
/* Pop the top element from the stack */
float pop() {
float data;
if (top == -1)
return -1;
data = stack[top];
stack[top] = 0;
top--;
return (data);
}
int main() {
char str[500];
FILE *p;
if((p=fopen("testfile1.txt","r"))==NULL){
printf("\n Unable to open file string.txt");
return 1;
}
while(fgets(str,500,p)!='\0'){
float data = -1, operand1, operand2, result;
for (int i = 0; i < strlen(str); i++) {
if (isdigit(str[i])) {
/*
* if the i/p char is digit, parse
* character by character to get
* complete operand
*/
data = (data == -1) ? 0 : data;
data = (data * 10) + (str[i] - 48);
continue;
}
if (data != -1) {
/* if the i/p is operand, push it into the stack */
push(data);
}
if (str[i] == '+' || str[i] == '-'
|| str[i] == '*' || str[i] == '/') {
/*
* if the i/p is an operator, pop 2 elements
* from the stack and apply the operator
*/
operand2 = pop();
operand1 = pop();
if (operand1 == -1 || operand2 == -1)
break;
switch (str[i]) {
case '+':
result = operand1 + operand2;
/* push the result into the stack */
push(result);
break;
case '-':
result = operand1 - operand2;
push(result);
break;
case '*':
result = operand1 * operand2;
push(result);
break;
case '/':
result = operand1/operand2;
push(result);
break;
}
}
data = -1;
}
if (top == 0)
printf("Output:%3.2f\n", stack[top]);
else
printf("have given wrong postfix expression\n");
return 1;
}
}
Auch die Gleichungen Ich brauche in lesen sind:
13 1 - 2/3 155 + *
100 100 100 100 + + +
10.33 2 2 2 2 2 * * * * *
30 10 - 10 - 10 - 2 *
300 13.25 - 11 3 -/4 5 - * 3/
Es funktioniert für die erste Gleichung nur. Was kann ich in Bezug auf die Schleifenstruktur tun, um alle diese Gleichungen aus der Textdatei zu lesen?
Beachten Sie, dass 'fgets()' ein 'char *' zurückgibt, und während es syntaktisch gültig ist, ist es definitiv üblich, ''\ 0'' als Nullzeigerkonstante zu schreiben. Verwenden Sie "0" oder "NULL". –
Ja, wollte gerade das hinzufügen. Lies dies: http://stackoverflow.com/questions/1483232/array-of-chars-should-end-at-0-or-0 für mehr Informationen darüber, was Jonathan meinte. Können Sie den Inhalt der Textdatei auch anzeigen? – Tacocat
Sie könnten den obigen Code auch an http://codereview.stackexchange.com senden, da er mehrere andere Fehler enthält. –