# 1101 計算機概論 4abc
[TOC]
## 資源分享
### 課堂用書 PDF
- [C: How to Program: with an introduction to C++ Global Edition 8th Edition](https://faculty.ksu.edu.sa/sites/default/files/c_how_to_program_with_an_introduction_to_c_global_edition_8th_edition.pdf)
### 適合新手的 C compiler/IDE
- 線上(不須安裝軟體)
- [OnlineGBD debuger](https://www.onlinegdb.com/)
- GUI 軟體編譯器
- [Code::Blocks](https://www.codeblocks.org/)
- [Visual Studio](https://visualstudio.microsoft.com/zh-hant/vs/)
> 選 community 2019版才是免費的
- [Dev-C++](https://sourceforge.net/projects/orwelldevcpp/)
### 其他
- [ACM NCNU Linux note](https://hackmd.io/h6ZHt9RpSWGrofwXeEHKEA?view)
## 9/23
### 上課內容
- [Introduction PPT](https://moodle.ncnu.edu.tw/pluginfile.php/1265949/mod_resource/content/1/cop.pdf)
- [CPU simulator](http://erdos.csie.ncnu.edu.tw/~klim/cpu-simulator/index.html)
### 在 lilina 文字介面的常用指令
- **pwd**: 取得目前所在的位置
```bash=
pwd
```
> 會顯示目前自己在哪個目錄底下
- **ls**: 列出當前的目錄檔案列表
```bash=
ls
```
- **cd**: 切換目錄(directiry),目錄可以想像成Windows系統中的資料夾,就像是一個放檔案的容器
```bash=
cd my_directiry
```
> 空格後加上要切換到的目錄名稱,必須是已經存在的目錄
- **mkdir**: 建立新的目錄
```bash=
mkdir my_directiry2
```
> 空格後加上要創建的新目錄名稱
- **nano**: 使用nano文字編輯器開啟文字檔(進入編輯介面),==程式都要打在文字檔裡面==
```bash=
nano my_file
```
> 空格後加上要編輯的文字檔,如果該檔案不存在就會自動建立新的文字檔
> `ctrl+x`可以退出編輯介面,會詢問是否儲存新的修改內容,按`Y`(yes)/`N`(no)
- **rm**: 刪除檔案
```bash=
rm my_file
```
> 空格後加上要刪除的檔案名稱,謹慎刪除(無法復原)
- **rmdir**: 刪除目錄,使用方式同上
### 作業
- 安裝 [Pietty/Putty](https://moodle.ncnu.edu.tw/mod/resource/view.php?id=679056)
- 連線至 lilina.csie.ncnu.edu.tw (163.22.17.162)
> 使用帳號密碼成功登入後會進到自己的home目錄,是屬於自己的個人空間
- 在目錄`~/c-1001/exer1`底下建立檔案`hello.c`
```bash=
cd ~/c-1001/exer1
nano hello.c
```
- 接著會進到編輯介面,輸入下方程式
```c=
#include <stdio.h>
int main()
{
printf("hello, world!\n");
}
```
> `ctrl-x`:退出編輯,按`Y`儲存內容
- 用 gcc 編譯`hello.c`
```bash=
gcc hello.c
```
- 得到`a.out`並執行
```bash=
./a.out
```
- 成功的話會顯示程式執行結果
```
hello, world!
```
## String op
- `strncpy`
```c=
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "This is a string.";
char sub[5];
strncpy(sub,s,4);
printf("%s", sub);
return 0;
}
```
- `strtok`
```c=
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "This is a string.";
char *sub = strtok(s," ");
printf("%s", sub);
return 0;
}
```
- [Substring function DIY](https://job.achi.idv.tw/2013/12/02/in-the-c-language-substr/)
## ex9
### ex9a.c
```c=
#include <stdio.h>
void print_arr(int a[], int N, int W)
{
int R = N/W;
if (N % W > 0) R = R + 1;
for (int i = 0; i < R; i++)
{
for (int j = 0; j < W; j++)
{
int index = i*W + j;
if(index < N)
printf("%5d", a[index]);
}
printf("\n");
}
}
int main()
{
int N, W;
scanf("%d %d", &N, &W);
int a[N];
for(int i = 0; i < N; i++)
scanf("%d", &a[i]);
print_arr(a, N, W);
return 0;
}
```
### ex9b.c
```c=
#include <stdio.h>
void reverse(int a[], int count)
{
int sum = count - 1;
for (int i = 0; i < count/2; i++)
{
int tmp = a[sum - i];
a[sum - i] = a[i];
a[i] = tmp;
}
}
int main()
{
int num, count = 0;
int a[50];
while(scanf("%d", &num) != EOF)
{
a[count] = num;
count++;
}
reverse(a,count);
for(int i = 0; i <= count; i++)
printf("\n%d", a[i]);
return 0;
}
```
### ex9c.c
```c=
#include <stdio.h>
int main()
{
while(1)
{
int R, C;
scanf("%d %d", &R, &C);
if (R == 0 || C == 0) break;
char a[R][C];
for(int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
a[i][j] = '.';
}
while(1)
{
int n;
scanf("%d", &n);
if (n == 0) break;
else if (n > 0)
{
n = n-1;
for(int j = 0; j < C; j++)
a[n][j] = 'o';
}
else
{
n = -n -1;
for(int i = 0; i < R; i++)
a[i][n] = 'o';
}
}
for(int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
printf("%c", a[i][j]);
printf("\n");
}
}
return 0;
}
```
## Structure