Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
[C] 陣列:元素個數、列數、行數
#include <stdio.h>
#include <stdlib.h>
int main() {
// 成本邊線陣列: 邊線數 = [8] {from, to, weight}
int edge[8][3] = { { 1, 2, 2 },
{ 2, 4, 3 },
{ 1, 4, 4 },
{ 3, 5, 5 },
{ 2, 5, 6 },
{ 2, 3, 8 },
{ 3, 4, 10 },
{ 4, 5, 15 } };
int a, b, c;
// 元素個數 a=24 列數 b=8 行數 c=3
a = sizeof(edge)/sizeof(edge[0][0]);
b = sizeof(edge)/sizeof(edge[0]);
c = a/b;
printf("%d \n", a);
printf("%d \n", b);
printf("%d \n", c);
system("pause");
return 0;
}
#include <stdlib.h>
int main() {
// 成本邊線陣列: 邊線數 = [8] {from, to, weight}
int edge[8][3] = { { 1, 2, 2 },
{ 2, 4, 3 },
{ 1, 4, 4 },
{ 3, 5, 5 },
{ 2, 5, 6 },
{ 2, 3, 8 },
{ 3, 4, 10 },
{ 4, 5, 15 } };
int a, b, c;
// 元素個數 a=24 列數 b=8 行數 c=3
a = sizeof(edge)/sizeof(edge[0][0]);
b = sizeof(edge)/sizeof(edge[0]);
c = a/b;
printf("%d \n", a);
printf("%d \n", b);
printf("%d \n", c);
system("pause");
return 0;
}
[C][資料結構] 06-堆疊-後序表示法
06-堆疊-後序表示法
https://drive.google.com/open?id=0B1cXVSjadhC0WThhY25ZUzFCYlk
https://drive.google.com/open?id=0B1cXVSjadhC0WThhY25ZUzFCYlk
|
中序(Infix)
|
前序(Prefix)
|
後序(Postfix)
|
|
A*B+C
|
+*ABC
|
AB*C+
|
|
人類易讀
|
|
計算機不用考慮運算子的優先順序,讀取後立即執行運算
|
|
中序(Infix)
|
A*(B+C)
|
A+B*(C+D)-E
|
6*7+(4+5)
|
|
轉換過程
|
A(BC+)*
ABC+*
|
A+(B(CD+)*)-E
(AB(CD+)*+)-E
(AB(CD+)*+)E-
ABCD+*+E-
|
(67*)+(45+)
67*45++
|
|
後序(Postfix)
|
ABC+*
|
ABCD+*+E-
|
67*45++
|
[C][資料結構] 02-鏈結串列(Linked List)基本介紹
鏈結串列(Linked List)基本介紹
https://drive.google.com/open?id=0B1cXVSjadhC0VG16bE5hbU44UVk
https://drive.google.com/open?id=0B1cXVSjadhC0VG16bE5hbU44UVk
Array 陣列
|
Struct 結構
|
Linked List 鏈結串列
|
|
資料型態
|
多筆、同類型的資料
|
單筆、不同類型的資料
|
多筆、不同類型的資料
(相同結構的多個結構變數)
|
記憶體配置
(資料筆數)
|
固定大小(固定筆數)
靜態資料結構(static)
無效率
|
動態配置
動態資料結構(dynamic)
可彈性修改
有效率
|
|
要件
|
必須事先得知資料個數
|
可任意增減,不必事先知道
資料個數
|
|
資料存取
|
可利用索引值存取任意元素
|
必須一一走訪每個節點
|
|
資料增刪
|
陣列大小固定,難以增刪,尤其多維陣列更難以處理資料的增刪
|
只需更改指標指向,不必搬移元素,增刪容易而具有彈性
|
[C] 遞迴練習
// 遞迴練習
#include <stdio.h>
#include <stdlib.h>
void ape(int x, int y)
{
int total;
printf("(x, y)=(%2d, %2d)\n", x, y);
/*******************************
遞迴:結束條件 + 自我呼叫
*******************************/
// 結束條件
if (x<0 && y<0)
total = (x+y)*10;
// 自我呼叫
else{
--x;
--y;
ape(x, y);
}
printf("\ntotal = %d\n", total);
system("pause");
}
int main()
{
int x=9, y=8;
ape(x, y);
}
#include <stdio.h>
#include <stdlib.h>
void ape(int x, int y)
{
int total;
printf("(x, y)=(%2d, %2d)\n", x, y);
/*******************************
遞迴:結束條件 + 自我呼叫
*******************************/
// 結束條件
if (x<0 && y<0)
total = (x+y)*10;
// 自我呼叫
else{
--x;
--y;
ape(x, y);
}
printf("\ntotal = %d\n", total);
system("pause");
}
int main()
{
int x=9, y=8;
ape(x, y);
}
[C] 利用 struct 回傳多個參數
/* 利用 struct 回傳多個參數 */
#include <stdio.h>
#include <stdlib.h>
// 定義新的變數型態 struct num
struct num{
int v1, v2;
};
// 副函式:回傳新的變數型態 struct num
struct num oper(struct num);
// 主函式
int main()
{
struct num XandY, result;
XandY.v1=1;
XandY.v2=2;
result = oper(XandY);
printf("%d , %d\n", result.v1, result.v2);
system("pause");
return 0;
}
struct num oper(struct num XandY)
{
struct num CandD;
CandD.v1 = XandY.v1;
CandD.v2 = XandY.v2;
return CandD;
}
#include <stdio.h>
#include <stdlib.h>
// 定義新的變數型態 struct num
struct num{
int v1, v2;
};
// 副函式:回傳新的變數型態 struct num
struct num oper(struct num);
// 主函式
int main()
{
struct num XandY, result;
XandY.v1=1;
XandY.v2=2;
result = oper(XandY);
printf("%d , %d\n", result.v1, result.v2);
system("pause");
return 0;
}
struct num oper(struct num XandY)
{
struct num CandD;
CandD.v1 = XandY.v1;
CandD.v2 = XandY.v2;
return CandD;
}
[C][104資料結構][HwA] Operation of Sparse Matrix
資料結構(Data Structures) 程式作業-A 以陣列做稀疏矩陣之運算
以Ch2-4.c 的稀疏矩陣陣列表示法為基礎,擴充支援稀疏矩陣之加、減、乘、轉置,等運算。
由『敎學支援平台』本課程之「$程式作業-規格要求」中下載ExerciseA.rar。
ExerciseA.rar內之檔案:
ExerciseA.h :宣告用標頭檔,不應有所更動。
ExerciseA.c : 作業中要求補齊功能的五個函數應由學生定義在其中
• void printSparseMatrix(Matrix *mat)
• void transSparseMatrix(Matrix *result, Matrix *source)
• void addSparseMatrix(Matrix *sum, Matrix *matA, Matrix *matB)
• void subSparseMatrix(Matrix *diff, Matrix *matA, Matrix *matB)
• void multSparseMatrix(Matrix *prod, Matrix *matA, Matrix *matB)
ExerA-Data.c :含矩陣輸入資料與運算驅動程式
作業繳交時,學生必須包括至少一份可正確執行的測試資料(即ExerA-Data.c )
作業批改時,助教會換上新的測試資料(新的ExerA-Data.c )以驗證程式正確性
※程式功能要求
補上這五個C函數的定義(在ExerciseA.c 之中)
• printSparseMatrix(Matrix *mat)
用長方型格式輪出稀疏矩陣*mat;原稀疏矩陣無值的項補零印出
• transSparseMatrix(Matrix *result, Matrix *source)
將稀疏矩陣*source 轉置(transpose),置入稀疏矩陣*result 之中
• addSparseMatrix(Matrix *sum, Matrix *matA, Matrix *matB)
將矩陣*matA 加上矩陣*matB, 結果置入矩陣*sum 之中
(*sum) = (*matA) + (*matB)
• subSparseMatrix(Matrix *diff, Matrix *matA, Matrix *matB)
將矩陣*matA 減去矩陣*matB, 結果置入矩陣*diff 之中
(*diff) = (*matA) - (*matB)
• multSparseMatrix(Matrix *prod, Matrix *matA, Matrix *matB)
將矩陣*matA 乘上*matB, 結果置入矩陣*prod 之中
(*prod) = (*matA) X (*matB)
注意參數的順序!
※書面報告
1.時間複雜度分析.doc
以Big Oh, O()符號, 表示出你的程式所用之演算法,各計算函數之時間複雜度,
表示成rows, cols, numOfTerms 的函數。參考課本p.1-31頁「1-7-1 Big Oh函數的基礎」,
簡單分析並概要說明如下你寫的C函數其時間複雜度為何?
1) void transSparseMatrix(Matrix *result, Matrix *source)以r, c, n, 表示
2) void addSparseMatrix(Matrix *sum, Matrix *matA, Matrix *matB) &
void subSparseMatrix(Matrix *diff, Matrix *matA, Matrix *matB)
以r, c, nA, nB, 表示(note: r = rA = rB, c = cA = cB )
3) void multSparseMatrix(Matrix *prod, Matrix *matA, Matrix *matB)
以rA, cr, nA, cB, nB, 表示(note: cr = cA = rB )
2.程式作業A說明.doc
概要說明程式使用及設計方式,及程式撰寫及測試過程是否遭遇困難及解決之道。
以上分為兩個MS Word檔,連同程式碼一併繳交。
題目:ExerciseA.rar
https://drive.google.com/open?id=0B1cXVSjadhC0R0djVmZTcUVCdFE
以Ch2-4.c 的稀疏矩陣陣列表示法為基礎,擴充支援稀疏矩陣之加、減、乘、轉置,等運算。
由『敎學支援平台』本課程之「$程式作業-規格要求」中下載ExerciseA.rar。
ExerciseA.rar內之檔案:
ExerciseA.h :宣告用標頭檔,不應有所更動。
ExerciseA.c : 作業中要求補齊功能的五個函數應由學生定義在其中
• void printSparseMatrix(Matrix *mat)
• void transSparseMatrix(Matrix *result, Matrix *source)
• void addSparseMatrix(Matrix *sum, Matrix *matA, Matrix *matB)
• void subSparseMatrix(Matrix *diff, Matrix *matA, Matrix *matB)
• void multSparseMatrix(Matrix *prod, Matrix *matA, Matrix *matB)
ExerA-Data.c :含矩陣輸入資料與運算驅動程式
作業繳交時,學生必須包括至少一份可正確執行的測試資料(即ExerA-Data.c )
作業批改時,助教會換上新的測試資料(新的ExerA-Data.c )以驗證程式正確性
※程式功能要求
補上這五個C函數的定義(在ExerciseA.c 之中)
• printSparseMatrix(Matrix *mat)
用長方型格式輪出稀疏矩陣*mat;原稀疏矩陣無值的項補零印出
• transSparseMatrix(Matrix *result, Matrix *source)
將稀疏矩陣*source 轉置(transpose),置入稀疏矩陣*result 之中
• addSparseMatrix(Matrix *sum, Matrix *matA, Matrix *matB)
將矩陣*matA 加上矩陣*matB, 結果置入矩陣*sum 之中
(*sum) = (*matA) + (*matB)
• subSparseMatrix(Matrix *diff, Matrix *matA, Matrix *matB)
將矩陣*matA 減去矩陣*matB, 結果置入矩陣*diff 之中
(*diff) = (*matA) - (*matB)
• multSparseMatrix(Matrix *prod, Matrix *matA, Matrix *matB)
將矩陣*matA 乘上*matB, 結果置入矩陣*prod 之中
(*prod) = (*matA) X (*matB)
注意參數的順序!
※書面報告
1.時間複雜度分析.doc
以Big Oh, O()符號, 表示出你的程式所用之演算法,各計算函數之時間複雜度,
表示成rows, cols, numOfTerms 的函數。參考課本p.1-31頁「1-7-1 Big Oh函數的基礎」,
簡單分析並概要說明如下你寫的C函數其時間複雜度為何?
1) void transSparseMatrix(Matrix *result, Matrix *source)以r, c, n, 表示
2) void addSparseMatrix(Matrix *sum, Matrix *matA, Matrix *matB) &
void subSparseMatrix(Matrix *diff, Matrix *matA, Matrix *matB)
以r, c, nA, nB, 表示(note: r = rA = rB, c = cA = cB )
3) void multSparseMatrix(Matrix *prod, Matrix *matA, Matrix *matB)
以rA, cr, nA, cB, nB, 表示(note: cr = cA = rB )
2.程式作業A說明.doc
概要說明程式使用及設計方式,及程式撰寫及測試過程是否遭遇困難及解決之道。
以上分為兩個MS Word檔,連同程式碼一併繳交。
題目:ExerciseA.rar
https://drive.google.com/open?id=0B1cXVSjadhC0R0djVmZTcUVCdFE
[圖解C] Example 908: there are some data with the same structure
// Example 908: there are some data with the same structure
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct student
{
char name[10];
int math;
int english;
};
// declare an array with the same structure
struct student classA[3] =
{{"Jay", 87, 69}, {"Jolin", 77, 88}, {"Henrry", 78, 70}};
int i;
float aveMath=0, aveEng=0;
// print by using array
for (i=0 ; i<3 ; i++){
printf("%s \t math= %d \t english= %d \n",
classA[i].name, classA[i].math, classA[i].english);
aveMath += classA[i].math;
aveEng += classA[i].english;
}
printf("============================\n");
// print by using pointer
for (i=0 ; i<3 ; i++){
printf("%s \t math= %d \t english= %d \n",
(classA+i)->name, (classA+i)->math, (classA+i)->english);
}
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct student
{
char name[10];
int math;
int english;
};
// declare an array with the same structure
struct student classA[3] =
{{"Jay", 87, 69}, {"Jolin", 77, 88}, {"Henrry", 78, 70}};
int i;
float aveMath=0, aveEng=0;
// print by using array
for (i=0 ; i<3 ; i++){
printf("%s \t math= %d \t english= %d \n",
classA[i].name, classA[i].math, classA[i].english);
aveMath += classA[i].math;
aveEng += classA[i].english;
}
printf("============================\n");
// print by using pointer
for (i=0 ; i<3 ; i++){
printf("%s \t math= %d \t english= %d \n",
(classA+i)->name, (classA+i)->math, (classA+i)->english);
}
system("pause");
return 0;
}
[圖解C] Example 907: there are some data with the same structure
// Example 907: there are some data with the same structure
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct student
{
char name[10];
int math;
int english;
};
// declare an array with the same structure
struct student classA[3] =
{{"Jay", 87, 69}, {"Jolin", 77, 88}, {"Henrry", 78, 70}};
int i;
float aveMath=0, aveEng=0;
for (i=0 ; i<3 ; i++){
printf("%s \t math= %d \t english= %d \n",
classA[i].name, classA[i].math, classA[i].english);
aveMath += classA[i].math;
aveEng += classA[i].english;
}
printf("============================\n");
printf(" Math average = %f\n", aveMath/3);
printf(" English average = %f\n", aveEng/3);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct student
{
char name[10];
int math;
int english;
};
// declare an array with the same structure
struct student classA[3] =
{{"Jay", 87, 69}, {"Jolin", 77, 88}, {"Henrry", 78, 70}};
int i;
float aveMath=0, aveEng=0;
for (i=0 ; i<3 ; i++){
printf("%s \t math= %d \t english= %d \n",
classA[i].name, classA[i].math, classA[i].english);
aveMath += classA[i].math;
aveEng += classA[i].english;
}
printf("============================\n");
printf(" Math average = %f\n", aveMath/3);
printf(" English average = %f\n", aveEng/3);
system("pause");
return 0;
}
[圖解C] Example 904: pointer to struct
// Example 904: pointer to struct
// 2 ways to access
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct animal
{
float weight;
int age;
}tiger = {180, 3};
// declare a pointer to struct
struct animal *ptr;
ptr = &tiger; // deposit address of tiger in ptr
// way 1 to access the member of struct: pointer -> variable
// way 2 to access the member of struct: *
printf("weight: %.2f\n", ptr -> weight);
printf("weight: %d\n", (*ptr).age);
system("pause");
return 0;
}
// 2 ways to access
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct animal
{
float weight;
int age;
}tiger = {180, 3};
// declare a pointer to struct
struct animal *ptr;
ptr = &tiger; // deposit address of tiger in ptr
// way 1 to access the member of struct: pointer -> variable
// way 2 to access the member of struct: *
printf("weight: %.2f\n", ptr -> weight);
printf("weight: %d\n", (*ptr).age);
system("pause");
return 0;
}
[圖解C] Example 901: foundation of struct (2)
// Example 901: foundation of struct (2)
#include <stdio.h>
#include <stdlib.h>
// declare a struct (type_name)
struct TTarea
{
int aa, bb;
};
int main()
{
// variable name (with content)
struct TTarea f1, f2, f3={212, 610};
printf("aa1, bb1: ");
scanf("%d %d", &f1.aa, &f1.bb);
printf("aa2, bb2: ");
scanf("%d %d", &f2.aa, &f2.bb);
printf("area1=%d\n", f1.aa * f1.bb);
printf("area2=%d\n", f2.aa * f2.bb);
printf("area3=%d\n", f3.aa * f3.bb);
printf("----------------------\n");
// edit the content of f1 and f2
f1.aa=1;
f1.bb=10;
f2.aa=12;
f2.bb=5;
printf("area1=%d\n", f1.aa * f1.bb);
printf("area2=%d\n", f2.aa * f2.bb);
printf("area2=%d\n", f3.aa + f3.bb);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// declare a struct (type_name)
struct TTarea
{
int aa, bb;
};
int main()
{
// variable name (with content)
struct TTarea f1, f2, f3={212, 610};
printf("aa1, bb1: ");
scanf("%d %d", &f1.aa, &f1.bb);
printf("aa2, bb2: ");
scanf("%d %d", &f2.aa, &f2.bb);
printf("area1=%d\n", f1.aa * f1.bb);
printf("area2=%d\n", f2.aa * f2.bb);
printf("area3=%d\n", f3.aa * f3.bb);
printf("----------------------\n");
// edit the content of f1 and f2
f1.aa=1;
f1.bb=10;
f2.aa=12;
f2.bb=5;
printf("area1=%d\n", f1.aa * f1.bb);
printf("area2=%d\n", f2.aa * f2.bb);
printf("area2=%d\n", f3.aa + f3.bb);
system("pause");
return 0;
}
[圖解C] Example 901: foundation of struct (1)
// Example 901: foundation of struct (1)
#include <stdio.h>
#include <stdlib.h>
int main()
{
// declare a struct
struct
{
int aa, bb;
}f1, f2, f3={212, 610}; // variable name (with content)
printf("aa1, bb1: ");
scanf("%d %d", &f1.aa, &f1.bb);
printf("aa2, bb2: ");
scanf("%d %d", &f2.aa, &f2.bb);
printf("area1=%d\n", f1.aa * f1.bb);
printf("area2=%d\n", f2.aa * f2.bb);
printf("area3=%d\n", f3.aa * f3.bb);
printf("----------------------\n");
// edit the content of f1 and f2
f1.aa=1;
f1.bb=10;
f2.aa=12;
f2.bb=5;
printf("area1=%d\n", f1.aa * f1.bb);
printf("area2=%d\n", f2.aa * f2.bb);
printf("area2=%d\n", f3.aa + f3.bb);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
// declare a struct
struct
{
int aa, bb;
}f1, f2, f3={212, 610}; // variable name (with content)
printf("aa1, bb1: ");
scanf("%d %d", &f1.aa, &f1.bb);
printf("aa2, bb2: ");
scanf("%d %d", &f2.aa, &f2.bb);
printf("area1=%d\n", f1.aa * f1.bb);
printf("area2=%d\n", f2.aa * f2.bb);
printf("area3=%d\n", f3.aa * f3.bb);
printf("----------------------\n");
// edit the content of f1 and f2
f1.aa=1;
f1.bb=10;
f2.aa=12;
f2.bb=5;
printf("area1=%d\n", f1.aa * f1.bb);
printf("area2=%d\n", f2.aa * f2.bb);
printf("area2=%d\n", f3.aa + f3.bb);
system("pause");
return 0;
}
Subscribe to:
Posts (Atom)