2020年哈尔滨工业大学C语言程序设计精髓 第五周练兵区 第一题

你好


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    printf("Please enter the number:\n");
    int a,b,c;
    int q=0,w=0,e=0,r=0,t=0,y=0,u=0,k=0,o=0,p=0;
    scanf("%d",&a);
    a=fabs(a);
    c=a;
    int i=0;
    while(1)
    {
        a/=10;
        i++;
        if(a==0)
        break;
    }
    printf("%d: %d bits\n",c,i);
    while(i)
    {
       b=c%10;
       if(c>=10)
       c=c/10;
       else
       b=c;
       i--;
       switch(b)
       {
           case 0: q+=1;
           break;
           case 1: w+=1;
           break;
           case 2: e+=1;
           break;
           case 3: r+=1;
           break;
           case 4: t+=1;
           break;
           case 5: y+=1;
           break;
           case 6: u+=1;
           break;
           case 7: k+=1;
           break;
           case 8: o+=1;
           break;
           case 9: p+=1;
           break;
       }

    }
    if(q>0)
    printf("0: %d\n",q);
    if(w>0)
    printf("1: %d\n",w);
    if(e>0)
    printf("2: %d\n",e);
    if(r>0)
    printf("3: %d\n",r);
    if(t>0)
    printf("4: %d\n",t);
    if(y>0)
    printf("5: %d\n",y);
    if(u>0)
    printf("6: %d\n",u);
    if(k>0)
    printf("7: %d\n",k);
    if(o>0)
    printf("8: %d\n",o);
    if(p>0)
    printf("9: %d\n",p);
    return 0;
}

2020年哈尔滨工业大学C语言程序设计精髓 第五周练兵编程题 第一题判断一个整型数据有几位v2.0(4分)的第二个用例上面的代码无法通过,想知道为什么

img


哪里运行不出来了,什么无法通过是什么意思?

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7447515
  • 你也可以参考下这篇文章:C语言苏小红版5.1从键盘任意输入一个实数不使用计算绝对值函数编译计算
  • 除此之外, 这篇博客: 【C语言进阶深度学习记录】三十二 函数指针与使用函数指针实现回调函数中的 2.2 使用函数指针实现回调函数 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 如何使用C语言直接跳转到某个固定的地址开始执行?(不是goto)

    使用回调函数,回调函数是一种很重要的思想。那么什么是回调机制呢?

    • 回调机制原理:
    1. 调用者不知道具体事件发生时需要调用哪一个具体的函数 (是不是与C++中的多态很像,是的C++中的多态原理就是这样)
    2. 具体的事件发生的时候 调用者通过函数指针,调用具体的函数。(是不是很像C++中的虚函数指针)

    回调机制中的调用者和被调用者没有依赖关系

    • 上面的理论,看起来并不是很容易让人理解,下面直接上手写代码就知道什么是回调函数了。

    • 代码 36-2.c

    #include <stdio.h>
    
    typedef int (Fruit)(int); //定义Fruit为int(int)类型的函数 参数:吃多少克,返回值:获得多少能量
    
    void Eat(Fruit* fruit, int n){ //函数指针fruit:指向吃什么水果的函数  参数n:吃多少克
        int ret = 0;
        
        printf("Eat...\n");
        
        ret = fruit(n);    //获得多少能量
        
        printf("Increase : %d\n", ret);
    }
    
    int Apple(int n){    //吃n克苹果获得ret克能量
        int ret = 0;
        int i = 0;
        
        for(i=0; i<n; i++){
            printf("Eat apple get energy : %d\n", 1);
            ret++;
        }
        return ret;
    }
    
    int Banana(int n){   //吃n克香蕉获得ret克能量
        int ret = 0;
        int i =0;
        
        for(i=0; i<n; i++){
            printf("Eat banana get energy : %d\n",3);
            ret+=3;
        }
        
        return ret;
    }
    
    int Pear(int n){     //吃n克梨子获得ret克能量
        int ret = 0;
        int i = 0;
        
        for(i=0; i<n; i++){
            printf("Eat pear get energy : %d\n",5);
            ret+=5;
        }
        return ret;
    }
    int main(){
        
        Eat(Apple, 5);  //get 5   
        printf("\n");
        Eat(Banana, 2);  // get 6
        printf("\n");
        Eat(Pear, 3);   //get 15
        printf("\n");
        
        return 0;
    }
    
    • 上述代码的意思是:吃水果获得能量。但是吃什么水果,只有在程序运行起来之后才知道。
    • 所以吃这个动作Eat函数的参数无法指定吃哪种水果
    • 只能使用函数指针作为Eat函数的参数,当程序运行起来时根据传进来的参数确定吃什么水果,以及获得多少能量

    上述程序编译运行结果为:

    在这里插入图片描述

    分析:

    • 上面代码主要的核心就在于函数指针的使用。一定要学会函数指针的定义使用。
    • 学会使用函数指针来实现回调函数
  • 您还可以看一下 张颜源老师的2020新版C语言程序设计零基础入门小白自学编程课程中的 C语言课程内容总结小节, 巩固相关知识点

题主的代码没啥问题,就是写的太复杂了,修改如下,供参考:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Please enter the number:\n");
    int a,c;
    int q[10] = {0};
    scanf("%d",&a);
    c=abs(a);
    int i=0;
    while(1)
    {
        q[c%10]++;
        c/=10;
        i++;
        if(c==0)
        break;
    }
    printf("%d: %d bits\n",a,i);
    for (i = 0;i < 10; i++)
        if (q[i])
            printf("%d: %d\n", i, q[i]);
    return 0;
}