第1題
#include <stdio.h>
double min(double, double);
int main(void)
{
double x, y;
printf("Enter two numbers (q to quit):\n");
while (scanf("%lf %lf", &x, &y) == 2)
{
printf("The smaller number is %f.\n", min(x, y));
printf("Next two values (q to quit):\n");
}
printf("Bye.\n");
return 0;
}
double min(double a, double b)
{
return a < b ? a : b;
}
第2題
#include <stdio.h>
void chline(char, int, int);
int main(void)
{
char c;
int col, row;
printf("Enter the character to print: ");
scanf("%c", &c);
printf("Enter the number of rows: ");
scanf("%d", &row);
printf("Enter the number of columns: ");
scanf("%d", &col);
chline(c, col, row);
return 0;
}
void chline(char ch, int i, int j)
{
int a, b;
for (a = 1; a <= j; a++)
{
for (b = 1; b <= i; b++)
putchar(ch);
putchar('\n');
}
}
第3題
#include <stdio.h>
void chline(char, int, int);
int main(void)
{
char c;
int col, row;
printf("Enter a character (# to quit): ");
while ((c = getchar()) != '#')
{
if (c == '\n')
continue;
printf("Enter number of columns and number of rows: ");
if (scanf("%d %d", &col, &row) != 2)
break;
chline(c, col, row);
printf("\nEnter next character (# to quit): ");
}
printf("Bye.\n");
return 0;
}
void chline(char ch, int i, int j)
{
int a, b;
for (a = 1; a <= j; a++)
{
for (b = 1; b <= i; b++)
putchar(ch);
putchar('\n');
}
}
第4題
#include <stdio.h>
double harmonic_average(double, double);
int main(void)
{
double num1, num2;
double har_ave;
printf("Enter two numbers to calculate (q to quit): ");
while (scanf("%lf %lf", &num1, &num2) ==2)
{
if (num1 == 0 || num2 == 0)
{
printf("Neither number can be zero!\nPlease enter two numbers again (q to quit): ");
continue;
}
har_ave = harmonic_average(num1, num2);
printf("The harmonic average of %f and %f is: %f\n", num1, num2, har_ave);
printf("Enter next numbers (q to quit): ");
}
printf("Bye.\n");
return 0;
}
double harmonic_average(double x, double y)
{
double x1, y1;
x1 = 1 / x;
y1 = 1 / y;
return 2 / (x1 + y1);
}
第5題
#include <stdio.h>
void larger_of(double*, double*);
int main(void)
{
double x, y;
printf("Enter two numbers (q to quit): ");
while (scanf("%lf %lf", &x, &y) == 2)
{
larger_of(&x, &y);
printf("The modified values are %f and %f.\n", x, y);
printf("Next two values (q to quit): ");
}
printf("Bye.\n");
return 0;
}
void larger_of(double* p1, double* p2)
{
if (*p1 > *p2)
*p2 = *p1;
else
*p1 = *p2;
}
//alternatively
/*
void larger_of(double* p1, double* p2)
{
*p1 = *p2 = *p1 > *p2 ? *p1 : *p2;
}
*/
第6題
#include <stdio.h>
void sort(double*, double*, double*);
int main(void)
{
double x, y, z;
printf("Enter three numbers (q to quit): ");
while (scanf("%lf %lf %lf", &x, &y, &z) == 3)
{
sort(&x, &y, &z);
printf("The modified values are %f, %f and %f.\n", x, y, z);
printf("Next three values (q to quit): ");
}
printf("Bye.\n");
return 0;
}
void sort(double* a, double* b, double* c)
{
double temp;
if (*a > *b)
{
temp = *a;
*a = *b;
*b = temp;
}
if (*b > *c)
{
temp = *b;
*b = *c;
*c = temp;
}
if (*a > *b)
{
temp = *a;
*a = *b;
*b = temp;
}
}
第7題
#include <stdio.h>
#include <ctype.h>
void rep_location(void);
int location(char);
int main(void)
{
rep_location();
return 0;
}
void rep_location(void)
{
int c;
int get_loca;
printf("Start reading...\n");
while ((c = getchar()) != EOF)
{
get_loca = location(c);
if (get_loca != -1)
printf("%c is a letter, its location is %d\n", c, get_loca);
else
printf("%c is not a letter.\n", c);
}
printf("Done.\n");
}
int location(char ch)
{
int loca;
if (isalpha(ch))
{
ch = tolower(ch);
loca = ch - 'a' + 1;
}
else
loca = -1;
return loca;
}
第8題
#include <stdio.h>
double power(double, int);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the integer power to which\n"
"the number will be raised. Enter q to quit.\n");
while (scanf("%lf %d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double pow = 1;
int i;
if (p == 0)
{
if (n == 0)
printf("0 to the power 0 is undefined, using 1 as the value.\n");
pow = 1;
}
else if (n == 0)
pow = 0.0;
else if (p > 0)
for (i = 1; i <= p; i++)
pow *= n;
else
{
for (i = 1; i <= p; i++)
pow *= n;
pow = 1.0 / pow;
}
return pow;
}
第9題
#include <stdio.h>
double power(double, int);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the integer power to which\n"
"the number will be raised. Enter q to quit.\n");
while (scanf("%lf %d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double pow = 1;
if (p == 0)
{
if (n == 0)
printf("0 to the power 0 is undefined, using 1 as the value.\n");
pow = 1;
}
else if (n == 0)
pow = 0.0;
else if (p > 0)
pow = n * power(n, p - 1);
else
pow = 1.0 / power(n, -p);
return pow;
}
第10題
#include <stdio.h>
void to_base_n(unsigned long, unsigned int);
int main(void)
{
unsigned long number;
unsigned int b;
int count;
printf("Enter an integer (q to quit): ");
while (scanf("%lu", &number) == 1)
{
printf("Enter the number base (2-10): ");
while ((count = scanf("%u", &b)) == 1 && (b < 2 || b > 10))
printf("Base should be in the range 2-10: ");
if (count != 1)
break;
printf("Base %u equivalent: ", b);
to_base_n(number, b);
putchar('\n');
printf("Enter next integer (q to quit): ");
}
printf("Done.\n");
return 0;
}
void to_base_n(unsigned long n, unsigned int base)
{
int r;
r = n % base;
if (n >= base)
to_base_n(n / base, base);
printf("%d", r);
}
第11題
#include <stdio.h>
unsigned long Fibonacci(unsigned);
int main(void)
{
unsigned number;
printf("Enter the number of Fibonacci sequence (q to quit): ");
while (scanf("%u", &number) == 1)
{
if (number == 0)
{
printf("Please enter number bigger than 0: ");
continue;
}
printf("No.%u of Fibonacci sequence is %lu.\n", number, Fibonacci(number));
printf("Enter next number (q to quit): ");
}
printf("Bye.\n");
return 0;
}
unsigned long Fibonacci(unsigned n)
{
unsigned i;
unsigned long f1 = 1;
unsigned long f2 = 1;
unsigned long temp;
for (i = 3; i <= n; i++)
{
temp = f2;
f2 = f1 + f2;
f1 = temp;
}
return f2;
}