📄 programs.shar
字号:
XXiseven.o: iseven.cX $(CC) $(CFLAGS) -c iseven.cXXc2ftable.o: c2ftable.cX $(CC) $(CFLAGS) -c c2ftable.cXXfact.o: fact.cX $(CC) $(CFLAGS) -c fact.cXXcombine.o: combine.cX $(CC) $(CFLAGS) -c combine.cXXcalendar.o: calendar.cX $(CC) $(CFLAGS) -c calendar.cXXXgameloop: gameloop.oX $(CC) $(CFLAGS) -o gameloop gameloop.oXXiseven: iseven.oX $(CC) $(CFLAGS) -o iseven iseven.oXXc2ftable: c2ftable.oX $(CC) $(CFLAGS) -o c2ftable c2ftable.oXXfact: fact.oX $(CC) $(CFLAGS) -o fact fact.oXXcombine: combine.oX $(CC) $(CFLAGS) -o combine combine.oXXcalendar: calendar.oX $(CC) $(CFLAGS) -o calendar calendar.oEND_OF_FILEif test 1604 -ne `wc -c <'programs/05-Functions/Makefile'`; then echo shar: \"'programs/05-Functions/Makefile'\" unpacked with wrong size!fi# end of 'programs/05-Functions/Makefile'fiif test -f 'programs/05-Functions/c2ftable.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/05-Functions/c2ftable.c'\"elseecho shar: Extracting \"'programs/05-Functions/c2ftable.c'\" \(1023 characters\)sed "s/^X//" >'programs/05-Functions/c2ftable.c' <<'END_OF_FILE'X/*X * File: c2ftable.cX * ----------------X * This program illustrates the use of functions by generatingX * a table of Celsius to Fahrenheit conversions.X */XX#include <stdio.h>X#include "genlib.h"XX/*X * ConstantsX * ---------X * LowerLimit -- Starting value for temperature tableX * UpperLimit -- Final value for temperature tableX * StepSize -- Step size between table entriesX */XX#define LowerLimit 0X#define UpperLimit 100X#define StepSize 5XX/* Function prototypes */XXdouble CelsiusToFahrenheit(double c);XX/* Main program */XXmain()X{X int c;XX printf("Celsius to Fahrenheit table.\n");X printf(" C F\n");X for (c = LowerLimit; c <= UpperLimit; c += StepSize) {X printf("%3d %3g\n", c, CelsiusToFahrenheit(c));X }X}XX/*X * Function: CelsiusToFahrenheitX * Usage: f = CelsiusToFahrenheit(c);X * ----------------------------------X * Returns the Fahrenheit equivalent of the CelsiusX * temperature c.X */XXdouble CelsiusToFahrenheit(double c)X{X return (9.0 / 5.0 * c + 32);X}END_OF_FILEif test 1023 -ne `wc -c <'programs/05-Functions/c2ftable.c'`; then echo shar: \"'programs/05-Functions/c2ftable.c'\" unpacked with wrong size!fi# end of 'programs/05-Functions/c2ftable.c'fiif test -f 'programs/05-Functions/calendar.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/05-Functions/calendar.c'\"elseecho shar: Extracting \"'programs/05-Functions/calendar.c'\" \(5334 characters\)sed "s/^X//" >'programs/05-Functions/calendar.c' <<'END_OF_FILE'X/*X * File: calendar.cX * ----------------X * This program is used to generate a calendar for a yearX * entered by the user.X */XX#include <stdio.h>X#include "genlib.h"X#include "simpio.h"XX/*X * Constants:X * ----------X * Days of the week are represented by the integers 0-6.X * Months of the year are identified by the integers 1-12;X * because this numeric representation for months is inX * common use, no special constants are defined.X */XX#define Sunday 0X#define Monday 1X#define Tuesday 2X#define Wednesday 3X#define Thursday 4X#define Friday 5X#define Saturday 6XX/* Function prototypes */XXvoid GiveInstructions(void);Xint GetYearFromUser(void);Xvoid PrintCalendar(int year);Xvoid PrintCalendarMonth(int month, int year);Xvoid IndentFirstLine(int weekday);Xint MonthDays(int month, int year);Xint FirstDayOfMonth(int month, int year);Xstring MonthName(int month);Xbool IsLeapYear(int year);XX/* Main program */XXmain()X{X int year;XX GiveInstructions();X year = GetYearFromUser();X PrintCalendar(year);X}XX/*X * Function: GiveInstructionsX * Usage: GiveInstructions();X * --------------------------X * This procedure prints out instructions to the user.X */XXvoid GiveInstructions(void)X{X printf("This program displays a calendar for a full\n");X printf("year. The year must not be before 1900.\n");X}XX/*X * Function: GetYearFromUserX * Usage: year = GetYearFromUser();X * --------------------------------X * This function reads in a year from the user and returnsX * that value. If the user enters a year before 1900, theX * function gives the user another chance.X */XXint GetYearFromUser(void)X{X int year;XX while (TRUE) {X printf("Which year? ");X year = GetInteger();X if (year >= 1900) return (year);X printf("The year must be at least 1900.\n");X }X}XX/*X * Function: PrintCalendarX * Usage: PrintCalendar(year);X * ---------------------------X * This procedure prints a calendar for an entire year.X */XXvoid PrintCalendar(int year)X{X int month;XX for (month = 1; month <= 12; month++) {X PrintCalendarMonth(month, year);X printf("\n");X }X}XX/*X * Function: PrintCalendarMonthX * Usage: PrintCalendarMonth(month, year);X * ---------------------------------------X * This procedure prints a calendar for the given monthX * and year.X */XXvoid PrintCalendarMonth(int month, int year)X{X int weekday, nDays, day;XX printf(" %s %d\n", MonthName(month), year);X printf(" Su Mo Tu We Th Fr Sa\n");X nDays = MonthDays(month, year);X weekday = FirstDayOfMonth(month, year);X IndentFirstLine(weekday);X for (day = 1; day <= nDays; day++) {X printf(" %2d", day);X if (weekday == Saturday) printf("\n");X weekday = (weekday + 1) % 7;X }X if (weekday != Sunday) printf("\n");X}XX/*X * Function: IndentFirstLineX * Usage: IndentFirstLine(weekday);X * --------------------------------X * This procedure indents the first line of the calendarX * by printing enough blank spaces to get to the positionX * on the line corresponding to weekday.X */XXvoid IndentFirstLine(int weekday)X{X int i;XX for (i = 0; i < weekday; i++) {X printf(" ");X }X}XX/*X * Function: MonthDaysX * Usage: ndays = MonthDays(month, year);X * --------------------------------------X * MonthDays returns the number of days in the indicatedX * month and year. The year is required to handle leap years.X */XXint MonthDays(int month, int year)X{X switch (month) {X case 2:X if (IsLeapYear(year)) return (29);X return (28);X case 4: case 6: case 9: case 11:X return (30);X default:X return (31);X }X}XX/*X * Function: FirstDayOfMonthX * Usage: weekday = FirstDayOfMonth(month, year);X * ----------------------------------------------X * This function returns the day of the week on which theX * indicated month begins. This program simply countsX * forward from January 1, 1900, which was a Monday.X */XXint FirstDayOfMonth(int month, int year)X{X int weekday, i;XX weekday = Monday;X for (i = 1900; i < year; i++) {X weekday = (weekday + 365) % 7;X if (IsLeapYear(i)) weekday = (weekday + 1) % 7;X }X for (i = 1; i < month; i++) {X weekday = (weekday + MonthDays(i, year)) % 7;X }X return (weekday);X}XX/*X * Function: MonthNameX * Usage: name = MonthName(month);X * -------------------------------X * MonthName converts a numeric month in the range 1-12X * into the string name for that month.X */XXstring MonthName(int month)X{X switch (month) {X case 1: return ("January");X case 2: return ("February");X case 3: return ("March");X case 4: return ("April");X case 5: return ("May");X case 6: return ("June");X case 7: return ("July");X case 8: return ("August");X case 9: return ("September");X case 10: return ("October");X case 11: return ("November");X case 12: return ("December");X default: return ("Illegal month");X }X}XX/*X * Function: IsLeapYearX * Usage: if (IsLeapYear(year)) . . .X * ----------------------------------X * This function returns TRUE if year is a leap year.X */XXbool IsLeapYear(int year)X{X return ( ((year % 4 == 0) && (year % 100 != 0))X || (year % 400 == 0) );X}END_OF_FILEif test 5334 -ne `wc -c <'programs/05-Functions/calendar.c'`; then echo shar: \"'programs/05-Functions/calendar.c'\" unpacked with wrong size!fi# end of 'programs/05-Functions/calendar.c'fiif test -f 'programs/05-Functions/combine.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/05-Functions/combine.c'\"elseecho shar: Extracting \"'programs/05-Functions/combine.c'\" \(1523 characters\)sed "s/^X//" >'programs/05-Functions/combine.c' <<'END_OF_FILE'X/*X * File: combine.cX * ---------------X * This program tests a function to compute the mathematicalX * combination function Combinations(n, k), which gives theX * number of ways to choose a subset of k objects from a setX * of n distinct objects.X */XX#include <stdio.h>X#include "genlib.h"X#include "simpio.h"XX/* Function prototypes */XXint Combinations(int n, int k);Xint Factorial(int n);XX/* Main program */XXmain()X{X int n, k;XX printf("Enter number of objects in the set (n)? ");X n = GetInteger();X printf("Enter number to be chosen (k)? ");X k = GetInteger();X printf("C(%d, %d) = %d\n", n, k, Combinations(n, k));X}XX/*X * Function: CombinationsX * Usage: ways = Combinations(n, k);X * ---------------------------------X * Implements the Combinations function, which returns the numberX * of distinct ways of choosing k objects from a set of n objects.X * In mathematics, this function is often written as C(n,k), but aX * function called C is not very self-descriptive, particularly inX * a language which has precisely the same name.X */XXint Combinations(int n, int k)X{X return (Factorial(n) / (Factorial(k) * Factorial(n - k)));X}XX/*X * Function: FactorialX * Usage: f = Factorial(n);X * ------------------------X * Returns the factorial of the argument n, where factorialX * is defined as the product of all integers from 1 up to n.X */XXint Factorial(int n)X{X int product, i;XX product = 1;X for (i = 1; i <= n; i++) {X product *= i;X }X return (product);X}END_OF_FILEif test 1523 -ne `wc -c <'programs/05-Functions/combine.c'`; then echo shar: \"'programs/05-Functions/combine.c'\" unpacked with wrong size!fi# end of 'programs/05-Functions/combine.c'fiif test -f 'programs/05-Functions/fact.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/05-Functions/fact.c'\"elseecho shar: Extracting \"'programs/05-Functions/fact.c'\" \(970 characters\)sed "s/^X//" >'programs/05-Functions/fact.c' <<'END_OF_FILE'X/*X * File: fact.cX * ------------X * This program includes the Factorial function and a testX * program that prints the factorials of the numbers betweenX * the limits LowerLimit and UpperLimit, inclusive.X */XX#include <stdio.h>X#include "genlib.h"XX/*X * ConstantsX * ---------X * LowerLimit -- Starting value for factorial tableX * UpperLimit -- Final value for factorial tableX */XX#define LowerLimit 0X#define UpperLimit 7XX/* Function prototypes */XXint Factorial(int n);XX/* Main program */XXmain()X{X int i;XX for (i = LowerLimit; i <= UpperLimit; i++) {X printf("%d! = %5d\n", i, Factorial(i));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -