【C语言 动态创建链表 Linked List】

#include <stdio.h>
#include <stdlib.h>

typedef struct LinkedListNode
{
    int data;
    struct LinkedListNode *next;
} NODE;

NODE *creatList(int n, int m)
{
    NODE *temp=NULL;
    NODE *origin;
    NODE *extend;

    origin=(NODE*)malloc(sizeof(NODE));
    temp=origin;
    origin->next=NULL;

    for(int i=1;i<=n;i+=1){
        extend=(NODE*)malloc(sizeof(NODE));
        extend->data=i;
        extend->next=origin->next;
        origin->next=extend;
        origin=origin->next;
    }
    return temp;
}

void outputLink(NODE *head)
{
    NODE *temp;
    temp=head->next;

    printf("HEAD");
    while(temp){
        printf("->%d",temp->data);
        temp=temp->next;
    }
}

int main() {
    NODE *head;
    head=creatList(10,10);
    outputLink(head);
    return 0;
}

【C语言 大数加法 减法 乘法】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void stringReverse(char * p)//逆序字符串
{
    int i,len;
    char temp;
    len = strlen(p);

    for(i=0; i<(len/2); i++)
    {
        temp = p[i];
        p[i] = p[len-1-i];
        p[len-1-i] = temp;
    }
}

char* getString()//动态字符串输入
{
    char *str;
    char *_str;
    int i=1;

    str=(char*)malloc(sizeof(char)*(i+1));

    while((str[i-1]=getchar())!='\n'){
        i+=1;

        _str=(char*)malloc(strlen(str)+1);
        str[i-1]='\0';

        strcpy(_str,str);
        free(str);

        str=(char*)malloc(sizeof(char)*(i+1));
        if(str==NULL){
            free(_str);
            printf("Not enough memory!");
            return NULL;
        }

        strcpy(str,_str);
        free(_str);
    }
    str[i-1]='\0';
    return str;
}

char* remove_0(char *c)//删除0
{
    char *temp,*ch;
    temp=c;
    while(*temp){
        if(*temp!='0') break;
        temp+=1;
    }

    ch=c;
    while(*temp){
        *ch=*temp;
        ch+=1;
        temp+=1;
    }
    *ch='\0';

    return c;
}

char* add(char *a,char *b)//加法
{
    char *c;
    char *ch1,*ch2,*ch3;
    int len=0;
    int i=0,j=0;
    int x=0,y=0;

    len = strlen(a)>=strlen(b) ? strlen(a) : strlen(b);

    c=(char*)malloc(sizeof(char)*(len+1));

    stringReverse(a);
    stringReverse(b);

    ch1=a;
    ch2=b;
    ch3=c;
    while(*ch1!='\0' || *ch2!='\0'){
        if(*ch1!='\0') i=*ch1-'0';
        if(*ch2!='\0') j=*ch2-'0';
        x=i+j+y;
        *ch3=(x%10)+'0';
        ch3+=1;
        y=x/10;
        if(*ch1!='\0') ch1+=1;
        if(*ch2!='\0') ch2+=1;
        i=0;
        j=0;
    }
    *ch3='\0';

    stringReverse(c);

    return c;
}


char* minus(char *a,char *b)//减法
{
    char *c;
    char *temp;
    char *ch1,*ch2,*ch3;
    int sign=0;
    int len=0;
    int i=0,j=0;
    int x=0,y=0;

    len = strlen(a)>=strlen(b) ? strlen(a) : strlen(b);

    c=(char*)malloc(sizeof(char)*len);

    if((strlen(a)==strlen(b) && strcmp(a,b)<0) || strlen(a)<strlen(b)){
        temp=a;
        a=b;
        b=temp;
        sign=1;
    }

    stringReverse(a);
    stringReverse(b);

    ch1=a;
    ch2=b;
    ch3=c;
    while(*ch1!='\0' || *ch2!='\0'){
        if(*ch1!='\0') i=*ch1-'0';
        if(*ch2!='\0') j=*ch2-'0';
        x=i-j-y;
        if(x<0){
            y=1;
            x+=10;
        }else{
            y=0;
        }
        *ch3=(x%10)+'0';
        ch3+=1;
        if(*ch1!='\0') ch1+=1;
        if(*ch2!='\0') ch2+=1;
        i=0;
        j=0;
    }
    *ch3='\0';

    stringReverse(c);

    remove_0(c);

    if(sign==0){
        return c;
    }else{
        printf("-");
        return c;
    }
}


char* by(char *a,char *b)//乘法
{
    char *c;
    int len=0;
    int temp=0;
    int i=0,j=0;
    int x=0,y=0;

    len=strlen(a)+strlen(b);

    stringReverse(a);
    stringReverse(b);

    c=(char*)malloc(sizeof(char)*(len+1));
    memset(c,0,sizeof(char)*(len+1));

    for(i=0;i<strlen(b);i+=1){
        for(j=0;j<strlen(a);j+=1){
            x=a[j]-'0';
            y=b[i]-'0';
            temp=x*y;
            c[i+j]+=temp;
        }
    }

    x=0;
    for(i=0;i<len;i+=1){
        c[i]+=x;
        x=c[i]/10;
        c[i]%=10;
    }

    for(i=0;i<len;i+=1){
        c[i]+='0';
    }

    stringReverse(c);

    remove_0(c);

    return c;
}

int main(){
    char *a,*b,*operator;

    printf("Infinite Positive Integer Operation\n");
    printf("-----------------------\n");

    printf("Please enter the first positive integer:\n");
    a=getString();

    printf("Please enter the operator(+ - * /):\n");
    operator=getString();

    printf("Please enter the second positive integer:\n");
    b=getString();

    printf("The result is:\n");

    switch (*operator){
        case '+':
            puts(add(a,b));
            break;
        case '-':
            puts(minus(a,b));
            break;
        case '*':
            puts(by(a,b));
            break;
            /*case '/':
                puts(divide(a,b));
                break;*/
        default:
            printf("error");
            break;
    }


    return 0;
}

【C语言 扫雷】

Minesweeper in C

C语言扫雷

9*9

可以自动消除一大片

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 9 //9*9扫雷

int sum = 10; //雷数目
int mine[N][N] = {0}; //雷数组
int map[N][N]; //显示数组
int markDirection[8][2] = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; //8个方向

void printMap() //打印地图
{
    int i = 0,j = 0;
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%d ",map[i][j]);
        }
        printf("\n");
    }
}

void setMine() //埋雷
{
    srand((int)time(0));
    int x=0,y=0;

    while(sum >= 0)
    {
        x = (rand()%(9-1+1))+1;
        y = (rand()%(9-1+1))+1;

        if(mine[x][y] != 1)
        {
            mine[x][y] = 1;
            sum -= 1;
        }
    }
}

int countMine(int x,int y) //数雷
{
    int count=0;
    int x1 = 0, y1 = 0;
    int i = 0;
    
    for(i=0;i<N;i++)
    {
        x1 = x + markDirection[i][0];
        y1 = y + markDirection[i][1];
        if(x1<0 || x1>=N || y1<0 || y1>=N) continue;
        if(mine[x1][y1] == 1) count += 1;
    }
    return count;
}

void autoClear(int x,int y) //自动消一大片
{
    int x1 = 0, y1 = 0;
    int i = 0;
    int count = 0;
    
    map[x][y] = 0;
    
    for(i=0;i<N;i++)
    {
        x1 = x + markDirection[i][0];
        y1 = y + markDirection[i][1];
        if(x1<0 || x1>=N || y1<0 || y1>=N) continue;
        count = countMine(x1,y1);
        if(count != 0) 
        {
            map[x1][y1] = count;
        }else if(map[x1][y1]==0){
            continue;
        }else{
            autoClear(x1,y1);
        }
    }
}

int winCheck() //赢了吗
{
    int count = 0;
    int i=0,j=0;
    
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            if(map[i][j] == 9) count += 1;
        }
    }
    if(count == sum)
    {
        return 1;
    }else{
        return 0;
    }
}

void Gameover() //炸了
{
    int i = 0,j = 0;
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%d ",mine[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int X=0,Y=0;
    int i=0,j=0;
    int check=0;
    int result = 1;
    
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            map[i][j] = 9;
        }
    }
        

    
    printf("*********扫雷**********\n");
    printMap();
    setMine();
    
    while(result)
    {
        printf("输入 行,列\n");
        scanf("%d,%d",&X,&Y);
        
        X -= 1;
        Y -= 1;
        
        if(mine[X][Y] == 1)
        {
            printf("Game over!\n");
            Gameover();
            result = 0;
        }else{
            check = countMine(X,Y);
            if(check != 0)
            {
                map[X][Y] = check;
            }else{
                autoClear(X,Y);
            }
            printMap();
        }
        
        if(winCheck())
        {
            printf("You win!");
            break;
        }
        
    }
    return 0;
}

 

代码:https://github.com/chiuxingxiang/Minesweeper-C