---
# System prepended metadata

title: Lab1 參考解答

---

# Lab1 參考解答

# Calculator
> Author: 111550107
```c=
#include<stdio.h>
int main(){
    int n1, n2;
    char operater;
    scanf("%d %c %d", &n1, &operater, &n2);
    if(operater == '+'){
        printf("%d", n1 + n2);
    } else if(operater == '-'){
        printf("%d", n1 - n2);
    } else if(operater == '*'){
        printf("%d", n1 * n2);
    } else if(operater == '/' && n2 == 0){
        printf("This calculator couldn't handle.");
    } else {
        printf("%d", n1 / n2);
    }
    return 0;
}
```

# Tic-tac-toe
> Author: 111550033
```c=
#include <stdio.h>
#define F(i,n)  FF(i,0,(n))
#define F1(i,n)  FF(i,1,(n)+1)
#define FF(i,n,m) for(int i=n;i<(m);++i)
#define rF(i,a,b) for(int i=a;i>=b;--i)
int a[3][3];
char c[4]="-OX";

int main(){
	int x,cur=0;
	while(scanf("%d",&x)!=EOF){
		int X=x/3,Y=x%3;
		a[X][Y]=cur+1;
		F(i,3)F(j,3){
			printf("%c ",c[a[i][j]]);
			if(j==2) putchar('\n');
		}
		putchar('\n');
		F(i,3){
			if(a[i][0]&&a[i][0]==a[i][1]&&a[i][1]==a[i][2]) {
				printf("%c is the winner!\n",(!cur?'O':'X'));
				return 0;
			}
		}
		F(i,3){
			if(a[0][i]&&a[0][i]==a[1][i]&&a[1][i]==a[2][i]) {
				printf("%c is the winner!\n",(!cur?'O':'X'));
				return 0;
			}
		}
		if(a[0][0]&&a[0][0]==a[1][1]&&a[1][1]==a[2][2]){
			printf("%c is the winner!\n",(!cur?'O':'X'));
			return 0;
		}
		if(a[0][2]&&a[0][2]==a[1][1]&&a[1][1]==a[2][0]){
			printf("%c is the winner!\n",(!cur?'O':'X'));
			return 0;
		}
		cur=!cur;
	}
	printf("Here comes a tie game.\n");
}
```