static int gcd(int num1,int num2) { if(num1< num2) 返回gcd( num2,num1); else if((num1%num2)== 0) 返回num2; else 返回gcd(num2,num1%num2); } static void normalize(分数* fr) { int factor; if(fr-> denominator == 0)/ *函数无效* / { fr = NULL; / *将其设置为NULL并返回* / printf(设置fr = NULL \ n); if(fr!= NULL ) printf(非空); 其他 printf(" null"); 返回; } / *从符号变量的分子和分母处取符号 * / fr-> sign * = signOf(fr-> numerator); fr-> sign * = signOf(fr-> denominator); / *绝对分子和分母* / fr->分子* = signOf(fr->分子); fr->分母* = signOf(fr->分母); if(fr->分子== 0) {/ *如果分子为0 ,简化为0/1 * / fr->分母= 1; fr-> sign = 1; } 其他 {/ *找到分子的gcd和 分母,用gcd除以它们* / factor = gcd(fr->分子,fr->分母); fr-> numerator / = factor; fr-> denominator / = factor; } } / *内部公共函数====== ===================================== * / / *条目积分================================================= = ====== * / 分数newFraction(int num,int den) { 分数fr; 分数* frp; fr.numerator = num; fr.denominator = den; fr.sign = 1; frp =& fr; normalize(frp); if(frp!= NULL) printf(" Not null"); else printf(" null"); 返回fr; } 无效printFraction(分数fr) { if(& fr == NULL) printf(无效分数); if(fr.sign == -1) printf(" - && ;); printf("%i /%i",fr.numerator,fr.denominator); } / *公共变量=========================================== ======= == * / 请注意,我没有更改您的代码。 - -ed- em**********@noos.fr [在回答我之前删除YOURBRA] C语言常见问题解答: http://www.eskimo.com/~scs/C-faq/top.html C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp FAQ de fclc: http://www.isty-info.uvsq.fr/~rumeau/fclc/<snipped>First of all, I highly recommend that you change your code organization. It''sa very bad idea to have code included in a .h. I suggest a very commonapproach as follow:/* main.c */#include "Fraction.h"#include <stdio.h>/* macros ================================================== ============ *//* constants ================================================== ========= *//* types ================================================== ============= *//* structures ================================================== ======== *//* private variables ================================================== = *//* private functions ================================================== = */static void Case1c (void){printFraction (newFraction (0, 4));printf ("\n");printFraction (newFraction (4, 0));printf ("\n");printFraction (newFraction (0, 0));printf ("\n");}/* entry point ================================================== ======= */int main (void){printf ("Case 1c:\n");Case1c ();return 0;}/* public variables ================================================== == *//* fraction.h */#ifndef H_FRACTION#define H_FRACTION/* macros ================================================== ============ *//* constants ================================================== ========= *//* types ================================================== ============= *//* structures ================================================== ======== */typedef struct{int sign;int numerator;int denominator;}Fraction;/* internal public functions =========================================== *//* entry points ================================================== ====== */Fraction newFraction (int num, int den);void printFraction (Fraction fr);/* public variables ================================================== == */#endif /* guard *//* fraction.c */#include <stdio.h>#include "fraction.h"/* macros ================================================== ============ *//* constants ================================================== ========= *//* types ================================================== ============= *//* structures ================================================== ======== *//* private variables ================================================== = *//* private functions ================================================== = */static int signOf (int number){if (number < 0)return -1;elsereturn 1;}static int gcd (int num1, int num2){if (num1 < num2)return gcd (num2, num1);else if ((num1 % num2) == 0)return num2;elsereturn gcd (num2, num1 % num2);}static void normalize (Fraction * fr){int factor;if (fr->denominator == 0) /* Invalid function */{fr = NULL; /* set it to NULL and return */printf ("Set fr = NULL\n");if (fr != NULL)printf ("Not null");elseprintf ("null");return;}/* Take the signs form the numerator and denominator to the sign variable*/fr->sign *= signOf (fr->numerator);fr->sign *= signOf (fr->denominator);/* Absolute the numerator and denominator */fr->numerator *= signOf (fr->numerator);fr->denominator *= signOf (fr->denominator);if (fr->numerator == 0){ /* If the numerator is 0, simplify it to 0/1*/fr->denominator = 1;fr->sign = 1;}else{ /* Find the gcd of the numerator anddenominator, divide them by the gcd */factor = gcd (fr->numerator, fr->denominator);fr->numerator /= factor;fr->denominator /= factor;}}/* internal public functions =========================================== *//* entry points ================================================== ====== */Fraction newFraction (int num, int den){Fraction fr;Fraction *frp;fr.numerator = num;fr.denominator = den;fr.sign = 1;frp = &fr;normalize (frp);if (frp != NULL)printf ("Not null");elseprintf ("null");return fr;}void printFraction (Fraction fr){if (&fr == NULL)printf ("Invalid fraction");if (fr.sign == -1)printf ("-");printf ("%i/%i", fr.numerator, fr.denominator);}/* public variables ================================================== == */Note that I have no changed your code.---ed- em**********@noos.fr [remove YOURBRA before answering me]The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.htmlC-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cppFAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ 这篇关于返回后是否更改了NULL指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 05:24
查看更多