1、請寫函式proc(),其功能是:求正整數x和y的最大公約,
例如,程式執行時,若輸入的兩個正整數為12,24,則它們的最大公約數為12,最小公倍數為24,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入所寫的若干陳述句,
#include <stdio.h> int proc(int x,int y) { } int main() { int num1,num2,gcd; printf("\nInput two numbers:\n"); scanf("%d %d",&num1,&num2); gcd=proc(num1,num2); printf("Greatest common divisor:%d\n",gcd); printf("Least common multiple:%d\n",num1*num2/gcd); return 0; }
int r; while (x%y!=0) { r=x%y; x=y; y=r; } return y;參考程式
2、請撰寫函式proc(),其功能是:將str所指字串中除下標為偶數、同時ASCIl碼值為奇數的字符外,其余的字符都洗掉,串中剩余字符所形成的一個新串放在t所指的陣列中,例如,若str所指字串中的內容為ABCDEFG12345,其中字符B的ASCIl碼值為偶數,所在元素的下標為奇數,因此必須洗掉;而字符A的ASCII碼值為奇數,所在陣列中的下標為偶數,因此不應當洗掉,依此類推,后t所指的陣列中的內容應是ACEG,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入所寫的若干陳述句,
#include <stdio.h> void proc(char *str,char t[]) { } int main() { char str[100],t[100]; printf("\nPlease enter string str:"); scanf("%s",str); proc(str,t); printf("\nThe result is:%s\n",t); return 0; }
int i,j=0; for (i=0;str[i]!='\0';i++) // 從陣列的第一個元素開始,到其后一個 { if (i%2==0 && str[i]%2!=0) // 下標為偶數、同時ASCIl碼值為奇數的字符 t[j++]=str[i]; // 如果成立,則把它放到t陣列中 } t[j]='\0'; // 字串結束標志為'\0'參考程式
3、下列給定程式中,函式proc()的功能是:從字串str中,洗掉所有大寫字母’F’,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入所撰寫的若干陳述句,
#include <stdio.h> void proc(char *str) { } int main() { char str[80]; printf("\Enter a string:"); gets(str); printf("The original string:"); puts(str); proc(str); printf("The string after deleted:"); puts(str); printf("\n"); return 0; }
int i,j; for (i=j=0;str[i]!='\0'; i++) if (str[i]!='F') str[j++]=str[i]; str[j]='\0';參考程式
4、假定輸入的字串中只包含字母和*號,請撰寫函式proc(),它的功能是:將字串中的前導*號全部洗掉,中間和后面的*號不洗掉,
例如,若字串中的內容為****a*bc*def*g****,洗掉后,字串中的內容應當是a*bc*def*g****,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入所撰寫的若干陳述句,
#include <stdio.h> void proc(char *str) { } int main() { char str[81]; printf("Enter a string:"); gets(str); proc(str); printf("The string after deleted:"); puts(str); return 0; }
char *p=str,*q=str; while (*p=='*') p++; // 通過p的移動來達到使p指向首個不是*號的字符 for (;*p!='\0';p++,q++) *q=*p; *q='\0';參考程式
5、請撰寫函式proc(),其功能是:將str所指字串中下標為偶數的字符洗掉,串中剩余字符形成的新串放在t所指字符陣列中,
例如,當str所指字串中的內容為abcdefg,則在t所指陣列中的內容應是bdf,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入所撰寫的若干陳述句,
#include <stdio.h> void proc(char *str,char t[]) { } int main() { char s[81],t[81]; printf("Enter a string:"); gets(s); proc(s,t); printf("The result is::"); puts(t); return 0; }
int i,j=0; for(i=0; str[i]!='\0';i++) if (i%2!=0) t[j++]=str[i]; t[j]='\0';參考程式
6、請撰寫函式proc(),它的功能是計算:s=(In(1)4+ln(2)4+ln(3)+…+In(m))0.5
在C語言中可呼叫log(n)函式求ln(n),
例如,若m的值為30,則proc()函式值為8.640500,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入你所撰寫的若干陳述句,
#include <stdio.h> #include <math.h> double proc(int m) { } int main() { printf("%lf\n",proc(30)); return 0; }
int i; double s=0.0; for (i=1;i<=m; i++) s=s+log(i); return sqrt(s);參考程式
7、請撰寫函式proc(),其功能是:判斷形參n中的正整數是幾位數,并將結果通過函式值回傳,例如:若輸入的資料為123,則輸出結果為3,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入你所撰寫的若干陳述句,
#include <stdio.h> int proc(int n) { } int main() { int n; scanf("%d",&n); printf("%d 是一個 %d 位數,\n",n,proc(n)); return 0; }
int cnt=0; do { cnt++; n=n/10; }while (n!=0); return cnt;參考程式
8、請撰寫函式proc(),該函式的功能是:將放在字串陣列中的M個字串(每串的長度不超過N),按順序合并組成一個新的字串,
例如,若字串陣列中的M個字串為:
ABCD
BCDEFG
CDEFGHI
則合并后的字串內容應該是ABCDBCDEFGCDEFGHl,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入所撰寫的若干陳述句,
#include <stdio.h> #define M 3 #define N 20 void proc(char arr[M][N],char *b) { } int main() { char str[M][N]={"ABCD","BCDEFG","CDEFGHl"},arr[M*N+1]; int i; printf("The string:\n"); for (i=0;i<M;i++) puts(str[i]); printf("\n"); proc(str,arr); printf("The A string:%s\n",arr); return 0; }
int i,j,k=0; for (i=0;i<M;i++) for(j=0; arr[i][j]!='\0'; j++) b[k++]=arr[i][j]; b[k]='\0';參考程式
9、某學生的記錄由學號、8門課程成績和平均分組成,學號和8門課程的成績已在主函式中給出,請撰寫函式fun(),其功能是:求出該學生的平均分,并放入記錄的ave成員中,
例如,學生的成績是:85.5,76,69.5,85,91,72,64.5,87.5,則他的平均分應為78.875,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式fun()花括號中填入你撰寫的若干陳述句,
#include <stdio.h> #define N 8 typedef struct { char num[10]; double s[N]; double ave; }STREC; void fun(STREC *a) { } int main() { STREC stu={"GA005",85.5,76,69.5,85,91,72,64.5,87.5}; int i; fun(&stu); printf("The %s's student data:\n",stu.num); for (i=0; i<N; i++) printf("%5.1lf ",stu.s[i]); printf("\n average=%7.3lf\n",stu.ave); return 0; }
int i; a->ave=0.0; for (i=0;i<N;i++) a->ave=a->ave+a->s[i]; a->ave=a->ave/N;參考程式
10、學生的記錄由學號和成績組成,M名學生的資料已在主函式中放入結構體陣列stu中,請撰寫函式proc(),其功能是:按分數的高低排列學生的記錄,高分在前,
注意:部分源程式給出如下,
請勿改動main()函式和其他函式中的任何內容,僅在函式proc()的花括號中填入所撰寫的若干陳述句,
#include <stdio.h> #define M 16 typedef struct { char num[10]; int s; }STREC; void proc(STREC a[]) { } int main() { STREC stu[M]={{"GA005",85},{"GA003",76}, {"GA002",69},{"GA004",85},{"GA001",91}, {"GA007",72},{"GA008",64},{"GA006",87}, {"GA015",85},{"GA013",91},{"GA012",64}, {"GA014",91},{"GA011",66},{"GA017",64}, {"GA018",64},{"GA016",72}}; int i; proc(stu); printf("The data after sorted:\n"); for (i=0; i<M; i++) { if (i%4==0) printf("\n"); printf("%s%4d ",stu[i].num,stu[i].s); } printf("\n"); return 0; }
int i,j; STREC t; for (i=0; i<M-1; i++) for (j=0;j<M-1-i;j++) if (a[j].s<a[j+1].s) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; }參考程式
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/535956.html
標籤:C
