安卓版的c语言记事本程序代码测试通过

安卓版的c语言编译器如何实现简单的记事本程序?

以下是一个使用 C 语言在安卓环境中实现简单记事本程序的基本示例,此示例只是一个概念框架,实际在安卓中开发可能需要更多的安卓特定的编程知识和库。

c
复制
正确例题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 最大输入长度
#define MAX_INPUT_LENGTH 1024

void writeToFile(char *content) {
FILE *file = fopen(“notepad.txt”, “a”);
if (file == NULL) {
printf(“无法打开文件\n”);
return;
}
fprintf(file, “%s\n”, content);
fclose(file);
}

void readFromFile() {
FILE *file = fopen(“notepad.txt”, “r”);
if (file == NULL) {
printf(“无法打开文件\n”);
return;
}
char line[MAX_INPUT_LENGTH];
while (fgets(line, MAX_INPUT_LENGTH, file)!= NULL) {
printf(“%s”, line);
}
fclose(file);
}

int main() {
int choice;
char input[MAX_INPUT_LENGTH];

printf("1. 写入\n");
printf("2. 读取\n");
printf("3. 退出\n");
scanf("%d", &choice);

switch (choice) {
    case 1:
        printf("请输入要写入的内容:");
        scanf(" %[^\n]", input);
        writeToFile(input);
        break;
    case 2:
        readFromFile();
        break;
    case 3:
        return 0;
    default:
        printf("无效的选择\n");
        break;
}

return 0;

}

c
复制
正确例题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 最大输入长度
#define MAX_INPUT_LENGTH 1024

// 存储记事本内容的缓冲区
char content[MAX_INPUT_LENGTH * 100];

// 写入内容到缓冲区
void writeContent(char *newContent) {
strcat(content, newContent);
}

// 从缓冲区读取内容
void readContent() {
printf(“%s”, content);
}

// 复制指定范围内的内容
void copyContent(int start, int end) {
// 此处简单实现为打印要复制的内容
printf(“复制的内容: “);
for (int i = start; i <= end; i++) {
printf(”%c”, content[i]);
}
printf(“\n”);
}

// 粘贴内容到指定位置
void pasteContent(char *pastedText, int position) {
// 简单实现,将粘贴的文本插入到指定位置
char temp[MAX_INPUT_LENGTH * 100];
strncpy(temp, content, position);
strcat(temp, pastedText);
strcat(temp, content + position);
strcpy(content, temp);
}

// 查找指定字符串
int findContent(char *searchString) {
char *found = strstr(content, searchString);
if (found) {
return found - content;
}
return -1;
}

// 替换指定字符串
void replaceContent(char *oldString, char *newString) {
char *found = content;
while ((found = strstr(found, oldString))!= NULL) {
char temp[MAX_INPUT_LENGTH * 100];
int foundPosition = found - content;
strncpy(temp, content, foundPosition);
strcat(temp, newString);
strcat(temp, found + strlen(oldString));
strcpy(content, temp);
found += strlen(newString);
}
}

int main() {
int choice;
char input[MAX_INPUT_LENGTH];
int start, end, position;

while (1) {
    printf("1. 写入\n");
    printf("2. 读取\n");
    printf("3. 复制\n");
    printf("4. 粘贴\n");
    printf("5. 查找\n");
    printf("6. 替换\n");
    printf("7. 继续\n");
    printf("8. 退出\n");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("请输入要写入的内容:");
            scanf(" %[^\n]", input);
            writeContent(input);
            break;
        case 2:
            readContent();
            break;
        case 3:
            printf("请输入复制的起始位置和结束位置: ");
            scanf("%d %d", &start, &end);
            copyContent(start, end);
            break;
        case 4:
            printf("请输入要粘贴的内容和粘贴位置: ");
            scanf(" %[^\n] %d", input, &position);
            pasteContent(input, position);
            break;
        case 5:
            printf("请输入要查找的内容: ");
            scanf(" %[^\n]", input);
            int foundPosition = findContent(input);
            if (foundPosition!= -1) {
                printf("找到于位置: %d\n", foundPosition);
            } else {
                printf("未找到\n");
            }
            break;
        case 6:
            printf("请输入要替换的旧内容和新内容: ");
            scanf(" %[^\n] %[^\n]", input, input + strlen(input) + 1);
            replaceContent(input, input + strlen(input) + 1);
            break;
        case 7:
            // 可以在此添加继续处理的逻辑,目前先不做具体操作
            break;
        case 8:
            return 0;
        default:
            printf("无效的选择\n");
            break;
    }
}

return 0;

}

c
复制
正确例题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 最大输入长度
#define MAX_INPUT_LENGTH 1024

// 存储记事本内容的缓冲区
char content[MAX_INPUT_LENGTH * 100];

// 写入内容到缓冲区
void writeContent(char *newContent) {
strcat(content, newContent);
}

// 从缓冲区读取内容
void readContent() {
printf(“%s”, content);
}

// 复制指定范围内的内容
void copyContent(int start, int end) {
// 此处简单实现为打印要复制的内容
printf(“复制的内容: “);
for (int i = start; i <= end; i++) {
printf(”%c”, content[i]);
}
printf(“\n”);
}

// 粘贴内容到指定位置
void pasteContent(char *pastedText, int position) {
// 简单实现,将粘贴的文本插入到指定位置
char temp[MAX_INPUT_LENGTH * 100];
strncpy(temp, content, position);
strcat(temp, pastedText);
strcat(temp, content + position);
strcpy(content, temp);
}

// 查找指定字符串
int findContent(char *searchString) {
char *found = strstr(content, searchString);
if (found) {
return found - content;
}
return -1;
}

// 替换指定字符串
void replaceContent(char *oldString, char *newString) {
char *found = content;
while ((found = strstr(found, oldString))!= NULL) {
char temp[MAX_INPUT_LENGTH * 100];
int foundPosition = found - content;
strncpy(temp, content, foundPosition);
strcat(temp, newString);
strcat(temp, found + strlen(oldString));
strcpy(content, temp);
found += strlen(newString);
}
}

int main() {
int choice;
char input[MAX_INPUT_LENGTH];
int start, end, position;

while (1) {
    printf("1. 写入\n");
    printf("2. 读取\n");
    printf("3. 复制\n");
    printf("4. 粘贴\n");
    printf("5. 查找\n");
    printf("6. 替换\n");
    printf("7. 继续\n");
    printf("8. 退出\n");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("请输入要写入的内容:");
            scanf(" %[^\n]", input);
            writeContent(input);
            break;
        case 2:
            readContent();
            break;
        case 3:
            printf("请输入复制的起始位置和结束位置: ");
            scanf("%d %d", &start, &end);
            copyContent(start, end);
            break;
        case 4:
            printf("请输入要粘贴的内容和粘贴位置: ");
            scanf(" %[^\n] %d", input, &position);
            pasteContent(input, position);
            break;
        case 5:
            printf("请输入要查找的内容: ");
            scanf(" %[^\n]", input);
            int foundPosition = findContent(input);
            if (foundPosition!= -1) {
                printf("找到于位置: %d\n", foundPosition);
            } else {
                printf("未找到\n");
            }
            break;
        case 6:
            printf("请输入要替换的旧内容和新内容: ");
            scanf(" %[^\n] %[^\n]", input, input + strlen(input) + 1);
            replaceContent(input, input + strlen(input) + 1);
            break;
        case 7:
            // 继续处理的逻辑
            printf("请输入继续执行的操作(1 - 写入,2 - 读取,3 - 复制,4 - 粘贴,5 - 查找,6 - 替换): ");
            scanf("%d", &choice);
            switch (choice) {
                case 1:
                    printf("请输入要写入的内容:");
                    scanf(" %[^\n]", input);
                    writeContent(input);
                    break;
                case 2:
                    readContent();
                    break;
                case 3:
                    printf("请输入复制的起始位置和结束位置: ");
                    scanf("%d %d", &start, &end);
                    copyContent(start, end);
                    break;
                case 4:
                    printf("请输入要粘贴的内容和粘贴位置: ");
                    scanf(" %[^\n] %d", input, &position);
                    pasteContent(input, position);
                    break;
                case 5:
                    printf("请输入要查找的内容: ");
                    scanf(" %[^\n]", input);
                    foundPosition = findContent(input);
                    if (foundPosition!= -1) {
                        printf("找到于位置: %d\n", foundPosition);
                    } else {
                        printf("未找到\n");
                    }
                    break;
                case 6:
                    printf("请输入要替换的旧内容和新内容: ");
                    scanf(" %[^\n] %[^\n]", input, input + strlen(input) + 1);
                    replaceContent(input, input + strlen(input) + 1);
                    break;
                default:
                    printf("无效的选择\n");
                    break;
            }
            break;
        case 8:
            return 0;
        default:
            printf("无效的选择\n");
            break;
    }
}

return 0;

}

c
复制
正确例题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 最大输入长度
#define MAX_INPUT_LENGTH 1024

// 存储记事本内容的缓冲区
char content[MAX_INPUT_LENGTH * 100];

// 写入内容到缓冲区
void writeContent(char *newContent) {
strcat(content, newContent);
}

// 从缓冲区读取内容
void readContent() {
printf(“%s”, content);
}

// 复制指定范围内的内容
void copyContent(int start, int end) {
// 此处简单实现为打印要复制的内容
printf(“复制的内容: “);
for (int i = start; i <= end; i++) {
printf(”%c”, content[i]);
}
printf(“\n”);
}

// 粘贴内容到指定位置
void pasteContent(char *pastedText, int position) {
// 简单实现,将粘贴的文本插入到指定位置
char temp[MAX_INPUT_LENGTH * 100];
strncpy(temp, content, position);
strcat(temp, pastedText);
strcat(temp, content + position);
strcpy(content, temp);
}

// 查找指定字符串
int findContent(char *searchString) {
char *found = strstr(content, searchString);
if (found) {
return found - content;
}
return -1;
}

// 替换指定字符串
void replaceContent(char *oldString, char *newString) {
char *found = content;
while ((found = strstr(found, oldString))!= NULL) {
char temp[MAX_INPUT_LENGTH * 100];
int foundPosition = found - content;
strncpy(temp, content, foundPosition);
strcat(temp, newString);
strcat(temp, found + strlen(oldString));
strcpy(content, temp);
found += strlen(newString);
}
}

int main() {
int choice;
char input[MAX_INPUT_LENGTH];
int start, end, position;
char newString[MAX_INPUT_LENGTH]; // 新增用于存储新输入的替换字符串

while (1) {
    printf("1. 写入\n");
    printf("2. 读取\n");
    printf("3. 复制\n");
    printf("4. 粘贴\n");
    printf("5. 查找\n");
    printf("6. 替换\n");
    printf("7. 继续\n");
    printf("8. 退出\n");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("请输入要写入的内容:");
            scanf(" %1023[^\n]", input);  // 添加最大长度限制
            writeContent(input);
            break;
        case 2:
            readContent();
            break;
        case 3:
            printf("请输入复制的起始位置和结束位置: ");
            scanf("%d %d", &start, &end);
            copyContent(start, end);
            break;
        case 4:
            printf("请输入要粘贴的内容和粘贴位置: ");
            scanf(" %1023[^\n] %d", input, &position);  // 添加最大长度限制
            pasteContent(input, position);
            break;
        case 5:
            printf("请输入要查找的内容: ");
            scanf(" %1023[^\n]", input);  // 添加最大长度限制
            int foundPosition = findContent(input);
            if (foundPosition!= -1) {
                printf("找到于位置: %d\n", foundPosition);
            } else {
                printf("未找到\n");
            }
            break;
        case 6:
            printf("请输入要替换的旧内容和新内容: ");
            scanf(" %1023[^\n] %1023[^\n]", input, newString);  // 添加最大长度限制,并使用新的字符数组存储新字符串
            replaceContent(input, newString);
            break;
        case 7:
            // 继续处理的逻辑
            printf("请输入继续执行的操作(1 - 写入,2 - 读取,3 - 复制,4 - 粘贴,5 - 查找,6 - 替换): ");
            scanf("%d", &choice);
            switch (choice) {
                case 1:
                    printf("请输入要写入的内容:");
                    scanf(" %1023[^\n]", input);  // 添加最大长度限制
                    writeContent(input);
                    break;
                case 2:
                    readContent();
                    break;
                case 3:
                    printf("请输入复制的起始位置和结束位置: ");
                    scanf("%d %d", &start, &end);
                    copyContent(start, end);
                    break;
                case 4:
                    printf("请输入要粘贴的内容和粘贴位置: ");
                    scanf(" %1023[^\n] %d", input, &position);  // 添加最大长度限制
                    pasteContent(input, position);
                    break;
                case 5:
                    printf("请输入要查找的内容: ");
                    scanf(" %1023[^\n]", input);  // 添加最大长度限制
                    foundPosition = findContent(input);
                    if (foundPosition!= -1) {
                        printf("找到于位置: %d\n", foundPosition);
                    } else {
                        printf("未找到\n");
                    }
                    break;
                case 6:
                    printf("请输入要替换的旧内容和新内容: ");
                    scanf(" %1023[^\n] %1023[^\n]", input, newString);  // 添加最大长度限制,并使用新的字符数组存储新字符串
                    replaceContent(input, newString);
                    break;
                default:
                    printf("无效的选择\n");
                    break;
            }
            break;
        case 8:
            return 0;
        default:
            printf("无效的选择\n");
            break;
    }
}

return 0;

}

  • 34
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EYYLTV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值