# Sample Code HW5
#### [Back to Computer Programming (I)](https://hackmd.io/9tPBK8QATpCD5UQ0uvAZ-g?both)
###### tags: `NTNU` `CSIE` `必修` `Programming(I)`
## hw0501


### Author :
```c=
```
## hw0502

### Author : 高仔
```c=
#include <stdio.h>
#include <stdint.h>
int32_t gcd(int32_t a, int32_t b);
int32_t lcm(int32_t a, int32_t b);
int main()
{
int32_t num = 0;
printf("Please enter the variable number: ");
scanf("%d", &num);
if(num < 2)
{
printf("Wrong input!\n");
return 0;
}
int32_t coe[num][num + 1], ans[num], mag1 = 0, mag2 = 0, check[num];
//input
for(int i = 1 ; i <= num ; i++)
{
check[i - 1] = 0;
if(i % 10 == 1 && i != 11) printf("Please enter the %dst equation: ", i);
else if(i % 10 == 2 && i != 12) printf("Please enter the %dnd equation: ", i);
else if(i % 10 == 3 && i != 13) printf("Please enter the %drd equation: ", i);
else printf("Please enter the %dth equation: ", i);
for(int j = 0 ; j <= num ; j++) scanf("%d", &coe[i - 1][j]);
}
//消消消
for(int i = 0 ; i < num ; i++)
for(int j = 0 ; j < num ; j++)
if(coe[i][i] != 0 && coe[j][i] != 0 && i != j)
{
mag1 = (lcm(coe[i][i], coe[j][i]) / coe[i][i]);
mag2 = (lcm(coe[i][i], coe[j][i]) / coe[j][i]);
for(int k = 0 ; k <= num ; k++)
{
coe[i][k] *= mag1;
coe[j][k] *= mag2;
coe[j][k] -= coe[i][k];
}
}
//ans + check
for(int i = 0 ; i < num ; i++)
for(int j = 0 ; j < num ; j++)
if(coe[i][j] != 0)
{
ans[j] = coe[i][num] / coe[i][j];
check[j] = 1;
break;
}
//check no answer situation
for(int i = 0 ; i < num ; i++)
if(check[i] == 0)
{
printf("\nNo answer!\n");
return 0;
}
//output answer
printf("\nAnswer:\n");
for(int i = 1 ; i <= num ; i++)
printf("x%d = %d\n", i, ans[i - 1]);
return 0;
}
int32_t gcd(int32_t a, int32_t b)
{
while((a %= b) && (b %= a));
return a + b;
}
int32_t lcm(int32_t a, int32_t b)
{
if(a > b) return a / (gcd(a, b)) * b;
else return b / (gcd(a, b)) * a;
}
```
## hw0503



### Author : 高仔
```c=
#include <stdio.h>
#include <stdint.h>
int32_t win();
int32_t card[34] = {0}, cnt = 0, pair = 0, melds = 0;
int main()
{
int32_t num = 0;
//input
while(1)
{
printf("Please enter the tile: ");
scanf("%d", &num);
if(num > 34 || num < -1)
{
printf("Wrong input!\n");
return 0;
}
if(num == -1)
break;
card[num - 1]++;
cnt++;
}
//check card amount
if(cnt < 17)
{
printf("No enough tiles!\n");
return 0;
}
else if(cnt > 22)
{
printf("Cheated!\n");
return 0;
}
//skip impossible situation
for(int i = 0 ; i < 34 ; i++)
if(card[i] > 4)
{
printf("Cheated!\n");
return 0;
}
for(int i = 27 ; i < 34 ; i++)
if(card[i] == 1)
{
printf("Not a winning hand.\n");
return 0;
}
//output
if(win() && melds == 5 && pair == 1)
printf("Winning Hand!\n");
else
printf("Not a winning hand.\n");
return 0;
}
int32_t win()
{
if(cnt == 0) return 1;
for(int i = 0 ; i < 34 ; i++)
{
if(card[i] == 4)
{
card[i] = 0;
cnt -= 4;
melds++;
if(win()) return 1;
card[i] = 4;
cnt += 4;
melds--;
}
if(card[i] >= 3)
{
card[i] -= 3;
cnt -= 3;
melds++;
if(win()) return 1;
card[i] += 3;
cnt += 3;
melds--;
}
if(card[i] >= 2 && !pair)
{
card[i] -= 2;
cnt -= 2;
pair++;
if(win()) return 1;
card[i] += 2;
cnt += 2;
pair--;
}
if(i < 28 && (i % 9) < 7 && card[i] > 0 && card[i + 1] > 0 && card[i + 2] > 0)
{
card[i]--;
card[i + 1]--;
card[i + 2]--;
melds++;
cnt -= 3;
if(win()) return 1;
card[i]++;
card[i + 1]++;
card[i + 2]++;
melds--;
cnt += 3;
}
}
return 0;
}
```
## hw0504


### Author : Joseph Tu
```c=
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
int main(){
uint32_t msec,count = 0;
printf("Please enter the time interval (1-1000 msec): ");
scanf("%d",&msec);
msec *= 1000;
while(1){
printf("\033[H\033[J"); //等於stdlib.h的system("clear")
for(int32_t i = count;i < 10 + count;i++)
printf("%d",i % 10);
printf("\n");
count++;
if(count >= 10)
count -= 10;
usleep(msec);
}
return 0;
}
```
## hw0505

### Author : Hsuan-Yu
```c=
#include <stdio.h>
#include <stdint.h>
#include "rational_number.h"
int main ( )
{
int32_t a , b , c , d , e , f ;
printf("Please enter 1st rational number:");
scanf("%d %d" , &a , &b ) ;
if ( b == 0 )
{
printf("invalid input\n");
return 0 ;
}
printf("Please enter 2nd rational number:");
scanf("%d %d" , &c , &d ) ;
if ( d == 0 )
{
printf("invalid input\n");
return 0 ;
}
int32_t *e_ptr = &e , *f_ptr = &f ;
add( a , b , c , d , e_ptr , f_ptr ) ;
if ( e == 0 )
{
printf("%d/%d + %d/%d = 0\n", a , b , c , d );
}
else if ( f == 1 )
{
printf("%d/%d + %d/%d = %d \n", a , b , c , d , e );
}
else
{
printf("%d/%d + %d/%d = %d/%d\n", a , b , c , d , e , f );
}
sub( a , b , c , d , e_ptr , f_ptr ) ;
if ( e == 0 )
{
printf("%d/%d - %d/%d = 0\n", a , b , c , d );
}
else if ( f == 1 )
{
printf("%d/%d - %d/%d = %d \n", a , b , c , d , e );
}
else
{
printf("%d/%d - %d/%d = %d/%d\n", a , b , c , d , e , f );
}
mul( a , b , c , d , e_ptr , f_ptr ) ;
if ( e == 0 )
{
printf("%d/%d * %d/%d = 0\n", a , b , c , d );
}
else if ( f == 1 )
{
printf("%d/%d * %d/%d = %d \n", a , b , c , d , e );
}
else
{
printf("%d/%d * %d/%d = %d/%d\n", a , b , c , d , e , f );
}
div( a , b , c , d , e_ptr , f_ptr ) ;
if ( f == 0 )
{
printf("invalid operation\n");
}
else if ( e == 0 )
{
printf("%d/%d / %d/%d = 0\n", a , b , c , d );
}
else if ( f == 1 )
{
printf("%d/%d / %d/%d = %d \n", a , b , c , d , e );
}
else
{
printf("%d/%d / %d/%d = %d/%d\n", a , b , c , d , e , f );
}
return 0 ;
}
```
function.c
```c=
#include "rational_number.h"
int gcd ( int32_t a , int32_t b )//we have to assume a is bigger than b
{
if ( a == 0 || b == 0 )
{
return 1 ;
}
int32_t temp = 0 ;
while(1)
{
if ( a < 0 )
{
a *= -1 ;
}
if ( b < 0 )
{
b *= -1 ;
}
if ( a < b )
{
temp = a ;
a = b ;
b = temp ;
}
else if ( a == b )
{
//return 1 ;
return a ;
}
if ( a % b != 0 )
{
a = a % b ;
}
else
{
a = b % a ;
return a ;
}
}
}
int32_t add( int32_t a , int32_t b , int32_t c , int32_t d , int32_t *e , int32_t *f )
{
int32_t g = 1 ;
a *= d ;
//b *= d ;
c *= b ;
//d *= b ;
*e = a + c ;
*f = b * d ;
g = gcd( *e , *f ) ;
*e /= g ;
*f /= g ;
if ( *f < 0 )
{
*e *= -1 ;
*f *= -1 ;
}
}
int32_t sub( int32_t a , int32_t b , int32_t c , int32_t d , int32_t *e , int32_t *f )
{
int32_t g = 1 ;
a *= d ;
//b *= d ;
c *= b ;
//d *= b ;
*e = a - c ;
*f = b * d ;
g = gcd( *e , *f ) ;
*e /= g ;
*f /= g ;
if ( *f < 0 )
{
*e *= -1 ;
*f *= -1 ;
}
}
int32_t mul( int32_t a , int32_t b , int32_t c , int32_t d , int32_t *e , int32_t *f )
{
int32_t g = 1 ;
*e = a * c ;
*f = b * d ;
g = gcd( *e , *f ) ;
*e /= g ;
*f /= g ;
if ( *f < 0 )
{
*e *= -1 ;
*f *= -1 ;
}
}
int32_t div( int32_t a , int32_t b , int32_t c , int32_t d , int32_t *e , int32_t *f )
{
int32_t g = 1 ;
*e = a * d ;
*f = b * c ;
g = gcd( *e , *f ) ;
*e /= g ;
*f /= g ;
if ( *f < 0 )
{
*e *= -1 ;
*f *= -1 ;
}
}
```
function.h
```c=
#pragma once
#include <stdio.h>
#include <stdint.h>
int32_t add( int32_t a , int32_t b , int32_t c , int32_t d , int32_t *e , int32_t *f );
int32_t sub( int32_t a , int32_t b , int32_t c , int32_t d , int32_t *e , int32_t *f );
int32_t mul( int32_t a , int32_t b , int32_t c , int32_t d , int32_t *e , int32_t *f );
int32_t div( int32_t a , int32_t b , int32_t c , int32_t d , int32_t *e , int32_t *f );
```
## hw0506

```c=
#include <stdio.h>
#include <stdint.h>
int main()
{
uint8_t array[5] = {0};
for( int32_t i = 4; i >= 0 ; i-- )
{
printf( "Please enter a number: " );
scanf( "%u", &( array[i] ) );
}
for( int32_t i = 0 ; i < 5 ; i++ )
{
printf( "%u ", array[i] );
}
printf( "\n" );
return 0;
}
```

### Author : Hsuan-Yu
以下程式碼
```c=
#include <stdio.h>
#include <stdint.h>
int main()
{
uint8_t array[5] = {0};
for( int32_t i = 4; i >= 0 ; i-- )
{
printf( "Please enter a number: " );
scanf( "%u", &( array[i] ) );
}
for( int32_t i = 0 ; i < 5 ; i++ )
{
printf( "%u ", array[i] );
}
printf( "\n" );
return 0;
}
```
終端機顯示畫面如下
```bash=
$ ./a.out
Please enter a number: 5
Please enter a number: 4
Please enter a number: 3
Please enter a number: 2
Please enter a number: 1
1 0 0 0 0
```
因為一開始程式跟電腦要記憶體時,跟記憶體要1bytes,但是在scanf時卻把輸入的資料以32bit儲存,所以每次儲存時都會比原先的超過3個bytes,所以會發生後面輸入的值會把前面的覆蓋掉。
下面表格示範記憶體的狀態,假設程式一開始跟電腦要的記憶體位置分別為00到04這五格,05到07則是超除原陣列的位置,粗體字為該次輸入對記憶體所改變的值。
| 記憶體位置 | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 |
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|第一次輸入|0|0|0|0|**5**|**0**|**0**|**0**|
|第二次輸入|0|0|0|**4**|**0**|**0**|**0**|0|
|第三次輸入|0|0|**3**|**0**|**0**|**0**|0|0|
|第四次輸入|0|**2**|**0**|**0**|**0**|0|0|0|
|第五次輸入|**1**|**0**|**0**|**0**|0|0|0|0|
所以當程式執行到printf時,會依序印出記憶體位置00到04的值,所以最後輸出會是1 0 0 0 0。