程序人生——苏嵌第四天

苏嵌教育                                                                     暑期实习


                                          学习日志                                            姓名:胡昊          日期:2018.7.19



                                                

  • 今日学习任务:                                 学习指针。

  • 今日任务完成情况:                         指针部分较难,因此仍有不懂之处,作业写的也比较艰辛。主要代码:
    /*************************************************************************
    	> File Name: work6.c
    	> Author: HuHao
    	> Mail: [email protected] 
    	> Created Time: 2018年07月19日 星期四 19时53分57秒
     ************************************************************************/
    //字符串连接
    #include <stdio.h>
    #include <stdlib.h>
    
    char *catstr(char*a,char*b)
    {
    	char*x=a;
    	while(*a!='\0')
        {
            a++;
        }
        while(*b!='\0')
        {
            *a=*b;
            a++;
            b++;
        }
        *a='\0';
        return x;
    }
    
    int main()
    {
    	char a[20]="hello";
    	char b[20]="world";
    	printf("%s\n",catstr(a,b));
    }
    /*************************************************************************
    	> File Name: work3.c
    	> Author: HuHao
    	> Mail: [email protected] 
    	> Created Time: 2018年07月19日 星期四 18时47分00秒
     ************************************************************************/
    //字符串复制
    #include <stdio.h>
    #include <stdlib.h>
    
    int count(char *s)
    {
    	int count=0;
    	while(*s++!='\0')
    		count++;
    	return count;
    }
    
    void strcpy_v1(char *dest,const char *scr,int len)
    {
    	if(dest==NULL||scr==NULL)
    		return;
    	char *pdest=dest;
    	char *pscr=(char *)scr;
    	if((scr<dest)&&(dest<scr+len))
    	{
    		pdest=pdest+len-1;
    		pscr=pscr+len-1;
    		while(len--)
    			*pdest--=*pscr--;
    		*pdest='\0';
    	}
    	else
    	{
    		while(len--)
    			*pdest++=*pscr++;
    		*pdest='\0';
    	}
    }
    
    int main()
    {
    	char a[]="Hello";
    	char b[]="afafafa";
    	printf("Before copy:%s\n",b);
    	int len;
    	len=count(a);
    	strcpy_v1(b,a,len);
    	printf("After copy:%s\n",b);
    }
    /*************************************************************************
    	> File Name: work1.c
    	> Author: HuHao
    	> Mail: [email protected] 
    	> Created Time: 2018年07月19日 星期四 18时30分40秒
     ************************************************************************/
    //统计随机数的分布,产生10000个以上的10以内随机数,统计0~9出现的次数,保存在数组里。
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define random(x) (rand()%x)
    
    int main()
    {
    	int temp;
    	srand((int)time(NULL));
    	int a[10]={0};
    	int i=0;
    	for(i=0;i<10000;i++)
    	{
    		temp=random(10);
    		if(temp==0)
    			a[0]++;
    		else if(temp==1)
    			a[1]++;
    		else if(temp==2)
    			a[2]++;
    		else if(temp==3)
    			a[3]++;
    		else if(temp==4)
    			a[4]++;
    		else if(temp==5)
    			a[5]++;
    		else if(temp==6)
    			a[6]++;
    		else if(temp==7)
    			a[7]++;
    		else if(temp==8)
    			a[8]++;
    		else if(temp==9)
    			a[9]++;
    		else
    			continue;
    	}
    	for(i=0;i<10;i++)
    		printf("a[%d] have %d\n",i,a[i]);
    	return 0;
    }
     
    /*************************************************************************
    	> File Name: work2.c
    	> Author: HuHao
    	> Mail: [email protected] 
    	> Created Time: 2018年07月19日 星期四 18时15分07秒
     ************************************************************************/
    //编写字符串逆序函数,不借助于任何字符串函数,使用指针完成。
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int count(char *s)
    {
         int n = 0;
         while(*s++ != '\0')
         n++;
         printf("n = %d\n",n);
         return n;
    }
    
    void conver(char *s,int n)
    {
    	int i;
    	char temp;
    	for(i=0;i<n;i++,n--)
    	{
    		if(i!=n)
    		{
    			temp=s[i];
    			s[i]=s[n-1];
    			s[n-1]=temp;
    		}
    	}
    }
    
    int main()
    {
    	char a[]="Hello World";
    	int i;
    	printf("%s\n",a);
    	i=count(a);
    	conver(a,i);
    	printf("%s\n",a);
    }
  • /*************************************************************************
    	> File Name: work4.c
    	> Author: HuHao
    	> Mail: [email protected] 
    	> Created Time: 2018年07月19日 星期四 19时09分42秒
     ************************************************************************/
    //用字符指针数组实现主程序的menu()菜单
    
    #include <stdio.h>
    #include <stdlib.h>
    
    char *menu[6]={"*************************",
    				"*****1.Hello       *****",
    				"*****2.World       *****",
    				"*****3.I like you  *****",
    				"*****0.Quit        *****",
    				"************************"};
    int main()
    {
    	int i=0;
    	int choose=0;
    	while(1)
    	{
    		for(i=0;i<6;i++)
    			printf("%s\n",menu[i]);
    		scanf("%d",&choose);
    		if(choose<0 || choose>3)
    			printf("input error!\n");
    		else
    		{
    		switch(choose)
    		{
    			case 0:printf("Thanks choose 0!\n");return 0;
    			case 1:printf("Thanks choose 1!\n");break;
    			case 2:printf("Thanks choose 2!\n");break;
    			case 3:printf("Thanks choose 3!\n");break;
    			default:break;
    		}
    		}
    	}
    	return 0;
    }
    /*************************************************************************
    	> File Name: work5.c
    	> Author: HuHao
    	> Mail: [email protected] 
    	> Created Time: 2018年07月19日 星期四 19时30分56秒
     ************************************************************************/
    //函数指针的应用
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void eat(int num)
    {
    	printf("I eat %d bread!\n",num);
    }
    
    void drink(int num)
    {
    	printf("I drink %d water!\n",num);
    }
    
    void dosomething(void (*pa)(int),int num)
    {
    	printf("I want to do something:");
    	(*pa)(num);
    }
    
    int main()
    {
    	void (*pa)(int);
    	pa=eat;
    	dosomething(pa,2);
    	pa=drink;
    	dosomething(pa,4);
    	return 0;
    }
    
    [点击并拖拽以移动]
    ​

  • 今日开发中出现的问题汇总:         指针函数,函数指针,指针数组。

  • 今日未解决问题:                             指针函数,字符串拷贝,连接。

  • 自我评价:                                         自我感觉良好,基本掌握了新知识。

  • 其他:                                                  随着一天的学习落幕,收获满满,期待着明天的学习和明天的收获。
发布了9 篇原创文章 · 获赞 2 · 访问量 652

猜你喜欢

转载自blog.csdn.net/a45654645/article/details/81121679
今日推荐