C Primer Plus 练习本

简介

用来存自己的练习答案,看情况存,简单的写完就过了

5.11

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#define SCALE 60
void main(void)
{
int tmins = 1, mins,hours;
while(tmins > 0)
{
printf("How many minutes?\n");
scanf("%d", &tmins);
mins = tmins % SCALE;
hours = tmins / SCALE;
if(tmins>0)
{
printf("%d minutes = %d hours,%d minutes\n",tmins,hours,mins);
}
else
{
printf("Bye");
}

}
}

2

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
void main(void)
{
int init;
printf("What's the int number?\n");
scanf("%d",&init);
int target = init +11;
while (++init < target ) // ++init < init + 11 会无限判断为True 因为右边也在变..
{
printf("%d\t", init);
}
}

7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
void cube(double val);

int main(void)
{
double init;
printf("Input a double num:");
scanf("%lf",&init); //double对应转换说明%lf
cube(init);
return 0;
}

void cube(double val)
{
printf("%f",val*val*val);
}

9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
void Temperatures(double val);

int main(void)
{
double init;
printf("Input a Fahrenheit degree(q to quit):\n");
while (scanf("%lf",&init) == 1) // 不要在这里加分号...不然while循环体就是空的了(踩坑)
{
Temperatures(init);
}
printf("Bye");
return 0;

}

void Temperatures(double val)
{
double c = 5.0*(val-32)/9;
double k = c+273.16;
printf("%f °F = %f °C = %f K \n",val,c,k);
}

6.16

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
int main(void)
{
char alphabet[26];
int i;
for(i=0;i<26;i++)
{
alphabet[i] = 'a' + i;
}
for(i=0;i<26;i++)
{
printf("%c",alphabet[i]);
}
}

2

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(void)
{
int i, n;
for (i = 0; i < 6; i++)
{
for (n = 0; n <= i; n++)
{
printf("%c", '$');
}
printf("\n");
}
}

3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main(void)
{
char last;
char start;
for (last = 'F'; last >= 'A';last--)
{
for (start = 'F'; start >= last;start-- )
{
printf("%c",start);
}
printf("\n");
}
}

4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
int main(void)
{
int line;
char letter = 'A';
int count;
for (line = 0; line < 6; line++)
{
for (count = 0; count <= line; count++,letter++)
{
printf("%c",letter);
}
printf("\n");
}
}

6

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(void)
{
int start,end;

printf("A start and a end num splited by comma:\n");
scanf("%d,%d",&start,&end);
printf("Num\tSuqare\tCube\n");
for (int i = start; i <= end; i++)
{
printf("%d\t%d\t%d\n",i,i*i,i*i*i);
}
}

7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <string.h>
int main(void)
{
char word[20];
int len;
printf("Input a word:\n");
scanf("%s",&word);
len = strlen(word)-1; // 最后一个\0不考虑

for (;len >= 0; len--) //不要在这里直接操作size_t类型
{
printf("%c",word[len]);
}
}

8

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <string.h>
int main(void)
{
double num1,num2;
printf("Two float nums splited by spacing(q to quit):\n");
while( scanf("%lf %lf",&num1,&num2) == 2) //scanf成功读取后返回值不是布尔值,是成功读取的参数数量..
{
printf("%lf\n", (num1-num2) / num1*num2);
printf("Two float nums splited by spacing(q to quit):\n");
}
}

11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <string.h>
int main(void)
{
int nums[8];
for ( int i = 0; i <=7; i++)
{
printf("Now is no.%d\n",i);
scanf("%d",&nums[i]);
}
printf("Done.\n");
int len = sizeof(nums)/sizeof(nums[0]) -1 ;// sizeof返回值是bytes,除以单位元素大小就是数组长度。因为索引从0开始所以这里-1
for (; len >= 0; len--)
{
printf("%d\n",nums[len]);
}
}

12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main(void)
{
int i; // 数列大小
printf("How many times?\n");
while (scanf("%d", &i) == 1)
{
int denominator = 1; // 循环中变化的分母
double expression = 0; // 全是正数的级数
double expression2 =0; // 交错级数
int num = 1; // 用于判定正负号
for (; denominator <= i; denominator++, num *= (-1))
{
expression = expression + (double)1 / denominator;
expression2 = expression2 + (double)1 / denominator * num;
printf("%lf %lf\n", expression,expression2);
}
printf("Done.\n");
printf("How many times?\n");
}
}

13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <math.h>
int main(void)
{
int i;
int array[8];
int len = sizeof(array)/sizeof(array[0]);
for (i = 0; i <= len - 1; i++)
{
array[i] = pow(2,i);
}
i = 0;
do
{
printf("%d\n",array[i]);
i++;
}
while (i <= len-1);
}

14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
int main(void)
{
double nums[8];
double sums[8];

int i, j;
int len = sizeof(nums) / sizeof(nums[0]);
printf("Now input nums by order:\n");
for (i = 0; i < len - 1; i++)
{
scanf("%lf", &nums[i]);
}
for (j = 0; j < len - 1; j++)
{
double sumtemp;
for (i = 0; i <= j; i++)
{
sumtemp += nums[i];
}
sums[j] = sumtemp;
}
for (i = 0; i < len - 1; i++)
{
printf("%lf\t", nums[i]);
}
printf("\n");
for (i = 0; i < len - 1; i++)
{
printf("%lf\t", sums[i]);
}
}
/*
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000
1.000000 4.000000 10.000000 20.000000 35.000000 56.000000 84.000000
*/

15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main(void)
{
int i;
char input[255];
for ( i = 0; i <= 255 && input[i] != '\n'; i++)
{
scanf("%c", &input[i]);
if (input[i] == '\n')
{
break;
}
}
for (;i >= 0; i--)
{
printf("%c",input[i]);
}

return 0;
}

16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
int main(void)
{
int year = 0;
double dap = 100, dei = 100;
double r_dei = 1.05;
while (dei <= dap)
{
dap += 110.0;
dei *= r_dei;
year++;
}
printf("%d",year);
// for (year = 0; dei <= dap; ++year)
// {
// dap += 110.0;
// dei *= r_dei;
// }
// printf("%d",year);
}

17

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(void)
{
int year = 0;
double balance = 1e5;
while (balance > 0)
{
balance *= 1.08;
balance -= 1e4;
year++;
}
printf("%d",year);
}

18

do-while

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main(void)
{
int friends = 5;
int week = 1;
do
{
friends -= week;
friends *= 2;
printf("Week%d Frienders:%d\n",week,friends);
week++;
} while (friends <= 150);
}

while

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(void)
{
int friends = 5;
int week = 0;
while (friends <= 150)
{
week++;
friends -= week;
friends *= 2;
printf("Week%d Frienders:%d\n", week, friends);
}
}

for

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main(void)
{
int friends;
int week;
for (friends = 5, week = 1; friends <= 150; week++)
{
friends -= week;
friends *= 2;
printf("Week%d Frienders:%d\n", week, friends);
}
}

7.12

5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
int main(void)
{
char ch;
int count = 0;

while( scanf("%c",&ch) && ch != '#' )
{
switch (ch)
{
case '.':
ch = '!';
count++;
break;
case '!':
ch = '!!';
count++;
default:
break;
}
}
printf("%d",count);
}

6

非提示解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
int main(void)
{
int num = 0;
int count = 0;
char ch;
while (scanf("%c", &ch) && ch != '\n')
{
if (ch == 'e')
{
num = 1;
}
else if (ch == 'i')
{
if (num == 1)
{
count++;
num = 0;
}
else
{
continue;
}
}
}
printf("%d", count);
}

9.11

3

1
2
3
4
5
6
7
8
9
10
11
void printline(char ch, int line, int num)
{
for (; line > 0; line--)
{
for (int count =num ; count > 0; count--) //每行循环重新初始化count为num
{
putchar(ch);
}
putchar('\n');
}
}

4

1
2
3
4
double harmonic(double x,double y)
{
return 2/(1/x+1/y);
}

5

1
2
3
4
5
6
double large_of(double *x,double *y)
{
double large = (*x<*y)? *y:*x;
*x = large;
*y = large; //不用if了,反正就俩值,直接赋
}

6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
double sort(double *x,double *y, double *z)
{
double i;
if (*x<*y)
{
i = *y;
*y = *x;
*x = i;
}
if (*x<*z)
{
i = *z;
*z = *x;
*x = i;
}
if (*y<*z)
{
i = *z;
*z = *y;
*y = i;
}
}

7

这道题题目有点绕

利用ctype.h简化了代码,并未对checkChar返回的-1做任何处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <ctype.h>
void checkStdInput();
int checkChar(char ch);

int main(void)
{
checkStdInput();
return 0;
}

void checkStdInput()
{
char ch;
while ((ch = getchar()) != EOF)
{
checkChar(ch);
}
}

int checkChar(char ch)
{
if (isalpha(ch))
{
int index = (islower) ? ch - 'a' + 1 : ch - 'A' + 1;
printf("%c is a letter and the index is %d\n", ch, index);
}
else
{
return -1;
}
}

8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
double power(double num, int n)
{
double res = num;
if (n == 0)
{
return 1;
}
else if (n == 1)
{
return num;
}

else
{
if (n > 0)
{
for (; n > 1; n--)
{
res *= num;
}
}
else
{
n = -n;
for (; n > 1; n--)
{
res *= num;
}
}
return 1 / res;
}
}

10

1
2
3
4
5
6
7
8
9
10
void to_base_n(int x, int base) 
{
int r;
r = x % base;
if (x >= base)
to_base_n(x / base, base);

putchar('0' + r);
return;
}

11

未加任何限定,无限打印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Fibonacci()
{
int a = 0, b = 1;
printf("%d %d", a, b);
int num = a + b;
printf(" %d",num);
while (1)
{
a = b;
b = num;
num = a + b;
printf(" %d",num);
}
}

打印指定前 n 项

1
2
3
4
5
6
7
8
9
10
11
12
13
void Fibonacci(int n)
{
n = n-3; // 前三项不在循环里
int a = 0, b = 1,c=1;
printf("%d %d %d",a,b,c);
for (; n > 0; n--)
{
a = b;
b = c;
c = a + b;
printf(" %d", c);
}
}

10.13

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
int main(void)
{
// initializing rainfall data for 2010 - 2014
const float rain[YEARS][MONTHS] =
{
{4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
{8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3},
{9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
{7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
{7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}};
int year, month;
float subtot, total;
float *p = rain;
float(*p1)[12] = rain;

printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{ // for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *p++;
//or this: subtot += *(*(rain + year) + month);
printf("%5d %15.1f\n", 2010 + year, subtot);
total += subtot; // total for all years
}
printf("\nThe yearly average is %.1f inches.\n\n",
total / YEARS);
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\n");

for (month = 0; month < MONTHS; month++)
{ // for each month, sum rainfall over years
for (year = 0, subtot = 0; year < YEARS; year++)
subtot += *(*(rain + year) + month);
//or use p1: subtot += *(*(p1+year)+month) ;
printf("%4.1f ", subtot / YEARS);
}
printf("\n");

return 0;
}

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void copy_arr(double target[], double source[], int len)
{
for (int count = 0; count <= len-1 ; count++)
{
target[count] = source[count];
}

}
void copy_ptr(double *target, double *source, int len)
{
for (int count = 0; count <= len-1;count++)
{
*(target++) = *(source++);
}

}
void copy_ptrs(double *target, double *start, double *end)
{
for (; start <= end-1;)
{
*(target++) = *(start++);
}
}

3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
int array_max(int array[], int len);
int max(int a, int b);

int main(void)
{
int array[5] = {5, 7, 1232, 8, 1};
int *p = (int[6]){2323, 2,1243,34547,45,3};//复合字面量,或者说匿名数组?
int res = array_max(array, 5);
int res2 = array_max(array, 6);
printf("%d %d", res,res2);
}

int array_max(int array[], int len) // 数组名和数组长度
{
int a = array[0], b;

for (int i = 1; i <= len - 1; i++)
{

b = array[i];
if (max(a, b) == b)
{
a = b;
continue;
}
else
{
continue;
}
}
return a;
}

int max(int a, int b) // 手写一个max()
{
return (a>=b)?a:b;
}
/*
1232 34547
*/

4

上一道题简单改了改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
int array_max_index(double array[], int len);
double max(double a, double b);

int main(void)
{
// 懒得写小数点,下面包含隐式类型转换
double array[5] = {5, 7123, 1232, 8, 1};
double *p = (double[6]){2323, 2, 1243, 34547, 45, 3}; //复合字面量,
int res1 = array_max_index(array, 5);
int res2 = array_max_index(p, 6);
printf("%d %d", res1, res2);
}

int array_max_index(double array[], int len) // 数组名和数组长度
{
double a = array[0], b;
int maxindex = 0;
for (int i = 1; i <= len - 1; i++)
{
b = array[i];
if (max(a, b) == b)
{
a = b;
maxindex = i;
continue;
}
else
{
continue;
}
}
return maxindex;
}

double max(double a, double b)
{
return (a>=b)?a:b;
}
// 1 3

5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
double array_max_min(double array[], int len);


int main(void)
{
double arr[5] = {1, 2, 3, 4, 6};
double res = array_max_min(arr, 5);
printf("%lf", res);
}
double array_max_min(double array[], int len)
{
double max = array[0];
double min = array[0];
for (int i = 1; i < len; i++)
{
if (max < array[i])
max = array[i];
else if (min > array[i])
min = array[i];
}
return max - min;
}

// 5.000000

6

双指针解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void array_reverse(double arr[], int len)
{
if (len % 2 == 0) // 偶数长度
{
double temp;
double *start = arr;
double *end = &arr[len - 1];
for (int i = 0; i < len / 2; i++)
{
temp = *start;
*start = *end;
*end = temp;

start++;
end--;
}
}
else
{
double temp;
double *start = arr;
double *end = &arr[len - 1];
for (int i = 0; i < (len / 2) + 1; i++)
{
temp = *start;
*start = *end;
*end = temp;

start++;
end--;
}
}
}

7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void copy_2darray(int row, int column, double array[][column], double newarray[][column])
// 复制一个二维数组的内容到另一个二维数组
//参数: 行,列,原数组,新数组
{
double (*ptr)[column] = array;
double (*ptr_new)[column] = newarray;
for (int i = 0; i < row; i++) //二维
{
ptr = array + i;
ptr_new = newarray + i;
for (int j = 0; j < column; j++) //一维
{
*(*ptr_new+j) = *(*ptr+j); // newarray[i][j] = array[i][j]
}
}
}

利用”内层越界”的方式,可以直接当一维数组处理

1
2
3
4
5
6
7
void copy_2darray(int row, int column, double array[][column], double newarray[][column])
{
for (int i = 0; i < row*column; i++)
{
newarray[0][i] =array[0][i];
}
}

9

复制函数参考第 7 题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void show_2darray(int row, int column, double array[][column])
{
for (int i = 0; i < row * column ; i++)
{
if ((i + 1) % column == 0)
{
printf("%g\n", array[0][i]);
}
else
{
printf("%g\t", array[0][i]);
}
}
}

10

1
2
3
4
5
6
7
void array_sumup(int len,int a[], int b[],int sum[])
{
for (int i = 0; i < len; i++)
{
sum[i] = a[i]+b[i];
}
}

13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
void data2array(double array[3][5]);
void show_average_subarray(double array[3][5]);
double average_subarray(double subarray[5]);
double average_all(double array[3][5]);
double maximum(double array[3][5]);
int main(void)
{
double array[3][5];
printf("Input 3 lines of double numbers(splited by space, enter to the next row):\n");
data2array(array);
show_average_subarray(array);
double avr_all = average_all(array);
double max = maximum(array);
printf("The average of all is: %lf\n", avr_all);
printf("The maximum is: %lf", max);
}

void data2array(double array[3][5])
{
for (int i = 0; i < 3; i++)
{
double(*start)[5] = array + i;
fflush(stdin);
scanf("%lf %lf %lf %lf %lf", *start, (*start) + 1, (*start) + 2, (*start) + 3, (*start) + 4);
}
}
void show_average_subarray(double array[3][5])
{
double sum;
printf("Average of each subarray:\n");
for (int i = 0; i < 3; i++)
{
double(*subarray)[5] = array + i;
double avr_subarray = average_subarray(subarray);
printf("Subarray%d: %g\n", i, avr_subarray);
}
}
double average_subarray(double subarray[5])
{
double subsum;
for (int i = 0; i < 5; i++)
{
subsum += subarray[i];
}
return subsum / 5;
}
double average_all(double array[3][5])
{
double sum;
for (int i = 0; i < 15; i++)
{
sum += array[0][i];
}
return sum / 15;
}
double maximum(double array[3][5])
{
double max = array[0][0];
for (int i = 1; i < 15; i++)
{
max = (array[0][i] > max) ? array[0][i] : max;
}
return max;
}

11.13

1

危险

1
2
3
4
5
6
7
void getn(int n, char array[])
{
while (n-->0)
{
*array++ = getchar();
}
}

2

危险

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void getn(int n, char array[])
{
fflush(stdin);
char tmp;
for (;n>0;n--)
{
tmp = getchar();
if(tmp != ' '&& tmp != '\n' && tmp!='\t')
{
*array++ = tmp;
}
else
{
break;
}
}
}

3

危险

1
2
3
4
5
void word(char array[])
{
fflush(stdin);
scanf("%s",array);
}

5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char* _strchr(char *string, char ch)
{
while (*string)
{
if ( *string == ch)
{
char *ptr = &ch;
return ptr;
}
else
{
string++;
}
}
return NUll;
}

7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
char *mystrncpy(char *s1, char *s2, int n)
{
char *s1end = s1 + strlen(s1);
if (strlen(s2) >= n)
{
while (n--)
{
*s1end++ = *s2++;
}
}
else
{
int s2len = strlen(s2);
while (s2len--)
{
*s1end++ = *s2++;
}
n = n - s2len;
while (n--)
{
*s1end++ = ' ';
}
}
return s1;
}
Author

BakaFT

Posted on

2020-07-16

Updated on

2023-12-28

Licensed under

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×