با سلام
من در برنامه نویسی C مشکل دارم از دوستان خواهشمندم راهنمایی کنند.
صورت سوال را در لینک زیر گذاشتم.
از آقای بهنام که در برنامه قبلی کمکم کرده بودند خیلی تشکر می کنم.
مهمان عزیز شما قادر به مشاهده پیوندهای انجمن مانشت نمیباشید. جهت مشاهده پیوندها ثبت نام کنید.
این کد برنامه هست:
کد:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE* inFile;
FILE* tempFile;
int tagout;
int tag_, op1_, op2_, expected_;
char cmd_;
char line[32];
while(true){
inFile = fopen("in.txt", "r");
tempFile = fopen("temp.txt", "w+");
scanf("%d", &tagout);
while(fgets(line, sizeof(line), inFile) != NULL){
int tag = line[0] - '0';
if(tag != tagout){
fprintf(tempFile, "%s", line);
}
else if(tag == tagout){
char *subs;
subs = strtok(line, "\t"); //first split
tag_ = atoi(subs); //char* to int
subs = strtok(NULL, "\t"); //next splits
op1_ = atoi(subs);
subs = strtok(NULL, "\t");
op2_ = atoi(subs);
subs = strtok(NULL, "\t");
cmd_ = subs[0];
subs = strtok(NULL, "\t");
expected_ = atoi(subs);
printf("%d\t%d\t%d\t%c\t%d\n", tag_, op1_, op2_, cmd_, expected_);
}
}
fclose(inFile);
fclose(tempFile);
remove("in.txt");
rename("temp.txt", "in.txt");
}
system("pause");
return 0;
}
اینم فایل ورودی:
کد:
tag op1 op2 cmd expected
۱ ۱۲ ۱۰ + ۲۲
۰ ۱۴ ۶ - ۸
۳ ۴ ۲ * ۸
۲ ۵ ۹ + ۱۴
دقت شود که در فایل ورودی، اعداد و ... با tab از هم جدا شدهاند (فایل ورودی رو ضمیمه هم کردم). بعد از هر tagout که از کاربر گرفته میشود، سطر مربوطه از فایل حذف میشود (در خروجی نیز چاپ میشود).
[
attachment=20311]