---
title: 程設二 Computer Programming(II)
---
# Computer Programming (II)
NTNU 程式設計(二)
##### [Back to Note Overview](https://reurl.cc/XXeYaE)
###### tags: `NTNU` `CSIE` `必修` `Programming(II)`
## [Moodle](https://moodle.ntnu.edu.tw/course/view.php?id=22976)
## [ClassWeb](https://sites.google.com/gapps.ntnu.edu.tw/neokent/teaching/2020spring-computer-programming-ii?authuser=0)
## [Calander](https://calendar.google.com/calendar?cid=MGVqaWduMzg1NGI4c3F2aHVuNHBydmR0a29AZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ)
<iframe src="https://calendar.google.com/calendar/embed?src=0ejign3854b8sqvhun4prvdtko%40group.calendar.google.com&ctz=Asia%2FTaipei" style="border: 0" width="600" height="400" frameborder="1" scrolling="no"></iframe>
## Score
- Midterm Exam 15%
- Final Exam 20%
- Homework 50%
- Project 15%
- Bonus
## Final Project
## Bonus

## String
- fgets
- strcpy
- strcmp
- strncmp
- strchar
- strrchr
- strspn
- strcspn
- strpbrk
- strstr
- strtok
- strlen
### IPv4 (strtok ver.)
```c=
#include <stdio.h>
#include <string.h>
int main(){
char str[128] = {0};
fgets(str, 128, stdin);
char *token = strtok(str, ".");
while(token != NULL){
//cooditions
token = strtok(NULL, ".");
}
return 0;
}
```
### IPv4 (sscanf ver.)
```c =
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[128] = {0};
int ipv4[4] = {0};
fgets( str, 128, stdin);
sscanf( str, "%d.%d.%d.%d", &(ipv4[0]),
&(ipv4[1]),
&(ipv4[2]),
&(ipv4[3]));
for(int i = 0; i < 4; i++)
{
printf("%d ", ipv4[i]);
}
printf("\n");
return 0;
}
```
## Structure
### Decalre
```c=
struct new_type_name
{
type member_name;
type member_name;
type member_name;
...
}
// in main, use it
// new_type_name struct_name;
```
### Structure operators
#### member 【.】
```c=
a_card.face = 5;
```
#### pointer 【->】
```c=
a_card_ptr = &a_card;
a_card_ptr -> face = 5;
//Equivalent to (*a_card_ptr).face = 5;
```
### Structure Attribute
```
一般電腦預設是4Bytes
```
#### Disable CPU alignment
```c=
struct student
{
char name[64];
uint8_t height;
uint8_t weight;
uint8_t gender;
uint16_t id;
uint8_t grade[8];
}__attribute__((packed));
// 不要CPU自己alignment
```
```c=
#pragma pack(push)
#pragma pack(1)//以幾個Bytes去對齊 通常只有1有用(關掉的意思)
//structure
#pragma pack(pop)
```

## Union
Union memory示意圖

```c=
typeof union _number
{
int8_t x;
int32_t y;
double z;
}number;
```
```
跟struct的主要差別在記憶體配置
```
## Bitwise Operation
```
& and
| or
~ not
^ xor
<< 左移 = *2
>> 右移 = /2
```
### Application
```c=
//if a is 2^n
if( ( a - 1 & a ) == 0)
```
## File
### I/O Redirection
### Example Code
#### fprint-main.v2.c
```c=
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main()
{
FILE *pFile = NULL;
char fileName[32] = {0};
char tmp[32] = {0};
printf( "Please enter the file name: " );
if( fgets( fileName, sizeof( fileName ), stdin ) == NULL )
{
printf( "Error!\n" );
return 0;
}
// Since fgets will include '\n', we need to remove this character.
if( fileName[ strlen( fileName ) - 1 ] == '\n' )
{
fileName[ strlen( fileName ) - 1 ] = 0;
}
else
{
// If the last byte is not '\n', we need to clean the input buffer.
int32_t c = 0;
while( ( c = fgetc( stdin ) ) != '\n' && c != EOF ){}
}
if( ( pFile = fopen( fileName, "w" ) ) == NULL )
{
printf( "File could not be opened!\n" );
return 0;
}
char account[32] = {0};
char password[32] = {0};
int32_t i = 1;
while( 1 )
{
printf( "Please enter your account and password.\n" );
printf( "The length of account and password cannot be over 31.\n" );
printf( "Account: " );
if( fgets( account, 32, stdin ) == NULL )
{
break;
}
if( account[ strlen( account ) - 1 ] == '\n' )
{
account[ strlen( account ) - 1 ] = 0;
}
else
{
int32_t c = 0;
while( ( c = fgetc( stdin ) ) != '\n' && c != EOF ){}
}
printf( "Password: " );
if( fgets( password, 32, stdin ) == NULL )
{
break;
}
if( password[ strlen( password ) - 1 ] == '\n' )
{
password[ strlen( password ) - 1 ] = 0;
}
else
{
int32_t c = 0;
while( ( c = fgetc( stdin ) ) != '\n' && c != EOF ){}
}
fprintf( pFile, "%02d %s/%s\n", i, account, password );
i++;
}
fclose( pFile );
return 0;
}
```
### rewind
### fseek
```c=
fseek(FILE * , long , SEEK_XXX)
// SEEK_SET start of file
// SEEK_CUR now
// SEEK_END eof
```
### fread
```c=
size_t fread(void* ptr, size_t size, size_t nmemb, FILE *stream)
// size建議放sizeof(char), sizeof(int)等等這種
// nmemb則放要讀多少個東西
// 雖然沒有太大差異 但是自己比較看得懂
```
### fwrite
```c=
size_t fwrite(void* ptr, size_t size, size_t nmemb, FILE *stream)
// size 跟 nmemb 同上
```
## Misc
### typedef
```c=
typedef struct _card card;
```
```c=
typedef struct _card
{
} card;
```
### const
> tips:由後往前讀
```c=
int *
int const *
int * const
int const * const
const int *
const int * const
```
[Article]()
### cdecl (in linux terminal)
```bash=
explain (code)
declare (English)
```
### BMP

### SDL
```
sudo apt-get install libsdl2-dev libsdl2-image-dev
```
## Homework
### HW1
- [Problem](https://drive.google.com/file/d/1fOsejqaCJ9WXTNGocXR6ev0Qhnefgwc6/view)
- Due: 2020.03.24 PM 11:59
### HW2
- [Problem]
- Due: 2020.04.14 PM 11:59
<!--
## File
## Variable Length Arguments
## Macro
## Tricks
-->
## Linked List
<!--
## Stack and Queue -->