实验12_1_初识ASCII码文件

已知一个ASCII码文件,文件名为dict.dic,该文件中只包含ASCII码中的字符,即可对应整数0—127。我们将ASCII码中的字符分为4类,第一类为大写字母“A—Z”、第二类为小写字母“a—z”、第三类为数字字符“0—9”、第四类为其他字符“!@#$%^&*” 等等(不属于前三类的字符即为第四类)。
要求:
1.统计出这四类字符在该文件中的数量。
2.统计出该文件的行数、行最大长度、行最小长度。这里要注意,虽然文件的换行符确实为一个字符,ASCII码为10,但在统计行长度时,文件中的换行符并不统计在内。
3.输出具体的大小写字母的统计信息,每行一个字母。
4.处理完成后,关闭文件。
内容提示:在本题对文件的操作内容中,会用到三个新的C语言文件操作函数,一种新的打开文件的方式,如下:
1.FILE *fp=fopen("file.txt","r");//fp即为文件指针,"file.txt"为待打开的文件名,此时应与该程序在一个目录下,"r"为以只读方式打开ASCII码文件。
2.int ch=fgetc(fp);//从fp指向的文件中读取一个字节(字符),存入变量ch内。
3.fclose(fp);//关闭fp指向的文件。

#include<stdio.h>
#include<stdlib.h>
void printftask1(FILE *fp,int capital,int lowercase,int digit,int others)
{
    printf("Task1:\n"
            "capital: %d\n"
            "lowercase: %d\n"
            "digit: %d\n"
            "others: %d\n",capital,lowercase,digit, others);
}
void printftask2(int line,int max,int min)
{
    printf("Task2:\n"
            "line: %d\n"
            "%d characters in max line.\n"
            "%d characters in min line.\n",line,max,min);
}
void printftask3(int capital[],int lowcase[])
{
    int i;
    char ch1='A',ch2='a';
    printf("Task3:\n");
    printf("CAPITAL:\n"); 
    
    for(i=0;i<26;i++)
    {
        printf("%c:%d\n",ch1,capital[i]);
        ch1++;
    }
    printf("LOWERCASE:\n");
    for(i=0;i<26;i++)
    {
        printf("%c:%d\n",ch2,lowcase[i]);
        ch2++;
    }

}
void task1(FILE *fp)
{   
    rewind(fp);
    int ch=fgetc(fp);
    int capital=0,lowercase=0,digit=0,others=0;

    while(!feof(fp))
    {
        if(ch>='A'&&ch<='Z')
        {
            capital++;
        }
        else
        if(ch>='a'&&ch<='z')
        {
            lowercase++;
        }
        else
        if(ch>='0'&&ch<='9')
        {
            digit++;
        }
        else
        {
            others++;
        }
        ch=fgetc(fp);

    }
    printftask1(fp,capital,lowercase,digit,others);
}
void task2(FILE *fp)
{   
    rewind(fp);
    int ch=fgetc(fp);
    int max=0,min,number=0,line=0,ch1;
    if(ch=='\n')
    {
        min=0;
        line++;
        ch1=ch;
        ch=fgetc(fp);
    }
    else
    {
        ch1=ch;
        ch=fgetc(fp);
        number++;
    }
    while(!feof(fp))
    {
        if(ch!='\n')
        {
            number++;
            ch1=ch;
            ch=fgetc(fp);
        }
        else
        {
            if(ch1=='\n')
            {
                min=0;
            }
            else
            {
                if(number>max)
                {
                    max=number;
                    if(line==0)
                    min=number;
                }
                if(number<min)
                {
                    min=number;
                }
                number=0;
            }
            ch1=ch;
            ch=fgetc(fp);
            line++;
        }
    }
    printftask2(line,max,min);
}
void task3(FILE *fp)
{
    int ch=fgetc(fp);
    int capital[26]={0};
    int lowercase[26]={0};

    while(!feof(fp))
    {
        if(ch>='A'&&ch<='Z')
        {
            capital[ch-'A']++;
        }
        if(ch>='a'&&ch<='z')
        {
            lowercase[ch-'a']++;
        }
        ch=fgetc(fp);
    }
    printftask3(capital,lowercase);
}
int main()
{
    FILE *fp = fopen("dict.dic","r");

    int task;
    scanf("%d",&task);

    if(task==1)
    {
        task1(fp);
    }
    else
    if(task==2)
    {
        task2(fp);
    }
    else
    if(task==3)
    {
        task3(fp);
    }

    fclose(fp);

    return 0;

}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题目是要求我们实现链表的基本操作,包括创建链表、插入节点、删除节点、遍历链表等。以下是一个简单的链表实现代码示例: ```c++ #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; // 创建链表 ListNode* createList(int nums[], int n) { if (n == 0) return NULL; ListNode *head = new ListNode(nums[0]); ListNode *cur = head; for (int i = 1; i < n; i++) { cur->next = new ListNode(nums[i]); cur = cur->next; } return head; } // 插入节点 void insertNode(ListNode *&head, int pos, int val) { if (pos < 0) return; if (pos == 0) { ListNode *newNode = new ListNode(val); newNode->next = head; head = newNode; return; } ListNode *cur = head; while (cur && pos > 1) { cur = cur->next; pos--; } if (cur) { ListNode *newNode = new ListNode(val); newNode->next = cur->next; cur->next = newNode; } } // 删除节点 void deleteNode(ListNode *&head, int pos) { if (pos < 0) return; if (pos == 0) { ListNode *delNode = head; head = head->next; delete delNode; return; } ListNode *cur = head; while (cur && pos > 1) { cur = cur->next; pos--; } if (cur && cur->next) { ListNode *delNode = cur->next; cur->next = delNode->next; delete delNode; } } // 遍历链表 void traverseList(ListNode *head) { while (head) { cout << head->val << " "; head = head->next; } cout << endl; } int main() { int nums[] = {1, 2, 3, 4, 5}; int n = sizeof(nums) / sizeof(int); ListNode *head = createList(nums, n); traverseList(head); insertNode(head, 2, 6); traverseList(head); deleteNode(head, 3); traverseList(head); return 0; } ``` 在上面的代码,我们定义了一个 `ListNode` 结构体作为链表节点,包括节点值 `val` 和指向下一个节点的指针 `next`。同时,我们实现了创建链表、插入节点、删除节点和遍历链表等基本操作。在使用链表时,我们可以先通过 `createList` 函数创建一个链表,然后对链表进行插入、删除和遍历操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值