c語言編碼函數
/*function: 測量字元串長度
*in: 需要測長度的字元串
*out: 字元串長度
*/
int strlen(string str)
{
assert( str != NULL ); // #include <assert.h>
char *ch = NULL;
int iLength = 0;
ch = str;
while ( *ch != '\0')
{
ch = ch + 1;
iLength = iLength + 1;
}
return iLength;
}
Ⅱ c語言函數編寫
先寫一個輸入函數,把輸入的名字存到變數name裡面,調用函數seek(name);
子函數 string seek(string name)
循環比較name跟類數組中的名字,如果相同則return返回該同學數據,否則顯示"no find"
照著寫就行了,寫了出錯再問
Ⅲ c語言編寫函數
#include <stdio.h>
#define M 30
#define N 10
typedef struct
{
int score[N];//選手的10個得分,得分與評委一一對應
double aver;//選手的平均分
char name[81];//選手的姓名
} Choice;
int inputChoice(Choice peo[],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
scanf("%s",peo[i].name);
for(j=0;j<n;j++)
scanf("%d",&peo[i].score[j]);
}
}
int sort(Choice peo[],int m,int n)
{
int i,j,k,t;
Choice temp;
for(i=0;i<n;i++)
{
peo[i].aver=0.0;
for(k=0;k<n-1;k++)
for(j=k+1;j<n;j++)
if(peo[i].score[k]>peo[i].score[j])
{
t=peo[i].score[k];
peo[i].score[k]=peo[i].score[j];
peo[i].score[j]=t;
}
for(j=1;j<n-1;j++)
peo[i].aver+=peo[i].score[j];
peo[i].aver/=n-2;
}
for(i=0;i<m-1;i++)
for(j=i+1;j<m;j++)
if(peo[i].aver<peo[j].aver)
{temp=peo[i];peo[i]=peo[j];peo[j]=temp;}
}
int main()
{
Choice peoples[M];
int m,n,i;
scanf("%d%d",&m,&n);
inputChoice(peoples,m,n);
sort(peoples,m,n);
for(i=0;i<3;i++)
printf("%s %.2lf
",peoples[i].name,peoples[i].aver);
return 0;
}
Ⅳ C語言函數編寫
#include<stdio.h>
void Reverse(int arr[], int size);
int main()
{
int arr[10] = {0};
printf("請輸入10個整數:\n");
for (int i=0; i<10; ++i)
{
scanf("%d", &arr[i]);
}
Reverse(arr, 10);
printf("反序:\n");
for (int i=0; i<10; ++i)
{
printf("%d ", arr[i]);
}
return 0;
}
void Reverse(int arr[], int size)
{
if (arr == NULL || size <=0)
return;
for (int beg=0, end=size-1; beg < end; ++beg, --end)
{
int tmp = arr[beg];
arr[beg] = arr[end];
arr[end] = tmp;
}
}
Ⅳ 用C語言編碼實現strlen函數
根據題意可得如下代碼:
#include<stdio.h>
intstrlen(char*s)
{
inti=0;
while(s[i]!='