Tuesday, 2 June 2015

Arithmetic operations on structures using functions

#include <stdio.h>
struct complex {
       int a;
       int b;
 }t1,t2;
void read(struct complex *);
void write(struct complex);
void add(struct complex,struct complex);
void sub(struct complex,struct complex);
void mul(struct complex,struct complex);
int main() {
    read(&t1);
    read(&t2);
    write(t1);
    write(t2);
    add(t1,t2); 
    sub(t1,t2);
    mul(t1,t2);
}
void read(struct complex *z) {
     printf("Enter the real part:");
     scanf("%d",&z->a);
     printf("Enter the imazinary part:");
     scanf("%d",&z->b);
}
void write(struct complex z) {
     printf("The complex number is :");
   if(z.b<0)
     printf("%d - i%d\n",z.a,(-z.b));
   else
       printf("%d +i%d\n",z.a,z.b);
}
void add(struct complex x,struct complex y) {
     struct complex z;
     z.a = t1.a + t2.a;
     z.b = t2.b + t2.b;
     printf("Sum of two complex numbers:");
     write(z);
}

void sub(struct complex x,struct complex y) {
     struct complex z;
     z.a = t1.a - t2.a;
     z.b = t2.b - t2.b;
     printf("Subtracting of two complex numbers:");
     write(z);
}
 void mul(struct complex x,struct complex y) {
     struct complex z;
     z.a = t1.a * t2.a - t1.b * t2.b;
     z.b = t1.a * t2.b + t1.b * t2.a;
printf("Multiplication of two complex numbers:");
     write(z);
}
 
I/O of  a program
 
Enter the real part:4
Enter the imazinary part:6
Enter the real part:4
Enter the imazinary part:7
The complex number is :4 +i6
The complex number is :4 +i7
Sum of two complex numbers:The complex number is :8 +i14
Subtracting of two complex numbers:The complex number is :0 +i0
Multiplication of two complex numbers:The complex number is :-26 +i52
 
 

No comments:

Post a Comment