僅供參考,請指正
1、以下程序的功能是借助一個變量交換兩個已知數據的值,程序中存在一些錯誤,修改這些錯誤並調試程序。

1 #include "iostream" 2 3 using namespace std; 4 5 int main( ) 6 7 { 8 9 int x,y; 10 11 t=x; 12 13 x=y; 14 15 t=y; 16 17 cin>>x>>y>>endl; 18 19 cout<<"x="<<x<<"y="<<y<<endl; 20 21 system("pause"); 22 23 return 0; 24 25 }

1 #include "iostream" 2 using namespace std; 3 4 int main( ) 5 { 6 int x,y,t;// 1.先定義后使用,t沒有定義 7 8 cin>>x>>y;// 2.先賦值再交換,endl將換行符寫入輸出流 9 10 // 3.交換算法,是x放到臨時變量,y賦值給x,再把臨時變量里的x的值賦值給y 11 t=x; 12 x=y; 13 y=t; 14 15 cout<<"x="<<x<<",y="<<y<<endl; // 4.輸出最好有個逗號分隔,更清晰一些 16 system("pause"); 17 return 0; 18 }
2、編寫一個計算梯形面積的程序。要求梯形的上底、下底和高在定義變量時直接賦值

1 #include "iostream" 2 using namespace std; 3 4 int main( ) 5 { 6 double a = 12,b = 15, h = 10;//初始化,即上底、下底和高在定義變量時直接賦值 7 cout<<(a+b)*h/2<<endl; //計算面積,不再定義面積變量了,直接輸出 8 system("pause"); 9 return 0; 10 }
3、編寫計算一個學生三門課平均成績的程序,要求學生成績從鍵盤輸入

1 #include "iostream" 2 using namespace std; 3 4 int main( ) 5 { 6 double a1,a2,a3; //定義3門課的成績double變量 7 cin>>a1>>a2>>a3; 8 cout<<(a1+a2+a3)/3<<endl; //直接輸出,不再定義平均成績變量 9 system("pause"); 10 return 0; 11 }
4、輸入直角坐標系中點P的坐標(x,y),若P點落在圖中的陰影區域內,輸出陰影部分面積,否則輸出數據0

1 #include "iostream" 2 using namespace std; 3 #include <cmath> 4 5 int main( ) 6 { 7 const double PI = 3.1415926; 8 double x,y; 9 double s = PI*(4*4 - 2*2); //陰影部分的面積 10 cin>>x>>y; 11 //如果p的坐標x,y在陰影內,直接輸出陰影面積 12 if(fabs(x)>=2 && fabs(x)<=4 && fabs(y)>=2 && fabs(y)<=4) 13 cout<<s<<endl; 14 else 15 cout<<"0"<<endl; 16 system("pause"); 17 return 0; 18 }
5、任意輸入3個整數數據,輸出它們中最小的一個數

1 #include "iostream" 2 using namespace std; 3 4 int main( ) 5 { 6 int a,b,c; 7 cin>>a>>b>>c; 8 int min = (a<b?a:b)<c?:c; 9 cout<<min<<endl; 10 system("pause"); 11 return 0; 12 }
6、將"fly"譯成密碼"iob"。編碼規律:將字母a變成字母d,即變成其后的第3個字母,x變成a,y變成b, z變成c。

1 #include "iostream" 2 using namespace std; 3 4 int main( ) 5 { 6 char c1 = 'f',c2 = 'l',c3 = 'y'; // fly 7 8 c1 = (c1+3-'a')%26 + 'a' ; // i 9 c2 = (c2+3-'a')%26 + 'a' ; // o 10 c3 = (c3+3-'a')%26 + 'a' ; // b 11 12 cout<<c1<<c2<<c3<<endl; // iob 13 system("pause"); 14 return 0; 15 }
7、以下程序的功能是求兩個非0整數相除的商和余數。程序有幾處錯誤,試找出它們加以修改,並上機驗證修改結果

1 #include "iostream" 2 3 using namespace std; 4 5 int main() 6 7 {int x,y,r1,r2; 8 9 cin>>x>>y; 10 11 if(x=0||y=0) 12 13 cout<<”input error”<<endl; 14 15 else 16 17 { if(x>y) 18 19 r1=x/y; 20 21 r2=x%y; 22 23 else 24 25 r1=y/x; 26 27 r2=y%x; 28 29 } 30 31 cout<<”商= ”<<r1<<” 余數= ”<<r2<<endl; 32 33 system("pause"); 34 35 return 0; 36 37 }

1 #include "iostream" 2 using namespace std; 3 4 int main() 5 { 6 int x,y,r1,r2; 7 cin>>x>>y; 8 9 if(x==0||y==0)// 比較運算不是賦值 10 cout<<"input error"<<endl; // 引號英文 11 else 12 { 13 if(x>y){ 14 r1=x/y; 15 r2=x%y; 16 } 17 else{ 18 r1=y/x; 19 r2=y%x; 20 } 21 cout<<"商= "<<r1<<" 余數= "<<r2<<endl; //引號英文,不可以放到選擇的外面 22 } 23 24 system("pause"); 25 return 0; 26 }
8、某商場購物時,若所選商品價值x在下述范圍內,則實付金額y按如下折扣支付:用switch語句實現已知x求y

1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 double x, y; 7 int n; 8 cin>>x; 9 10 if(x<1000) n = 10; 11 else if(x>=1000 && x<2000) n = 9; 12 else if(x>=2000 && x<3000) n = 8; 13 else n = 7; 14 15 switch(n) 16 { 17 case 10: 18 y = x;break; 19 case 9: 20 y = 0.9*x;break; 21 case 8: 22 y = 0.8*x;break; 23 case 7: 24 y = 0.7*x;break; 25 }; 26 27 cout<<y<<endl; 28 return 0; 29 }
9、編一模擬袖珍計算器的完整程序,運行結果見圖。要求:輸入兩個操作數和一個操作符,根據操作符決定所做的運算

1 #include <iostream> 2 using namespace std; 3 #include <cmath> 4 5 int main() 6 { 7 char ch; 8 double x,y,result; 9 cout<<"請輸入操作數1"<<" 運算符op"<<" 操作數2"<<endl; 10 cin>>x>>ch>>y; 11 12 switch(ch) 13 { 14 case '+': 15 result = x+y; break; 16 case '-': 17 result = x-y; break; 18 case '*': 19 result = x*y; break; 20 case '/': 21 if(fabs(y)<1e-6){ 22 cout<<"input error"; 23 exit(0); 24 } 25 else 26 result = x/y; 27 break; 28 }; 29 30 cout<<x<<ch<<y<<"="<<result<<endl; 31 return 0; 32 }
10、以下程序求20以內的奇數和。程序有幾處錯誤,試找出它們加以修改,並上機驗證修改結果

1 #include "iostream" 2 using namespace std; 3 int main() 4 { 5 int n,sum; 6 for(n=1; ;n+=2); 7 sum=sum+n; 8 if(n==20) break; 9 cout<<"sum="<<sum<<endl; 10 system("pause"); 11 return 0; 12 }
11、編寫程序將一個十進制整數按倒序形式輸出。即若輸入156,則輸出651

1 #include "iostream" 2 using namespace std; 3 int main() 4 { 5 int n; 6 cin>>n; 7 8 /* 9 //這段可以去掉尾部的0,比如120,輸出21 10 while(n%10==0) 11 { 12 n/=10; 13 } */ 14 15 while(n) 16 { 17 cout<<n%10; 18 n/=10; 19 } 20 21 system("pause"); 22 return 0; 23 }
12、編一程序,顯示出所有的水仙花數。所謂水仙花數,是指一個3位數,其各位數字立方和等於該數字本身。

1 #include "iostream" 2 using namespace std; 3 int main() 4 { 5 for(int i = 100; i<=999; ++i ) 6 { 7 int a = i%10, b = i/10%10, c = i/100; 8 if(a*a*a + b*b*b + c*c*c == i) 9 cout<<i<<" "; 10 } 11 12 system("pause"); 13 return 0; 14 }
13、

1 #include "iostream" 2 using namespace std; 3 #include<time.h> 4 int main() 5 { 6 srand((unsigned)time(NULL)); //種子 7 8 int a = rand()%9 + 1; //隨機產生1~9的數字 a 9 int n = 5 + rand()%5; //隨機產生 5~9的數字 n 10 //cout<<a<<","<<n<<endl; 11 12 long temp=0,sum = 0; // 數據類型 long 13 for(int i=1; i<=n; ++i)//計算sum 14 { 15 temp = temp*10+a; 16 sum += temp; 17 } 18 19 cout<<sum<<endl;//輸出sum 20 21 system("pause"); 22 return 0; 23 }
14、隨機產生10個30~100(包括30,100)的正整數,求它們的最大值、最小值、平均值,並顯示整個數組的值和結果。

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 int main() 5 { 6 srand(time(NULL)); 7 int max=30,min=100,sum=0; 8 int arr[10] = {0}; 9 for(int i=0; i<10; ++i){ 10 arr[i] = rand()%70 + 30; 11 sum += arr[i]; 12 if(arr[i]>max) 13 max =arr[i]; 14 if(arr[i]<min) 15 min = arr[i]; 16 } 17 18 printf("max=%d,min=%d,ave=%.2f\n",max,min,sum/10.0); 19 20 for(int j=0; j<10; ++j) 21 printf("%d ",arr[j]); 22 23 return 0; 24 }
15、隨機產生20個學生的計算機課程的成績(0~100),按照從大到小的順序排序,分別顯示排序前和排序后的結果

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 void SelectionSort(int *a,int left, int right); 6 int main() 7 { 8 srand(time(NULL)); 9 10 int arr[20] = {0}; 11 for(int i=0; i<20; ++i){ 12 arr[i] = rand()%100; 13 } 14 15 printf("排序前:\n"); 16 for(int j=0; j<20; ++j) 17 printf("%d ",arr[j]); 18 19 SelectionSort(arr,0,19); 20 21 printf("\n排序后:\n"); 22 for(int j=0; j<20; ++j) 23 printf("%d ",arr[j]); 24 25 return 0; 26 } 27 /*選擇排序--遞歸*/ 28 void SelectionSort(int *a,int left, int right) 29 { 30 if(left<right){ 31 int j,t; 32 for(j=right; left<j; j--){ 33 if(a[j]<a[left])/*與最左邊的比較*/ 34 t=a[left],a[left]=a[j],a[j]=t; 35 } 36 SelectionSort(a,j+1,right);/*遞歸*/ 37 } 38 }
16、隨機產生10個數,輸入1~10之間的正整數m,使數組元素右移m位,移出的元素再從左移入。如,假設原來的數組元素依次為:1 2 3 4 5 6 7 8 9 10,假設m為2,則右移2位后的數組元素依次為:9 10 1 2 3 4 5 6 7 8

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 int main() 5 { 6 srand(time(NULL)); 7 8 //1.隨機產生1~10十個數 9 int arr1[10] = {0}; 10 for(int i=0; i<10; ++i){ 11 arr1[i] = rand()%10+1; 12 } 13 14 //2.輸出產生的數組 15 printf("移動前:\n"); 16 for(int j=0; j<10; ++j) 17 printf("%d ",arr1[j]); 18 printf("\n"); 19 20 //3.輸入移動的數據m 21 printf("\n請輸入移動數據(1~10):\n"); 22 int arr2[10] = {0}; 23 int m; 24 scanf("%d",&m); 25 26 //4.復制移動覆蓋的數 27 for(int i=0,j=10-m; j<10; ++i,++j) 28 arr2[i] = arr1[j]; 29 30 //5.原數組向右移動m位 31 for(int j=10-m; j>=0; --j) 32 arr1[j+m] = arr1[j]; 33 34 //6.保存的數拷貝回數組 35 for(int j=0; j<m; ++j) 36 arr1[j] = arr2[j]; 37 38 //7.打印移動后的數組 39 printf("\n移動后:\n"); 40 for(int j=0; j<10; ++j) 41 printf("%d ",arr1[j]); 42 43 return 0; 44 }
17、按由大到小的順序輸入10個int類型的數據將其存放在一個一維數組中,再輸入一個整數到變量x,用二分法查找x是否是數組中的元素,若是,輸出其在數組中的位置,否則輸出不在數組中的提示。

1 #include <stdio.h> 2 3 int fun(int *arr,int left,int right,int x); 4 int main() 5 { 6 //輸入數組數據 7 printf("\n請輸入10個int型數據(從大到小有序輸入):\n"); 8 int arr[10] = {0}; 9 for(int i=0; i<10; ++i){ 10 scanf("%d",&arr[i]); 11 } 12 13 //輸入要查找的數據 14 printf("\n請輸入要查找的數據:\n"); 15 int x; 16 scanf("%d",&x); 17 18 //查找x 19 int y = fun(arr,0,9,x); 20 if(y==-1){ 21 printf("x不在數組中\n"); 22 } 23 else{ 24 printf("x是數據的第%d元素\n",y+1); 25 } 26 27 return 0; 28 } 29 30 int fun(int *arr,int left,int right,int x) 31 { 32 while(left<=right) 33 { 34 int mid = (left + right)/2; 35 //int mid = left + (right - left)/2; //防止溢出 36 if(arr[mid]>x) 37 left = mid+1; 38 else if(arr[mid]<x) 39 right = mid-1; 40 else 41 return mid; 42 } 43 return -1; 44 }
18、輸入一個小於10的正整數n,顯示具有如下形式的n行楊輝三角形。圖中n=6

1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 #define N 100 5 6 int main() 7 { 8 int a[N][N] = {0}; 9 int i, j, n = 6; 10 cin>>n; 11 12 for(i=1;i<=n;i++){ 13 a[i][1] = a[i][i] = 1; /*1.第一列和對角線的數都是1*/ 14 } 15 16 for(i=3;i<=n;i++) 17 { 18 for(j=2;j<=i-1;j++){ 19 a[i][j]=a[i-1][j-1]+a[i-1][j]; /*2.除兩邊的數, 等於左上/上兩數之和*/ 20 } 21 } 22 23 for(i=1;i<=n;i++) 24 { 25 cout<<setw((n-i)*3)<<' '; 26 27 for(j=1;j<=i;j++) 28 { 29 cout<<setw(6)<<a[i][j]; 30 } 31 32 cout<<endl; 33 } 34 35 return 0; 36 }
19、編寫程序,將某一指定字符從一個已知的字符串中刪除。假設已知字符串為“aaaasdfga”,將其中出現的'a'字母刪除,刪除后的字符串為“sdfg”

1 #include <iostream> 2 using namespace std; 3 #define N 100 4 5 int main() 6 { 7 char arr[N] = "aaaasdfga"; 8 char *p = arr, *q = arr; 9 char ch = 'a'; 10 11 while(*p) 12 { 13 if(*p!=ch){ 14 *q++ = *p; 15 } 16 p++; 17 } 18 *q = '\0'; 19 20 cout<< arr << endl; 21 22 return 0; 23 }
20、編一個程序,輸入一個字符串,將其倒序存放后輸出。例如,假設原數組a的內容為“VISUAL C++PROGRAM”,倒序后數組a中的內容為“MAGORP++C LASUIV”。要求:不能借助另外一個數組實現倒序存放

1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 #define N 100 5 6 int main() 7 { 8 char arr[N] = "VISUAL C++PROGRAM"; 9 char *p = arr, *q = arr + strlen(arr)-1; 10 gets(arr); 11 12 while(p < q) 13 { 14 char t = *p; 15 *p = *q; 16 *q = t; 17 p++,q--; 18 } 19 20 cout<< arr << endl; 21 22 return 0; 23 }
21、利用字符指針將輸入的一個字符串中的大小寫字母相互轉換,並輸出轉換后的字符串的內容。如,假設輸入的字符串的內容為“How are you”,則轉換后的內容為“hOW ARE YOU”

1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 char str[256] = ""; 7 cin.getline(str,256); 8 char *p = str; 9 10 while(*p) 11 { 12 if(*p>='a'&& *p<='z')//如果小寫轉大寫 13 *p -= 32; 14 else if(*p>='A' && *p<='Z')//如果大寫轉小寫 15 *p += 32; 16 p++; 17 } 18 19 cout<<str<<endl; 20 return 0; 21 }
22、利用字符指針將字符串s中從第n個字符開始的內容復制到字符串t中

1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 char s[256] = ""; 7 char t[256] = ""; 8 cin.getline(s,256); 9 10 int n; 11 cin>>n; 12 char *p = s+n-1;//第n個字符開始,數組下標從0開始的 13 char *q = t; 14 15 while(*q++ = *p++); 16 17 cout<<t<<endl; 18 return 0; 19 }
23、利用指針將一個包含10個整數的數組中的最大最小元素進行交換,並輸出交換后的內容。10個整數為隨機產生的0~100之間的數。

1 #include <iostream> 2 #include <ctime> 3 using namespace std; 4 5 int main() 6 { 7 srand(time(NULL)); 8 int arr[10] = {0}; 9 for(int i=0; i<10; ++i){ //隨機產生0~100的10個數 10 arr[i] = rand()%101; 11 } 12 13 for(int i=0; i<10; ++i){//打印10個隨機數 14 cout<<arr[i]<<","; 15 } 16 17 int maxi=0,mini=0 ; //最大值下標、最小值下標 18 for(int *p = arr; p-arr<10; p++)//利用指針p掃描數組 19 { 20 if(*p<*(arr+mini)){ 21 mini = p - arr; //更新最小值下標 22 } 23 if(*p> *(arr+maxi)){ 24 maxi = p - arr; //更新最大值下標 25 } 26 } 27 cout<<endl; 28 29 //最大值 最小值交換 30 int t = *(arr+maxi); 31 *(arr+maxi) = *(arr+mini); 32 *(arr+mini) = t; 33 34 //交換后的數組,10個數 35 for(int i=0; i<10; ++i){ 36 cout<<arr[i]<<","; 37 } 38 return 0; 39 }
24、編一判斷m是否為素數的函數,並在主函數中利用它輸出十對最小的孿生素數。所謂孿生素數是指兩個相差為2的素數,如3和5,11和13。程序運行結果見下圖。函數形式為:bool isprime(int m)

1 #include "iostream" 2 using namespace std; 3 4 bool isprime(int m); 5 int main() 6 { 7 int two = 2, count = 0; 8 for(int j = 2; count<10; ++j){ 9 if(isprime(j) && isprime(j+two)){ 10 count++; 11 printf("(%d,%d)\n",j,j+two); 12 } 13 } 14 return 0; 15 } 16 bool isprime(int m){ 17 for(int i=2; i<m; ++i){ 18 if(m%i==0) 19 return false; 20 } 21 return true; 22 }
25、編一函數,功能為判斷一字符串是否為回文,如果是回文則返回1,否則返回0。回文是指順讀和倒讀都一樣的字符串,如“deed”和“level”是回文。在主函數中對輸入的字符串加以調用函數形式為:int huiwen(char s[])

1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 5 int huiwen(char s[]); 6 int main() 7 { 8 const int N = 100; 9 char s[N] = ""; 10 cin>>s; 11 if(huiwen(s)) 12 cout<<"是回文"<<endl; 13 else 14 cout<<"不是回文"<<endl; 15 return 0; 16 } 17 int huiwen(char s[]) 18 { 19 char *p = s, *q = s+strlen(s)-1; 20 while(p<q) 21 { 22 if(*p!=*q) 23 return 0; 24 p++, q--; 25 } 26 return 1; 27 }
26、函數的功能是將學生成績從高分到低分排序,並統計優秀與不及格的人數。用下面兩種方法實現:
(1)函數形式為:int fun1(int s[],int n,int *x)
要求優秀人數通過return返回,不及格人數通過指針參數返回結果。
(2)函數形式為:void fun2(int s[],int n,int &x,int &y)
要求優秀與不及格的人數通過引用參數返回結果。
分別編二個函數,學生人數從鍵盤輸入。

1 #include "iostream" 2 #include <string.h> 3 using namespace std; 4 5 int fun1(int s[],int n,int *x); 6 void fun2(int s[],int n,int &x,int &y); 7 int main() 8 { 9 const int N = 100; 10 int s[N] = {0}; 11 int n; 12 cin>>n; 13 for(int i=0; i<n; ++i) 14 cin>>s[i]; 15 16 int x=0,y=0; 17 cout<<"優秀的人數:"<<fun1(s,n,&x)<<endl; 18 cout<<"不及格的人數:"<<x<<endl; 19 20 x=0,y=0; 21 fun2(s,n,x,y); 22 cout<<"優秀的人數:"<<y<<endl<<"不及格的人數:"<<x<<endl; 23 24 return 0; 25 } 26 int fun1(int s[],int n,int *x){ 27 int excellent=0; 28 * x = 0;// 29 for(int i=0; i<n-1; ++i){/*第0個元素有序,從第1個元素向右無序*/ 30 int max = s[i]; 31 int index = i; 32 for(int j = i+1; j<n; ++j){/*從i+1逐個比較*/ 33 if(max<s[j]){ /*是否比后面的小*/ 34 max = s[j]; 35 index = j; 36 } 37 } 38 if(index != i){/*找到了最大值才交換*/ 39 s[index] = s[i]; 40 s[i] = max; 41 } 42 } 43 for(int k=0; k<n; ++k){ 44 if(s[k]>=90) excellent++; 45 if(s[k]<60) (*x)++; 46 } 47 return excellent; 48 } 49 void fun2(int s[],int n,int &x,int &y){ 50 x=y=0; // 51 for(int i=0; i<n-1; ++i){/*第0個元素有序,從第1個元素向右無序*/ 52 int max = s[i]; 53 int index = i; 54 for(int j = i+1; j<n; ++j){/*從i+1逐個比較*/ 55 if(max<s[j]){ /*是否比后面的小*/ 56 max = s[j]; 57 index = j; 58 } 59 } 60 if(index != i){/*找到了最大值才交換*/ 61 s[index] = s[i]; 62 s[i] = max; 63 } 64 } 65 for(int k=0; k<n; ++k){ 66 if(s[k]>=90) y++; 67 if(s[k]<60) x++; 68 } 69 }
27、編一函數,功能為統計字符串中各個字母(不區分大、小寫)出現的頻率,同時找出頻率出現最高的字母及次數,假設出現次數最多的字母只有一個。函數形式為:void freq(char s[],int p[],char &chmax,int &max)程序運行結果如下:

1 #include "iostream" 2 #include <string.h> 3 using namespace std; 4 5 void freq(char s[],int p[],char &chmax,int &max); 6 int main() 7 { 8 char chmax; 9 int max = 0; 10 const int N = 256; 11 int p[N] = {0}; 12 char s[N] = ""; 13 gets(s); 14 freq(s,p,chmax,max); 15 for(int i=0; i<N; ++i){ 16 if(p[i]) 17 printf("%c-----%d\n",i,p[i]); 18 } 19 printf("出現頻率最高的字母:%c-----%d\n",chmax,max); 20 return 0; 21 } 22 void freq(char s[],int p[],char &chmax,int &max){ 23 char *q = s; 24 while(*q) 25 { 26 if(*q>='a'&&*q<='z'||*q>='A'&&*q<='Z') 27 { 28 if(*q<'a') 29 p[*q+32]++; 30 else 31 p[*q]++; 32 if(p[*q]>max){ 33 max = p[*q]; 34 chmax = *q; 35 } 36 } 37 q++; 38 } 39 }
28、編寫遞歸函數int sum(int a[],int n),其功能是求長度為n的數組的累加和,在主函數中隨機產生10個兩位數,調用sum函數,求這10個數的和

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 int sum(int a[],int n); 5 int main( ) 6 { 7 srand(time(NULL)); 8 int arr[10] = {0}; 9 for(int i=0; i<10; ++i)//隨機產生十個兩位數的數組 10 arr[i] = rand()%100; 11 12 for(int j=0; j<10; ++j)//顯示產生的數組 13 printf("%d ",arr[j]); 14 printf("\n"); 15 16 printf("%d\n", sum(arr,10));//調用遞歸函數sum求數組元素之和 17 return 0; 18 } 19 int sum(int a[],int n){ 20 if(n==1) 21 return a[0]; 22 else return a[n-1] + sum(a,n-1); 23 }
29、編寫函數get_max,其功能是將字符串s中最大字符的地址返回,再編寫一個主函數,調用該函數,將字符串s中從最大字符開始的子串中小寫字母轉換成大寫字母,然后輸出新字符串s。例如,假設s的內容為“qwertyou”,則從最大字符’y’開始的子串為“you”,處理后的s為“qwertYOU”。
函數形式為:char *get_max(char s[])

1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 100 4 char *get_max(char s[]); 5 int main( ) 6 { 7 char s[N] = ""; 8 scanf("%s",s); 9 10 char *p = get_max(s); 11 while(*p) 12 { 13 if(*p>='a'&&*p<='z') 14 *p -= 32; 15 p++; 16 } 17 18 printf("%s\n", s); 19 return 0; 20 } 21 /* 22 char *get_max(char s[]){ 23 char ch = s[0]; 24 int i,maxi; 25 for(i=0; s[i]; ++i) 26 if(s[i]>ch){ 27 ch = s[i]; 28 maxi = i; 29 } 30 return &s[maxi]; 31 } 32 */ 33 char *get_max(char s[]){ 34 char *max = s; 35 for(char *p = s; *p; p++) 36 if( *p>*max ) max = p; 37 return max; 38 }
30、有一組關於學生成績的信息,編寫函數max,該函數返回值為分數最高的學生的信息(包括學號和分數)。再編寫主函數對其進行調用並輸出最高分者的信息。假設結構類型定義為:

1 struct student 2 3 { 4 5 char *num; 6 7 int score; 8 9 };

1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 100 4 /* 結構體 */ 5 typedef struct student STUDENT; 6 struct student 7 { 8 char *num; 9 int score; 10 }; 11 /* 求最大分數學生的下標 */ 12 int fun(STUDENT arr[], int n) 13 { 14 int max = arr[0].score; 15 int maxi = 0; 16 for(int i=1; i<n; ++i) 17 if(arr[i].score>max){ 18 max = arr[i].score; 19 maxi = i; 20 } 21 return maxi; 22 } 23 24 int main() 25 { 26 STUDENT arr[N]; 27 printf("請輸入學生人數:\n"); 28 int n; 29 scanf("%d",&n); 30 /* 輸入n個學生的學號及分數 */ 31 printf("請輸入學號、成績:\n"); 32 for(int j=0; j<n; ++j){ 33 arr[j].num = (char*)malloc(sizeof(char)*10); 34 scanf("%s%d",arr[j].num,&arr[j].score); 35 } 36 /* 輸出分數最大值 */ 37 int index = fun(arr,n); 38 printf("num=%s,score=%d\n",arr[index].num, arr[index].score ); 39 return 0; 40 }
31、編寫程序,定義一個日期結構變量,計算某日期是本年度的第幾天。提示:為簡單起見,可定義一個存放12個月中每個月總天數的數組。

1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 100 4 /* 結構體 */ 5 typedef struct date DATE; 6 struct date 7 { 8 int year; 9 int month; 10 int day; 11 }; 12 /* 某日期是該年的第幾天 */ 13 void fun(DATE d) 14 { 15 int months[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 16 int days = d.day; 17 if(d.year%4==0 && d.year%100!=0 ||d.year%400==0) 18 months[2]++; 19 for(int i=1; i<d.month; ++i) 20 days += months[i]; 21 printf("是%d年的第%d天\n",d.year,days); 22 } 23 24 int main() 25 { 26 DATE d; 27 printf("請輸入日期(年/月/日):\n"); 28 scanf("%d/%d/%d",&d.year,&d.month,&d.day); 29 fun(d); 30 return 0; 31 }
32、編寫函數deln,具有刪除鏈表中第n個結點的功能。再編寫主函數,按輸入順序建立不帶頭結點的職工信息單鏈表,然后調用del函數刪除某個職工的信息,並輸出刪除后的職工信息表中的職工信息。假設鏈表結構如下:

1 struct staff 2 3 { 4 5 char num[6]; //職工工號 6 7 char name[20]; //職工姓名 8 9 double wage; //職工工資 10 11 };

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* node */ 5 typedef struct staff *STAFF; 6 struct staff 7 { 8 char num[6]; //職工工號 9 char name[20]; //職工姓名 10 double wage; //職工工資 11 STAFF next; 12 }; 13 14 /* makenode */ 15 STAFF makenode() 16 { 17 STAFF p = (STAFF)malloc(sizeof(struct staff)); 18 printf("請輸入職工的工號、姓名、工資:\n"); 19 scanf("%s%s%lf",p->num,p->name,&p->wage); 20 p->next = NULL; 21 return p; 22 } 23 /* create */ 24 STAFF create(int m) 25 { 26 STAFF head,p; 27 head = p = makenode(); 28 while(--m) 29 { 30 p->next = makenode();//尾插 31 p = p->next; 32 } 33 return head; 34 } 35 /* print */ 36 void printlist(STAFF p) 37 { 38 while(p){ 39 printf("num=%s,name=%s,wag=%.2f\n",p->num,p->name,p->wage); 40 p= p->next; 41 } 42 printf("\n"); 43 } 44 /* deln */ 45 STAFF deln(STAFF head,int n) 46 { 47 STAFF p = head; 48 if(p==NULL) //鏈表為空 49 { 50 return NULL; 51 } 52 if(n==1)//刪除的是頭結點 53 { 54 head = head->next; 55 free(p); 56 } /* */ 57 else 58 { 59 STAFF q = p; 60 while(--n) 61 { 62 q = p; 63 p = p->next; 64 } 65 q->next = p->next; 66 free(p); 67 } 68 return head; 69 } 70 int main() 71 { 72 STAFF head = create(3);//創建有3個結點的單鏈表 73 printlist(head); //打印鏈表 74 head = deln(head,2); //刪除鏈表的第2個元素 75 printlist(head); //打印鏈表 76 return 0; 77 }
33、從鍵盤輸入一個字符串,要求將該字符串的內容按輸入的相反順序組織到一個不帶表頭結點的單鏈表中。假設輸入的字符串為"abcdefg",則組織到鏈表中的內容為"gfedcba"

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define N 100 5 /* node */ 6 typedef struct node *NODE; 7 struct node 8 { 9 char ch; //字符 10 NODE next; 11 }; 12 13 /* makenode */ 14 NODE makenode(char ch) 15 { 16 NODE p = (NODE)malloc(sizeof(struct node)); 17 p->ch = ch; 18 p->next = NULL; 19 return p; 20 } 21 22 /* print */ 23 void printlist(NODE p) 24 { 25 while(p){ 26 printf("%c",p->ch); 27 p= p->next; 28 } 29 printf("\n"); 30 } 31 32 int main() 33 { 34 char s[N] = ""; 35 scanf("%s",s); 36 int len = strlen(s); 37 38 NODE head,p; 39 head = p = makenode(s[len-1]); 40 for(int i=len-2; i>=0; --i){ 41 p->next = makenode(s[i]);//尾插 42 p = p->next; 43 } 44 printlist(head); 45 return 0; 46 }
34、編寫程序,從鍵盤輸入一串字符,要求將該串字符的倒序串先寫入到文件f1.txt中,然后再將原字符串的內容接到該文件的末尾。例如,假設從鍵盤輸入的字符串為“How do you do?”,則文件f1.txt的內容為:
?od uoy od woHHow do you do?

1 #include <stdio.h> 2 #include <string.h> 3 #define N 256 4 int main() 5 { 6 FILE *fp; 7 fp = fopen("f1.txt","w"); 8 /* 1. */ 9 char s[N] = ""; 10 gets(s); 11 int len = strlen(s); 12 for(int i=len-1; i>=0; --i){ 13 fputc(s[i],fp); 14 } 15 /* 2. */ 16 fputs(s,fp); 17 fclose(fp); 18 return 0; 19 }
35、用記事本建立一個文本文件f2.txt,在該文件中任意存放一組整數。編寫程序統計該文件中正整數、負整數和零的個數。(提示:用fscanf函數讀取文件中的數據)

1 #include <stdio.h> 2 int main() 3 { 4 FILE *fp; 5 fp = fopen("f2.txt","r"); 6 7 int plus=0, minus=0, zero=0,num; 8 9 while(!feof(fp)) 10 { 11 fscanf(fp,"%d",&num); 12 if(num>0) 13 plus++; 14 if(num<0) 15 minus++; 16 if(num==0) 17 zero++; 18 } 19 printf("正數=%d,負數=%d,零=%d\n",plus,minus,zero); 20 21 fclose(fp); 22 return 0; 23 }
36、將從鍵盤輸入的N個學生的學號和成績存入到文件student.dat中。再從文件中讀取學生的信息,求出最高分、最低分和總分。N可通過符號常量自行定義大小。

1 #include <stdio.h> 2 #define N 4 3 4 typedef struct student STUDENT; 5 struct student{ 6 char no[20]; 7 double score; 8 }stu[N]; 9 10 int main() 11 { 12 FILE *fp; 13 /* 寫入 */ 14 fp = fopen("student.dat","w"); 15 for(int i=0; i<N; ++i){ 16 scanf("%s%lf",stu[i].no,&stu[i].score); 17 } 18 fwrite(stu, sizeof(STUDENT),4,fp); 19 fclose(fp); 20 /* 讀取 */ 21 fp = fopen("student.dat","r"); 22 STUDENT st[N];//也可以直接用stu 23 fread(st, sizeof(STUDENT),4,fp);//讀取到st 24 /* 處理 */ 25 double max = st[0].score,min=st[0].score,total = st[0].score; 26 for(int j=1; j<N; ++j){ 27 total += st[j].score; 28 if(st[j].score>max) 29 max = st[j].score; 30 if(st[j].score<min) 31 min = st[j].score; 32 } 33 printf("max=%.2f,min=%.2f,total=%.2f\n",max,min,total); 34 fclose(fp); 35 return 0; 36 }
37、設計一個點類(Point),具有數據成員x,y(點的坐標),以及設置、輸出數據成員及求兩點之間距離的功能。再編寫主函數對該類進行測試。

1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 class Point{ 6 double x,y; 7 public: 8 Point(double x,double y){ 9 this->x = x; 10 this->y = y; 11 } 12 double distance( const Point &p){ 13 return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); 14 } 15 }; 16 17 int main() 18 { 19 Point p1(2,3),p2(4,5); 20 cout<<p1.distance(p2)<<endl; 21 return 0; 22 }
38、設計一個字符串類(Mystring),除具有一般的輸入輸出字符串的功能外,還要求具有計算字符串長度、連接兩個字符串等功能,其中求字符串長度和連接字符串功能不能直接調用字符串處理函數。再編寫主函數對該類進行測試

1 #include <iostream> 2 using namespace std; 3 #define N 256 4 class Mystring{ 5 char *p; 6 long length; 7 public: 8 Mystring(); 9 void input(); 10 void show(); 11 long stringLength(); 12 Mystring& operator+(Mystring &s); 13 }; 14 /* 無參構造函數 */ 15 Mystring::Mystring() 16 { 17 p = new char[N]; 18 length = 0; 19 } 20 /* 輸入字符串(並計算長度) */ 21 void Mystring::input() 22 { 23 cin>>p; 24 char *t = p; 25 while(*t) {t++,length++;} 26 } 27 /* 顯示字符串 */ 28 void Mystring::show() 29 { 30 cout<<p<<endl; 31 } 32 /* 輸出字符串長度 */ 33 long Mystring::stringLength() 34 { 35 return length ; 36 } 37 /* +運算符重載(連接兩個字符串) */ 38 Mystring& Mystring::operator+(Mystring &s){ 39 char *t1 = p,*t2 = s.p; 40 while(*t1) t1++; 41 while(*t1++=*t2++); 42 return *this; 43 } 44 int main() 45 { 46 Mystring s1,s2; 47 cout<<"請輸入字符串1:"; 48 s1.input(); 49 cout<<"請輸入字符串2:"; 50 s2.input(); 51 cout<<"輸出字符串1:"; 52 s1.show(); 53 cout<<"輸出字符串1長度:"; 54 cout<<s1.stringLength()<<endl; 55 cout<<"連接2個字符串並輸出\n"; 56 s1 = s1+s2; 57 s1.show(); 58 return 0; 59 }
39、設計一個分數類Fraction。該類的數據成員包括分子fz和分母fm;類中還包括如下成員函數:
(1)構造函數,用於初始化分子和分母。
(2)成員函數print,將分數以 "fz/fm" 的形式輸出。
(3)成員函數Reduction,用於對分數的分子和分母進行約分。
再編寫主函數對該類進行測試。

#include <iostream> using namespace std; #define N 256 class Fraction{ private: int fz,fm; public: /* 構造 */ Fraction(int z,int m){ fz = z; fm = m; } /* 輸出 */ void print(){ Reduction(); cout<<fz<<"/"<<fm<<endl; } private: /* 約分 */ void Reduction(){ int t = fz<fm?fz:fm; while(fz%t!=0||fm%t!=0) t--; fz /= t; fm /= t; } }; int main() { Fraction f(12,16); f.print(); return 0; }
課程相關
1、十進制正整數進制轉化成八進制數(遞歸)
1 void convert(int n) 2 { 3 if(n>0) 4 { 5 convert(n/8);//base==8 6 cout<<n%8; 7 } 8 }
2、尋數組最大元素地址
int *maxaddr(int a[],int n) { int *max=a; for(int *p=a;p<a+n;p++) if(*p>*max) max=p; return max; }
3、設計一個字符串類(Mystring)
1 #include <cstdlib> 2 #include <iostream> 3 using namespace std; 4 int strlen(const char * s) 5 { int i = 0; 6 for(; s[i]; ++i); 7 return i; 8 } 9 void strcpy(char * d,const char * s) 10 { 11 int i = 0; 12 for( i = 0; s[i]; ++i) 13 d[i] = s[i]; 14 d[i] = 0; 15 16 } 17 int strcmp(const char * s1,const char * s2) 18 { 19 for(int i = 0; s1[i] && s2[i] ; ++i) { 20 if( s1[i] < s2[i] ) 21 return -1; 22 else if( s1[i] > s2[i]) 23 return 1; 24 } 25 return 0; 26 } 27 void strcat(char * d,const char * s) 28 { 29 int len = strlen(d); 30 strcpy(d+len,s); 31 } 32 class MyString 33 { 34 int len; 35 char * str; 36 public: 37 MyString (const char *s) :len(strlen(s)) 38 { 39 str=new char[len+1]; 40 strcpy(str,s); 41 } 42 MyString(const MyString &s):len(strlen(s.str)) 43 { 44 str=new char[len+1]; 45 strcpy(str,s.str); 46 } 47 48 //MyString () {str="\0";len=0;} 49 MyString () {str=NULL;len=0;} 50 //~MyString(){if (len!=0) delete []str;} 51 ~MyString(){if (str) delete []str;} 52 //重載<< 53 //friend ostream & operator<<(ostream & o,const MyString & s) 54 //{ 55 // o<<s.str; 56 // return o; 57 //} 58 59 //重載<< 60 friend ostream& operator << (ostream& os, const MyString &s) 61 { 62 if(s.str)os << s.str; 63 return os; 64 } 65 friend int operator <(MyString &s1,MyString &s2) 66 { 67 if(*(s1.str)<*(s2.str)) return 1; 68 else return 0; 69 } 70 friend int operator >(MyString &s1,MyString &s2) 71 { 72 if(*(s1.str)>*(s2.str)) return 1; 73 else return 0; 74 } 75 friend int operator ==(MyString &s1,MyString &s2) 76 { 77 if(*(s1.str)==*(s2.str)) return 1; 78 else return 0; 79 } 80 char * operator()(int start ,int len) 81 { 82 char*tem=new char[len]; 83 int i ; 84 for( i=0;i<len;++i) 85 tem[i]=str[i+start]; 86 tem[i]=0; 87 return tem; 88 } 89 //重載= 90 //MyString & operator =(const MyString &s) 91 //{ 92 // if (this==&s) return *this; 93 // delete [] str; 94 // str=new char[s.len+1]; 95 // strcpy(str,s.str); 96 // return *this; 97 //} 98 99 //重載= 100 MyString & operator=(const MyString & x) 101 { 102 if(str == x.str) 103 return *this; 104 if(str) 105 delete[] str; 106 if(x.str){ 107 str = new char[strlen(x.str)+1]; 108 strcpy(str,x.str); 109 } 110 else 111 str = NULL; 112 return *this; 113 } 114 //多余的字符串轉對象 115 //MyString & operator =(const char *s) 116 //{ 117 // if (len>0)delete []str; 118 // len=strlen(s); 119 // str=new char[len+1]; 120 // strcpy(str,s); 121 // return *this; 122 //} 123 124 125 //對象相加 126 //MyString operator +(MyString &s) 127 //{ 128 // static MyString s1; 129 // if (s1.len>0) delete []s1.str; 130 // s1.len=len+s.len; 131 // s1.str=new char[s1.len]; 132 // strcpy(s1.str,str); 133 // strcat(s1.str,s.str); 134 // return s1; 135 //} 136 137 //對象相加 138 MyString operator+(const MyString &ms) 139 { 140 MyString temp; 141 temp.str = new char[strlen(str) + strlen(ms.str)+1]; 142 strcpy(temp.str, str); 143 strcat(temp.str, ms.str); 144 return temp; 145 } 146 147 //多余的對象與字符串相加 148 //MyString operator +(char *s) 149 //{ 150 // static MyString s1; 151 // if (s1.len>0) delete []s1.str; 152 // s1.len=len+strlen(s); 153 // s1.str=new char[s1.len]; 154 // strcpy(s1.str,str); 155 // strcat(s1.str,s); 156 // return s1; 157 //} 158 159 // 160 MyString & operator +=(const char* s) 161 { 162 len=len+strlen(s); 163 char*p=new char[len]; 164 strcpy(p,str); 165 strcat(p,s); 166 if(len>0) delete []str; 167 str=p; 168 return *this; 169 } 170 //字符串與對象相加 171 //friend MyString & operator +(const char*s,MyString &s2) 172 //{ 173 // static MyString s1; 174 // if (s1.len>=0) delete []s1.str; 175 // s1.len=strlen(s)+s2.len; 176 // s1.str=new char[s1.len]; 177 // strcpy(s1.str,s); 178 // strcat(s1.str,s2.str); 179 // return s1; 180 //} 181 //字符串與對象相加 182 friend MyString operator+(const char*str, const MyString &ms) 183 { 184 MyString temp(str);//字符串轉對象 185 temp = temp + ms; 186 return temp; 187 } 188 // 189 char &operator [](int i) 190 { 191 return str[i]; 192 } 193 }; 194 195 196 int CompareString( const void * e1, const void * e2) 197 { 198 MyString * s1 = (MyString * ) e1; 199 MyString * s2 = (MyString * ) e2; 200 if( * s1 < *s2 ) 201 return -1; 202 else if( *s1 == *s2) 203 return 0; 204 else if( *s1 > *s2 ) 205 return 1; 206 } 207 int main() 208 { 209 MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); 210 MyString SArray[4] = {"big","me","about","take"}; 211 cout << "1. " << s1 << s2 << s3<< s4<< endl; 212 s4 = s3; 213 s3 = s1 + s3; 214 cout << "2. " << s1 << endl; 215 cout << "3. " << s2 << endl; 216 cout << "4. " << s3 << endl; 217 cout << "5. " << s4 << endl; 218 cout << "6. " << s1[2] << endl; 219 s2 = s1; 220 s1 = "ijkl-"; 221 s1[2] = 'A' ; 222 cout << "7. " << s2 << endl; 223 cout << "8. " << s1 << endl; 224 s1 += "mnop"; 225 cout << "9. " << s1 << endl; 226 s4 = "qrst-" + s2; 227 cout << "10. " << s4 << endl; 228 s1 = s2 + s4 + " uvw " + "xyz"; 229 cout << "11. " << s1 << endl; 230 qsort(SArray,4,sizeof(MyString),CompareString); 231 for( int i = 0;i < 4;i ++ ) 232 cout << SArray[i] << endl; 233 //s1的從下標0開始長度為4的子串 234 cout << s1(0,4) << endl; 235 //s1的從下標5開始長度為10的子串 236 cout << s1(5,10) << endl; 237 return 0; 238 }
------------------------------------------------------------------------------------------------------
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 #define N 256 5 class Mystring{ 6 char *p; 7 static char *temp; 8 public: 9 Mystring(); 10 ~Mystring(); 11 long stringLength(); 12 Mystring& operator=(const char *t); 13 14 friend char* operator+(Mystring &s1,Mystring &s2); 15 friend char* operator+(char *s1,Mystring &s2); 16 friend istream &operator>>( istream &in, Mystring &s ); 17 friend ostream &operator<<( ostream &out, Mystring &s ); 18 }; 19 char* Mystring::temp = new char[N]; 20 /* 無參構造函數 */ 21 Mystring::Mystring() 22 { 23 p = new char[N]; 24 } 25 /* 析構函數 */ 26 Mystring::~Mystring() 27 { 28 if(p) delete p; 29 } 30 /* 輸出字符串長度 */ 31 long Mystring::stringLength() 32 { 33 return strlen(p) ; 34 } 35 /* +運算符重載(連接字符串與對象) */ 36 char* operator+(char *s1,Mystring &s2){ 37 if(s1==Mystring::temp){ 38 strcat(s1,s2.p); 39 } 40 return s1; 41 } 42 /* +運算符重載(連接兩個對象)*/ 43 char* operator+(Mystring &s1,Mystring &s2){ 44 strcat(strcpy(Mystring::temp,s1.p),s2.p); 45 return Mystring::temp; 46 } 47 /* =運算符重載(char*轉對象引用)*/ 48 Mystring& Mystring::operator=(const char *t){ 49 strcpy(this->p,t); 50 return *this; 51 } 52 /* 友元函數,重載>>輸入字符串 */ 53 istream &operator>>( istream &in, Mystring &s ) 54 { 55 in>>s.p; 56 return in; 57 } 58 /* 友元函數,重載<<輸出字符串 */ 59 ostream &operator<<( ostream &out, Mystring &s ) 60 { 61 out<<s.p; 62 return out; 63 } 64 int main() 65 { 66 cout<<"請輸入3個字符串:\n"; 67 Mystring s1,s2,s3,s4; 68 cin>>s1>>s2>>s3; 69 cout<<"連接3個字符串並輸出:\n"; 70 cout<<s1+s2+s3<<endl; 71 cout<<"連接3個字符串並賦值給字符串4,並輸出:\n"; 72 s4 = s1+s2+s3; 73 cout<<s4<<endl; 74 cout<<"輸出字符串1:\n"; 75 cout<<s1<<endl; 76 cout<<"輸出字符串1長度:\n"; 77 cout<<s1.stringLength()<<endl; 78 return 0; 79 }
4、將一個字符串中的大小寫字母相互轉換后寫入文件alp.txt中
1 #define N 100 2 #include <iostream> 3 #include <string.h> 4 using namespace std; 5 int main() 6 { 7 FILE *fp; 8 int i; 9 char s[N]; 10 fp =fopen("alp.txt","w");//1.只寫打開一個文件 11 if (fp == NULL) 12 { 13 cout << "can't open alp.txt" << endl; 14 exit(0); 15 } 16 gets(s); 17 i=0; //2. i 賦初值 18 while (s[i] != '\0') 19 { 20 if (s[i] >= 'a'&&s[i] <= 'z') 21 s[i] = s[i] - 'a' + 'A'; 22 else if(s[i] >= 'A'&& s[i] <= 'Z')//3. else if 23 s[i] = s[i] - 'A' + 'a'; 24 i++; 25 } 26 fputs(s,fp);//4.寫文件 27 fclose(fp); 28 system("pause"); 29 return 0; 30 }
5、定義了一個日期類,具有設置日期、判斷閏年、輸出日期等功能。程序對2018年8月23日進行測試。
1 #include <iostream> 2 using namespace std; 3 class Tdate 4 { 5 private: //1. 6 int year,month,day; 7 public: 8 void setdate(int y,int m,int d) 9 {year=y; month=m; day=d;} 10 int isleapyear()//要求為閏年時函數的返回值為1,否則為0 11 { 12 if(year%400==0||(year%4==0&&year%100!=0)) 13 return 1; 14 else 15 return 0; 16 } 17 void print(); 18 }; 19 void Tdate::print() //2. 20 {cout<<year<<"."<<month<<"."<<day<<endl;} 21 22 int main() 23 { 24 Tdate *date1; 25 date1 = new Tdate(); //3. 26 date1->setdate(2018,8,23); 27 date1->print(); 28 if(date1->isleapyear()) //4. 29 cout<<"leap year."<<endl; 30 else 31 cout<<"not leap year."<<endl; 32 system("pause"); 33 return 0; 34 }
6、某球類比賽根據第一階段的積分情況將8支隊伍分成兩組再進行第二階段的比賽,分組原則是:積分第1名分在A組,第2名分在B組,第3名分在A組,第4名分在B組,依次類推。
1 #define N 8 2 #include <iostream> 3 using namespace std; 4 struct Team 5 { 6 char name[30]; 7 int score; 8 }; 9 int main() 10 { 11 Team t[N], t1[N/2], t2[N/2]; 12 int i, j,imax; 13 for (i = 0; i < N; i++) 14 cin >> t[i].name >> t[i].score; 15 /* 交換排序 */ 16 for (i = 0; i < N - 1; i++) 17 { 18 imax = i;//1. 19 for (j = imax + 1; j < N; j++) 20 if (t[imax].score<t[j].score) //2. 21 imax = j; 22 if (imax != i) 23 { 24 Team temp = t[i];//3. 25 t[i] = t[imax]; 26 t[imax] = temp; 27 } 28 } 29 for (i = 0; i < N; i++) 30 if (i%2==0) //4. 31 t1[i/2] = t[i]; 32 else 33 t2[i/2] = t[i]; 34 cout << "A組隊伍及積分:" << endl; 35 for (i = 0; i < N / 2; i++) 36 cout << t1[i].name << ' '<<t1[i].score <<endl; 37 cout << "B組隊伍及積分:" << endl; 38 for (i = 0; i < N / 2; i++) 39 cout << t2[i].name << ' ' << t2[i].score<<endl; 40 system("pause"); 41 return 0; 42 }
7、函數invert的功能是將字符串s倒序存放。
1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 int main() 5 { 6 void invert(char s[]);//1. 7 char s[100]; 8 cin>>s; 9 invert(s);//2. 10 cout<<s<<endl; 11 system("pause"); 12 return 0; 13 } 14 void invert(char s[]) 15 { 16 int i,len; 17 char ch; 18 len=strlen(s); //3. 19 for(i=0;i<len/2;i++) 20 { 21 ch=s[i]; 22 s[i]=s[len-1-i];//4. 23 s[len-1-i]=ch; 24 } 25 }
8、驗證“滿足公式 p=n*n+n+41 (n是正整數)的p一定是素 數”這個說法的不正確性。
1 #include <iostream> 2 using namespace std; 3 int fun(int p) 4 { 5 int n; 6 for (n=2; p%n != 0; n++);//1. 7 return n==p; //2. 8 } 9 int main() 10 { 11 int n = 1, p; 12 do 13 { 14 p = n * n + n + 41; 15 if (!fun(p)) //3. 16 break; 17 n++; //4. 18 } while (1); 19 cout<< p<<"="<<n<<"*"<<n<<"+"<<n<<"+41不是素數,故結論不正確"<<endl; 20 system("pause"); 21 return 0; 22 }