编程题(C/C++程序设计,同济大学mooc)
僅供參考,請指正
1、以下程序的功能是借助一個(gè)變量交換兩個(gè)已知數(shù)據(jù)的值,程序中存在一些錯誤,修改這些錯誤并調(diào)試程序。
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 }
View Code
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放到臨時(shí)變量,y賦值給x,再把臨時(shí)變量里的x的值賦值給y
11 t=x;
12 x=y;
13 y=t;
14
15 cout<<"x="<<x<<",y="<<y<<endl; // 4.輸出最好有個(gè)逗號分隔,更清晰一些
16 system("pause");
17 return 0;
18 }
View Code
2、編寫一個(gè)計(jì)算梯形面積的程序。要求梯形的上底、下底和高在定義變量時(shí)直接賦值
1 #include "iostream"
2 using namespace std;
3
4 int main( )
5 {
6 double a = 12,b = 15, h = 10;//初始化,即上底、下底和高在定義變量時(shí)直接賦值
7 cout<<(a+b)*h/2<<endl; //計(jì)算面積,不再定義面積變量了,直接輸出
8 system("pause");
9 return 0;
10 }
View Code
3、編寫計(jì)算一個(gè)學(xué)生三門課平均成績的程序,要求學(xué)生成績從鍵盤輸入
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 }
View Code
4、輸入直角坐標(biāo)系中點(diǎn)P的坐標(biāo)(x,y),若P點(diǎn)落在圖中的陰影區(qū)域內(nèi),輸出陰影部分面積,否則輸出數(shù)據(jù)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的坐標(biāo)x,y在陰影內(nèi),直接輸出陰影面積
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 }
View Code
5、任意輸入3個(gè)整數(shù)數(shù)據(jù),輸出它們中最小的一個(gè)數(shù)
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 }
View Code
6、將"fly"譯成密碼"iob"。編碼規(guī)律:將字母a變成字母d,即變成其后的第3個(gè)字母,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 }
View Code
7、以下程序的功能是求兩個(gè)非0整數(shù)相除的商和余數(shù)。程序有幾處錯誤,試找出它們加以修改,并上機(jī)驗(yàn)證修改結(jié)果
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<<” 余數(shù)= ”<<r2<<endl;
32
33 system("pause");
34
35 return 0;
36
37 }
View Code
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)// 比較運(yùn)算不是賦值
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<<" 余數(shù)= "<<r2<<endl; //引號英文,不可以放到選擇的外面
22 }
23
24 system("pause");
25 return 0;
26 }
View Code
8、某商場購物時(shí),若所選商品價(jià)值x在下述范圍內(nèi),則實(shí)付金額y按如下折扣支付:用switch語句實(shí)現(xiàn)已知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 }
View Code
9、編一模擬袖珍計(jì)算器的完整程序,運(yùn)行結(jié)果見圖。要求:輸入兩個(gè)操作數(shù)和一個(gè)操作符,根據(jù)操作符決定所做的運(yùn)算
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<<"請輸入操作數(shù)1"<<" 運(yùn)算符op"<<" 操作數(shù)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 }
View Code
10、以下程序求20以內(nèi)的奇數(shù)和。程序有幾處錯誤,試找出它們加以修改,并上機(jī)驗(yàn)證修改結(jié)果
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 }
View Code
11、編寫程序?qū)⒁粋€(gè)十進(jìn)制整數(shù)按倒序形式輸出。即若輸入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 }
View Code
12、編一程序,顯示出所有的水仙花數(shù)。所謂水仙花數(shù),是指一個(gè)3位數(shù),其各位數(shù)字立方和等于該數(shù)字本身。
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 }
View Code
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; //隨機(jī)產(chǎn)生1~9的數(shù)字 a
9 int n = 5 + rand()%5; //隨機(jī)產(chǎn)生 5~9的數(shù)字 n
10 //cout<<a<<","<<n<<endl;
11
12 long temp=0,sum = 0; // 數(shù)據(jù)類型 long
13 for(int i=1; i<=n; ++i)//計(jì)算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 }
View Code
14、隨機(jī)產(chǎn)生10個(gè)30~100(包括30,100)的正整數(shù),求它們的最大值、最小值、平均值,并顯示整個(gè)數(shù)組的值和結(jié)果。
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
",max,min,sum/10.0);
19
20 for(int j=0; j<10; ++j)
21 printf("%d ",arr[j]);
22
23 return 0;
24 }
View Code
15、隨機(jī)產(chǎn)生20個(gè)學(xué)生的計(jì)算機(jī)課程的成績(0~100),按照從大到小的順序排序,分別顯示排序前和排序后的結(jié)果
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("排序前:
");
16 for(int j=0; j<20; ++j)
17 printf("%d ",arr[j]);
18
19 SelectionSort(arr,0,19);
20
21 printf("
排序后:
");
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 }
View Code
16、隨機(jī)產(chǎn)生10個(gè)數(shù),輸入1~10之間的正整數(shù)m,使數(shù)組元素右移m位,移出的元素再從左移入。如,假設(shè)原來的數(shù)組元素依次為:1 2 3 4 5 6 7 8 9 10,假設(shè)m為2,則右移2位后的數(shù)組元素依次為: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.隨機(jī)產(chǎn)生1~10十個(gè)數(shù)
9 int arr1[10] = {0};
10 for(int i=0; i<10; ++i){
11 arr1[i] = rand()%10+1;
12 }
13
14 //2.輸出產(chǎn)生的數(shù)組
15 printf("移動前:
");
16 for(int j=0; j<10; ++j)
17 printf("%d ",arr1[j]);
18 printf("
");
19
20 //3.輸入移動的數(shù)據(jù)m
21 printf("
請輸入移動數(shù)據(jù)(1~10):
");
22 int arr2[10] = {0};
23 int m;
24 scanf("%d",&m);
25
26 //4.復(fù)制移動覆蓋的數(shù)
27 for(int i=0,j=10-m; j<10; ++i,++j)
28 arr2[i] = arr1[j];
29
30 //5.原數(shù)組向右移動m位
31 for(int j=10-m; j>=0; --j)
32 arr1[j+m] = arr1[j];
33
34 //6.保存的數(shù)拷貝回?cái)?shù)組
35 for(int j=0; j<m; ++j)
36 arr1[j] = arr2[j];
37
38 //7.打印移動后的數(shù)組
39 printf("
移動后:
");
40 for(int j=0; j<10; ++j)
41 printf("%d ",arr1[j]);
42
43 return 0;
44 }
View Code
17、按由大到小的順序輸入10個(gè)int類型的數(shù)據(jù)將其存放在一個(gè)一維數(shù)組中,再輸入一個(gè)整數(shù)到變量x,用二分法查找x是否是數(shù)組中的元素,若是,輸出其在數(shù)組中的位置,否則輸出不在數(shù)組中的提示。
1 #include <stdio.h>
2
3 int fun(int *arr,int left,int right,int x);
4 int main()
5 {
6 //輸入數(shù)組數(shù)據(jù)
7 printf("
請輸入10個(gè)int型數(shù)據(jù)(從大到小有序輸入):
");
8 int arr[10] = {0};
9 for(int i=0; i<10; ++i){
10 scanf("%d",&arr[i]);
11 }
12
13 //輸入要查找的數(shù)據(jù)
14 printf("
請輸入要查找的數(shù)據(jù):
");
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不在數(shù)組中
");
22 }
23 else{
24 printf("x是數(shù)據(jù)的第%d元素
",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 }
View Code
18、輸入一個(gè)小于10的正整數(shù)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.第一列和對角線的數(shù)都是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.除兩邊的數(shù), 等于左上/上兩數(shù)之和*/
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 }
View Code
19、編寫程序,將某一指定字符從一個(gè)已知的字符串中刪除。假設(shè)已知字符串為“aaaasdfga”,將其中出現(xiàn)的'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 = '';
19
20 cout<< arr << endl;
21
22 return 0;
23 }
View Code
20、編一個(gè)程序,輸入一個(gè)字符串,將其倒序存放后輸出。例如,假設(shè)原數(shù)組a的內(nèi)容為“VISUAL C++PROGRAM”,倒序后數(shù)組a中的內(nèi)容為“MAGORP++C LASUIV”。要求:不能借助另外一個(gè)數(shù)組實(shí)現(xiàn)倒序存放
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 }
View Code
21、利用字符指針將輸入的一個(gè)字符串中的大小寫字母相互轉(zhuǎn)換,并輸出轉(zhuǎn)換后的字符串的內(nèi)容。如,假設(shè)輸入的字符串的內(nèi)容為“How are you”,則轉(zhuǎn)換后的內(nèi)容為“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')//如果小寫轉(zhuǎn)大寫
13 *p -= 32;
14 else if(*p>='A' && *p<='Z')//如果大寫轉(zhuǎn)小寫
15 *p += 32;
16 p++;
17 }
18
19 cout<<str<<endl;
20 return 0;
21 }
View Code
22、利用字符指針將字符串s中從第n個(gè)字符開始的內(nèi)容復(fù)制到字符串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個(gè)字符開始,數(shù)組下標(biāo)從0開始的
13 char *q = t;
14
15 while(*q++ = *p++);
16
17 cout<<t<<endl;
18 return 0;
19 }
View Code
23、利用指針將一個(gè)包含10個(gè)整數(shù)的數(shù)組中的最大最小元素進(jìn)行交換,并輸出交換后的內(nèi)容。10個(gè)整數(shù)為隨機(jī)產(chǎn)生的0~100之間的數(shù)。
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){ //隨機(jī)產(chǎn)生0~100的10個(gè)數(shù)
10 arr[i] = rand()%101;
11 }
12
13 for(int i=0; i<10; ++i){//打印10個(gè)隨機(jī)數(shù)
14 cout<<arr[i]<<",";
15 }
16
17 int maxi=0,mini=0 ; //最大值下標(biāo)、最小值下標(biāo)
18 for(int *p = arr; p-arr<10; p++)//利用指針p掃描數(shù)組
19 {
20 if(*p<*(arr+mini)){
21 mini = p - arr; //更新最小值下標(biāo)
22 }
23 if(*p> *(arr+maxi)){
24 maxi = p - arr; //更新最大值下標(biāo)
25 }
26 }
27 cout<<endl;
28
29 //最大值 最小值交換
30 int t = *(arr+maxi);
31 *(arr+maxi) = *(arr+mini);
32 *(arr+mini) = t;
33
34 //交換后的數(shù)組,10個(gè)數(shù)
35 for(int i=0; i<10; ++i){
36 cout<<arr[i]<<",";
37 }
38 return 0;
39 }
View Code
24、編一判斷m是否為素?cái)?shù)的函數(shù),并在主函數(shù)中利用它輸出十對最小的孿生素?cái)?shù)。所謂孿生素?cái)?shù)是指兩個(gè)相差為2的素?cái)?shù),如3和5,11和13。程序運(yùn)行結(jié)果見下圖。函數(shù)形式為: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)
",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 }
View Code
25、編一函數(shù),功能為判斷一字符串是否為回文,如果是回文則返回1,否則返回0。回文是指順讀和倒讀都一樣的字符串,如“deed”和“level”是回文。在主函數(shù)中對輸入的字符串加以調(diào)用函數(shù)形式為: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 }
View Code
26、函數(shù)的功能是將學(xué)生成績從高分到低分排序,并統(tǒng)計(jì)優(yōu)秀與不及格的人數(shù)。用下面兩種方法實(shí)現(xiàn):
(1)函數(shù)形式為:int fun1(int s[],int n,int *x)
要求優(yōu)秀人數(shù)通過return返回,不及格人數(shù)通過指針參數(shù)返回結(jié)果。
(2)函數(shù)形式為:void fun2(int s[],int n,int &x,int &y)
要求優(yōu)秀與不及格的人數(shù)通過引用參數(shù)返回結(jié)果。
分別編二個(gè)函數(shù),學(xué)生人數(shù)從鍵盤輸入。
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<<"優(yōu)秀的人數(shù):"<<fun1(s,n,&x)<<endl;
18 cout<<"不及格的人數(shù):"<<x<<endl;
19
20 x=0,y=0;
21 fun2(s,n,x,y);
22 cout<<"優(yōu)秀的人數(shù):"<<y<<endl<<"不及格的人數(shù):"<<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個(gè)元素有序,從第1個(gè)元素向右無序*/
30 int max = s[i];
31 int index = i;
32 for(int j = i+1; j<n; ++j){/*從i+1逐個(gè)比較*/
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個(gè)元素有序,從第1個(gè)元素向右無序*/
52 int max = s[i];
53 int index = i;
54 for(int j = i+1; j<n; ++j){/*從i+1逐個(gè)比較*/
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 }
View Code
27、編一函數(shù),功能為統(tǒng)計(jì)字符串中各個(gè)字母(不區(qū)分大、小寫)出現(xiàn)的頻率,同時(shí)找出頻率出現(xiàn)最高的字母及次數(shù),假設(shè)出現(xiàn)次數(shù)最多的字母只有一個(gè)。函數(shù)形式為:void freq(char s[],int p[],char &chmax,int &max)程序運(yùn)行結(jié)果如下:
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
",i,p[i]);
18 }
19 printf("出現(xiàn)頻率最高的字母:%c-----%d
",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 }
View Code
28、編寫遞歸函數(shù)int sum(int a[],int n),其功能是求長度為n的數(shù)組的累加和,在主函數(shù)中隨機(jī)產(chǎn)生10個(gè)兩位數(shù),調(diào)用sum函數(shù),求這10個(gè)數(shù)的和
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)//隨機(jī)產(chǎn)生十個(gè)兩位數(shù)的數(shù)組
10 arr[i] = rand()%100;
11
12 for(int j=0; j<10; ++j)//顯示產(chǎn)生的數(shù)組
13 printf("%d ",arr[j]);
14 printf("
");
15
16 printf("%d
", sum(arr,10));//調(diào)用遞歸函數(shù)sum求數(shù)組元素之和
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 }
View Code
29、編寫函數(shù)get_max,其功能是將字符串s中最大字符的地址返回,再編寫一個(gè)主函數(shù),調(diào)用該函數(shù),將字符串s中從最大字符開始的子串中小寫字母轉(zhuǎn)換成大寫字母,然后輸出新字符串s。例如,假設(shè)s的內(nèi)容為“qwertyou”,則從最大字符’y’開始的子串為“you”,處理后的s為“qwertYOU”。
函數(shù)形式為: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
", 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 }
View Code
30、有一組關(guān)于學(xué)生成績的信息,編寫函數(shù)max,該函數(shù)返回值為分?jǐn)?shù)最高的學(xué)生的信息(包括學(xué)號和分?jǐn)?shù))。再編寫主函數(shù)對其進(jìn)行調(diào)用并輸出最高分者的信息。假設(shè)結(jié)構(gòu)類型定義為:
1 struct student
2
3 {
4
5 char *num;
6
7 int score;
8
9 };
View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define N 100
4 /* 結(jié)構(gòu)體 */
5 typedef struct student STUDENT;
6 struct student
7 {
8 char *num;
9 int score;
10 };
11 /* 求最大分?jǐn)?shù)學(xué)生的下標(biāo) */
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("請輸入學(xué)生人數(shù):
");
28 int n;
29 scanf("%d",&n);
30 /* 輸入n個(gè)學(xué)生的學(xué)號及分?jǐn)?shù) */
31 printf("請輸入學(xué)號、成績:
");
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 /* 輸出分?jǐn)?shù)最大值 */
37 int index = fun(arr,n);
38 printf("num=%s,score=%d
",arr[index].num, arr[index].score );
39 return 0;
40 }
View Code
31、編寫程序,定義一個(gè)日期結(jié)構(gòu)變量,計(jì)算某日期是本年度的第幾天。提示:為簡單起見,可定義一個(gè)存放12個(gè)月中每個(gè)月總天數(shù)的數(shù)組。
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define N 100
4 /* 結(jié)構(gòu)體 */
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天
",d.year,days);
22 }
23
24 int main()
25 {
26 DATE d;
27 printf("請輸入日期(年/月/日):
");
28 scanf("%d/%d/%d",&d.year,&d.month,&d.day);
29 fun(d);
30 return 0;
31 }
View Code
32、編寫函數(shù)deln,具有刪除鏈表中第n個(gè)結(jié)點(diǎn)的功能。再編寫主函數(shù),按輸入順序建立不帶頭結(jié)點(diǎn)的職工信息單鏈表,然后調(diào)用del函數(shù)刪除某個(gè)職工的信息,并輸出刪除后的職工信息表中的職工信息。假設(shè)鏈表結(jié)構(gòu)如下:
1 struct staff
2
3 {
4
5 char num[6]; //職工工號
6
7 char name[20]; //職工姓名
8
9 double wage; //職工工資
10
11 };
View Code
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("請輸入職工的工號、姓名、工資:
");
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
",p->num,p->name,p->wage);
40 p= p->next;
41 }
42 printf("
");
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)//刪除的是頭結(jié)點(diǎn)
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);//創(chuàng)建有3個(gè)結(jié)點(diǎn)的單鏈表
73 printlist(head); //打印鏈表
74 head = deln(head,2); //刪除鏈表的第2個(gè)元素
75 printlist(head); //打印鏈表
76 return 0;
77 }
View Code
33、從鍵盤輸入一個(gè)字符串,要求將該字符串的內(nèi)容按輸入的相反順序組織到一個(gè)不帶表頭結(jié)點(diǎn)的單鏈表中。假設(shè)輸入的字符串為"abcdefg",則組織到鏈表中的內(nèi)容為"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("
");
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 }
View Code
34、編寫程序,從鍵盤輸入一串字符,要求將該串字符的倒序串先寫入到文件f1.txt中,然后再將原字符串的內(nèi)容接到該文件的末尾。例如,假設(shè)從鍵盤輸入的字符串為“How do you do?”,則文件f1.txt的內(nèi)容為:
?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 }
View Code
35、用記事本建立一個(gè)文本文件f2.txt,在該文件中任意存放一組整數(shù)。編寫程序統(tǒng)計(jì)該文件中正整數(shù)、負(fù)整數(shù)和零的個(gè)數(shù)。(提示:用fscanf函數(shù)讀取文件中的數(shù)據(jù))
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("正數(shù)=%d,負(fù)數(shù)=%d,零=%d
",plus,minus,zero);
20
21 fclose(fp);
22 return 0;
23 }
View Code
36、將從鍵盤輸入的N個(gè)學(xué)生的學(xué)號和成績存入到文件student.dat中。再從文件中讀取學(xué)生的信息,求出最高分、最低分和總分。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
",max,min,total);
34 fclose(fp);
35 return 0;
36 }
View Code
37、設(shè)計(jì)一個(gè)點(diǎn)類(Point),具有數(shù)據(jù)成員x,y(點(diǎn)的坐標(biāo)),以及設(shè)置、輸出數(shù)據(jù)成員及求兩點(diǎn)之間距離的功能。再編寫主函數(shù)對該類進(jìn)行測試。
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 }
View Code
38、設(shè)計(jì)一個(gè)字符串類(Mystring),除具有一般的輸入輸出字符串的功能外,還要求具有計(jì)算字符串長度、連接兩個(gè)字符串等功能,其中求字符串長度和連接字符串功能不能直接調(diào)用字符串處理函數(shù)。再編寫主函數(shù)對該類進(jìn)行測試
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 /* 無參構(gòu)造函數(shù) */
15 Mystring::Mystring()
16 {
17 p = new char[N];
18 length = 0;
19 }
20 /* 輸入字符串(并計(jì)算長度) */
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 /* +運(yùn)算符重載(連接兩個(gè)字符串) */
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個(gè)字符串并輸出
";
56 s1 = s1+s2;
57 s1.show();
58 return 0;
59 }
View Code
39、設(shè)計(jì)一個(gè)分?jǐn)?shù)類Fraction。該類的數(shù)據(jù)成員包括分子fz和分母fm;類中還包括如下成員函數(shù):
(1)構(gòu)造函數(shù),用于初始化分子和分母。
(2)成員函數(shù)print,將分?jǐn)?shù)以 "fz/fm" 的形式輸出。
(3)成員函數(shù)Reduction,用于對分?jǐn)?shù)的分子和分母進(jìn)行約分。
再編寫主函數(shù)對該類進(jìn)行測試。
#include <iostream>
using namespace std;
#define N 256
class Fraction{
private:
int fz,fm;
public:
/* 構(gòu)造 */
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;
}
View Code
課程相關(guān)
1、十進(jìn)制正整數(shù)進(jìn)制轉(zhuǎn)化成八進(jìn)制數(shù)(遞歸)
1 void convert(int n)
2 {
3 if(n>0)
4 {
5 convert(n/8);//base==8
6 cout<<n%8;
7 }
8 }
2、尋數(shù)組最大元素地址
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、設(shè)計(jì)一個(gè)字符串類(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="";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 //多余的字符串轉(zhuǎn)對象
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);//字符串轉(zhuǎn)對象
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的從下標(biāo)0開始長度為4的子串
234 cout << s1(0,4) << endl;
235 //s1的從下標(biāo)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 /* 無參構(gòu)造函數(shù) */
21 Mystring::Mystring()
22 {
23 p = new char[N];
24 }
25 /* 析構(gòu)函數(shù) */
26 Mystring::~Mystring()
27 {
28 if(p) delete p;
29 }
30 /* 輸出字符串長度 */
31 long Mystring::stringLength()
32 {
33 return strlen(p) ;
34 }
35 /* +運(yùn)算符重載(連接字符串與對象) */
36 char* operator+(char *s1,Mystring &s2){
37 if(s1==Mystring::temp){
38 strcat(s1,s2.p);
39 }
40 return s1;
41 }
42 /* +運(yùn)算符重載(連接兩個(gè)對象)*/
43 char* operator+(Mystring &s1,Mystring &s2){
44 strcat(strcpy(Mystring::temp,s1.p),s2.p);
45 return Mystring::temp;
46 }
47 /* =運(yùn)算符重載(char*轉(zhuǎn)對象引用)*/
48 Mystring& Mystring::operator=(const char *t){
49 strcpy(this->p,t);
50 return *this;
51 }
52 /* 友元函數(shù),重載>>輸入字符串 */
53 istream &operator>>( istream &in, Mystring &s )
54 {
55 in>>s.p;
56 return in;
57 }
58 /* 友元函數(shù),重載<<輸出字符串 */
59 ostream &operator<<( ostream &out, Mystring &s )
60 {
61 out<<s.p;
62 return out;
63 }
64 int main()
65 {
66 cout<<"請輸入3個(gè)字符串:
";
67 Mystring s1,s2,s3,s4;
68 cin>>s1>>s2>>s3;
69 cout<<"連接3個(gè)字符串并輸出:
";
70 cout<<s1+s2+s3<<endl;
71 cout<<"連接3個(gè)字符串并賦值給字符串4,并輸出:
";
72 s4 = s1+s2+s3;
73 cout<<s4<<endl;
74 cout<<"輸出字符串1:
";
75 cout<<s1<<endl;
76 cout<<"輸出字符串1長度:
";
77 cout<<s1.stringLength()<<endl;
78 return 0;
79 }
4、將一個(gè)字符串中的大小寫字母相互轉(zhuǎn)換后寫入文件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.只寫打開一個(gè)文件
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] != '')
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、定義了一個(gè)日期類,具有設(shè)置日期、判斷閏年、輸出日期等功能。程序?qū)?018年8月23日進(jìn)行測試。
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()//要求為閏年時(shí)函數(shù)的返回值為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、某球類比賽根據(jù)第一階段的積分情況將8支隊(duì)伍分成兩組再進(jìn)行第二階段的比賽,分組原則是:積分第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組隊(duì)伍及積分:" << endl;
35 for (i = 0; i < N / 2; i++)
36 cout << t1[i].name << ' '<<t1[i].score <<endl;
37 cout << "B組隊(duì)伍及積分:" << 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、函數(shù)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、驗(yàn)證“滿足公式 p=n*n+n+41 (n是正整數(shù))的p一定是素?cái)?shù)”這個(gè)說法的不正確性。
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不是素?cái)?shù),故結(jié)論不正確"<<endl;
20 system("pause");
21 return 0;
22 }
總結(jié)
以上是生活随笔為你收集整理的编程题(C/C++程序设计,同济大学mooc)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 藿香正气水说明书(为什么家中要常备藿香正
- 下一篇: 玻璃浴室柜的选择技巧玻璃浴室柜选择注意事