云班课实验补充

云班课实验补充

第六周多线程代码

题目详情:编译运行多线程程序,提交编译和运行命令截图。

代码

#include<stdio.h>
#include<pthread.h>
#define NUM 5
void *print_msg(void *);
int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,print_msg,(void *)"hello");
    pthread_create(&t2,NULL,print_msg,(void *)"world\n");
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    printf("t1,t2 finished\n");
    return 0;
}
void *print_msg(void *m)
{
    char *cp=(char *)m;
    int i;
    for(i=0;i<NUM;i++)
    {
        printf("%s",m);
        fflush(stdout);
        sleep(1);
    }
    return NULL;
}

运行结果

第六周SumN

题目详情:1-N求和

代码

#include<stdio.h>
 
int fun(int i);
int main()
{
    int sum=0;
    int n;
    printf("请输入N=");
    scanf("%d",&n);
    sum=fun(n);
    printf("1-%d的求和为%d\n",n,sum);
}
int fun(int i)
{
    if(i==1)
    { return 1;}
    else
    { return i+fun(i-1);}
}

猜你喜欢

转载自www.cnblogs.com/fanxiaonan/p/12099105.html