* Structure-06. arithmetic operation with four plural values

Source: Internet
Author: User
1/* 2 * Main. C 3 * F6-structure-06. the number of four arithmetic operations 4 * created on: August 26, 2014 5 * Author: in the boomkeeper 6 ******** section, ************* 7 */8 9 # include <stdio. h> 10 # include <math. h> 11 12 # define epsinon 0.1 13 14/* 15 * plural struct. C1 and C2 correspond to C1, C2 16 */17 struct complexnumber {18 float real; 19 float imaginary; 20} C1, C2; 21 22/* 23 * follow the "(A1 + b1i) operator (A2 + b2i) = The result is formatted in order to output two plural numbers 24 * Ch corresponding to "+" "-" "*" "/" four operators 25 */26 Void printcomplex (struct complexnumber C1, char CH, struct complexnumber C2) {27 28 If (c1.imaginary <0) {29 If (c2.imaginary <0) 30 printf ("(%. 1f %. 1fi) % C (%. 1f %. 1fi) = ", c1.real, c1.imaginary, CH, 31 c2.real, c2.imaginary); 32 If (c2.imaginary> 0) 33 printf (" (%. 1f %. 1fi) % C (%. 1f + %. 1fi) = ", c1.real, c1.imaginary, CH, 34 c2.real, c2.imaginary); 35} else if (c1.imaginary> 0) {36 IF (c2. I Maginary <0) 37 printf ("(%. 1f + %. 1fi) % C (%. 1f %. 1fi) = ", c1.real, c1.imaginary, CH, 38 c2.real, c2.imaginary); 39 if (c2.imaginary> 0) 40 printf (" (%. 1f + %. 1fi) % C (%. 1f + %. 1fi) = ", c1.real, c1.imaginary, CH, 41 c2.real, c2.imaginary ); 42} 43} 44 45/* 46 * output the sum, difference, product, and Quotient of two complex numbers 47 */48 void printresult (float re, float IM) {49 50 if (FABS (re) <epsinon) 51 Re = 0; 52 If (FABS (IM) <epsinon) 53 im = 0; 54 55 if (RE = 0 & im = 0) 56 printf ("0.0 \ n"); 57 else {58 If (re! = 0 & im> 0) 59 printf ("%. 1f + %. 1fi \ n", re, Im); 60 if (re! = 0 & im <0) 61 printf ("%. 1f %. 1fi \ n", re, Im); 62 if (re! = 0 & im = 0) 63 printf ("%. 1f \ n", RE); 64 if (RE = 0 & Im! = 0) 65 printf ("%. 1fi \ n ", Im); 66} 67} 68 69 int main (void) {70 71 float Re = 0, Im = 0; // temporary variable, calculate real and virtual parts 72 73 scanf ("% F", & c1.real, & c1.imaginary, & c2.real, & c2.imaginary ); 74 75 // and 76 printcomplex (C1, '+', C2); 77 Re = c1.real + c2.real; 78 im = c1.imaginary + c2.imaginary; 79 printresult (Re, Im ); 80 81 // difference 82 printcomplex (C1, '-', C2); 83 Re = c1.real-c2.real; 84 im = c1.imaginary-c2.imaginary; 85 printresult (Re, Im ); 86 87 // product 88 printcomplex (C1, '*', C2); 89 Re = c1.real * c2.real-c1.imaginary * c2.imaginary; 90 im = c1.real * c2.imaginary + c1.imaginary * c2.real; 91 printresult (Re, Im); 92 93 // vendor 94 printcomplex (C1, '/', C2); 95 Re = (c1.real * c2.real + c1.imaginary * c2.imaginary) 96/(c2.real * c2.real + c2.imaginary * c2.imaginary); 97 im = (c1.imaginary * c2.real-c1.real * c2.imaginary) 98/(c2.real * c2.real + c2.imaginary * numeric ); 99 printresult (Re, Im); 100 101 return 0; 102}

 

1. The question that makes me grasp the most is the output of the symbol of the imaginary part of the plural number. The arithmetic operation on the four digits of the plural number is still easy to calculate. This is the plus or minus sign, and it will be messy when I am not careful;

2. Compare floating-point numbers with 0. For this question, because the output result only needs to be accurate to 0.1, <= 0.1 &> =-0.1 is regarded as 0. At first I referred to other articles. According to 0.00001, the output results are always irrelevant.

 

Question link:

Http://pat.zju.edu.cn/contests/basic-programming/%E7%BB%93%E6%9E%84-06

Refer:

Http://blog.csdn.net/rabbit8848/article/details/30284651

Http://blog.sina.com.cn/s/blog_64a475370101119h.html

* Structure-06. arithmetic operation with four plural values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.