2011年12月25日 星期日

100設計一甲_Merry Christmas

Merry Christmas tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
void triangel(int num){
int i,j,space=num-1,x;
for (i=1 ; i<=num ; i++,space--){
for(x=1;x<=space;x++)
printf(" ");
for (j=1 ; j<=(-1+2*i) ; j++)
printf("*");
printf("\n");
}
for(i=1;i<=num;i++){
for(x=1;x<=num-1;x++)
printf(" ");
printf("*\n");
}
for(i=1;i<=num-1-5;i++){
printf(" ");
}
printf("Merry Christmas!");
}
int main(void){
triangel(10);
return 0;
}

2011年12月20日 星期二

100設計一甲_week15

libw15.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>
int add(int a, int b, int c) {
return a+b+c;
}
double add2(double a, double b, double c) {
return a+b+c;
}
int mul(int a, int b, int c) {
return a*b*c;
}
ex1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#coding: utf-8
# 本程式透過 ctype 模組, 直接導入 dll 動態連結庫, 並使用其函式
from ctypes import cdll,c_int,c_double
lib = "libw15.dll"
dll = cdll.LoadLibrary(lib)
#add = (lambda x,y: dll.add(c_int(x), c_int(y)))
# 以上 lambda 函式定義與下列函式相同
def add(x,y,z):
return dll.add(c_int(x), c_int(y), c_int(z))
'''
ctypes 介面僅支援整數, 長整數與字串資料型別, 若使用(雙)浮點數或其他 ctype 所不支援的
資料型別, 則必須要明確告訴 ctypes 如何進行函式傳回值的轉換, 否則無法得到精確資料
'''
def add2(x,y,z):
return dll.add2(c_double(x), c_double(y), c_double(z))
# 指定 add2 函式的資料傳回型別為 c_double
dll.add2.restype = c_double
multiply = (lambda x,y,z: dll.mul(c_int(x), c_int(y), c_int(z)))
變數1 = 12
變數2 = 10
變數3 = 15
print ("變數1加上變數2加上變數3, 其值為:",add(變數1,變數2,變數3))
print ("變數1乘上變數2乘上變數3, 其值為:",multiply(變數1,變數2,變數3))
'''
15*12 = 15*2+15*10 = 150+30 = 180
15*12 = 15*2*6 = 180
'''
變數4 = 12.345
變數5 = 10.6
變數6 = 66.6
print ("變數4加上變數5加上變數6, 其值為:",add2(變數4,變數5,變數6))
123.c complier出來的dll重新命名為->sum.pyd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <Python.h>
int sum2(int a, int b, int c);
int sum2(int a, int b, int c){
return a+b+c;
}
static PyObject* mod_sum(PyObject *self, PyObject *args)
{
int a;
int b;
int c;
int s;
if (!PyArg_ParseTuple(args,"iii",&a,&b,&c))
return NULL;
s = sum2(a,b,c);
return Py_BuildValue("i",s);
}
static struct PyMethodDef Mod_Methods[] = {
{"sum", mod_sum, METH_VARARGS, "Description.."},
{NULL,NULL,0,NULL}
};
static struct PyModuleDef ModMethods = {
PyModuleDef_HEAD_INIT,
"SumModule",
"SumModule_doc",
-1,
Mod_Methods
};
PyMODINIT_FUNC
PyInit_sum(void)
{
(void) PyModule_Create(&ModMethods);
}
ex2.py
1
2
3
4
5
6
7
8
'''
from sum import *
print(sum(10,20))
'''
import sum
print(sum.sum(10,20,30))
w15.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#define pi 3.14159
double circlearea(double r){
return r*r*pi;
}
int plus(int a,int b,int c){
return a*b-c;
}
float bmi(float heigh,float weight){
return weight/(heigh*heigh);
}
char *mystring(char str[]){
return str;
//printf("%s",str);
}
int main(void) {
printf("%e\n",circlearea(1.));
printf("%d\n",plus(1.5,6,5));
printf("%d\n",plus(1,6,5));
printf("%f\n",bmi(1.8,60));
printf("%s\n",mystring("asd"));
mystring("asd");
return 0;
}

100設計一甲_week14

http://blog.kmol.info/?p=617
講解範例1~4、8~10。

2011年12月6日 星期二

100設計一甲_week13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
void fun1(){
int i=0;
printf("while test1----\n");
while(i<15){
printf("%d\n",i);
i++;
}
i=15;
printf("while test2----\n");
while(i){
printf("%d\n",i);
i--;
}
printf("for test1----\n");
for(i=0;i<15;i++){
printf("%d\n",i);
}
}
int main(){
int num1 = 123;
float num2 = 3.14159;
double num3 = 3.14159e-3;
char name1[6] = {'h','e','l','l','o','\0'};
char name2[6] = "hello";
/*
字串最後必須加上結束\0字元,代表字串結束
hello 為五個字元 但最後必須加上\0,所以為六個字元。
*/
printf("%d\n", num1);
printf("%f\n", num2);
printf("%e\n", num3);
printf("%s\n", name1);
printf("%s\n", name2);
printf("%c\n", name1[1]);
printf("%c\n", name2[1]);
fun1();
return 0;
}