标签:
Write a C program to add two fractions and display the result fraction. Your program will prompt the user to input fraction 1 and fraction 2. The numerator and denominator of each fraction are input separately by space. See the example output below:
Enter fraction 1(numerator denominator): 1 2
Enter fraction 2(numerator denominator): 2 5
Result: 9/10
//main.c 1.#include <stdio.h> 2.#include "fract.h" 3. 4. 5. 6.int main() { 7. int num1, deno1, num2, deno2; 8. printf("\nEnter fraction 1: numerator denominator:"); 9. scanf("%d%d", &num1, &deno1); 10. printf("\nEnter fraction 2: numerator denominator:"); 11. scanf("%d%d", &num2, &deno2); 12. 13. Fract f1 = {num1, deno1}; 14. Fract f2 = {num2, deno2}; 15. Fract result = sum(f1, f2); 16. printf("\nResult=%d/%d", result.num, result.deno); 17. 18. return 0; 19.} 20.
1 //fract.h 2 typedef struct node { 3 int num; 4 int deno; 5 }Fract; 6 Fract sum(Fract number1, Fract number2) { 7 number1.num *= number2.deno; 8 number2.num *= number1.deno; 9 number1.deno *= number2.deno; 10 number2.deno *= number1.deno; 11 Fract sum; 12 sum.deno = number1.deno; 13 sum.num = number1.num + number2.num; 14 return sum; 15 }
//标程 1.#ifndef Header_h 2.#define Header_h 3. 4.typedef struct { 5. int num; 6. int deno; 7.} Fract; 8. 9.Fract sum(Fract f1, Fract f2) { 10. Fract result = {(f1.num * f2.deno) + (f2.num * f1.deno), f1.deno * f2.deno}; 11. return result; 12.} 13. 14. 15.#endif /* Header_h */
函数的名字不能够直接进入赋值,必须要定义一个和函数一样的类型,返回函数值才能够,函数名称是用来调用的,不能够直接操作!
标签:
原文地址:http://www.cnblogs.com/iamxiaoyubei/p/5088024.html