c语言经典编程282例
C语言经典编程实例
C语言是一种强大的编程语言,广泛应用于各行各业。以下是一些C语言经典编程实例,希望能够帮助您更好地掌握和应用C语言。
该程序可以将一个文件的内容复制到另一个文件中。它使用文件输入输出函数如fopen()、fread()和fwrite()等进行文件操作。
include <stdio.h>
include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *src, *dst;
char buffer[1024];
size_t bytes_read;
if (argc != 3) {
printf("Usage: %s <source_file> <dest_file>\n", argv[0]);
return 1;
}
src = fopen(argv[1], "rb");
if (src == NULL) {
printf("Error opening source file: %s\n", argv[1]);
return 1;
}
dst = fopen(argv[2], "wb");
if (dst == NULL) {
printf("Error opening destination file: %s\n", argv[2]);
fclose(src);
return 1;
}
while ((bytes_read = fread(buffer, 1, sizeof(buffer), src)) > 0) {
if (fwrite(buffer, 1, bytes_read, dst) != bytes_read) {
printf("Error writing to destination file\n");
fclose(src);
fclose(dst);
return 1;
}
}
fclose(src);
fclose(dst);
printf("File copied successfully\n");
return 0;
}
链表是一种常见的数据结构,可以用C语言实现各种链表操作,如添加、删除、查找等。以下是一个简单的单链表实现示例:
include <stdio.h>
include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode>data = data;
newNode>next = NULL;
return newNode;
}
void insertAtBeginning(Node **head, int data) {
Node *newNode = createNode(data);
newNode>next = *head;
*head = newNode;
}
void deleteNode(Node **head, int data) {
Node *current = *head;
Node *prev = NULL;
while (current != NULL) {
if (current>data == data) {
if (prev == NULL) {
*head = current>next;
} else {
prev>next = current>next;
}
free(current);
return;
}
prev = current;
current = current>next;
}
}
int main() {
Node *head = NULL;
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
deleteNode(&head, 2);
// Print the list
Node *current = head;
while (current != NULL) {
printf("%d ", current>data);
current = current>next;
}
printf("\n");
return 0;
}
C语言中数组是非常常用的数据结构,以下是一些常见的数组操作示例:
include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
// 遍历数组
printf("Array elements: ");
for (int i = 0; i < size; i ) {
printf("%d ", arr[i]);
}
printf("\n");
// 查找元素
int target = 3;
int index = 1;
for (int i = 0; i < size; i ) {
if (arr[i] == target) {
index = i;
break;
}
}
if (index != 1) {
printf("Element %d found at index %d\n", target, index);
} else {
printf("Element %d not found in the array\n", target);
}
// 排序数组
for (int i = 0; i < size 1; i ) {
for (int j = 0; j < size i 1; j ) {
if (arr[j] > arr[j 1]) {
int temp = arr[j];
arr[j] = arr[j 1];
arr[j 1] = temp;
}
}
}
printf("Sorted array: ");
for (int i = 0; i < size; i ) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
C语言中字符串是一种常见的数据类型,以下是一些常见的字符串操作示例:
include <stdio.h>
include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
// 字符串拼接
char combined[strlen(str1) strlen(str2) 1];
strcpy(combined, str1);
strcat(combined, " ");
strcat(combined, str2);
printf("Combined string: %s\n", combined);
// 字符串比较
if (strcmp(str1, str2) == 0) {
printf("The strings are equal\n");
} else {
printf("The strings are not equal\n");
}
// 字符串长度
printf("Length of \"%s\": %lu\n", str1, strlen(str1));
// 字符串复制
char copy[strlen(str1) 1];
strcpy(copy, str1);
printf("Copy of \"%s\": %s\n", str1, copy);
return 0;
}
以上是一些C语言经典编程实例,涵盖了文件操作、链表、数组和字符串等常见的编程任务。这些示例可以帮助您更好地理解和应用C语言的各种功能。希望对您有所帮助。