2012年10月26日 星期五

101設計一甲_w6

#此為注釋

"""
test
此為範圍註釋
"""

#9*9*9 乘法表
def multi():
    for num1 in range(1,10):
        for num2 in range(1,10):
            for num3 in range(1,10):
                print(num1,'*',num2,'*',num3,'=',num1*num2*num3,sep='')

#how to use while
def sample_while():
    n = 0
    while (n<100):
        print(n)
        n = n + 1
def sample_if():
    num = (int)(input('enter a number:'))
    if(num>0):
        print('this number is positive number')
    elif(num<0):
        print('this number is negative number')
    else:
        print('this number is zero')
#use while and if
def sample_guessnumber():
    answer = 76
    guess = None
    count = 0
    print('guess a number,if you guess a right number,loop will berak.')
    while(answer!=guess):
        guess = int(input('guess a number:'))
        count = count + 1
    print('you guess',count,'time')
import math
def sample_w3():
    直徑 = 1
    # pi 與 e 屬於 math 模組(類別)屬性
    轉動慣性矩 = math.pi*直徑**4/64
    截面模數 = math.pi*直徑**3/32
    迴轉半徑 = 直徑/4
    截面積 = math.pi*(直徑/2)**2
    print('轉動慣性矩:',轉動慣性矩)
    print('截面模數:',截面模數)
    print('截面積:',截面積)
    # 利用 round() 取到小數點 3 位數
    print('轉動慣性矩取到小數點第三位:',round(轉動慣性矩, 3))
def sample_w3_exten():
    for 直徑 in range(1,10):
        print('直徑:',直徑)
        # pi 與 e 屬於 math 模組(類別)屬性
        轉動慣性矩 = math.pi*直徑**4/64
        截面模數 = math.pi*直徑**3/32
        迴轉半徑 = 直徑/4
        截面積 = math.pi*(直徑/2)**2
        print('轉動慣性矩:',轉動慣性矩)
        print('截面模數:',截面模數)
        print('截面積:',截面積)
        # 利用 round() 取到小數點 3 位數
        print('轉動慣性矩(取到小數點第三位):',round(轉動慣性矩, 3))
sample_w3_exten()


2011年12月25日 星期日

100設計一甲_Merry Christmas

Merry Christmas tree.c

#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
#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
#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

#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

'''
from sum import *
 
print(sum(10,20))
'''
import sum
 
print(sum.sum(10,20,30))
w15.c
#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

#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;
}

2011年11月29日 星期二

100設計一甲_week12 小考答案

exam1.c
#include<stdio.h>
void q1(){
  int i;
  int num1=0,num3=0;
  int total=0;
  for(i=399;i<=8993;i++){
    num1=i%10;
    num3=(i/100)%10;
    if((num1+num3)==11){
      total+=i;
    }
  }
  printf("%d\n",total);  
}
void q2(){
  int i;
  int num1=0,num3=0;
  int total=0;
  for(i=359;i<=8691;i++){
    if(i%7==0 && i%2==0){
      total+=i;
    }
  }
  printf("%d\n",total);  
}
void q3(){
  int i;
  int num1=0,num3=0;
  int total=0;
  for(i=95;i<=8505;i++){
    if(i%14!=0){
      total+=i;
    }
  }
  printf("%d\n",total);  
}
void q4(){
  int i=0,j=0;
  int total=0;
  for(i=473;i<=8393;i++){
    int count=0;
    j=i;
    while(j!=0 && count==0){
      if(j%10==3||j%10==6||j%10==8)
        count++;
      j=j/10;
    }
    if(count!=0)
      total+=i;
  }
  printf("%d\n",total);  
}
void q5(){
  int i;
  int num1=0,num3=0;
  int total=0;
  for(i=486;i<=8817;i++){
    num1=i%10;
    num3=(i/100)%10;
    if((num1*num3)==16)
      total+=i;
  }
  printf("%d\n",total);  
}
int main(){
  q1();
  q2();
  q3();
  q4();
  q5();
  return 0;
}
exam2.py
def q1():
  total=0;
  for i in range(399,8993+1):
    num1=i%10;
    num3=int(i/100)%10;
    if((num1+num3)==11):
      total+=i;
  print(total)
def q2():
  total=0
  for i in range(359,8691+1):
    if(i%7==0 and i%2==0):
      total+=i
  print(total)
def q3():
  total=0
  for i in range(95,8505+1):
    if(i%14!=0):
      total+=i
  print(total)
def q4():
    total=0
    for 索引 in range(473,8393+1):
        if str(索引).find("3") != -1 or str(索引).find("6") != -1 or str(索引).find("8") != -1:
          total+=索引
    print(total)
    
def q5():
  total=0;
  for i in range(486,8817+1):
    num1=i%10;
    num3=int(i/100)%10;
    if((num1*num3)==16):
      total+=i;
  print(total)
q1()
q2()
q3()
q4()
q5()

100設計一甲_week12

example1.c
#include<stdio.h>
int main(){
    int num1 = 123;
    float num2 = 3.14159;
    double num3 = 3.14159e-3;
    char name1[6] = "hello";
    char name2[6]={'h','e','l','l','o','\0'};
    /*
    字串最後必須加上結束\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]);
    return 0;
}

Python Module in C 動態教學(Save as...)

Python_module.7z

tcc_gnuplot3.7z