第五次實驗

202383310033孙卓航發表於2024-05-26

include <stdio.h>

define N 5

void input(int x[], int n);
void output(int x[], int n);
void find_min_max(int x[], int n, int *pmin, int *pmax);

int main() {
int a[N];
int min, max;

printf("錄入%d個資料:\n", N);
input(a, N);

printf("資料是: \n");
output(a, N);

printf("資料處理...\n");
find_min_max(a, N, &min, &max);

printf("輸出結果:\n");
printf("min = %d, max = %d\n", min, max);

return 0;

}

void input(int x[], int n) {
int i;

for(i = 0; i < n; ++i)
    scanf("%d", &x[i]);

}

void output(int x[], int n) {
int i;

for(i = 0; i < n; ++i)
    printf("%d ", x[i]);
printf("\n");

}

void find_min_max(int x[], int n, int *pmin, int *pmax) {
int i;

*pmin = *pmax = x[0];

for(i = 1; i < n; ++i)
    if(x[i] < *pmin)
        *pmin = x[i];
    else if(x[i] > *pmax)
        *pmax = x[i];

}

find_min_max的功能是找出五個資料的最大值和最小值;
執行到line45時,指標變數pmin,pmax都指向x[0]的地址

include <stdio.h>

define N 5

void input(int x[], int n);
void output(int x[], int n);
int *find_max(int x[], int n);

int main() {
int a[N];
int *pmax;

printf("錄入%d個資料:\n", N);
input(a, N);

printf("資料是: \n");
output(a, N);

printf("資料處理...\n");
pmax = find_max(a, N);

printf("輸出結果:\n");
printf("max = %d\n", *pmax);

return 0;

}

void input(int x[], int n) {
int i;

for(i = 0; i < n; ++i)
    scanf("%d", &x[i]);

}

void output(int x[], int n) {
int i;

for(i = 0; i < n; ++i)
    printf("%d ", x[i]);
printf("\n");

}

int *find_max(int x[], int n) {
int max_index = 0;
int i;

for(i = 1; i < n; ++i)
    if(x[i] > x[max_index])
        max_index = i;

return &x[max_index];

}

find_max返回的是五個資料中最大值的地址;
可以將find_max函式改寫

include <stdio.h>

include <string.h>

define N 80

int main() {
char s1[] = "Learning makes me happy";
char s2[] = "Learning makes me sleepy";
char tmp[N];

printf("sizeof(s1) vs. strlen(s1): \n");
printf("sizeof(s1) = %d\n", sizeof(s1));
printf("strlen(s1) = %d\n", strlen(s1));

printf("\nbefore swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);

printf("\nswapping...\n");
strcpy(tmp, s1);
strcpy(s1, s2);
strcpy(s2, tmp);

printf("\nafter swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);

return 0;

}

陣列s1的大小是24個位元組,sizeof(s1)計算的是字串s1佔的位元組數,包括字串末尾自動新增的‘\0';strlen(s1)統計的是字串s1不包括末尾'\0'所佔用的位元組數
定義字串s1不可以改成以下寫法,因為陣列名無法賦值
line19-21執行後,字元陣列s1,s2的內容發生了交換

include <stdio.h>

include <string.h>

define N 80

int main() {
char *s1 = "Learning makes me happy";
char *s2 = "Learning makes me sleepy";
char *tmp;

printf("sizeof(s1) vs. strlen(s1): \n");
printf("sizeof(s1) = %d\n", sizeof(s1));
printf("strlen(s1) = %d\n", strlen(s1));

printf("\nbefore swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);

printf("\nswapping...\n");
tmp = s1;
s1 = s2;
s2 = tmp;

printf("\nafter swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);

return 0;

}

指標變數s1存放的是指向字串s1首字母“L”的地址,sizeof(s1)計算的是指標變數s1佔用的記憶體大小,strlen(s1)統計的是s1指向的字串的實際長度
line6程式碼可以替換成下面的寫法
line19-21交換的是兩個字串常量的地址,兩字串在記憶體儲存單元中沒有交換

include <stdio.h>

int main() {
int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
int i, j;
int ptr1; // 指標變數,存放int型別資料的地址
int(
ptr2)[4]; // 指標變數,指向包含4個int元素的一維陣列

printf("輸出1: 使用陣列名、下標直接訪問二維陣列元素\n");
for (i = 0; i < 2; ++i) {
    for (j = 0; j < 4; ++j)
        printf("%d ", x[i][j]);
    printf("\n");
}

printf("\n輸出2: 使用指向元素的指標變數ptr1間接訪問二維陣列元素\n");
for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
    printf("%d ", *ptr1);

    if ((i + 1) % 4 == 0)
        printf("\n");
}
                     
printf("\n輸出3: 使用指向一維陣列的指標變數ptr2間接訪問二維陣列元素\n");
for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
    for (j = 0; j < 4; ++j)
        printf("%d ", *(*ptr2 + j));
    printf("\n");
}

return 0;

}

ptr1指標變數,存放int型別資料的地址;(ptr2)[4]指標變數,指向包含4個整型元素的一維陣列

include <stdio.h>

define N 80

void replace(char *str, char old_char, char new_char); // 函式宣告

int main() {
char text[N] = "c programming is difficult or not, it is a question.";

printf("原始文字: \n");
printf("%s\n", text);

replace(text, 'i', '*'); // 函式呼叫 注意字元形參寫法,單引號不能少

printf("處理後文字: \n");
printf("%s\n", text);

return 0;

}

// 函式定義
void replace(char *str, char old_char, char new_char) {
int i;

while(*str) {
    if(*str == old_char)
        *str = new_char;
    str++;
}

}

函式 replace 的功能是將i全部改成*;
line24, 圓括號裡迴圈條件可以改寫成 *str != '\0'。

include <stdio.h>

define N 80

void str_trunc(char *str, char x);

int main() {
char str[N];
char ch;

printf("輸入字串: ");
gets(str);

printf("輸入一個字元: ");
ch = getchar();

printf("截斷處理...\n");
str_trunc(str, ch);

printf("截斷處理後的字串: %s\n", str);

}

void str_trunc(char str, char x) {
while(
str) {
if(*str == x)
*str='\0'; // blank1

      *str++; // blank2
}

 // blank3

}

include <stdio.h>

include <string.h>

void sort(char *name[], int n);

int main() {
char *course[4] = {"C Program",
"C++ Object Oriented Program",
"Operating System",
"Data Structure and Algorithms"};
int i;

sort(course, 4);

for (i = 0; i < 4; i++)
    printf("%s\n", course[i]);

return 0;

}

void sort(char *name[], int n) {
int i, j;
char *tmp;

for (i = 0; i < n - 1; ++i)
    for (j = 0; j < n - 1 - i; ++j)
        if (strcmp(name[j], name[j + 1]) > 0) {
            tmp = name[j];
            name[j] = name[j + 1];
            name[j + 1] = tmp;
        }

}

include <stdio.h>

include <string.h>

void sort(char *name[], int n);

int main() {
char *course[4] = {"C Program",
"C++ Object Oriented Program",
"Operating System",
"Data Structure and Algorithms"};
int i;

sort(course, 4);
for (i = 0; i < 4; i++)
    printf("%s\n", course[i]);

return 0;

}

void sort(char *name[], int n) {
int i, j, k;
char *tmp;

for (i = 0; i < n - 1; i++) {
    k = i;
    for (j = i + 1; j < n; j++)
        if (strcmp(name[j], name[k]) < 0)
            k = j;

    if (k != i) {
        tmp = name[i];
        name[i] = name[k];
        name[k] = tmp;
    }
}

}

這兩種演算法交換的是記憶體中字串的儲存位置

include <stdio.h>

include <string.h>

define N 5

int check_id(char *str); // 函式宣告

int main() {
char *pid[N] = {"31010120000721656X",
"330106199609203301",
"53010220051126571",
"510104199211197977",
"53010220051126133Y"};
int i;

for (i = 0; i < N; ++i)
    if (check_id(pid[i])) // 函式呼叫
        printf("%s\tTrue\n", pid[i]);
    else
        printf("%s\tFalse\n", pid[i]);

return 0;

}

// 函式定義
// 功能: 檢查指標str指向的身份證號碼串形式上是否合法。
// 形式合法,返回1,否則,返回0
int check_id(char *str) {
int n,i;
n=strlen(str);
if(n18){
for(i=0;i<n;i++){
if(i
(n-1)){
if(!(str[i]>='0'&&str[i]<='9'||str[i]=='X'))
return 0;}
else if(!(str[i]>='0'&&str[i]<='9'))
return 0;}
return 1;}
else return 0;

}

include <stdio.h>

include<string.h>

define N 80

void encoder(char *str); // 函式宣告
void decoder(char *str); // 函式宣告

int main() {
char words[N];

printf("輸入英文文字: ");
gets(words);

printf("編碼後的英文文字: ");
encoder(words); // 函式呼叫
printf("%s\n", words);

printf("對編碼後的英文文字解碼: ");
decoder(words); // 函式呼叫
printf("%s\n", words);

return 0;

}

/*函式定義
功能:對s指向的字串進行編碼處理
編碼規則:
對於az或AZ之間的字母字元,用其後的字元替換; 其中,z用a替換,Z用A替換
其它非字母字元,保持不變
*/
void encoder(char *str) {
int i,len=strlen(str);
for(i=0;i<len;i++){
if(str[i]'Z') str[i]='A';
if(str[i]
'z') str[i]='a';
if(str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z') str[i]++;
}

}

/*函式定義
功能:對s指向的字串進行解碼處理
解碼規則:
對於az或AZ之間的字母字元,用其前面的字元替換; 其中,a用z替換,A用Z替換
其它非字母字元,保持不變
*/
void decoder(char *str) {
int i,len=strlen(str);
for(i=0;i<len;i++){
if(str[i]'A') str[i]='Z';
if(str[i]
'a') str[i]='z';
if(str[i]>'a'&&str[i]<='z'||str[i]>'A'&&str[i]<='Z') str[i]--;
}

}