Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions calc.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
#include <stdio.h>
#include "operators.h"

#include "my_fscanf.h"
int main(){
FILE *fp = NULL;
int operand1, operand2;
int operand1, operand2,line=0;
char operator = ' ';
int result, line = 0;

double result;
double (*function_p) (int ,int );

fp = fopen("read.txt","r");
if(fp!=NULL){
fscanf(fp, "%d", &line);

for(int i=0; i<line; i++) {
fscanf(fp, "%d %c %d",&operand1, &operator, &operand2);
my_fscanf(fp, "%d", &line);
for(int i=0; i<line && !feof(fp); i++) {
my_fscanf(fp, "%d %c %d",&operand1, &operator, &operand2);
switch(operator) {
case '+':
result = add(operand1, operator);
function_p = add;
break;
case '-':
result = minus(operand1, operator);
function_p = minus;
break;
case '*':
result = mul(operand1, operator);
function_p = mul;
break;
case '/':
result = div(operand1, operator);
function_p = div;
break;
}
printf("%d %c %d = %d\n",
operand1, operator, operand2, result);
}
result = function_p(operand1,operand2);
printf("%d %c %d = %f\n",
operand1, operator, operand2, result);
}
}
return 0;
}

40 changes: 40 additions & 0 deletions my_fscanf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "my_fscanf.h"
#include <stdarg.h>
#include <ctype.h>
#include <stdio.h>
void my_fscanf(FILE *fp, const char *format, ...) {
va_list list;
va_start(list, format);
char* cc;
while (*format) {
if (*format == '%') {
format++;
int c,*_num;
switch(*format) {
case 'd':
while(isspace(c = getc(fp))) {}
_num = va_arg(list, int*);
unsigned int num = 0;
while (isdigit(c)) {
num = num * 10 + c - '0';
c = getc(fp);
}
*_num = num;
while(isspace(c = getc(fp))) {}
ungetc(c,fp);
break;
case 'c' :
cc = va_arg(list, char*);
c = getc(fp);
*cc =c;
while(isspace(c = getc(fp))) {}
ungetc(c,fp);
break;
}
}
else
format++;
}
va_end(list);

}
7 changes: 7 additions & 0 deletions my_fscanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#indef MY_FSCANF_H
#define MY_FSCANF_H
#include <stdio.h>

void my_fscanf(FILE *fp, const char *format, ...);

#endif /* !MY_SCANF_H */