第10周編程題在線測試
NOTE:
- 指針警示: 永遠清楚自己在操作哪塊內存;永遠清楚自己的操作是否合理、合法。
- 對於 char *ptr = “hello”,指的是將hello這個字符串常量的首地址賦值給ptr指針變量,那麼我們知道,字符串常量是存儲在常量存儲區中的,故只能對其進行讀操作,無法進行寫操作,也就無法通過 *ptr = ‘w’ 來修改字符串使其從"hello"變成"wello"。但我們可以修改指針變量ptr的值,另其指向別的字符類型的數據。
- 同樣,對於 char str[] = “hello”,str作爲數組名是一個地址常量,故不能修改,但能修改字符串"hello"。
1. 數字字符串轉換爲整型數
題目內容:
從鍵盤輸入一串字符(假設字符數少於8個),以回車表示輸入結束,編程將其中的數字部分轉換爲整型數並以整型的形式輸出。
函數原型: int Myatoi(char str[]);
其中,形參數組str[]對應用戶輸入的字符串,函數返回值爲轉換後的整型數。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int Myatoi(char str[]);
int main()
{
char str[8];
printf("Input a string:");
scanf("%7s", str);
printf("%d\n", Myatoi(str));
return 0;
}
int Myatoi(char str[])
{
char num[8];
char *p = num;
char *q = str;
while(*q)
{
if(isdigit(*q))
{
*p = *q;
p++;
}
q++;
}
return atoi(num);
}
2. 查找子串
題目內容:
用字符數組作函數參數,編程實現在從鍵盤輸入的字符串(假設長度小於80)中查找與指定的子串,並輸出該子串在字符串中首次出現的位置,如果該字符不存在,則輸出"Not found!"。
函數原型: int SearchString(char s[], char d[])
函數功能: 在字符數組s中查找子串d,返回d在s中首次出現的位置,若找不到,則返回-1。
#include<stdio.h>
#include<string.h>
#define MAX_LEN 80
int SearchString(char s[], char d[]);
int main()
{
char s[MAX_LEN + 1],d[MAX_LEN + 1];
printf("Input a string:");
gets(s);
printf("Input another string:");
gets(d);
if(SearchString(s,d) != -1)
printf("Searching results:%d\n",SearchString(s,d));
else
printf("Not found!\n");
return 0;
}
int SearchString(char s[], char d[])
{
int slen, dlen, pos;
int i=0, j=0, find;
slen = strlen(s);
dlen = strlen(d);
for(i = 0;i < slen; i++)
{
pos = i;
for(j = 0;j < dlen; j++)
{
if(s[pos + j] != d[j])
break;
}
if(j == dlen)
return (pos + 1);
}
return -1;
}
3. 統計連續重複字符
題目內容:
輸入一串字符(字符數小於80),以回車表示輸入結束,編程計算並輸出這串字符中連續重複次數最多的字符和重複次數。如果重複次數最多的字符有兩個,則輸出最後出現的那一個。
函數原型: int CountRepeatStr(char str[], int *tag);
#include<stdio.h>
#define STR_LEN 80
int CountRepeatStr(char str[], int *tag);
int main()
{
char str[STR_LEN + 1];
int ret,tag;
printf("Input a string:\n");
gets(str);
ret = CountRepeatStr(str,&tag);
printf("%c:%d\n",str[tag],ret);
return 0;
}
int CountRepeatStr(char str[], int *tag)
{
int count = 1;
int max = count;
*tag = 0;
int i = 0;
while(str[i] != '\0')
{
if(str[i] == str[i + 1])
{
count++;
if(count >= max)
{
*tag = i + 1;
max = count;
}
}
else
{
count = 1;
}
i++;
}
return max;
}
4. 凱撒密碼
題目內容:
凱撒密碼是羅馬擴張時期朱利斯•凱撒(Julius Caesar)創造的,用於加密通過信使傳遞的作戰命令,其原理很簡單,就是通過將字母表中的字母移動一定位置而實現加密。例如,每個字母按字母表順序向後移3位,如a加密後變成d,b加密後變成e,……x加密後變成a,y加密後變成b,z加密後變成c。請編寫一個程序,將用戶從鍵盤輸入的文本字符串(只包含a~z的字符且長度小於100)進行加密後輸出。
函數原型: void Caesar(char c[]);
函數功能: 計算凱撒密碼
#include<stdio.h>
#define MAX 100
void Caesar(char c[]);
int main()
{
char str[MAX + 1];
printf("Input a string:");
gets(str);
Caesar(str);
return 0;
}
void Caesar(char c[])
{
char *p ;
p = &c[0];
while(*p)
{
if(*p == 'x')
*p = 'a';
else if(*p == 'y')
*p = 'b';
else if(*p == 'z')
*p = 'c';
else
*p = *p + 3;
p++;
}
puts(c);
}
第10周練兵區編程題
1. 有趣的“迴文”檢測
題目內容:
英文中有很多的迴文詞,迴文詞的拼法十分有趣,無論是從前往後拼讀,還是從後往前拼讀,他們的拼法和詞義都不變。例如:dad(爸爸),mum(媽媽),noon(中午),eve(前夕),eye(眼睛),pop(流行),deed(行爲),level(水平)等。簡單地說,“迴文”就是指順讀和倒讀都一樣的字符串。現在請你編程輸入一個單詞,判斷它是否是迴文。
提示:
(1)設置兩個指針pStart和pEnd,讓pStart指向字符串首部,讓pEnd指向字符串尾部。
(2)利用循環從字符串兩邊對指針所指字符進行比較,當對應的兩字符相等且兩指針未超越對方時,使指針pStart向前移動一個字符位置(加1),使指針pEnd向後移動一個字符位置(減1),一旦發現兩字符不等或兩指針已互相超越(不可能是迴文),則立即停止循環。
(3)根據退出循環時兩指針的位置,判斷字符串是否爲迴文。
#include<stdio.h>
#include<string.h>
#define N 100
int main()
{
char str[N + 1];
int str_len;
char *pstart,*pend;
printf("Input string:");
gets(str);
str_len = strlen(str);
pstart = &str[0];
pend = &str[str_len - 1];
while(pstart < pend)
{
if(*pstart == *pend)
{
pstart++;
pend--;
}
else
break;
}
if(pstart >= pend)
printf("Yes!\n");
else
printf("No!\n");
return 0;
}
2. 學生成績管理系統V1.0
題目內容:
某班有最多不超過30人(具體人數由鍵盤輸入)參加某門課程的考試,用一維數組作函數參數編程實現如下學生成績管理:
(1)錄入每個學生的學號和考試成績;
(2)計算課程的總分和平均分;
(3)按成績由高到低排出名次表;
(4)按學號由小到大排出成績表;
(5)按學號查詢學生排名及其考試成績;
(6)按優秀(90~ 100)、良好(80~ 89)、中等(70~ 79)、及格(60~ 69)、不及格(0~ 59)5個類別,統計每個類別的人數以及所佔的百分比;
(7)輸出每個學生的學號、考試成績。
#include<stdio.h>
#include<string.h>
void Input(long id[],float score[],int n);
float TotalScore(long id[],float score[],int n);
float AveScore(long id[],float score[],int n);
void score_dsort(long id[],float score[],int n);
void id_asort(long id[],float score[],int n);
void find_id(long id[],float score[],int n);
void analysis(long id[],float score[],int n);
int main()
{
int n,m,ret;
printf("Input student number(n<30):\n");
scanf("%d",&n);
long id[n];
float score[n];
do{
printf("Management for Students' scores\n");
printf("1.Input record\n");
printf("2.Caculate total and average score of course\n");
printf("3.Sort in descending order by score\n");
printf("4.Sort in ascending order by number\n");
printf("5.Search by number\n");
printf("6.Statistic analysis\n");
printf("7.List record\n");
printf("0.Exit\n");
printf("Please Input your choice:\n");
ret = scanf(" %d",&m);
switch(m)
{
case 1:
Input(id,score,n);
break;
case 2:
printf("sum=%.0f,aver=%.2f\n",TotalScore(id,score,n),AveScore(id,score,n));
break;
case 3:
printf("Sort in descending order by score:\n");
score_dsort(id,score,n);
break;
case 4:
printf("Sort in ascending order by number:\n");
id_asort(id,score,n);
break;
case 5:
find_id(id,score,n);
break;
case 6:
analysis(id,score,n);
break;
case 7:
id_asort(id,score,n);
break;
default:
break;
}
if(ret != 1 || m < 1 || m > 7)
break;
}while(1);
if(ret != 1 || m < 0 || m > 7)
printf("Input error!\n");
if(m == 0)
printf("End of program!\n");
return 0;
}
void Input(long id[],float score[],int n)
{
printf("Input student's ID, name and score:\n");
for(int i = 0; i < n; i++)
scanf("%ld%f",&id[i],&score[i]);
}
float TotalScore(long id[],float score[],int n)
{
float sum = 0;
for(int i = 0; i < n; i++)
sum += score[i];
return sum;
}
float AveScore(long id[],float score[],int n)
{
float sum = 0;
for(int i = 0; i < n; i++)
sum += score[i];
return sum/n;
}
void score_dsort(long id[],float score[],int n)
{
int i,j;
for(i = 0;i < n ; i++)
{
for(j = i + 1; j < n ; j++)
{
if(score[j] > score[i])
{
float ex_score;
long ex_id;
ex_score = score[i];
score[i] = score[j];
score[j] = ex_score;
ex_id = id[i];
id[i] = id[j];
id[j] = ex_id;
}
}
}
for(i = 0;i < n; i++)
printf("%ld\t%.0f\n",id[i],score[i]);
}
void id_asort(long id[],float score[],int n)
{
int i,j;
for(i = 0;i < n ; i++)
{
for(j = i + 1; j < n ; j++)
{
if(id[j] < id[i])
{
float ex_score;
long ex_id;
ex_score = score[i];
score[i] = score[j];
score[j] = ex_score;
ex_id = id[i];
id[i] = id[j];
id[j] = ex_id;
}
}
}
for(i = 0;i < n; i++)
printf("%ld\t%.0f\n",id[i],score[i]);
}
void find_id(long id[],float score[],int n)
{
long num;
printf("Input the number you want to search:\n");
scanf("%ld",&num);
for(int i = 0;i < n; i++)
{
if(num == id[i])
{
printf("%ld\t%.0f\n",id[i],score[i]);
return;
}
}
printf("Not found!\n");
}
void analysis(long id[],float score[],int n)
{
float a1,b1,c1,d1,e1,f1;
int a2,b2,c2,d2,e2,f2;
a2 = b2 = c2 = d2 = e2 = f2 = 0;
a1 = b1 = c1 = d1 = e1 = f1 = 0;
for(int i = 0; i < n; i++)
{
switch((int)(score[i]/10))
{
case 10:
a2++;
break;
case 9:
b2++;
break;
case 8:
c2++;
break;
case 7:
d2++;
break;
case 6:
e2++;
break;
default:
f2++;
break;
}
}
a1 = a2*1.0/n*100;
b1 = b2*1.0/n*100;
c1 = c2*1.0/n*100;
d1 = d2*1.0/n*100;
e1 = e2*1.0/n*100;
f1 = f2*1.0/n*100;
printf("<60\t%d\t%.2f%%\n",f2,f1);
printf("%d-%d\t%d\t%.2f%%\n",60,69,e2,e1);
printf("%d-%d\t%d\t%.2f%%\n",70,79,d2,d1);
printf("%d-%d\t%d\t%.2f%%\n",80,89,c2,c1);
printf("%d-%d\t%d\t%.2f%%\n",90,99,b2,b1);
printf("%d\t%d\t%.2f%%\n",100,a2,a1);
}
3. 程序改錯——1
#include <stdio.h>
#include <string.h>
char* MyStrcat(char *dest, char *source);
int main(void)
{
char *first, *second, *result;
char dest[81], src[81];
first = dest;
second = src;
printf("Input the first string:\n");
gets(dest);
printf("Input the second string:\n");
gets(src);
result = MyStrcat(first, second);
printf("The result is : %s\n", result);
return 0;
}
char* MyStrcat(char *dest, char *source)
{
int i = 0, p = 0;
while (*(dest+i)!='\0') i++;
for (; *(source+p)!='\0'; i++, p++)
{
*(dest+i) = *(source+p);
}
*(dest+i) = '\0';
return dest;
}
4. 程序改錯——2
#include<stdio.h>
#define ARR_SIZE 5
void YH(int a[][ARR_SIZE], int n);
void PrintYH(int a[][ARR_SIZE], int n);
int main(void)
{
int a[ARR_SIZE][ARR_SIZE];
YH(a, ARR_SIZE);
PrintYH(a, ARR_SIZE);
return 0;
}
void YH(int a[][ARR_SIZE], int n)
{
int i, j ;
for (i=1; i<n; i++)
{
a[i][1] = 1;
a[i][i] = 1;
}
for (i=3; i<n; i++)
{
for (j=2; j<=i-1; j++)
{
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
void PrintYH(int a[][ARR_SIZE], int n)
{
int i , j ;
for (i=1; i<n; i++)
{
for (j=1; j<=i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
}
5. 出售金魚
題目內容:
買買提將養的一缸金魚分五次出售:第一次賣出全部的一半加二分之一條;第二次賣出餘下的三分之一加三分之一條;第三次賣出餘下的四分之一加四分之一條;第四次賣出餘下的五分之一加五分之一條;最後賣出剩下的11條。問原來魚缸中共有幾條魚?
#include<stdio.h>
int main()
{
float num = 11;
for(int i = 5;i >=2; i--)
{
num = (num + 1.0/i)/(1 - 1.0/i);
}
printf("There are %d fishes at first.\n",(int)num);
return 0;
}
6. 找最值
題目內容:
從鍵盤任意輸入10個整數,用指針變量作函數參數編程計算最大值和最小值,並返回它們所在數組中的位置。
函數原型: int FindMax(int num[], int n, int *pMaxPos);//函數返回最大值,pMaxPos返回最大值所在的下標
int FindMin(int num[], int n, int *pMinPos);//函數返回最小值,pMaxPos返回最小值所在的下標
#include<stdio.h>
int FindMax(int num[], int n, int *pMaxPos);
int FindMin(int num[], int n, int *pMinPos);
int main()
{
int a[10] = {0};
int pMaxPos,pMinPos,max,min;
pMaxPos = 0;
pMinPos = 0;
printf("Input 10 numbers:\n");
for(int i = 0; i < 10; i++)
scanf("%d",&a[i]);
max = FindMax(a,10,&pMaxPos);
min = FindMin(a,10,&pMinPos);
printf("Max=%d,Position=%d,Min=%d,Position=%d\n",max,pMaxPos,min,pMinPos);
return 0;
}
int FindMax(int num[], int n, int *pMaxPos)
{
int max = num[0];
for(int i = 0;i < n; i++)
{
if(num[i] > max)
{
max = num[i];
*pMaxPos = i;
}
}
return max;
}
int FindMin(int num[], int n, int *pMinPos)
{
int min = num[0];
for(int i = 0;i < n; i++)
{
if(num[i] < min)
{
min = num[i];
*pMinPos = i;
}
}
return min;
}
7. 楊輝三角形
題目內容:
編程打印具有如下形式的楊輝三角形,其中輸出數據的行數n從鍵盤輸入,並且n<=10。
#include<stdio.h>
int main()
{
int n;
printf("Input n (n<=10):\n");
scanf("%d",&n);
int a[n][n];
for(int i = 0;i < n; i++)
{
a[i][0] = 1;
a[i][i] = 1;
}
for(int i = 2;i < n; i++)
{
for(int j = 1;j < i;j++)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for(int i = 0;i < n; i++)
{
for(int j = 0; j <= i; j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
return 0;
}
8. 顛倒句子中的單詞順序
題目內容:
從鍵盤輸入一個句子(假設字符數小於100個),句子中的單詞之間用空格分隔,句子必須以一個標點符號作爲結尾,句子開頭和末尾標點符號前均沒有空格,以回車表示輸入結束,請編程顛倒句中的單詞順序並輸出。
函數原型: int Inverse(char str1[], char str2[][N])
#include<stdio.h>
#define N 100
int Inverse(char str1[], char str2[][N]);
int main()
{
char str1[N],str2[N][N];
printf("Input a sentence:");
gets(str1);
int num = Inverse(str1,str2);
for(;num > 0; num--)
{
printf("%s ",str2[num]);
}
printf("%s%c\n",str2[0],str1[0]);
return 0;
}
int Inverse(char str1[], char str2[][N])
{
int num = 0,i;
int j = 0;
int str1_len = strlen(str1);
for(i = 0; i < str1_len - 1; i++)
{
if(str1[i] != ' ')
{
str2[num][j++] = str1[i];
}
else
{
num++;
j = 0;
}
}
str1[0] = str1[str1_len - 1];
return num;
}