分类 编程练习 下的文章

题目:

计算字符串中子串出现的次数

程序分析:

定义两个指针,一个用于遍历,另一个用于标记开始位置;

程序源代码:

#include "string.h" 

#include "stdio.h" 

int main()
{
    char str1[20], str2[20], *p1, *p2;

    int sum = 0;

    printf("please input two strings\n");

    scanf("%s%s", str1, str2);

    p1 = str1;
    
    p2 = str2;

    while (*p1 != '\0')
    {

        if (*p1 == *p2)
        {
            while (*p1 == *p2 && *p2 != '\0')
            {
                p1 ++;

                p2 ++;
            }
        } else

            p1 ++;

        if (*p2 == '\0')

            sum ++;

        p2 = str2;
    }

    printf("%d", sum);

    getchar();
}

题目

时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)。

程序分析:

程序源代码:

#include "time.h" 

#include "stdlib.h" 

#include "stdio.h" 

int main()
{
    char c;

    clock_t start, end;

    time_t a, b;

    double var;

    int i, guess;

    srand(time(NULL));

    printf("do you want to play it.('y' or 'n')\n");

    loop: 

    while((c = getchar()) == 'y')
    { 

        i = rand() % 100;

        printf("\nplease input number you guess:\n");

        start = clock();

        a = time(NULL);

        scanf("%d", &guess);

        while (guess != i)
        {
            if (guess > i)
            {
                printf("please input a little smaller.\n");

                scanf("%d", &guess);

            } else {

                printf("please input a little bigger.\n");

                scanf("%d", &guess);
            }
        }
        
        end = clock();

        b = time(NULL);

        printf("\1: It took you %6.3f seconds\n", var = (double)(end - start) / 18.2);

        printf("\1: it took you %6.3f seconds\n\n", difftime(b, a));

        if (var < 15)
            
            printf("\1\1 You are very clever! \1\1\n\n");
        
        else if (var < 25)

            printf("\1\1 you are normal! \1\1\n\n");

        else

            printf("\1\1 you are stupid! \1\1\n\n");

        printf("\1\1 Congradulations \1\1\n\n");

        printf("The number you guess is %d", i);
    }

    printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");

    if ((c = getchar()) == 'y')

    goto loop;
}

题目:

时间函数举例3

程序分析:

clock():函数返回自程序开始运行的处理器时间,如果无可用信息,返回-1。 转换返回值以秒记, 返回值除以CLOCKS_PER_SECOND. (注: 如果编译器是POSIX兼容的, CLOCKS_PER_SECOND定义为1000000.)

程序源代码:

/*calculate time*/ 

#include "time.h" 
#include "stdio.h" 

int main()
{
    clock_t start,end;

    int i;

    double var;

    start = clock();

    for (i = 0;i < 10000;i ++) 
    {
        printf("\1\1\1\1\1\1\1\1\1\1\n");
    }

    end = clock();

    printf("\1: The different is %6.3f\n", (double)(end - start));
}

题目

时间函数举例2

程序分析:           

C 语言里的定时器用法。

程序源代码:

/*calculate time*/ 
#include "time.h" 
#include "stdio.h" 

int main() 
{
    time_t start, end;

    int i;

    start = time(NULL);

    for (i = 0;i < 3000;i ++) 
    {
        printf("\1\1\1\1\1\1\1\1\1\1\n");
    } 

    end = time(NULL);

    printf("\1: The different is %6.3f\n", difftime(end, start));
}

题目:输出9*9口诀。

程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

程序源代码:

#include "stdio.h" 

int main() 
{ 
    int i, j, result; 

    printf("\n"); 

    for (i = 1; i < 10; i ++) 
    { 
        for (j = 1; j < 10; j ++) 
        {
            result = i * j; 

            /*-3d表示左对齐,占3位*/ 
            printf("%d * %d = %-3d", i, j, result);
        }

        /*每一行后换行*/
        printf("\n");
    }
}