Skip to content

Commit cef7674

Browse files
committed
Solutions to questions in Programming In C, completed in middle 2015
1 parent 248d3a8 commit cef7674

File tree

235 files changed

+7932
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

235 files changed

+7932
-0
lines changed

Chapter10Exercises/1/1.1.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
int count = 10, x;
6+
int *int_pointer;
7+
8+
int_pointer = &count;
9+
x = *int_pointer;
10+
11+
printf("count = %i, x = %i\n", count, x);
12+
13+
return 0;
14+
}

Chapter10Exercises/1/1.10.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
3+
struct entry
4+
{
5+
int value;
6+
struct entry *next;
7+
};
8+
9+
struct entry *findEntry(struct entry *listPtr, int match)
10+
{
11+
while(listPtr != (struct entry *) 0)
12+
if(listPtr->value == match)
13+
return listPtr;
14+
else
15+
listPtr = listPtr->next;
16+
17+
return (struct entry *) 0;
18+
}
19+
20+
21+
int main(void)
22+
{
23+
struct entry *findEntry(struct entry *listPtr, int match);
24+
struct entry n1, n2, n3;
25+
struct entry *listPtr, *listStart = &n1;
26+
27+
int search;
28+
29+
n1.value = 100;
30+
n1.next = &n2;
31+
32+
n2.value = 200;
33+
n2.next = &n3;
34+
35+
n3.value = 300;
36+
n3.next = (struct entry *) 0;
37+
38+
printf("Enter value to locate: ");
39+
scanf("%i", &search);
40+
41+
listPtr = findEntry(listStart, search);
42+
43+
if(listPtr != (struct entry *) 0)
44+
printf("Found %i.\n", listPtr->value);
45+
else
46+
printf("Not found.\n");
47+
48+
return 0;
49+
}

Chapter10Exercises/1/1.11.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//此函数对一个整数数组中的各元素进行求和
2+
3+
#include <stdio.h>
4+
5+
int arraySum(int array[], const int n)
6+
{
7+
int sum = 0, *ptr;
8+
int *const arrayEnd = array + n;
9+
10+
for(ptr = array; ptr < arrayEnd; ++ptr)
11+
sum += *ptr;
12+
13+
return sum;
14+
}
15+
16+
int main(void)
17+
{
18+
int arraySum(int array[], const int n);
19+
int values[10] = {3, 7, -9, 3, 6, -1, 7, 9, 1, -5};
20+
21+
printf("The sum is %i\n", arraySum(values, 10));
22+
23+
return 0;
24+
}

Chapter10Exercises/1/1.12.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//此函数对一个整数数组中的各元素进行求和(第二版)
2+
3+
#include <stdio.h>
4+
5+
int arraySum(int array[], const int n)
6+
{
7+
int sum = 0;
8+
int *const arrayEnd = array + n;
9+
10+
for(; array < arrayEnd; ++array)
11+
sum += *array;
12+
13+
return sum;
14+
}
15+
16+
int main(void)
17+
{
18+
int arraySum(int array[], const int n);
19+
int values[10] = {3, 7, -9, 3, 6, -1, 7, 9, 1, -5};
20+
21+
printf("The sum is %i\n", arraySum(values, 10));
22+
23+
return 0;
24+
}

Chapter10Exercises/1/1.13.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
3+
void copyString(char *to, char *from)
4+
{
5+
for(; *from != '\0'; ++from, ++to)
6+
*to = *from;
7+
8+
*to = '\0';
9+
}
10+
11+
int main(void)
12+
{
13+
void copyString(char *to, char *from);
14+
char string1[] = "A string to be copied.";
15+
char string[50];
16+
17+
copyString(string2, string1);
18+
printf("%s\n", string2);
19+
20+
copyString(string2, "So is this.");
21+
printf("%s\n", string2);
22+
23+
return 0;
24+
}

Chapter10Exercises/1/1.14.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
3+
void copyString(char *to, char *from)
4+
{
5+
while(*from)
6+
*to++ = *from++;
7+
8+
*to = '\0';
9+
}
10+
11+
int main(void)
12+
{
13+
void copyString(char *to, char *from);
14+
char string1[] = "A string to be copied.";
15+
char string2[50];
16+
17+
copyString(string2, string1);
18+
printf("%s\n", string2);
19+
20+
copyString(string2, "So is this.");
21+
printf("%s\n", string2);
22+
23+
return 0;
24+
}

Chapter10Exercises/1/1.15.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//此函数求字符串中的字符数,指针版本
2+
3+
#include <stdio.h>
4+
5+
int stringLength(const char *string)
6+
{
7+
const char *cptr = string;
8+
9+
while(*cptr)
10+
++cptr;
11+
12+
return cptr - string;
13+
}
14+
15+
int main(void)
16+
{
17+
int stringLength(const char *string);
18+
19+
printf("%i ", stringLength("StringLength test"));
20+
printf("%i ", stringLength(""));
21+
printf("%i\n", stringLength("complete"));
22+
23+
return 0;
24+
}

Chapter10Exercises/1/1.2.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
char c = 'Q';
6+
char *char_pointer = &c;
7+
8+
printf("%c %c\n", c, *char_pointer);
9+
10+
c = '/';
11+
printf("%c %c\n", c, *char_pointer);
12+
13+
*char_pointer = '(';
14+
printf("%c %c\n", c, *char_pointer);
15+
16+
return 0;
17+
}

Chapter10Exercises/1/1.3.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//关于指针的更多知识
2+
3+
#include <stdio.h>
4+
5+
int main(void)
6+
{
7+
int i1, i2;
8+
int *p1, *p2;
9+
10+
i1 = 5;
11+
p1 = &i1;
12+
i2 = *p1 / 2 + 10;
13+
p2 = p1;
14+
15+
printf("i1 = %i, i2 = %i, *p1 = %i, *p2 = %i\n", i1, i2, *p1, *p2);
16+
17+
return 0;
18+
}

Chapter10Exercises/1/1.4.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//´Ë³ÌÐòÑÝʾ½á¹¹ÌåÖ¸Õë
2+
3+
#include <stdio.h>
4+
5+
int main(void)
6+
{
7+
struct date
8+
{
9+
int month;
10+
int day;
11+
int year;
12+
};
13+
14+
struct date today, *datePtr;
15+
16+
datePtr = &today;
17+
18+
datePtr->month = 9;
19+
datePtr->day = 25;
20+
datePtr->year = 2015;
21+
22+
printf("Today's date is %i/%i/%.2i.\n",
23+
datePtr->month, datePtr->day, datePtr->year % 100);
24+
25+
return 0;
26+
}

Chapter10Exercises/1/1.5.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//此函数使用包含指针的结构体
2+
3+
#include <stdio.h>
4+
5+
int main(void)
6+
{
7+
struct intPtrs
8+
{
9+
int *p1;
10+
int *p2;
11+
};
12+
13+
struct intPtrs pointers;
14+
int i1 = 100, i2;
15+
16+
pointers.p1 = &i1;
17+
pointers.p2 = &i2;
18+
*pointers.p2 = -97;
19+
20+
printf("i1 = %i, *pointers.p1 = %i\n", i1, *pointers.p1);
21+
printf("i2 = %i, *pointers.p2 = %i\n", i2, *pointers.p2);
22+
23+
return 0;
24+
}

Chapter10Exercises/1/1.6.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//此函数使用链表
2+
3+
#include <stdio.h>
4+
5+
int main(void)
6+
{
7+
struct entry
8+
{
9+
int value;
10+
struct entry *next;
11+
};
12+
13+
struct entry n1, n2, n3;
14+
int i;
15+
16+
n1.value = 100;
17+
n2.value = 200;
18+
n3.value = 300;
19+
20+
n1.next = &n2;
21+
n2.next = &n3;
22+
23+
i = n1.next->value;
24+
printf("%i ", i);
25+
26+
printf("%i\n", n2.next->value);
27+
28+
return 0;
29+
}

Chapter10Exercises/1/1.7.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//此程序遍历一个链表
2+
3+
#include <stdio.h>
4+
5+
int main(void)
6+
{
7+
struct entry
8+
{
9+
int value;
10+
struct entry *next;
11+
};
12+
13+
struct entry n1, n2, n3;
14+
struct entry *list_pointer = &n1;
15+
16+
n1.value = 100;
17+
n1.next = &n2;
18+
19+
n2.value = 200;
20+
n2.next = &n3;
21+
22+
n3.value = 300;
23+
n3.next = (struct entry *) 0;
24+
25+
while(list_pointer != (struct entry *) 0){
26+
printf("%i\n", list_pointer->value);
27+
list_pointer = list_pointer->next;
28+
}
29+
30+
return 0;
31+
}

Chapter10Exercises/1/1.8.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//此程序演示指针与函数的使用
2+
3+
#include <stdio.h>
4+
5+
void test(int *int_pointer)
6+
{
7+
*int_pointer = 100;
8+
}
9+
10+
int main(void)
11+
{
12+
void test(int *int_pointer);
13+
int i = 50, *p = &i;
14+
15+
printf("Before the call to test i = %i\n", i);
16+
17+
test(p);
18+
printf("After the call to test i = %i\n", i);
19+
20+
return 0;
21+
}

0 commit comments

Comments
 (0)