write a C program using data files. the input data Numbers.txt contains several values for the base and power on alternating lines

  • برمجة
  • برمجة سي c

write a C program using data files. the input data Numbers.txt contains several values for the base and power on alternating lines.

Each time the program reads two numbers, computes x^n and stores the result in the output file Result.txt

below are examples of the input and output files:

Numbers.txt:

-------------

1.2345

5

12.345

3

2.0

16

 

Results.txt:

-------------

2.86=5^1.23

1881.36=3^12.34

65536.00=16^2.00

الأجوبة

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
//read any text file from currect directory
char const* const fileName = "Numbers.txt";

FILE* file = fopen(fileName, "r"); 
FILE* fileToWrite = fopen(fileName, "w"); 

if(!file){
printf("\n Unable to open : %s ", fileName);
return -1;
}

char line[100];
char num1[100];
char num2[100];
int counter=0;
while (fgets(line, sizeof(line), file)) {

if(counter%2==0)
num1=line;
else { 
    //here we make the power operator then write in Results.txt
    num2=line;
    fprintf (fileToWrite, "%d^%d=%d\n",num1,num2,pow(atoi(num1),atoi(num2)));
}
counter++;
}
fclose(fileToWrite);
fclose(file);
  
    return 0;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...