个人作业尚未归纳的代码_1


1.books.c

#include <stdio.h>
#include <string.h>

int cmp(const void *p1,const void *p2);

struct books
{
    char name[51];
    char author[21];
    char press[31];
    char date[11];
}s[505];

int main()
{
    FILE *in,*out;
    in=fopen("books.txt","r");
    out=fopen("ordered.txt","w");
    int i=0,n,flag=0;   //i是个数
    struct books hold;
    for(i=0;fscanf(in,"%s %s %s %s\n",s[i].name,s[i].author,s[i].press,s[i].date)!=EOF;i++);
    qsort(s,i,sizeof(s[2]),cmp);   //曾错 个数应该是i而非i-1;循环后得到的i特别容易搞错它代表的含义
    /*
    for(int j=0;j<i-1;j++)
    {
        flag=0;
        for(int k=0;k<i-1-j;k++)
        {
            if(strcmp(s[k].name,s[k+1].name)>0)
            {
                flag=1;
                strcpy(hold.name,s[k].name);strcpy(s[k].name,s[k+1].name);strcpy(s[k+1].name,hold.name);
                strcpy(hold.author,s[k].author);strcpy(s[k].author,s[k+1].author);strcpy(s[k+1].author,hold.author);
                strcpy(hold.press,s[k].press);strcpy(s[k].press,s[k+1].press);strcpy(s[k+1].press,hold.press);
                strcpy(hold.date,s[k].date);strcpy(s[k].date,s[k+1].date);strcpy(s[k+1].date,hold.date);
            }
        }
        if(flag==0)break;
    }
    */
    //printf("%s\n",s[0].name);   //初步测试qsort排序是否有用
    while(1)
    {
        char words[120]={0};
        scanf("%d",&n);
        if(n==0)
        {
            for(int j=0;j<i;j++)
            {
                fprintf(out,"%-50s%-20s%-30s%-10s\n",s[j].name,s[j].author,s[j].press,s[j].date);
            }
            fclose(in);fclose(out);
            return 0;
        }
        else if(n==1)
        {
            int j;
            //fscanf(stdin,"%s %s %s %s",hold.name,hold.author,hold.press,hold.date);
            fscanf(stdin,"%s %s %s %s",s[i].name,s[i].author,s[i].press,s[i].date);
            i++;

            qsort(s,i,sizeof(s[0]),cmp);   //为何没有起到排序的效果?
            /*或者用这种排序
            for(j=i-2;strcmp(s[j].name,hold.name)>0;j--)   //曾错了好多遍...使用循环时,要注意若一开始就不满足循环条件,应当如何处理。这或许也是循环的一个“弊端”
            {
                memcpy(s+j+1,s+j,sizeof(s[0]));   //曾错 memcpy后跟的是地址
            }
            if(j==i-2)memcpy(s+i-1,&hold,sizeof(s[0]));   //曾错 #若一开始不满足循环条件,则进行此操作T_T;或者在循环前(循环外)设置好初始情况(更常用)
            memcpy(s+j+1,&hold,sizeof(s[0]));
            */
            //memset(hold,'\0',sizeof(s[0]));   //memset也是...
            //printf("%s %s %s %s\n",s[i-1].name,s[i-1].author,s[i-1].press,s[i-1].date);
        }
        else if(n==2)
        {
            fscanf(stdin,"%s",words);
            for(int j=0;j<i;j++)
            {
                if(strstr(s[j].name,words)!=NULL)
                {
                    fprintf(stdout,"%-50s%-20s%-30s%-10s\n",s[j].name,s[j].author,s[j].press,s[j].date);
                }
            }
        }
        else if(n==3)
        {
            fscanf(stdin,"%s",words);
            for(int j=0;j<i;j++)
            {
                if(strstr(s[j].name,words)!=NULL)
                {
                    for(int k=j;k<i-1;k++)
                    {
                        memcpy(s+k,s+k+1,sizeof(s[0]));
                    }
                    memset(s+i-1,'\0',sizeof(s[0]));
                    i--;
                    j--;   //曾错 #超级易错#删除数据或添加数据时,数组编号要注意是否有变动
                }

            }
        }
    }

}

int cmp(const void *p1,const void *p2)   //曾错 cmp()函数要返回正负,所以是int,而非void
{
    //printf("[%-50s %-50s]\n",(*(struct books*)p1).name,(*(struct books*)p2).name);
    return strcmp((*(struct books*)p1).name,(*(struct books*)p2).name);
}

 2.books2.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <alloc.h>

typedef struct books
{
    char title[51];
    char author[21];
    char press[31];
    char date[11];
    struct books *next;
}books;

books *initbooks(FILE *in);

int main()
{
    FILE *in,*out;
    in=fopen("books.txt","r");
    out=fopen("ordered.txt","w");
    books *head=initbooks(in),*p=head,*pr=head;//pr一般在p前面

    int n=0,number,flag=0;
    while((p=p->next) != NULL)n++;//存储数据的节点个数
    p=head->next;
    books *temp1=(books *)malloc(sizeof(books));
    books *pa=(books *)malloc(sizeof(books));
    for(int i=0;i<n-1;i++)
    {
        flag=0;
        pr=head;p=head->next;pa=p->next;   //曾错 冒泡排序的初始化遇到指针,指针要从头开始移动,ε=(´ο`*)))唉
        for(int j=0;j<n-i-1;j++)
        {
            if(strcmp(p->title,pa->title)>0)   //$曾错$ 使用p->next->**时,#p->next可能已经被改动过了!#所以赋值改值的顺序很重要,=右边 和 =左边的p->next最好是改动前的数值(本质)
            {
                /*
                temp1=p->next;
                pr->next=p->next;   //temp1和p->next还是有区别的!他们的内容 一个是已输入的,一个是tnnd空的: pr->next=temp1;(X)
                p->next=temp1->next;
                temp1->next=p;
                */
                pr->next=pa;
                p->next=pa->next;
                pa->next=p;
                /*当链表碰上循环,真是满满的恶意啊……
                在循环内改变顺序或删去节点,会让原本的pr=p,p=p->next;完全混乱!
                因此,循环的《核心》在于循环中心p,和确定了p后《真实》链表中p逻辑顺序前的pr!
                */
                p=p;pr=pa;pa=p->next;
                /*
                strcpy(temp1->title,p->title);strcpy(temp1->author,p->author);strcpy(temp1->press,p->press);strcpy(temp1->date,p->date);
                strcpy(p->title,p->next->title);strcpy(p->author,p->next->author);strcpy(p->press,p->next->press);strcpy(p->date,p->next->date);
                strcpy(p->next->title,temp1->title);strcpy(p->next->author,temp1->author);strcpy(p->next->press,temp1->press);strcpy(p->next->date,temp1->date);
                */
                printf("%-50s%s\n",p->title,pa->title);
                flag=1;
            }
            else
                pr=p;p=p->next;pa=pa->next;


        }
        //printf("%d\n",i);
        if(flag==0)break;
    }

    while(~scanf("%d",&number))
    {
        pr=head;p=head->next;//循环开头先初始化要重复使用的数据
        if(number==0)
        {
            while(p!=NULL)
            {
                fprintf(out,"%-50s%-20s%-30s%-10s\n",p->title,p->author,p->press,p->date);
                p=p->next;
            }
            fclose(in);fclose(out);
            return 0;
        }
        else if(number==1)
        {
            books *newbk=(books *)malloc(sizeof(books));newbk->next=NULL;//新申请的指针中next初始化为NULL
            fscanf(stdin,"%s %s %s %s",newbk->title,newbk->author,newbk->press,newbk->date);
            //$曾错$ p!=NULL应该先判断!要不然具体含义的语句会执行错误。因此把p!=NULL或'\0'或0等放在条件判断的最前面!
            while(p!=NULL && strcmp(p->title,newbk->title)<0)   //此处循环只是为了找到正确的p位置;因此除了移动链表指针,循环内不进行任何操作
            {
                pr=p;p=p->next;//移动指针
                //printf("over\n");
            }

            pr->next=newbk;newbk->next=p;   //这样应该就可以了吧?
            /*
            if(p!=NULL)
            {
                newbk->next=p;
                pr->next=newbk;
            }
            else if(p==NULL)//考虑遍历后仍未符合条件的特殊情况
                pr->next=newbk;
            */
        }
        else if(number==2)
        {
            char finds[51];
            fscanf(stdin,"%s",finds);
            while(p!=NULL)
            {
                if(strstr(p->title,finds)!=NULL)
                {
                    fprintf(stdout,"%-50s%-20s%-30s%-10s\n",p->title,p->author,p->press,p->date);
                }
                p=p->next;   //曾错 每次循环要注意在结尾改变“检查值”,即移动指针
            }
        }
        else if(number==3)
        {
            char finds[51];
            fscanf(stdin,"%s",finds);
            while(p!=NULL)
            {
                if(strstr(p->title,finds)!=NULL)
                {
                    pr->next=p->next;   //搞清楚pr的含义:《有效》链表中正在处理的p的上一个节点,因此进行链表删除时可能用上删去的节点而致错
                    //pr=p;free(pr);   //删除一个节点后,pr值仍不变
                }
                else
                    pr=pr->next;
                p=p->next;
            }
        }
    }
    return 0;
}

books *initbooks(FILE *in)
{
    books *p=(books *)malloc(sizeof(books));
    books *temp=p;
    temp->next=NULL;
    //for(books *a=(books *)malloc(sizeof(books));
    //~fscanf(in,"%s %s %s %s\n",a->title,a->author,a->press,a->date);
    //a->next=NULL,temp->next=a,temp=a,books *a=(books *)malloc(sizeof(books)));

    while(1)
    {
        books *a=(books *)malloc(sizeof(books));
        if(fscanf(in,"%s %s %s %s\n",a->title,a->author,a->press,a->date)!=EOF)
        {
            a->next=NULL;
            temp->next=a;
            temp=a;
        }
        else
            return p;
    }

}

3.sort.c

#include <stdio.h>
#include <string.h>

int cmp(const void *p1,const void *p2);
struct people
{
    char name[30];
    char tel[12];
}s[101];

int main()
{
    int n,flag[100]={0};
    char repeat[10]={0};
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%s %s",s[i].name,s[i].tel);
    }

    for(int i=0;i<n-1;i++)
    {
        flag[i]+=1;
        for(int j=i+1;j<n;j++)
        {
            if(strcmp(s[i].name,s[j].name)==0 && strcmp(s[i].tel,s[j].tel)!=0)   //记住此处的用法
            {
                sprintf(repeat,"_%c",flag[i]+'0');
                strcat(s[j].name,repeat);
                flag[j]+=1;flag[i]++;
            }
        }
    }

    qsort(s,n,sizeof(s[0]),cmp);   //曾错 sizeof(s[0])而不是sizeof(s)
    printf("%s %s\n",s[0].name,s[0].tel);
    for(int i=1;i<n;i++)
    {
        if(strcmp(s[i].name,s[i-1].name)!=0)   //重复名字的处理位置可以在输入时,也可以在输出时,这里用得妙
        {
            printf("%s %s\n",s[i].name,s[i].tel);
        }
    }
    return 0;
}

int cmp(const void *p1,const void *p2)
{
    return strcmp((*(struct people *)p1).name,(*(struct people *)p2).name);   //类型转换要加上struct
}

4.encrypt.c

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *in,*out;
    in=fopen("encrypt.txt","r");
    out=fopen("output.txt","w");
    char trans[27]={0},key[51];
    int flag[27]={0},i=0,j;
    char c,hold;
    gets(key);
    for(i=0,j=0;i<strlen(key);i++)   //曾错 j++的位置应是判断成功的那个地方
    {
        if(flag[key[i]-'a']==0)
        {
            trans[j++]=key[i];
            flag[key[i]-'a']=1;   //曾错 -'a'
        }
    }
    for(char k='z';j<26 && k>='a';k--)   //曾错
    {
        if(flag[k-'a']==0)
        {
            trans[j++]=k;
        }
    }

    while((c=fgetc(in))!=EOF)
    {
        if(c>='a' && c<='z')
        {
            hold=trans[c-'a'];
        }
        else
        {
            hold=c;
        }
        fputc(hold,out);
        //fputc(hold,stdout);
    }

    fclose(in);
    fclose(out);
    return 0;
}

5.replace.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void sstrlwr(char *s);   //judge系统没有strlwr()函数...

int main()
{
    FILE *in,*out;
    in=fopen("filein.txt","r");
    out=fopen("fileout.txt","w");
    char str[100],rep[100],strl[100],repl[100];   //一个数组用于转化为小写,从而能够检测出相对位置
    char save[1024],*p=save,savel[1024],*pl=savel,resave[1024];   //在原储存字符串的数组上直接修改有时会出现问题,因此定义另一个数组逐步储存修改后的字符串
    int len;
    gets(str);gets(rep);

    len=strlen(str);
    strcpy(strl,str);strcpy(repl,rep);   //曾错 strcpy!=strcmp
    sstrlwr(strl);sstrlwr(repl);   //曾错 strlwr()用法
    while(fgets(save,1024,in)!=NULL)
    {
        memset(resave,'\0',1024);
        strcpy(savel,save);sstrlwr(savel);
        p=save;pl=savel;

        while(strstr(pl,strl)!=NULL)   //如果没有满足条件呢,又该执行什么命令?这就是while的误用了
        {
            *(p+(strstr(pl,strl)-pl))='\0';   //曾错 减反了->乱码输出
            strcat(resave,p);   //不能在save[]上直接改,可能替换的字符会超界(替换结果覆盖等)  *曾错:在循环中要用strcat而不是strcpy(进行第二次循环时,会把第一次的结果通过copy覆盖住,但是strcat就会继续进行;因此写while循环时,注意考虑:哪些是要每次重复使用【也就是暂时储存数值的】量,哪些是在循环中呈一条线不断推进的)
            strcat(resave,rep);

            p=p+(strstr(pl,strl)-pl)+len-1;
            //p++;*(p-1)='\0';   //+1 -> 若没有+1,下个字符串的开头则会是'\0'
            *p='\0';p++;

            pl=strstr(pl,strl)+len;   //曾错 要加上len!!!->从而跳过已经检测到的字符串,避免无限循环
            printf("-%d\n",(int)(pl-savel));
        }

        if(*p!='\0')strcat(resave,p);
        fputs(resave,out);
    }

    if(fclose(in)==EOF && fclose(out)==EOF)printf("wrong!\n");
    //fclose(in);
    //fclose(out);
    return 0;
}

void sstrlwr(char *s)
{
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        if(s[i]>='A' && s[i]<='a')
        {
            s[i]=s[i]-'A'+'a';
        }
    }
}

6.子数组

#include <stdio.h>
#include <string.h>

int main()
{
    int a[100001]={0},b[100001]={0},n,m,flag=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int j=0;j<m;j++)
        scanf("%d",&b[j]);

    for(int j=0;j<m;j++)
    {
        //在for循环前加上辅助判断条件的if(),防止出现此类情况
        if(flag>=n && j==m-1)   //也有可能判断完全正确后遇到此情况。。。且判断在循环中放的位置也很重要。。
        {
            printf("NIE\n");
            return 0;
        }
        //注意可能没有进入二重循环的特殊情况(此时要在二重循环前加if判断)
        for(int i=flag;i<n;i++)   //若flag>=n,甚至都不会进入循环,执行里面的功能!这就是循环的另一大“缺陷”
        {
            if(b[j]==a[i])
            {
                flag=i+1;break;
            }
            else if((i==n-1 && b[j]!=a[i]))
            {
                printf("NIE\n");
                return 0;
            }
        }

    }
    printf("TAK\n");
    return 0;
}

this is a test block.

wow, the blank can be expanded when I press Enter.

test part
where is my abstract?

 

\vee \bullet \Theta \Gamma \Gamma \Delta \sum

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值