例题7-1
#include <stdio.h>
void print_some_stars(void);
void print_some_words(void);
int main(void)
{
print_some_stars();
print_some_words();
print_some_stars();
return 0;
}
void print_some_stars(void)
{
printf("******************\n");
}
void print_some_words(void)
{
printf(" How do you do! \n");
}
例题7-2
#include <stdio.h>
int a = 0, b = 0;
int max(int x, int y);
int main(void)
{
printf("please type a value and b value:");
scanf("%d%d", &a, &b);
printf("max=%d", max(a, b));
return 0;
}
int max(int x, int y)
{
int max_value = 0;
if (x > y)
max_value = x;
else
max_value = y;
return max_value;
}
或者下面的代码
#include <stdio.h>
int a = 0, b = 0;
int max(int a, int b);
int main(void)
{
printf("please type a value and b value:");
scanf("%d%d", &a, &b);
printf("max=%d", max(a, b));
return 0;
}
int max(int a, int b)
{
int max_value = 0;
if (a > b)
max_value = a;
else
max_value = b;
return max_value;
}
例题7-3
#include <stdio.h>
float a = 0, b = 0;
int max(float x, float y);
int main(void)
{
printf("please type a value and b value:");
scanf("%f%f", &a, &b);
printf("max=%d", max(a, b));
return 0;
}
int max(float x, float y)
{
float z = 0;
if (x > y)
z = x;
else
z = y;
return z;
}
例题7-4
#include <stdio.h>
float a = 0, b = 0;
float add(float x, float y);
int main(void)
{
printf("please type a value and b value:");
scanf("%f%f", &a, &b);
printf("%0.2f+%0.2f=%0.2f", a, b, add(a, b));
return 0;
}
float add(float x, float y)
{
return x + y;
}
例题7-5
#include <stdio.h>
int a = 0, b = 0, c = 0, d = 0;
int max_four(int x, int y, int z,int t);
int max_two(int x, int y);
int main(void)
{
printf("please type a,b,c,d:");
scanf("%d%d%d%d", &a, &b, &c, &d);
printf("max= %d", max_four(a, b, c, d));
return 0;
}
int max_four(int x, int y, int z, int t)
{
return max_two(x, max_two(y, max_two(z, t)));
}
int max_two(int x, int y)
{
int max = 0;
if (x > y)
max = x;
else
max = y;
return max;
}
例题7-6
#include <stdio.h>
//分析
int age(int n);
int main(void)
{
printf("NO.5 age = %d", age(5));
return 0;
}
int age(int n)
{
int c = 0;
if (n == 1)
c = 10;
else
c = age(n - 1) + 2;
return c;
}
/*age5 = age4 + 2
age4 = age3 + 2
age3 = age2 + 2
age2 = age1 + 2
age1 = 10*/
例题7-7
#include <stdio.h>
int n = 0;
int Factorial(int n);
int main(void)
{
printf("please type n value:");
scanf("%d", &n);
printf("%d!=%d", n, Factorial(n));
return 0;
}
int Factorial(int n)
{
int c = 0;
if (n == 0)
c = 1;
else
c = Factorial(n - 1) * n;
return c;
}
例题7-8 略
例题7-9
#include <stdio.h>
int a[10] = { 0 };
int max(int x, int y);
int max_value = 0, max_compare = 0;
int number = 0;
int main(void)
{
printf("please type ten number:");
for (int i = 0; i <= 9; i++)
{
//int a1 = 0;
//scanf("%d", &a1);
//a[i] = a1;
scanf("%d", &a[i]);
}
max_value = a[0];
for (int i = 1; i <= 9; i++)
{
max_compare = max_value;
max_value = max(max_value, a[i]);
if (max_value != max_compare)
number = i+1;
}
printf("max_value = %d,number = %d", max_value, number);
return 0;
}
int max(int x, int y)
{
int result = 0;
if (x > y)
result = x;
else
result = y;
return result;
}
例题7-10
#include <stdio.h>
float sorce[10] = { 0 };
float average(float sorce[10]);
int main(void)
{
printf("please type ten value:");
for (int i = 0; i <= 9; i++)
scanf("%f", &sorce[i]);
printf("average = %0.2f", average(sorce));
return 0;
}
float average(float sorce[10])
{
float sum = 0;
for (int i = 0; i <= 9; i++)
sum = sum + sorce[i];
return (float)(sum / 10.0);
}
例题7-11
#include <stdio.h>
int number = 0;
float sorce1[5] = { 98.5,97,91.5,60,55 };
float sorce2[10] = {67.5,89.5,99,69.5,77,89.5,76.5,54,60,99.5};
float average(float sorce[], int number);
int main(void)
{
printf("the average of class A is %0.2f\n", average(sorce1, 5));
printf("the average of class B is %0.2f", average(sorce2, 10));
return 0;
}
float average(float sorce[],int number)
{
float sum = 0;
for (int i = 0; i <= number; i++)
sum = sum + sorce[i];
return (float)(sum / number);
}
例题7-12
#include <stdio.h>
int a[10] = { 45,2,9,0,-3,54,12,5,66,33 };
int b[10] = { 0 };
void min(int a[], int number);
int main(void)
{
for(int n = 1;n<=9;n++)
min(a, n);
for (int i = 0; i <= 9; i++)
printf("%d ", a[i]);
return 0;
}
void min(int a[],int number)
{
int bottle = 0;
int number1 = 0;
for (int i = 0; i <= 9; i++)
{
b[i] = a[i];
}
for (int i = number-1; i <= 8; i++)
{
if (b[i] < b[i + 1])
{
bottle = b[i];
b[i] = b[i + 1];
b[i + 1] = bottle;
}
}
for (int i = number-1; i <= 9; i++)
{
if (a[i] == b[9])
number1 = i;
}
bottle = a[number1];
a[number1] = a[number - 1];
a[number - 1] = bottle;
}
例题7-13
#include <stdio.h>
int a[3][4] = { {1,3,5,7},{2,4,6,7},{15,17,34,12} };
int max(int a[][4]);
int main(void)
{
printf("max = %d", max(a));
return 0;
}
int max(int a[][4])
{
int max_value = 0;
max_value = a[0][0];
for(int i = 0;i<=2;i++)
for (int j = 0; j <= 3; j++)
{
if (max_value < a[i][j])
max_value = a[i][j];
}
return max_value;
}
例题7-14
#include <stdio.h>
float max = 0, min = 0;
float score[10] = { 89,95,87.5,100,67.5,97,59,84,73,90 };
float score_average(float score[10]);
int main(void)
{
printf("max= %0.2f\nmin = %0.2f\naverage = %0.2f", max, min, score_average(score));
return 0;
}
float score_average(float score[10])
{
max = score[0];
min = score[0];
float sum = score[0];
for (int i = 1; i <= 9; i++)
{
if (max < score[i])
max = score[i];
if (min > score[i])
min = score[i];
sum = sum + score[i];
}
return (float)(sum / 10.0);
}
例题7-15
#include <stdio.h>
int a = 3, b = 5;
int max(int a, int b);
int main(void)
{
int a = 8;
printf("max=%d\n", max(a, b));
return 0;
}
int max(int a, int b)
{
int c;
c = a > b ? a : b;
return c;
}
#include <stdio.h>
int a = 3, b = 5;
int max(int a, int b);
int main(void)
{
//int a = 8;
printf("max=%d\n", max(a, b));
return 0;
}
int max(int a, int b)
{
int c;
c = a > b ? a : b;
return c;
}
例题7-16
#include <stdio.h>
int f(int a);
int main(void)
{
int a = 2, i;
for (i = 0; i < 3; i++)
printf("%d\n", f(a));
return 0;
}
int f(int a)
{
auto int b = 0;
static c = 3;
b = b + 1;
c = c + 1;
return(a + b + c);
}
例题7-17
#include <stdio.h>
int F(int a);
int main(void)
{
for (int i = 1; i <= 5; i++)
printf("%d!=%d\n", i, F(i));
return 0;
}
int F(int a)
{
static value = 1;
value = value * a;
return value;
}
例题7-18
#include <stdio.h>
int max(int a, int b, int c);
int main(void)
{
extern a, b, c;
printf("max=%d", max(a, b, c));
return 0;
}
int a=34, b=67, c=12;
int max(int a, int b, int c)
{
int max_value = 0;
max_value = a;
if(max_value<b)
max_value = b;
if (max_value < c)
max_value = c;
return max_value;
}
例题7-19
f1.c程序
#include <stdio.h>
int b = 3;
int a = 0, m = 0;
int power(int a);
int main(void)
{
printf("please type a value and m value:");
scanf("%d%d", &a, &m);
printf("%d*%d=%d\n", a, m, a*m);
printf("%d**%d=%d", a, m, power(m));
return 0;
}
f2.c程序
extern a;
int power(int m)
{
int value = a;
if (m == 1)
value = a;
else if(m >=2)
{
for (int i = 1; i <= m - 1; i++)
{
value = value * a;
}
}
return value;
}
例题7-20
f1.c
#include <stdio.h>
char str[80] = { '\0' };
char element = ' ';
extern void get_string(char str[80]);
extern void delete_element(char str[80], char element);
extern void print_string(char str[80]);
int main(void)
{
printf("please type a string:");
get_string(str);
delete_element(str, element);
print_string(str);
return 0;
}
f2.c
#include <stdio.h>
void get_string(char str[80])
{
gets(str);
}
f3.c
#include <stdio.h>
void delete_element(char str[80], char element)
{
char str1[80] = { '\0' };
int count = 0;
for(int i = 0;str[i]!='\0';i++)
if (str[i] != element)
{
str1[count] = str[i];
count = count + 1;
}
for (int i = 0; i < 80; i++)
{
str[i] = str1[i];
}
}
f4.c
#include <stdio.h>
void print_string(char str[80])
{
printf("%s", str);
}