c言語ポインタ、文字列(自己実現関数)、ファイル操作【コード版】
ポインタの使用
ポインタは本当にポイントです.もちろん後でデータ構造を勉強するときは多く使うといいです.
1.関数ポインタ
2.関数ポインタの汎用バブルソート
3.関数のポインタ
ある会社に社員がいます.(最大50人)最高賃金、最低賃金、平均賃金を印刷するプログラムを作成します.会社の人数はメイン関数で与えられ、従業員の賃金入力はInput関数を呼び出し、最高賃金、最低賃金、平均賃金を計算してCompute関数を呼び出し、最高賃金、最低賃金、平均賃金をメイン関数で印刷します.所定のフレームワークの下で完全なプログラムを書いてください.
4.文字ポインタ配列
文字ポインタ配列:各配列要素は文字列を指すポインタである.区別:かっこなし:文字列は定数格納領域にあり、配列の内側のタイプはポインタであり、それぞれ対応する文字列を指す.
5.行ポインタと列ポインタ
文字列
1.の関数
次のコードは主に対応するc言語関数を模倣しています.
strlen():文字列の長さを求める
strcpy():文字列のコピー
strcat():文字列の接合
strstr():クエリーサブ列
atoi()
関数atoi()は文字列の数字文字を整形数に変換し、整形値を返します.注意:変換時に前のスペース文字をスキップし、数値または正負の記号に遭遇するまで変換を開始し、非数値または文字列の終了時'0'に遭遇してから変換を終了し、結果を返します.
数値文字列を整数に変換
同じ文字を削除
ファイル
書類ここは確かに面倒で、よく勉強していません.しかしjava IOストリームを見ると少しはっきりします.
読み取り
fgetc
fgets
fscanf
書き込み
fputc
fputs
fprintf
ポインタは本当にポイントです.もちろん後でデータ構造を勉強するときは多く使うといいです.
1.関数ポインタ
#include
int getMax(int x, int y);
int getMin(int x, int y);
int add(int x, int y);
int process( int x, int y, int (*p)(int, int) ) // 1
{ // int
return p(x, y); // 2 ( )
}
int main()
{
int a, b;
printf(" : ");
scanf("%d %d", &a, &b);
printf("max=%d
", process(a, b, getMax) ); // 3:
printf("min=%d
", process(a, b, getMin) );
printf("add=%d
", process(a, b, add) );
return 0;
}
int getMax(int x, int y)
{
return x > y ? x : y;
}
int getMin(int x, int y)
{
return x < y ? x : y;
}
int add(int x, int y)
{
return x + y;
}
2.関数ポインタの汎用バブルソート
#include
#include
bool Ascending(int a, int b)
{
return a > b; // ,
} // : a[j] > a[j + 1]
bool Descending(int a, int b)
{
return a < b; // ,
}
/* */
void bubbleSort( int a[], int n, bool (*compare)(int, int) )
{
for(int i = 0; i < n - 1; i++)
{
for(int j = 0; j < n - 1 - i; j++)
{
if( (*compare)(a[j], a[j + 1]) )
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
int main()
{
int Array[5] = {1, 456, 78, 30, 89};
bubbleSort(Array, 5, Ascending); //
for(int i = 0; i < 5; i++)
{
printf("%d ", Array[i]);
}
return 0;
}
3.関数のポインタ
ある会社に社員がいます.(最大50人)最高賃金、最低賃金、平均賃金を印刷するプログラムを作成します.会社の人数はメイン関数で与えられ、従業員の賃金入力はInput関数を呼び出し、最高賃金、最低賃金、平均賃金を計算してCompute関数を呼び出し、最高賃金、最低賃金、平均賃金をメイン関数で印刷します.所定のフレームワークの下で完全なプログラムを書いてください.
#include
void Input(double wage[], int n);
double Compute(double wage[], int n, double *pmaxwage, double *pminwage);
int main()
{
int n;
printf("Please input n:
");
scanf("%d",&n);
double wage[50], maxwage = -1, minwage = -1, avewage;
Input(wage, n);
avewage = Compute(wage, n, &maxwage, &minwage);
printf("maxwage=%.2f, minwage=%.2f, avewage=%.2f
", maxwage, minwage, avewage);
return 0;
}
void Input(double wage[], int n)
{
for(int i = 0; i < n; i++)
{
scanf("%lf", &wage[i]);
}
}
double Compute(double wage[], int n, double *pmaxwage, double *pminwage)
{
double sum = 0; // * : , &
*pmaxwage = wage[0], *pminwage = wage[0];
for(int i = 0; i < n; i++)
{
sum += wage[i];
if(wage[i] > *pmaxwage)
{
*pmaxwage = wage[i];
}
if(wage[i] < *pminwage)
{
*pminwage = wage[i];
}
}
return sum / n;
}
/* :
10
1200.34
2378.48
8600.56
5372.42
6317.25
7265.88
2156.39
1876.31
1792.14
4326.22
*/
4.文字ポインタ配列
文字ポインタ配列:各配列要素は文字列を指すポインタである.区別:かっこなし:文字列は定数格納領域にあり、配列の内側のタイプはポインタであり、それぞれ対応する文字列を指す.
char *country[5] = {"Aass", "xsax", "scasc", "cdscc", "csdcyjt"};
for(int i = 0; i < 5; i++)
{
printf("%s
", country[i]);
} //country[i] --->
// : char *temp = pStr[i]; ......
void SortString(char *pStr[], int n) // :
{
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++){
if(strcmp(pStr[j], pStr[i]) < 0)
{
char *temp = pStr[i]; //
pStr[i] = pStr[j]; //
pStr[j] = temp;
}
}
}
}
5.行ポインタと列ポインタ
/* :
int (*p)[3] = a; // , a[][3]
: *(*(p + i) + j)
int *p; //
p = a[0]; // &a[0][0]
: *(p + i*n + j) // n for(int j = 0; j < n; j++)
1. : --> a[][]
: void InputArray(int (*p)[N], int m, int n)
: InputArray(a, m, n);
: *(p + i) + j
: *( *(p + i) + j )
2.
: void InputArray(int *a, int m, int n)
: InputArray(*a, m, n);
: &p[i*n+j]
: p[i*n+j]
3. const
void InputArray(const int (*p)[N], int m, int n)
void InputArray(const int *p, int m, int n)
*/
#include
#define N 15
void InputArray(int *p, int m, int n); //
int FindMax(int *p, int m, int n, int *pRow, int *pColumn);
int main()
{
int m = 0, n = 0;
printf("Input m,n:
");
scanf("%d,%d", &m, &n);
int a[N][N] = {0};
printf("Input %d*%d array:
", m, n);
InputArray(*a, m, n); //
int rowPos = 0, columnPos = 0;
int Max = FindMax(*a, m, n, &rowPos, &columnPos);
printf("max=%d,row=%d,col=%d
", Max, rowPos, columnPos);
return 0;
}
void InputArray(int *p, int m, int n) //int (*p)[N]
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
scanf("%d", &p[i * n + j]);
} // p++ : *p++ *p --> p++
}
}
/* , pRow pCol */
int FindMax(int *p, int m, int n, int *pRow, int *pColumn)
{
int max = *p;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(p[i * n + j] > max)
{
max = p[i * n + j];
*pRow = i;
*pColumn = j;
}
}
}
return max;
}
/* :
1 2 3 4
5 6 7 8
9 0 -1 -2
*/
文字列
1.の関数
次のコードは主に対応するc言語関数を模倣しています.
strlen():文字列の長さを求める
// const , str[]
int MyStrlen( const char str[] )
{
int len = 0;
for(int i = 0; str[i] != '\0'; i++)
{
len++;
}
return len;
}
strcpy():文字列のコピー
/* str2 str1( )*/
void MyStrcpy(char str1[], const char str2[])
{
int end = 0;
for(int i = 0; str2[i] != '\0'; i++)
{
str1[i] = str2[i];
end = i;
}
str1[end] = '\0'; // !
}
/* str2 str1( )*/
void MyStrcpy(char *str1, const char *str2)
{
while(*str2 != '\0') // : while(*str2)
{
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0'; // !
}
strcat():文字列の接合
#include
#include
char *MyStrcat( char *str1, char *str2 );
int main()
{
char str1[100], str2[30];
printf("please input the first string:
");
gets(str1);
printf("please input the second string:
");
gets(str2);
char *pResult = NULL;
pResult = MyStrcat(str1, str2);
printf("The result is: %s
", pResult);
return 0;
}
/* str2 str1 */
char *MyStrcat( char *str1, char *str2 )
{
char *pStr = str1; //pStr str1 ,
while(*str1 != '\0') // str1 \0,
{
str1++;
}
//
while(*str2 != '\0') // , !!!
{
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
return pStr;
}
strstr():クエリーサブ列
/* :
How are you!
are 5
abcabc
bc 2 5
*/
#include
#include
int SearchString( char s[], char d[] );
int main()
{
char str1[100], str2[100];
printf("Input a string:");
gets(str1);
printf("Input another string:");
gets(str2);
int ans = SearchString(str1, str2);
if(ans == -1)
{
printf("Not found!
");
}
else
{
printf("Searching results:%d
", ans);
}
return 0;
}
/* s d, d s , , -1*/
int SearchString( char s[], char d[] )
{
int position = 0;
int len = strlen(s);
for(int i = 0; i < len; i++)
{
if(s[i] == d[0])
{
int j = 0;
position = i;
while(s[i] == d[j] && d[j] != '\0')
{
i++;
j++;
}
if(d[j] == '\0') // ,
{
return position + 1;
}
else //
{
i = position;
}
}
}
return -1; // , -1
}
atoi()
関数atoi()は文字列の数字文字を整形数に変換し、整形値を返します.注意:変換時に前のスペース文字をスキップし、数値または正負の記号に遭遇するまで変換を開始し、非数値または文字列の終了時'0'に遭遇してから変換を終了し、結果を返します.
/* :
7hg09y 7
9w2k7m0 9
happy 0
*/
#include
#include //
int main()
{
char str[10];
printf("Input a string:");
scanf("%7s", str);
int ans = atoi(str);
printf("%d
", ans);
return 0;
}
数値文字列を整数に変換
/* :
7hg09y 709
9w2k7m0 9270
happy 0
*/
#include
#include
int Myatoi(char str[]);
int main()
{
char str[10];
printf("Input a string:");
scanf("%7s", str);
int ans = Myatoi(str);
printf("%d
", ans);
return 0;
}
/* str[] , */
int Myatoi(char str[])
{
int len = strlen(str);
int bits = 0;
int result[10] = {0}; //
for(int i = 0; i < len; i++)
{
if(str[i] >= '0' && str[i] <= '9')
{
int temp = str[i] - '0';
result[bits] = temp;
bits++;
}
}
int sum = 0, P = 1; // -->int
for(int i = bits - 1; i >= 0; i--) //
{
sum += result[i] * P;
P *= 10;
}
return sum;
}
同じ文字を削除
#include
#include
int main()
{
char str[110];
printf("Input a string:
");
gets(str);
char ch;
printf("Input a character:
");
ch = getchar();
int len = strlen(str);
for(int i = 0; i < len; i++) //
{
if(str[i] == ch)
{
int position = i;
while(str[i] != '\0') //
{
str[i] = str[i + 1];
i++;
}
len--; // 1
i = position - 1;
}
}
printf("Results:%s
", str);
return 0;
}
ファイル
書類ここは確かに面倒で、よく勉強していません.しかしjava IOストリームを見ると少しはっきりします.
読み取り
fgetc
#include
int main()
{
FILE *fp;
fp = fopen("C:\\Users\\deng\\Desktop\\zzz.txt", "r"); //read
/* */
char ch;
ch = fgetc(fp);
while(ch != EOF)
{
putchar(ch);
ch = fgetc(fp);
}
fclose(fp);
return 0;
}
fgets
#include
int main()
{
FILE *fp;
fp = fopen("C:\\Users\\deng\\Desktop\\zzz.txt", "r"); //read
/* */
char str[100];
fgets(str, 100*sizeof(str), fp);
puts(str);
fclose(fp);
return 0;
}
fscanf
#include
int main()
{
FILE *fp;
fp = fopen("C:\\Users\\deng\\Desktop\\zzz.txt", "r"); //read
/* */
char str[20];
fscanf(fp, "%s", str);
puts(str); // ( )
fclose(fp);
return 0;
}
書き込み
fputc
#include
int main()
{
FILE *fp;
fp = fopen("C:\\Users\\deng\\Desktop\\zzz.txt", "w");
char ch;
ch = getchar();
while(ch != '
') /* */
{
fputc(ch, fp); //fputc
ch = getchar();
}
fclose(fp);
return 0;
}
fputs
#include
int main()
{
FILE *fp;
fp = fopen("C:\\Users\\deng\\Desktop\\zzz.txt", "w");
char str[100];
gets(str);
fputs(str, fp); // str fp
fclose(fp);
return 0;
}
fprintf
#include
int main()
{
FILE *fp;
fp = fopen("C:\\Users\\deng\\Desktop\\zzz.txt", "w");
/* */
int a = 10;
fprintf(fp, "
"); // ...
fprintf(fp, "a=%d
", a);
fclose(fp);
return 0;
}