Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

exercise1-2:

#include <stdio.h>

int perrin(int n)
{
if(n==0){ return 3;}
if(n==1){return 0;}
if(n==2){return 2;}

return ( perrin(n-2)+ perrin(n-3) );

int ex2(int n)
{int i, s=0;
for(i=1;i<=n;i++)
{
s+=i;

}
return s;

int main()
{
printf("%d",ex2(4));
return 0;
}

exercise3:

#include <stdio.h>

typedef struct article{

int ID;
char name[20];
float price;

}article;

int count(int ID)


{
FILE *f;
article c;
int k;
f=fopen("DATA.txt","r");
fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);
while(!feof(f))
{
if(ID==c.ID){k++;}
fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);
}

return k;

article FIND(int ID)


{
FILE *f;
article c, max;

f=fopen("DATA.txt","r");

fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);

while(!feof(f))
{

if(ID==c.ID){ break; }
fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);
}

fclose(f);
f=fopen("DATA.txt","r");
max.price=c.price;
fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);
while(!feof(f))
{

if(c.price>max.price && ID==c.ID){max.price=c.price;}


fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);

}
fclose(f);
return max;

void delet(int ID)


{
FILE *f, *t;
article c;
f=fopen("DATA.txt","r");
t=fopen("temp.txt","w");

fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);

while(!feof(f))
{

if(c.ID!=ID){ fprintf(t,"%d %s %f",c.ID, c.name, c.price ); }


if(c.ID==ID && c.price==FIND(ID)){ fprintf(t,"%d %s %f",c.ID, c.name, c.price ); }

}fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);
fclose(f);
fclose(t);

f=fopen("DATA.txt","w");
t=fopen("temp.txt","r");
fscanf(t,"%d%s%f",&c.ID,c.name,&c.price);
while(!feof(t))
{
fprintf(f,"%d %s %f",c.ID, c.name, c.price );
fscanf(t,"%d%s%f",&c.ID,c.name,&c.price);

}
fclose(f);
fclose(t);
}

void clean()
{
FILE *f;
article c;

f=fopen("DATA.txt","r");

fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);

while(!feof(f))
{
delet(c.ID);
fscanf(f,"%d%s%f",&c.ID,c.name,&c.price);
}

int main()
{

return 0;
}

You might also like