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
17 changes: 9 additions & 8 deletions calc.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@ int main(){
FILE *fp = NULL;
int operand1, operand2;
char operator = ' ';
int result, line = 0;
double result, line = 0;

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

for(int i=0; i<line; i++) {
fscanf(fp, "%d %c %d",&operand1, &operator, &operand2);
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);
result = add(operand1, operand2);
break;
case '-':
result = minus(operand1, operator);
result = minus(operand1, operand2);
break;
case '*':
result = mul(operand1, operator);
result = mul(operand1, operand2);
break;
case '/':
result = div(operand1, operator);
break;
}
printf("%d %c %d = %d\n",
printf("%d %c %d = %f\n",
operand1, operator, operand2, result);
}
}
Expand Down
Binary file added lab_14.tar
Binary file not shown.
52 changes: 52 additions & 0 deletions my_fscanf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "my_fscanf.h"
#include <stdarg.h>
#include <ctype.h>

void my_fscanf(FILE* fp, const char* format, ...){
va_list list; //가변 인자 목록 포인터
int dtemp=0;
char temp;
char ctemp= ' ';
va_start(list, format);

while(*format){
if(*format == '%'){
format++;
switch(*format){
case 'd':
while(isspace(temp)){
//empty
}
while(isdigit(temp)){
dtemp = temp;
temp = getc(fp);
}
dtemp = va_arg(list, int);
dtemp = 0;
format++;
break;

case 'c':
while(isspace(temp)){
//empty
}
while(isgraph(temp)){
ctemp =temp;
temp = getc(fp);
}
ctemp = va_arg(list, char);
format++;
break;
default break;
}
}
else if(isspace(*format)){
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 @@
#ifdef MY_FSCANF_H
#define MY_FSCANF_H
#include <stdio.h>

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

#endif /* !MY_FSCANF_H */
10 changes: 5 additions & 5 deletions operators.c
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "operators.h"

int add(int op1, int op2) {
double add(double op1, double op2) {
return op1+op2;
}
int minus(int op1, int op2) {
double minus(double op1, double op2) {
return op1-op2;
}
int mul(int op1, int op2) {
double mul(double op1, double op2) {
return op1*op2;
}

int div(int op1, int op2) {
return op1%op2;
double div(double op1, double op2) {
return op1/op2;
}

8 changes: 4 additions & 4 deletions operators.h
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int add(int op1, int op2);
int mul(int op1, int op2);
int div(int op1, int op2);
int minus(int op1, int op2);
double add(double op1, double op2);
double mul(double op1, double op2);
double div(double op1, double op2);
double minus(double op1, double op2);