Nothing Special   »   [go: up one dir, main page]

C PRG Examples

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

/* * math_ops.c * This program makes use of the various arithmetic operators. */ #include <stdio.

h> int main () { int a, b; float x, y; /* Read two integer numbers. */ printf ("Please enter two integer numbers: "); scanf ("%d %d", &a, &b); /* Perform some calculations. */ printf ("%d + %d = %d\n", a, b, a printf ("%d - %d = %d\n", a, b, a printf ("%d * %d = %d\n", a, b, a printf ("%d / %d = %d\n", a, b, a printf ("%d mod %d = %d\n", a, b, + * / a b); b); b); b); % b);

/* Read two real numbers. */ printf ("Please enter two real numbers: "); scanf ("%f %f", &x, &y); /* Perform some calculations. */ printf ("%g + %g = %g\n", x, y, x printf ("%g - %g = %g\n", x, y, x printf ("%g * %g = %g\n", x, y, x printf ("%g / %g = %g\n", x, y, x return (0); } + * / y); y); y); y);

/* * sign_pair.c * This program read an integer number and prints its sign and its parity. */ #include <stdio.h> int main () { int n; int sign; int is_even; /* Read the number. */ printf ("Please enter a number: "); scanf ("%d", &n); /* Find the sign. */ if (n < 0) { sign = -1; /* Find the parity (check whether 2 divides n with no remainder). */ if (n % 2 == 0) is_even = 1; else is_even = 0; } else if (n > 0) { sign = 1; /* Find the parity - alternative syntax. */ is_even = (n % 2) ? 0 : 1; } else /* n equals 0 */ { sign = 0; is_even = 1; } /* Print the result. */ printf ("sign(%d) = %d, ", n, sign); if (is_even) printf ("%d is even.\n", n);

else printf ("%d is odd.\n", n); return (0); } /* * power.c * This program reads two integer numbers and raises the first by the power * of the second. */ #include <stdio.h> int main () { int base; int power; int res; int is_neg_power = 0; int i; /* Read the two numbers. */ printf ("Please enter two integers: "); scanf ("%d %d", &base, &power); /* Check the case of a negative power. */ if (power < 0) { is_neg_power = 1; power = -power; } /* Compute the result. */ res = 1; for (i = 0; i < power; i++) res *= base; /* Print the result. */ printf ("The result is: "); if (! is_neg_power) printf ("%d\n", res); else printf ("%g\n", 1 / (double)res); return (0); }

/* * prime.c * This program read and integer number and checks whether it is prime. */ #include <stdio.h> int main () { int n; int is_prime = 1; int i; /* Read the number. */ printf ("Please enter a positive integer: "); scanf ("%d", &n); /* Check the input. */ if (n <= 0) { printf ("Illegal input!\n"); return (1); } /* Check if any of the numbers 2, ... , n-1 divide it. */ for (i = 2; i < n; i++) { if (n % i == 0) { is_prime = 0; break; } } /* Print the result. */ if (is_prime) printf ("%d is a prime.\n", n); else printf ("%d is not a prime.\n", n); return (0); }

/* * dist.c * This program read two points and computes the distance between them. */ #include <stdio.h> #include <math.h> int main () { double x1, y1; point. */ double x2, y2; point. */ double dist;

/* The coordinates of the first /* The coordinates of the second

/* Read the two points. */ printf ("Please enter the first point: "); scanf ("%lf %lf", &x1, &y1); printf ("Please enter the second point: "); scanf ("%lf %lf", &x2, &y2); /* Compute the distance between these points: * ________________________ * / 2 2 * dist = \/ (x2 - x1) + (y2 - y1) */ dist = sqrt ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); printf ("dist((%g,%g),(%g,%g)) = %g\n", x1, y1, x2, y2, dist); return (0); }

/* * lat2dec.c * This program reads an "alphabetic number" (in base-26), converts it * to a numerical value and prints it as a decimal number. */ #include <stdio.h> int main () { const int "digits":

base = 26;

/* Our alphabetic base has 26 {A, B, ... , Z}. */ /* The current "digit" /* The current numerical value.

int c; (character). */ unsigned int n = 0; */

printf ("Please enter an alphabetic number: "); /* Read characters until reaching while ((c = getchar()) != '\n') { /* Make sure that the character "digit". */ if (! (c >= 'A' && c <= 'Z')) { /* Print an error message and printf ("\n Illegal digit: %c return (1); } the end of the line. */ represents an alphabetic

exit. */ -- Aborting.\n", c);

/* The digit c is in the range 'A' -- 'Z', so 0 <= (c - 'A') <= 25. * We multiply the current value by the base, and add to it (c - 'A'). */ n = n*base + (c - 'A'); } /* Print the result. */ printf ("The decimal value is %d.\n", n); return (0); }

/* * quad_eq.c * This program reads the coefficients of a quadratic equation and solves * the equation. */ #include <stdio.h> #include <math.h> /* Prototypes: */ int solve_quad (double a, double b, double c, double *x1, double *x2); int main () { double a, b, c; double x1, x2; int n_sols;

/* The equation coefficients. */ /* Its solutions. */ /* The number of real solutions. */

/* Read the coeffcients. */ printf ("Please enter the three coefficients of the quadratic equation: "); scanf ("%lf %lf %lf", &a, &b, &c); /* Solve the equation. */ n_sols = solve_quad (a, b, c, &x1, &x2); /* Print the solutions. */ switch (n_sols) { case 0: printf ("The equation has no real solutions.\n"); break; case 1: printf ("The equation has a single solution: (x = %g).\n", x1); break; case 2: printf ("The equation has two solutions: (x1 = %g) (x2 = %g).\n", x1, x2); break;

default: printf ("You must be kidding!\n"); return (1); } return (0); } /* ----------------------------------------------------------------------* Function: solve_quad * Purpose : Solve the quadratic equation: (a*x^2 + bx + c = 0). * Input : a, b, c - The equation coefficients. * Output : x1, x2 - The solutions. * Returns : The number of real solutions to the equation (0, 1 or 2). */ int solve_quad (double a, double b, double c, double *x1, double *x2) { double disc; double sqrt_disc; /* Check if this is really a linear equation. */ if (a == 0) { /* The equation is: (b*x + c = 0). */ if (b == 0) return (0); *x1 = -c / b; return (1); } /* Compute the discriminant and act according to its sign. */ disc = b*b - 4*a*c; if (disc < 0) { return (0); } else if (disc == 0) { *x1 = -b / (2*a); return (1);

} /* -b +/- sqrt(b^2 - 4ac) * Use the formula: x1,2 = -----------------------* 2a */ sqrt_disc = sqrt (disc); *x1 = (-b + sqrt_disc) / (2*a); *x2 = (-b - sqrt_disc) / (2*a); return (2); } /* * bubble.c * This program reads a sequence of intergers, terminated by (1), and sorts * it using the bubble-sort algorithm. */ #include <stdio.h> #define MAX_SIZE 100 /* Prototypes: */ void bubble_sort (int *arr, int n); int main () { const int int int int int

stop_code = -1; array[MAX_SIZE]; n_numbers = 0; curr; i;

/* Read the input numbers. */ while (n_numbers < MAX_SIZE) { /* Read the next number. */ printf ("Please enter the next number (or -1 to stop): "); scanf ("%d", &curr); if (curr == stop_code) break; /* Store it. */

array[n_numbers] = curr; n_numbers++; } /* Sort the numbers and print them. */ bubble_sort (array, n_numbers); for (i = 0; i < n_numbers; i++) printf ("%d ", array[i]); printf ("\n"); return (0); } /* ----------------------------------------------------------------------* Function: bubble_sort * Purpose : Sort an array of integers in an ascending order, using the * bubble-sort algorithm. * Input : n - The number of intergers in the array. * In/Out : arr - The array. * Returns : Nothing. */ void bubble_sort (int *arr, int n) { int i, j; int temp; /* Let i run over all entries (except the last). */ for (i = 0; i < n - 1; i++) { /* Let j run over all entries from (i+1) until the last. */ for (j = i + 1; j < n; j++) { /* If arr[i] > arr[j], swap the two entries. */ if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return;

} /* * dates_1.c * This program reads a date and prints the dates of the days before and after * this date. */ #include <stdio.h> struct { int int int }; Date day; month; year;

/* Prototypes: */ void print_date (struct Date date); struct Date next_date (struct Date date); struct Date prev_date (struct Date date); int main { int struct struct struct () Date Date Date dd, mm, yy; date; prev; next;

/* Read the date (no validity checks for now). */ printf ("Please enter a date: "); scanf ("%d %d %d", &dd, &mm, &yy); /* Set the date. */ date.day = dd; date.month = mm; date.year = yy; /* Compute the previous date and the next date. */ prev = prev_date (date); next = next_date (date); /* Print out the results. */ printf ("Yesterday was: "); print_date (prev);

printf ("\n"); printf ("Today is: "); print_date (date); printf ("\n"); printf ("Tomorrow will be: "); print_date (next); printf ("\n"); return (0); } /* ----------------------------------------------------------------------* Function: print_date * Purpose : Print a date in a nice format. * Input : date - The date. * Returns : Nothing. */ void print_date (struct Date date) { const char* month_names[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "No v", "Dec"}; printf ("%d %s %d", date.day, month_names[date.month - 1], date.year); return; } /* ----------------------------------------------------------------------* Function: next_date * Purpose : Compute the next date. * Input : date - The current date. * Returns : The date of the next day. */ struct Date next_date (struct Date date) { int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct Date res = date;

/* Increment the day and check whether the date is still legal. */ res.day++; if (res.day > days_in_month[res.month - 1]) { /* Move to the next month. */ res.day = 1; res.month++; if (res.month > 12) { /* Move to the next year. */ res.month = 1; res.year++; } } return (res); } /* ----------------------------------------------------------------------* Function: prev_date * Purpose : Compute the previous date. * Input : date - The current date. * Returns : The date of the previous day. */ struct Date prev_date (struct Date date) { int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct Date res = date; /* Decrement the day and check whether the date is still legal. */ res.day--; if (res.day == 0) { /* Move to the previous month. */ res.month--; if (res.month == 0) { /* Move to the previous year. */

res.month = 12; res.year--; } /* Set the day to be the last of the month. */ res.day = days_in_month[res.month - 1]; } return (res); }

You might also like