總網頁瀏覽量

顯示具有 c 標籤的文章。 顯示所有文章
顯示具有 c 標籤的文章。 顯示所有文章

2012年3月30日 星期五

argc argv

argc argc用法

int main(int argc, char **argv)

假設執行為:  test.exe  a b c

則輸出為:   argv[0] = test.exe, argv[1] = a, argv[2] = b, argv[3] = c

argc = 4

2012年1月27日 星期五

C指標

指標 int *ptr;   陣列 int array[10];

陣列指定給指標:
ptr = array;  或者是  ptr = &array[0];
寫array跟&array[0]是相同的
===> a[i]就可寫為 *(a + i)

若指標ptr為指向陣列的第一個元素array[0]
則 *(ptr + 1)表示指向array[1], 第二個元素 ===> *(ptr+i) 指向 array[i]

char *array[] 指標陣列 ===> *array[i]指向文字列的第一個字元
char (*array)[] 指標指向陣列

int *f(); f為函數, 傳回值為指向整數的指標   ----------    函式返回指標
int (*f)(); f是個指標, 指向一個會傳回整數的函數   ---------   函式指標

const char i=5; 不可改值寫法
int* const: 指向const的指標 可以改值, 不可以改位址
const int* const:指向constconst指標


typedef int (*fp)(char a);
fp = fun;   //fun為一個指向函數的函數指標(參數為char值, 返回值為int)
int x = fun('x');

char (*(*x( )) [] )( ):函數, 傳回一指標,該指標指向指標陣列, 陣列中各元素指向一個會傳回char值的函數

char (*(*x[3]) ( )) [5]: 指標陣列, 有3個元素, 各指向一個會傳回另一指標的函數, 該指標是指向含有5個元素的字元陣列


錯誤: int *ptr;
        *ptr = 35;

正確: int *ptr, number = 10;
        ptr = &number;

ptri 位址為 0012ff84
ptri +1 位址改為  0012ff88
取得鄰近位址內的值 *(ptr+1)



記憶體配置:
配置一個浮點數指標
float *fp;
fp = (float *)malloc(sizeof(float));
free(fp);


int *num;
num = (int *)malloc(sizeof(int) * 10)
free(num)

int array[10][20]

int **array= (int **)malloc(sizeof(int*)*10);
for(i=0;i<10;i++)
   *(array+i) = (int *)malloc(sizeof(int) * 20);


------------------------------------------------------------------------------------

struct grade
{
    int math;
    int english;
    int computer;
};
struct grade *student;
student = (struct grade *) malloc(num * sizeof(struct grade));

------------------------------------------------------------------------------------

鏈結串列:
struct list                    
{
   int num;                    
   char name[10];            
   char address[50];    
   struct list *next;
};
typedef struct list node;
typedef node *link;

ptr = ( link ) malloc(sizeof(node));
ptr->next = NULL;

------------------------------------------------------------------------------------






















2011年12月12日 星期一


現在網路上很多都有提供電影字幕或是連續劇字幕的資源,也有不少學習英文的人喜歡邊看影片邊練習英聽或是會話,所以這個程式運用在練習中英閱讀及會話方面,透過隨手可得的字幕檔後,將其合併來練習。當中可練習不少開關檔案的語法,及思考排版的設計,在程式中用了4stream來協助讀檔與寫檔。

執行內容及結果
英文字幕檔內容
1
00:03:27,600 --> 00:03:28,400
Little girl?

2
00:03:30,900 --> 00:03:31,700
I'm a policeman.

3
00:03:32,700 --> 00:03:33,500
Little girl.

4
00:03:38,600 --> 00:03:39,700
Don't be afraid.

中文字幕檔內容
1
00:03:27,600 --> 00:03:28,400
小姑娘?

2
00:03:30,900 --> 00:03:31,700
我是警察

3
00:03:32,700 --> 00:03:33,500
小姑娘

4
00:03:38,600 --> 00:03:39,700
別害怕

合併中英字幕結果
Little girl?
小姑娘?

I'm a policeman.
我是警察

Little girl.
小姑娘

Don't be afraid.
別害怕

Little girl.
小姑娘

//中英字幕合併By Cheng yu-zheng
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


FILE *stream_eng, *check_eng, *stream_cht, *check_cht, *write_data;
char checkString1[100], writeString1[100], checkString2[100], writeString2[100];
int counter_eng = 2, counter_cht = 2;
int flag_eng = 0, flag_cht = 0;

void write_eng(char checkString1[], FILE *check_eng, FILE *stream_eng, FILE *write_data, FILE *check_cht, FILE *stream_cht);
int call_print(FILE *stream_eng,  FILE *write_data);
void write_cht(char checkString2[], FILE *check_cht);
int call_print_cht(FILE *stream_cht,  FILE *write_data);

int main(void)
{
        stream_eng = fopen("eng1.txt", "r");
        check_eng = fopen("eng1.txt", "r");
        stream_cht = fopen("cht1.txt", "r");
        check_cht = fopen("cht1.txt", "r");
        write_data = fopen("text.txt", "w");

        if(stream_eng == NULL || stream_cht == NULL || check_eng == NULL ||  check_cht == NULL || write_data == NULL)
        {
                printf("開始檔案失敗, 請將正確檔案放入資料夾內");
        }
        else
        {
                write_eng(checkString1, check_eng, stream_eng, write_data, check_cht, stream_eng);
        }
        fclose(stream_eng);
        fclose(check_eng);
        fclose(stream_cht);
        fclose(check_cht);
        fclose(write_data);
}

void write_eng(char checkString1[], FILE *check_eng, FILE *stream_eng, FILE *write_data, FILE *check_cht, FILE *stream_cht)
{
        while(fgets(checkString1, 100, check_eng) != NULL){
                while(checkString1[3] != ':' && atoi(checkString1) == counter_eng){
                        flag_eng = call_print(stream_eng, write_data);
                        if(flag_eng == 1){
                                counter_eng++;
                                if(counter_eng >= counter_cht)
                                        write_cht(writeString2, check_cht);
                        }
                }
        }
}

int call_print(FILE *stream_eng, FILE *write_data)
{
        while(fgets(writeString1, 100, stream_eng) != NULL){
                int stop_flag = 0;
                if(writeString1[2] != ':' && (isdigit(writeString1[0]) == 0 || strlen(writeString1) >=5) && writeString1[0] != '\n'){
                        fputs(writeString1, write_data);
                }

                if(atoi(writeString1) == counter_eng){
                        return stop_flag = 1;
                }
        }
        return 0;
}

void write_cht(char checkString2[], FILE *check_cht)
{
        int gobackto_eng = 0;
        while(fgets(checkString2, 100, check_cht) != NULL &&  gobackto_eng == 0){
                while(checkString2[3] != ':' &&  gobackto_eng == 0 && (( 0 <= checkString2[0] <= 127) ? atoi(checkString2) == counter_cht : 0)){
                        flag_cht = call_print_cht(stream_cht, write_data);
                        if(flag_cht == 1){
                                counter_cht++;
                                if(counter_cht == counter_eng)
                                        gobackto_eng = 1;  
                        }
                }
        }     
}

int call_print_cht(FILE *stream_cht, FILE *write_data)
{
        char temp[] = "\n";
        while(fgets(writeString2, 100, stream_cht) != NULL){
                int stop_flag = 0;
                if(writeString2[2] != ':' && strlen(writeString2) >=5){
                        fputs(writeString2, write_data);
                }

                if(atoi(writeString2) == counter_cht){
                        fputs(temp, write_data);
                        return stop_flag = 1;
                }
        }
        return 0;
}