Skip to content

Commit d8ae229

Browse files
ZheFeng7110H-Sofie
authored andcommitted
【通信软件技术(C++版)】增加PPT课件与课件参考代码
1 parent 427cfe9 commit d8ae229

18 files changed

Lines changed: 400 additions & 0 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
static const char *year_name[] = {"鼠", "牛", "虎", "兔",
6+
"龙", "蛇", "马", "羊",
7+
"猴", "鸡", "狗", "猪"};
8+
9+
printf("%s\n", year_name[2 + (29 % 12)]);
10+
11+
return 0;
12+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
4+
typedef struct Calc {
5+
size_t states;
6+
size_t now_state;
7+
8+
double sum;
9+
} Calc;
10+
11+
double func(Calc* calc, int a, int b)
12+
{
13+
if (calc->now_state >= calc->states) {
14+
return 0.0;
15+
}
16+
17+
calc->now_state++;
18+
calc->sum += (double)a / (double)b + func(calc, b, a + b);
19+
}
20+
21+
int main(void)
22+
{
23+
int n;
24+
25+
__attribute__((unused)) int ret = scanf("%d", &n);
26+
27+
Calc ans = {0};
28+
ans.states = n;
29+
30+
func(&ans, 1, 1);
31+
32+
printf("%lf\n", ans.sum);
33+
34+
return 0;
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
3+
int generateStateNum(int a, int state)
4+
{
5+
int sum = 0;
6+
int ret = 1;
7+
8+
for (int i = 1; i <= state; i++) {
9+
sum += a * ret;
10+
11+
ret *= 10;
12+
}
13+
14+
return sum;
15+
}
16+
17+
int func(int a, int state)
18+
{
19+
int sum = 0;
20+
21+
for (int i = 1; i <= state; i++) {
22+
sum += generateStateNum(a, i);
23+
}
24+
25+
return sum;
26+
}
27+
28+
int main(void)
29+
{
30+
int a, state;
31+
__attribute__((unused)) int ret = scanf("%d%d", &a, &state);
32+
33+
printf("%d\n", func(a, state));
34+
35+
return 0;
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define countof(array) (sizeof(array) / sizeof(array[0]))
5+
6+
int main(void)
7+
{
8+
char str[9999];
9+
__attribute__((unused)) char* dummy_ptr = fgets(str, countof(str), stdin);
10+
11+
int upper = 0, lower = 0;
12+
13+
for (int i = 0; i < strlen(str); i++) {
14+
const char ch = str[i];
15+
16+
if (ch >= 'a' && ch <= 'z') {
17+
lower++;
18+
}
19+
if (ch >= 'A' && ch <= 'Z') {
20+
upper++;
21+
}
22+
}
23+
24+
printf("Upper: %d, Lower: %d\n", upper, lower);
25+
26+
return 0;
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define countof(array) (sizeof(array) / sizeof(array[0]))
5+
6+
char toUpper(char* ch)
7+
{
8+
if (*ch >= 'a' && *ch <= 'z') {
9+
*ch -= 'a' - 'A';
10+
}
11+
}
12+
13+
int main(void)
14+
{
15+
char str[9999];
16+
__attribute__((unused)) char* dummy_ptr = fgets(str, countof(str), stdin);
17+
18+
toUpper(&str[0]);
19+
20+
const int len = strlen(str);
21+
for (int i = 0; i < len; i++) {
22+
if (str[i] == ' ') {
23+
toUpper(&str[i + 1]);
24+
}
25+
}
26+
27+
printf("%s", str);
28+
29+
return 0;
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
#include <unistd.h>
4+
#include <stdlib.h>
5+
6+
#define FILE_NAME "test.txt"
7+
#define MY_NAME "your_name"
8+
#define MY_ID "12345678"
9+
10+
int main(void)
11+
{
12+
if (access(FILE_NAME, 0) != 0) {
13+
if (system("touch " FILE_NAME) != 0) {
14+
printf("Error: failed to create file!");
15+
return 1;
16+
}
17+
}
18+
19+
FILE* stream = fopen(FILE_NAME, "w");
20+
if (stream == NULL) {
21+
printf("Error: Failed to open file " FILE_NAME "!\n");
22+
return 1;
23+
}
24+
25+
fprintf(stream, "Name: " MY_NAME "\n");
26+
fprintf(stream, "Id: " MY_ID "\n");
27+
fprintf(stream, "Time: ");
28+
29+
time_t time_stamp;
30+
struct tm* time_info;
31+
time(&time_stamp);
32+
time_info = localtime(&time_stamp);
33+
34+
const char* week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
35+
const int year = time_info->tm_year + 1900;
36+
const int month = time_info->tm_mon + 1;
37+
const int day = time_info->tm_mday;
38+
const int week_num = time_info->tm_wday;
39+
const int hour = time_info->tm_hour;
40+
const int min = time_info->tm_min;
41+
const int sec = time_info->tm_sec;
42+
43+
fprintf(stream, "%d/%d/%d %s %d:%d:%d\n", year, month, day, week[week_num], hour, min, sec);
44+
45+
fclose(stream);
46+
47+
return 0;
48+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
#include <unistd.h>
4+
#include <stdlib.h>
5+
6+
#include <sys/wait.h>
7+
8+
#define FILE_NAME "test.txt"
9+
10+
int main(void)
11+
{
12+
pid_t pid = fork();
13+
if (pid == -1) {
14+
printf("Error: failed to fork!\n");
15+
return 1;
16+
}
17+
18+
if (pid == 0) {
19+
printf("Sub process start.\n");
20+
sleep(10);
21+
22+
if (access(FILE_NAME, 0) != 0) {
23+
printf("Error: file " FILE_NAME "does not exist!\n");
24+
return 1;
25+
}
26+
27+
FILE *stream = fopen(FILE_NAME, "r");
28+
if (stream == NULL) {
29+
printf("Error: Failed to open file " FILE_NAME "!\n");
30+
return 1;
31+
}
32+
33+
char buffer[100];
34+
size_t index = 0U;
35+
do {
36+
__attribute__((unused)) int ret = fscanf(stream, "%c", &buffer[index]);
37+
index++;
38+
} while(buffer[index - 1] != EOF);
39+
40+
printf("%s\n", buffer);
41+
42+
fclose(stream);
43+
}
44+
else {
45+
printf("Sub process pid=%d\n", pid);
46+
int status;
47+
wait(&status);
48+
}
49+
50+
return 0;
51+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <stdio.h>
2+
#include <sys/stat.h>
3+
#include <sys/types.h>
4+
#include <fcntl.h>
5+
#include <unistd.h>
6+
#include <stdbool.h>
7+
#include <string.h>
8+
#include <stdlib.h>
9+
10+
#include "fifo_def.h"
11+
12+
#define TX_FIFO_NAME "client_to_server"
13+
#define RX_FIFO_NAME "server_to_client"
14+
15+
int main(void)
16+
{
17+
if (access(FIFO_DIR, F_OK) != 0) {
18+
printf("Fifo dir not exists, now crerate it.\n");
19+
__attribute__((unused)) int ret = system("mkdir " FIFO_DIR);
20+
}
21+
22+
int err = mkfifo(FIFO_DIR TX_FIFO_NAME, 0666);
23+
if (err == -1) {
24+
fprintf(stderr, "Failed to create fifo " TX_FIFO_NAME "! Error Code: %d\n", err);
25+
return 1;
26+
}
27+
printf("finished to create fifo " TX_FIFO_NAME ".\n");
28+
29+
printf("waitting for " RX_FIFO_NAME "...\n");
30+
while (access(FIFO_DIR RX_FIFO_NAME, F_OK) != 0) {
31+
}
32+
printf(RX_FIFO_NAME " created.\n");
33+
34+
int rx_fifo = open(FIFO_DIR RX_FIFO_NAME, O_RDONLY);
35+
if (rx_fifo == -1) {
36+
fprintf(stderr, "Failed to open fifo " RX_FIFO_NAME "! ErrorCode: %d\n", rx_fifo);
37+
unlink(FIFO_DIR TX_FIFO_NAME);
38+
return 1;
39+
}
40+
printf("Fininshed to open fifo " RX_FIFO_NAME ".\n");
41+
42+
int tx_fifo = open(FIFO_DIR TX_FIFO_NAME, O_WRONLY);
43+
if (tx_fifo == -1) {
44+
fprintf(stderr, "Failed to open fifo " TX_FIFO_NAME "! ErrorCode: %d\n", tx_fifo);
45+
unlink(FIFO_DIR TX_FIFO_NAME);
46+
close(rx_fifo);
47+
return 1;
48+
}
49+
printf("Finished to open fifo " TX_FIFO_NAME ".\n");
50+
51+
char rx_buffer[256], tx_buffer[256];
52+
while (true) {
53+
int size = read(rx_fifo, rx_buffer, sizeof rx_buffer / sizeof rx_buffer[0] - 1);
54+
rx_buffer[size] = '\0';
55+
printf("Readden buffer: %s, size = %d\n", rx_buffer, size);
56+
57+
if (strcmp(rx_buffer, "esc") == 0) {
58+
break;
59+
}
60+
61+
printf("Input message: ");
62+
fflush(stdin);
63+
__attribute__((unused)) int ret = scanf("%s", tx_buffer);
64+
size = write(tx_fifo, tx_buffer, strlen(tx_buffer));
65+
printf("Written buffer: %s, size = %d\n", tx_buffer, size);
66+
67+
if (strcmp(tx_buffer, "esc") == 0) {
68+
break;
69+
}
70+
}
71+
72+
close(rx_fifo);
73+
close(tx_fifo);
74+
unlink(FIFO_DIR TX_FIFO_NAME);
75+
76+
return 0;
77+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#ifndef FIFO_DEF_H
3+
#define FIFO_DEF_H
4+
5+
#define FIFO_DIR "./fifos/"
6+
7+
#endif // !FIFO_DEF_H

0 commit comments

Comments
 (0)