在计算机科学的世界里,C语言因其高效、灵活和接近硬件的特性,一直被广大程序员视为入门的基石。面对上机考试,如何通过实战演练来提升自己的C语言编程能力,成为许多学习者的关注焦点。本文将为你提供一系列实战演练的题目,帮助你轻松应对上机考试。
一、基础语法与数据类型
1.1 数据类型转换
题目描述: 编写一个C语言程序,实现将一个整数转换为字符串的功能。
代码示例:
#include <stdio.h>
void intToStr(int num, char *str) {
int i = 0, sign = 0;
if (num < 0) {
sign = 1;
num = -num;
}
do {
str[i++] = (num % 10) + '0';
} while (num /= 10);
if (sign) {
str[i++] = '-';
}
str[i] = '\0';
for (int j = 0; j < i / 2; j++) {
char temp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = temp;
}
}
int main() {
int num = -12345;
char str[20];
intToStr(num, str);
printf("The integer %d converts to string: %s\n", num, str);
return 0;
}
1.2 变量作用域
题目描述: 分析以下代码,解释变量a的作用域。
int a = 10;
if (a > 0) {
int a = 20;
printf("a = %d\n", a);
}
printf("a = %d\n", a);
解答: 在C语言中,变量的作用域是由其定义的位置决定的。在上述代码中,a的作用域分为两个部分:全局作用域和局部作用域。在if语句块内,a被重新定义,其作用域仅限于该块内部。因此,当执行printf("a = %d\n", a);时,输出的是全局作用域中的a的值,即10。
二、控制结构
2.1 循环结构
题目描述: 编写一个C语言程序,计算1到100之间所有偶数的和。
代码示例:
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
printf("The sum of even numbers from 1 to 100 is: %d\n", sum);
return 0;
}
2.2 选择结构
题目描述: 编写一个C语言程序,根据用户输入的年龄判断其是否成年。
代码示例:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}
return 0;
}
三、函数与递归
3.1 函数定义
题目描述: 编写一个C语言程序,定义一个函数计算两个整数的最大公约数。
代码示例:
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("The GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2));
return 0;
}
3.2 递归函数
题目描述: 编写一个C语言程序,使用递归函数计算斐波那契数列的第n项。
代码示例:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("The %d-th Fibonacci number is: %d\n", n, fibonacci(n));
return 0;
}
四、指针与数组
4.1 指针操作
题目描述: 编写一个C语言程序,使用指针交换两个整数的值。
代码示例:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
4.2 二维数组
题目描述: 编写一个C语言程序,计算一个3x3矩阵的主对角线元素之和。
代码示例:
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += matrix[i][i];
}
printf("The sum of the main diagonal elements is: %d\n", sum);
return 0;
}
五、文件操作
5.1 文件读写
题目描述: 编写一个C语言程序,将一个文本文件的内容复制到另一个文件中。
代码示例:
#include <stdio.h>
int main() {
FILE *fp1, *fp2;
char ch;
fp1 = fopen("source.txt", "r");
if (fp1 == NULL) {
printf("Cannot open source.txt\n");
return 1;
}
fp2 = fopen("destination.txt", "w");
if (fp2 == NULL) {
printf("Cannot open destination.txt\n");
fclose(fp1);
return 1;
}
while ((ch = fgetc(fp1)) != EOF) {
fputc(ch, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
5.2 文件信息
题目描述: 编写一个C语言程序,获取一个文件的长度。
代码示例:
#include <stdio.h>
int main() {
FILE *fp;
long pos, len;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Cannot open example.txt\n");
return 1;
}
fseek(fp, 0, SEEK_END);
pos = ftell(fp);
len = pos;
printf("The length of example.txt is: %ld\n", len);
fclose(fp);
return 0;
}
六、结构体与联合体
6.1 结构体定义
题目描述: 编写一个C语言程序,定义一个结构体表示学生信息,并创建一个学生实例。
代码示例:
#include <stdio.h>
typedef struct {
char name[50];
int age;
float score;
} Student;
int main() {
Student stu;
strcpy(stu.name, "Alice");
stu.age = 20;
stu.score = 92.5;
printf("Student name: %s\n", stu.name);
printf("Student age: %d\n", stu.age);
printf("Student score: %.2f\n", stu.score);
return 0;
}
6.2 联合体使用
题目描述: 编写一个C语言程序,定义一个联合体表示一个日期,并使用该联合体存储一个日期值。
代码示例:
#include <stdio.h>
typedef union {
int year;
int month;
int day;
} Date;
int main() {
Date date;
date.year = 2022;
date.month = 9;
date.day = 15;
printf("The date is: %d-%d-%d\n", date.year, date.month, date.day);
return 0;
}
七、指针与动态内存分配
7.1 动态内存分配
题目描述: 编写一个C语言程序,使用malloc和free函数动态分配内存,并使用该内存存储一个字符串。
代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str;
str = (char *)malloc(50 * sizeof(char));
if (str == NULL) {
printf("Memory allocation failed\n");
return 1;
}
strcpy(str, "Hello, world!");
printf("The string is: %s\n", str);
free(str);
return 0;
}
7.2 指针数组
题目描述: 编写一个C语言程序,定义一个指针数组,并使用该数组存储多个字符串。
代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *arr[3];
arr[0] = "Hello";
arr[1] = "world";
arr[2] = "!";
for (int i = 0; i < 3; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
八、文件操作与目录管理
8.1 文件操作
题目描述: 编写一个C语言程序,使用fopen和fclose函数打开和关闭一个文件。
代码示例:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("Cannot open example.txt\n");
return 1;
}
fprintf(fp, "This is a test.\n");
fclose(fp);
return 0;
}
8.2 目录管理
题目描述: 编写一个C语言程序,使用opendir和closedir函数列出指定目录下的所有文件。
代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main() {
DIR *dir;
struct dirent *entry;
dir = opendir(".");
if (dir == NULL) {
printf("Cannot open directory\n");
return 1;
}
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
return 0;
}
九、网络编程
9.1 套接字编程
题目描述: 编写一个C语言程序,使用TCP套接字实现一个简单的客户端-服务器模型。
代码示例:
// 服务器端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
char buffer[1024];
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
printf("Socket creation failed\n");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(8080);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
printf("Bind failed\n");
return 1;
}
if (listen(server_fd, 5) == -1) {
printf("Listen failed\n");
return 1;
}
while (1) {
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_fd == -1) {
printf("Accept failed\n");
continue;
}
memset(buffer, 0, sizeof(buffer));
read(client_fd, buffer, sizeof(buffer));
printf("Received: %s\n", buffer);
write(client_fd, "Hello, client!", 17);
close(client_fd);
}
close(server_fd);
return 0;
}
// 客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[1024];
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd == -1) {
printf("Socket creation failed\n");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(8080);
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
printf("Connect failed\n");
return 1;
}
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "Hello, server!");
write(client_fd, buffer, strlen(buffer));
memset(buffer, 0, sizeof(buffer));
read(client_fd, buffer, sizeof(buffer));
printf("Received: %s\n", buffer);
close(client_fd);
return 0;
}
9.2 网络编程库
题目描述: 编写一个C语言程序,使用libcurl库从网络上获取一个网页内容。
代码示例:
#include <stdio.h>
#include <curl/curl.h>
int main() {
CURL *curl;
CURLcode res;
char buffer[1024];
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](void *contents, size_t size, size_t nmemb, void *userp) {
((char **)userp)[0] = malloc(size * nmemb);
memcpy(((char **)userp)[0], contents, size * nmemb);
return size * nmemb;
});
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
printf("%s\n", buffer);
free(buffer);
return 0;
}
十、总结
通过以上实战演练的题目,相信你已经对C语言编程有了更深入的了解。在实际编程过程中,不断练习和总结是非常重要的。希望这些题目能够帮助你轻松应对上机考试,祝你学习顺利!
