C语言实验八(共5题)

代码个人制作,如需解析请私信。

8-1

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

int main()
{
    char s[81];
    gets(s);
    char *p;
    p=s;
    int i;
    for(i=0;*(p+i)!=NULL;i++){
        if(*(p+i)>=65&&*(p+i)<=90)  *(p+i)=90-(*(p+i)-65);
        putchar(*(p+i));
    }

    return 0;
}

8-2

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

int main()
{
    int k=0,r=0;
    int i,t;
    for(i=0;i<4;i++){
        r=0;
        k=0;
        for(t=7;t>=0;t--){
            k=getchar()-48;
            r+=k*pow(2,t);
        }
        printf("%d",r);
        if(i!=3) printf("%c",'.');
    }


    return 0;
}

8-3

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

int main()
{
    int number[10];
    int total=0;
    int i=0,t=0;
    for(i=0;i<10;i++){
        scanf("%d",&number[i]);
        total+=number[i];
    }

    if(total>50) exit(0);
    for(i=1;i<10;i++){
        if(number[i]!=0) t++;
    }
    if(t==0) exit(0);

    for(i=1;i<10;i++){
        if(number[i]!=0){
            printf("%d",i);
            number[i]--;
            goto L1;
        } 
    }//首位输出
    L1:
    i=0;
    do
    {
        if(number[i]>0) {printf("%d",i);number[i]--;}
        else i++;
    } while (i<10);
    return 0;
}

8-4

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

int main()
{
    int N;
    scanf("%d",&N);
    if(N<=0) exit(0);

    char (*p)[80]=(char (*)[80])malloc(N*(80*sizeof(char)));

    int i=0;
    fflush(stdin);
    for(i=0;i<N;i++){
        scanf("%s",p+i);
    }//输入字符串数据

    int t=0;
    for(i=0;i<N;i++){
        if(strlen(p+i)>strlen(p+t)) t=i;
    }//扫描抓取最长数据

    printf("The longest is: %s",*(p+t));

    return 0;
}

8-5

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

void swap(char *a,char *b){
    char c[11];
    strcpy(c,a);
    strcpy(a,b);
    strcpy(b,c);
}//定义交换函数;

char word[20][11];//在外部定义字符数组,以初始化为\0;

int main()
{
    char c,ch;
    int i=0,t=0,k=0,n=0;


    do{
        fgets(word[i],11,stdin);
        for(t=0;t<11;t++){
            if(word[i][t]=='\n') goto L1;
        }//检查缓存区是否还有字符;

        do
        {
            ch=getchar();
        } while (ch!='\0'&&ch!='\n');
        //清除读取输入后缓存区剩下的字符;

        L1:
        for(t=0;t<11;t++){
            if(word[i][t]=='\n') word[i][t]='\0';
        }//清除fgets所读入的换行符;

        strncpy(&c,word[i],1);//读入首字符进c,以识别#号;
        i++;
        n++;
    }while (i<21&&c!=35);
    n--;
    
    //以上为输入模块


    int l=0;
    for(i=0;i<n;i++){
        for(k=97;k<123;k++){
            if(strchr(word[i],k)!=NULL) l++;
        }
    }
    if(l==0) exit(0);
    
    //以上为空集检查;

    
    for(i=0;i<n;i++){
        l=0;
        for(k=97;k<123;k++){
            if(strchr(word[i],k)!=NULL) l++;
        }
        if(l==0){
            for(t=i;t<n;t++){
                swap(word[t],word[t+1]);
            }
            n--;
            i--;
        }
    }
    //以上为空行检查,遇空行则移至最后一行并丢弃;

    i=0;k=1;
    for(;k!=0;){
        k=0;
        for(i=0;i<n-1;i++){
            
            if (strlen(word[i])>strlen(word[i+1]))
            {
                swap(word[i],word[i+1]);
                k++;
            }
        }
    }
    //以上为排序模块;

    for(i=0;i<n;i++){
        printf("%s ",word[i]);
    }
    //以上为输出模块

    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值