1. 程式人生 > >C語言學習|陣列習題(10.17)

C語言學習|陣列習題(10.17)

特殊函式

1.exit(1):退出整個程式,終止程序,返回1給作業系統(返回0表示正常退出,其餘數表異常)

程式碼

猜隨機數

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10

void MakeNum (int a[]);
int GuessNum (int b[]);
int RightPosition (int a[],int b[]);
int RightNum (int a[],int b[]);

int main()
{
    int a[N],b[N],x,y,count = 1;
    MakeNum(a); //計算機隨機生成數
    printf("Please input the number:\n");
    do
    {
        printf("The %d time:\n",count);
        if (GuessNum(b) != 0) //若輸入合法,則開始判斷
        {
            count++;
            x = RightPosition(a,b);
            y = RightNum(a,b)-x;
            printf("%dx%dy\n",x,y);
        }
    }while (count <= 4 && x != 4); //若猜對或者次數用完,則退出
    if (x == 4)
        printf("Congratulations!\n");
    else
        printf("Sorry,you haven't got it,see you next time!\n");
    printf("The right answer is %d%d%d%d.\n",a[0],a[1],a[2],a[3]);

    return 0;
}
void MakeNum (int a[])
{
    int i,j,t,k;
    srand(time(NULL));
    for (i = 0; i < N; i++) //儲存0~9的數
        a[i] = i;
    for (j = 0; j < N; j++) //在0~9中隨機選擇數字,換入陣列前4個元素
    {
        k = rand()%10;
        t = a[j];
        a[j] = a[k];
        a [k] = t;
    }
    return;
}
/*使用者輸入資料猜數,若輸入合法,返回1,否則返回0*/
int GuessNum (int b[])
{
    int i,pol = 1;
    for (i = 0; i < 4; i++) //依次輸入每一位數
    {
        pol = scanf("%d",&b[i]);
        if (pol != 1) //若輸入非法
        {
            while(getchar() != '\n');
            printf("Please input the right number!\n");
            return 0;
        }
    }
    if (b[0] == b[1] || b[0] == b[2] || b[0] == b[3] || b[1] == b[2] || b[1] == b[3] || b[2] == b[3])
    {
        printf("The number can't be the same!\n"); //若輸入的兩數字相同
        return 0;
    }
    else
        return 1;
}
/*判斷位置數字均正確的數字個數*/
int RightPosition (int a[],int b[])
{
    int i,x = 0;
    for (i = 0; i < 4; i++)
        if (a[i] == b[i])
            x++;
    return x;
}
/*判斷正確的數字個數*/
int RightNum (int a[],int b[])
{
    int i,j,y = 0;
    for (i = 0; i < 4; i++)
        for (j = 0; j < 4; j++)
            if (a[i] == b[j])
                y++;
    return y;
}

執行結果

陣列的插入

int Insert (int a[],int x,int num)
{
    int i,j;
    for (i = 0; i < num; i++)
        if (x < a[i])
            break; //查詢x應該插入的位置,若找到,記錄i,退出查詢
    for (j = num; j > i; j-- )
        a[j] = a[j-1]; //將x後的元素依次後移一位
    a[i] = x;//將x插入陣列
    return i+1; //返回x所在下標值
}
#include <stdio.h>
#define N 51

int BigFact (int a[],int n);

int main()
{
    int i,j,n,index,a[N]={0};
    printf("Enter a number to be calculated:");
    scanf("%d",&n);
    for (i = 1; i <= n; i++) //迴圈每一個數的階乘
    {
        index = BigFact(a,i); //分別記錄每一個數階乘結果的位數,溢位則返回0
        if (index != 0)
        {
            printf("%d! = ",i);
            for (j = index; j > 0; j--)
                printf ("%d",a[j]);
            printf("\n");
        }
        else
        {
            printf("Overflow!\n");
            exit(1); //異常退出整個程式,終止程序,返回1給作業系統
        }
    }
    return 0;
}
/*計算階乘,並返回位數or溢位*/
int BigFact (int a[],int n)
{
    int i,j,k,index = 1;
    for (i = 0; i < N; i++) //將結果陣列初始化為0
        a[i] = 0;
    a[1] = 1; //將個位初始化為1
    for (i = 1; i <= n; i++) //表示一個階乘的每一次乘法運算
    {
        for (j = 1;j <= index; j++) //利用豎式乘法,將乘數與每一位數分別相乘,最高位數為index
            a[j] = a[j]*i;
        for (k = 1; k < index; k++) //除了最高位index,從個位開始,依次進位
        {
            if (a[k] >= 10)
            {
                a[k+1] = a[k+1]+a[k]/10;
                a[k] = a[k]%10;
            }
        }
        while (a[index] >= 10&&index <= N-1) //當最高為>=10,對最高位進位,此時位數index進一
        {
            a[index+1] = a[index]/10;
            a[index] = a[index]%10;
            index++;
        }
    }
    if (index <= N-1) //位數是否溢位陣列
        return index;
    else
        return 0;
    return 1;
}

執行結果