C: Data structures - Applications resolved


1) Declaring a records and its use.
#include<stdio.h>
/* structures declared in principle be declared outside
any function, to be used throughout the program */
struct employee
{
	int code_employee;
	char* name;
	char* forename;
	struct
		{
		int year;
		char month;
		char day;
		}time_hiring;
	float salary_framing;
};
void main()
{
	employee x;
	x.code_employee=1021;
	x.name="Cartlon";
	x.forename="Johnson";
	x.time_hiring.year=2002;
	x.time_hiring.month=9;
	x.time_hiring.day=10;
	x.salary_framing=1250;
 
	printf("employee: %s %s\n",x.name,x.forename);
	printf("time hiring: %d/%d/%d\n",x.time_hiring.day, x.time_hiring.month, x.time_hiring.year);
	printf("salary framing: %.2f",x.salary_framing);
 
	getchar();
	int k;
	scanf("%d", &k);
}
2) write a C program that will highlight the differences between using a structure and a union.
a)Will use a record structure:
#include<stdio.h>
struct example
{
	long int a;
	char* b;
	char* c;
};
void main()
{
	example x;
	printf("The space occupied by a variable to the structure: %d\n",sizeof(x));
	x.a=10;
	x.b="ABCD";
	x.c="EFG";
	printf("%ld\n",x.a);
	printf("%s\n",x.b);
	printf("%s\n",x.c);
 
	getchar();
	int k;
	scanf("%d", &k);
}
b) will use a union type structure
#include<stdio.h>
union example 
{
	long int a;
	char* b;
	char* c;
};
void main()
{
	example x;
	printf("The space occupied by the variable to union: %d\n",sizeof(x));
	x.a=10;
	x.b="ABCD";
	x.c="EFG";
 
	printf("%ld\n",x.a);
	printf("%s\n",x.b);
	printf("%s\n",x.c);
 
	getchar();
	int k;
	scanf("%d", &k);
}
3) Make an application which perform processes with complex numbers, ie reading, display, mode, sum and product of two such numbers.
#include<stdio.h>
#include<math.h>
typedef struct{
float a;	//the part real 
float b;	//the part imaginary
}complex;	//structure name
//reading a no. complex
void read(complex* z,char s[]) //s retain the name complex number
{
	printf("Enter no. complex %s:\n",s);
	printf("\tthe part real : ");
	scanf("%f",&(*z).a);
	printf("\tthe part imaginary: ");
	scanf("%f",&z->b); //another way of writing
}
//displaying a no. complex
void display(complex z,char s[])
{
	printf("No. complex %s: ",s);
	if(z.b>=0)
	printf("%.2f+%.2fi\n",z.a,z.b);
	else
	printf("%.2f%.2fi\n",z.a,z.b);
}
//a module no. complex
float module(complex z)
{
	return sqrt(pow(z.a,2)+pow(z.b,2));
}
//conjugate of a complex number
complex conjugate(complex z)
{
	complex t;
	t.a=z.a;
	t.b=-z.b;
	return t;
}
//sum of two no. complex (variant with transfer by type result)
complex sum1(complex x,complex y)
{
	complex z;
	z.a=x.a+y.a;
	z.b=x.b+y.b;
return z;
}
//sum of two no. complex  (variant with transfer through line parameters)
void sum2(complex x,complex y,complex* z)
{
	z->a=x.a+y.a; //(*z).a=x.a+y.a;
	z->b=x.b+y.b;
} 
//product of two no. complex
complex product(complex x,complex y)
{
	complex z;
	z.a=x.a*y.a-x.b*y.b;
	z.b=x.a*y.b+x.b*y.a;
	return z;
}
//main function to run
void main()
{
	complex z1,z2;
	//reading two no. complex
	read(&z1,"z1");
	read(&z2,"z2");
	printf("\n");
	//display two no. complex
	display(z1,"z1");
	display(z2,"z2");
	printf("\n");
	//modules of two complex numbers
	printf("Modules of z1: %.2f\n",module(z1));
	printf("Modules of z2: %.2f\n",module(z2));
	printf("\n");
	//conjugates of two complex numbers
	display(conjugate(z1),"conjugate z1");
	display(conjugate(z2),"conjugate z2");
	//sum of two no. complex - version 1
	complex s1;
	s1=sum1(z1,z2);
	display(s1,"z1+z2");
	//sum of two no. complex - version 2
	complex s2;
	sum2(z1,z2,&s2);
	display(s2,"z1+z2");
	//product of two no. complex
	complex p;
	p=product(z1,z2);
	display(p,"z1*z2");
 
	getchar();
	int k;
	scanf("%d", &k);
}
4) Realize an application for working with rational numbers (numerical values ​​can be written as a fraction) as follows: read, display, summing the product of two numbers such numbers.
#include<stdio.h>
#include<math.h>
typedef struct
{
	int a,b;
	int gcd(int x,int y)
	{
		if(x==y) return x;
		else
			if(x>y) return gcd(x-y,y);
			else
			return gcd(x,y-x);
	}
	void irreducible()
	{
		if(b<0)
		{
			a=-a;
			b=-b;
		}
		if(!a)
			b=1;
		else
			if(abs(a)!=1 && abs(b)!=1)
			{
				int d=gcd(abs(a),abs(b));
				a=a/d;
				b=b/d;
			}
	}
}rational;
void read(rational* x, char c)
{
	printf("Enter rational number %c:\n",c);
	printf("\tthe numerator: ");
	scanf("%d",&x->a);
	int n;
	do
	{
		printf("\tthe denominator: ");
		scanf("%d",&n);
	}
	while(n==0);
	x->b=n;
	x->irreducible();
}
 
void display(rational x, char c[])
{
	printf("Rational number %s: %d/%d\n",c,x.a,x.b);
}
rational addition(rational x, rational y)
{
	rational r;
	r.a=x.a*y.b+x.b*y.a;
	r.b=x.b*y.b;
	r.irreducible();
	return r;
}
rational multiplication(rational x, rational y)
{
	rational r;
	r.a=x.a*y.a;
	r.b=x.b*y.b;
	r.irreducible();
	return r;
}
void main()
{
	rational x,y;
 
	read(&x,'x');
	read(&y,'y');
 
	display(x,"x");
	display(y,"y");
 
	rational s;
	s=addition(x,y);
	display(s,"x+y");
 
	rational p;
	p=multiplication(x,y);
	display(p,"x*y");
 
	getchar();
	int k;
	scanf("%d", &k);
}
5) Write a program to manage a park of vehicles. Information related to a car are seats, power (in horsepower), brand, color, year of manufacture machine.
a) Will read the data on the n automobile from park of vehicles. Will show only those vehicles which have 5 seats;
b) Ascending sort the cars after power - will show ordered data;
c) Write a function that displays all cars manufactured in a given year as a parameter.
#include<stdio.h>
#include<conio.h>
typedef struct					//declaring structure
{
	char mark[20];
	char model[20];
	char color[20];
	int year;
	struct					//defining structure
	{
		char nl;
		int put;
	}date;					//variable list structure
}automobile;						//name structure
typedef automobile cars[20];			//own data type
void read(cars a, int *n)			//enter data
{
	int i;
	printf("Enter no. automobile: ");
	scanf("%d",n);
	for(i=0; i<*n; i++)
	{
		printf("Enter dates about the automobile %d\n",i+1);
		printf("\tthe mark: ");
		scanf("%s",a[i].mark);
		printf("\tthe model: ");
		scanf("%s",a[i].model);
		printf("\tthe color: ");
		scanf("%s",a[i].color);
		printf("\tthe year: ");
		scanf("%d",&a[i].year);
		printf("\tnumber of seats: ");
		scanf("%d",&a[i].date.nl);
		printf("\tpower in horsepower: ");
		scanf("%d",&a[i].date.put);
	}
}
 
void display(cars a,int n)			//display date
{
	int i;
	 for (i=0;i<n;i++)
	{
		printf("automobile [%d]:\n",i+1);
		printf("\tthe mark: %s\n",a[i].mark);
		printf("\tthe model: %s\n",a[i].model);
		printf("\tthe color: %s\n",a[i].color);
		printf("\tyear fabr.: %d\n",a[i].year);
		printf("\tseats: %d\n",a[i].date.nl);
		printf("\tpower: %d\n\n",a[i].date.put);
	 }
}
void year(cars a,int n,int p)
{
	int i;
	for(i=0;i<n-1;i++)
	 {
		 if(a[i].year=p)			//attribution values ​​to the structure members
		{
			printf("automobile [%d]:\n",i+1);
			printf("\tthe mark: %s\n",a[i].mark);
			printf("\tthe model: %s\n",a[i].model);
			printf("\tthe color: %s\n",a[i].color);
			printf("\tthe year fabr.: %d\n",a[i].year);
			printf("\tseats: %d\n",a[i].date.nl);
			printf("\tpower: %d\n\n",a[i].date.put);
		 }
	}
}
void main()
{	
	cars a;
	int n,aux,i,j,p;
	read(a,&n);
	display(a,n);
	for(i=0;i<n;i++)
	{ 
		if(a[i].date.nl=5)			//attribution values ​​to the structure members
		{
			printf("automobile [%d]:\n",i+1);
			printf("\tthe mark: %s\n",a[i].mark);
			printf("\tthe model: %s\n",a[i].model);
			printf("\tthe color: %s\n",a[i].color);
			printf("\tthe year fabr.: %d\n",a[i].year);
			printf("\tseats: %d\n",a[i].date.nl);
			printf("\tpower: %d\n\n",a[i].date.put);
		}
	}
	for(i=0;i<n-1;i++)
		for(j=i+1;j<n;j++)
		{
			if(a[i].date.put>a[j].date.put)
             {
				aux=i;
				i=j;
				j=aux;
			}
		}
	display(a,n);
	printf("\n\tEnter the year after that will be displayed cars: ");
	scanf("%d",&p);
	year(a,n,p);
 
	getchar();
	int k;
	scanf("%d", &k);
 
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.