Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <stdio.h>
- void fn_add (float x1, float x2, float y1, float y2, float *res_x, float *res_y);
- void fn_sub (float x1, float x2, float y1, float y2, float *res_x, float *res_y);
- void fn_mul (float x1, float x2, float y1, float y2, float *res_x, float *res_y);
- void fn_div (float x1, float x2, float y1, float y2, float *res_x, float *res_y);
- main (void)
- {
- struct compl {
- float x;
- float y;
- };
- struct compl z1, z2, res;
- float *zx, *zy;
- int op;
- zx=&res.x;
- zy=&res.y;
- while (1) {
- op=5; // quit by default
- z1.x=z1.y=z2.x=z2.y=0;
- printf ("1. Add\n2. Substract\n3. Multiplication\n4. Division\n5. Quit\nPlese select operation: ");
- scanf ("%d", &op);
- if ((op>=1)&&(op<=4)) {
- printf ("\nPlease input real part of first complex value: ");
- scanf ("%f", &z1.x);
- printf ("\nPlease input imaginary part of first complex value: ");
- scanf ("%f", &z1.y);
- printf ("\nPlease input real part of second complex value: ");
- scanf ("%f", &z2.x);
- printf ("\nPlease input imaginary part of second complex value: ");
- scanf ("%f", &z2.y);
- if ((z2.x==0)&&(z2.y==0)&&(op==4)) {
- printf ("\nDivision by zero is impossible\n\n");
- } else {
- switch (op) {
- case 1: fn_add (z1.x, z2.x, z1.y, z2.y, zx, zy); break;
- case 2: fn_sub (z1.x, z2.x, z1.y, z2.y, zx, zy); break;
- case 3: fn_mul (z1.x, z2.x, z1.y, z2.y, zx, zy); break;
- case 4: fn_div (z1.x, z2.x, z1.y, z2.y, zx, zy); break;
- }
- printf ("\nResult z=(%2.2f, %2.2f)\n\n", res.x, res.y);
- }
- } else if (op==5) {
- break;
- }
- }
- }
- void fn_add (float x1, float x2, float y1, float y2, float *res_x, float *res_y)
- {
- *res_x=x1+x2;
- *res_y=y1+y2;
- }
- void fn_sub (float x1, float x2, float y1, float y2, float *res_x, float *res_y)
- {
- *res_x=x1-x2;
- *res_y=y1-y2;
- }
- void fn_mul (float x1, float x2, float y1, float y2, float *res_x, float *res_y)
- {
- *res_x=x1*x2-y1*y2;
- *res_y=x2*y1+x1*y2;
- }
- void fn_div (float x1, float x2, float y1, float y2, float *res_x, float *res_y)
- {
- *res_x=(x1*x2+y1*y2)/(x2*x2+y2*y2);
- *res_y=(y1*x2-x1*y2)/(x2*x2+y2*y2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement