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


### Author :
```c=
```
## mid02

```bash=
$ ./mid02
Please enter a decimal integer (uint32): 123
base 2: 1111011
base 3: 11120
base 4: 1323
base 5: 443
base 6: 323
base 7: 234
base 8: 173
base 9: 146
base 11: 102
base 12: A3
base 13: 96
base 14: 8B
base 15: 83
base 16: 7B
```
### Author : Joseph Tu
main.c:
```c=
#include <stdio.h>
#include <stdint.h>
#include "header.h"
int main(){
uint32_t n,i;
printf("Please enter a decimal integer ( uint32 ) : ");
scanf("%u",&n);
for(i = 2;i <= 16;i++)
if(i == 10)
continue;
else
numsys(n,i);
return 0;
}
```
header.h:
```c=
#pragma once
#include <stdint.h>
#include <stdio.h>
void numsys(uint32_t n,uint32_t base);
```
header.c:
```c=
#include "header.h"
void numsys(uint32_t n,uint32_t base){
int32_t count = 0;
char ans[1000];
while(n){
if(n % base < 10)
ans[count] = (n % base) + 48;
else
ans[count] = (n % base) + 55;
n /= base;
count++;
}
printf("base %u: ",base);
for(int32_t i = count-1;i >= 0;i--)
printf("%c",ans[i]);
printf("\n");
}
```
## mid03

### Author : Joseph Tu
main.c:
```c=
#include <stdio.h>
#include <stdint.h>
#include "header.h"
int main(){
uint32_t n;
printf("Please enter an integer : ");
scanf("%u",&n);
printf("%u\n",factorial(n));
return 0;
}
```
header.h:
```c=
#pragma once
#include <stdint.h>
uint32_t factorial(uint32_t n);
```
header.c:
```c=
#include "header.h"
uint32_t factorial(uint32_t n){
return n / 5;
}
```
## mid04




### Author : Joseph Tu
```c=
#include <stdio.h>
#include <stdint.h>
int main(){
int32_t HHP,MAXHHP,HATK,BHP,MAXBHP,BATK,MHP,choice = 0,lv = 1,exp = 0,money = 0,potion = 0;
printf("Hero ’ s HP (1 -255) : ");
scanf("%d",&MAXHHP);
HHP = MAXHHP;
printf("Hero ’ s ATK (1 -255) : ");
scanf("%d",&HATK);
printf("Boss ’ s HP (1 -1024) : ");
scanf("%d",&MAXBHP);
BHP = MAXBHP;
printf("Boss ’ s ATK (1 -255) : ");
scanf("%d",&BATK);
do{
printf("\n1)Satatus\n2)Shop\n3)Monster\n4)Boss\n5)Exit\n\nChoice : ");
scanf("%d",&choice);
switch(choice){
case 1:
choice = 0;
while(choice != 1){
printf("\nName: Hero\nLevel: %d\nEXP: %d\nMONEY: %d\n",lv,exp,money);
printf("------------------\n");
printf("HP: %d/%d\nATK: %d\nPotion: %d\n",HHP,MAXHHP,HATK,potion);
printf("------------------\n");
printf("1)Back\n\nChoice: ");
scanf("%d",&choice);
}
choice = 0;
break;
case 2:
choice = 0;
while(choice != 2){
printf("\nMONEY: %d\nPotion: %d\n",money,potion);
printf("------------------\n");
printf("1)Buy one potion\n2)Back\n\nChoice: ");
scanf("%d",&choice);
if(choice == 1)
printf("\nYou don't have enough money.\n");
}
choice = 0;
break;
case 3:
choice = 0;
MHP = 150;
while(choice != 3 && MHP > 0 && HHP > 0){
printf("\nMonster\nHP: %d/150\nATK: 15\n",MHP);
printf("------------------\n");
printf("Hero\nHP: %d/%d\nATK: %d\nPotion: %d\n",HHP,MAXHHP,HATK,potion);
printf("------------------\n");
printf("1)Attack\n2)Potion\n3)Escape\n\nChoice: ");
scanf("%d",&choice);
if(choice == 1)
MHP -= HATK;
if(MHP > 0 && choice != 3)
HHP -= 15;
}
choice = 0;
if(HHP <= 0){
printf("\nYou Lose\n");
return 0;
}
if(MHP <= 0){
exp += 20;
if(exp >= 100){
printf("\nLevel UP\nHP: %d → ",MAXHHP);
lv++;
exp -= 100;
MAXHHP += 20;
printf("%d\n",MAXHHP);
HHP = MAXHHP;
printf("Attack: %d → ",HATK);
HATK += 3;
printf("%d\n\n",HATK);
}
}
break;
case 4:
choice = 0;
BHP = MAXBHP;
while(choice != 3 && BHP > 0 && HHP > 0){
printf("\nBoss\nHP: %d/%d\nATK: %d\n",BHP,MAXBHP,BATK);
printf("------------------\n");
printf("Hero\nHP: %d/%d\nATK: %d\nPotion: %d\n",HHP,MAXHHP,HATK,potion);
printf("------------------\n");
printf("1)Attack\n2)Potion\n3)Escape\n\nChoice: ");
scanf("%d",&choice);
if(choice == 1)
BHP -= HATK;
if(BHP > 0 && choice != 3)
HHP -= BATK;
}
choice = 0;
if(HHP <= 0){
printf("\nYou Lose\n");
return 0;
}
if(BHP <= 0){
printf("\nCongratulation!\n");
return 0;
}
break;
case 5:
return 0;
}
}while(choice != 1||choice !=2||choice!=3||choice!=4||choice!=5);
return 0;
}
```
## mid05
