zoukankan      html  css  js  c++  java
  • C语言(C&C++程序设计基础, 电子科技大学MOOC)

    一、编程作业
    1、在屏幕上输出“hello! welcome to computer world!”

    提示:

    1. 注意所有符号都是英文,最后没有回车。

    2. 将要求输出字符串之外的所有printf或者cout的输出全部删除

    3. 将return 0;之前的getchar();或者system("pause");等暂停程序运行的输出都删除。

    4. 提交之前先在自己的开发环境下运行成功再拷贝到作业区提交。

    5. 输出的语句最好拷贝到程序中,避免不小心的键盘按键错误输入造成扣分。

    1 #include <stdio.h>
    2 
    3 int main()
    4 {
    5     printf("hello! welcome to computer world!");
    6     return 0;
    7 }

    2、多行打印

    打印下面的3行数据:

    Please display these words:

    1. press return keyboard to enter the game.

    2. press esc keyboard to exist the game.

    1. 注意所有符号都是英文,最后没有回车。

    2. 将要求输出字符串之外的所有printf或者cout的输出全部删除

    3. 将return 0;之前的getchar();或者system("pause");等暂停程序运行的输出都删除。

    4. 提交之前先在自己的开发环境下运行成功再拷贝到作业区提交。

    5. 输出的语句最好拷贝到程序中,避免不小心的键盘按键错误输入造成扣分。

    6. 1.和2.后面有一个空格。

    1 #include <stdio.h>
    2 
    3 int main()
    4 {
    5     printf("Please display these words:
    
    6 1. press return keyboard to enter the game.
    
    7 2. press esc keyboard to exist the game.");
    8     return 0;
    9 }
    3、明星捐款

    某明星每年都会做慈善,小明统计了一下这个明星今年做了3次慈善,第一次捐助希望小学x万元,第二次捐助一个癌症患者y万元,第三次举办了慈善晚会,募捐z万元,其中有t万元是其他人捐助的。

    请问,这个明星今年一共捐助了多少钱?(万元)

    输入: 4个空格分开的正实数(单精度实数)

    输出:总共捐款数(只输出数值,保留小数点后的小数2位)

    如果输入不合法,则输出error

    例如:

    输入:3.2 5 7 5.5

    输出:10.50

    输入:5 -2 1 3

    输出:error

    输入:3 a 2 1

    输出:error

     1 #include <stdio.h>
     2 
     3 int main()
     4 {
     5     float x,y,z,t;
     6     int ret = scanf("%f%f%f%f",&x,&y,&z,&t);
     7     if(ret != 4 || x<=0 || y<=0 || z<=0 || t<=0)
     8         printf("error");
     9     else
    10         printf("%.2f", x+y+z-t);
    11     return 0;
    12 }

    4、发工资

    小明每个月基本工资x元,还有奖金y元,每迟到1次扣奖金的50元。这个月迟到z次,最多将所有奖金扣完。

    请问小明这个月领多少钱?

    输入:3个正整数

    输出:1个整数

    如果输入不合法,则输出"error"

    比如:

    输入:3000 200 2

    输出:  3100

    输入:5600 500 3

    输出:5950

    输入:1000 -2 5

    输出:error

    输入: 2000 200 6

    输出:2000

    输入:8000 200 -3

    输出: error

     1 #include <stdio.h>
     2 int main()
     3 {
     4     int x,y,z,t;
     5     int ret = scanf("%d%d%d",&x,&y,&z);
     6     
     7     if(ret != 3 || x<=0 || y<=0 || z<=0){
     8         printf("error");
     9     }
    10     else{    
    11         t = y-z*50;
    12         if( t<0 ) t = 0;
    13         printf("%d", x+t);
    14     } 
    15     return 0;
    16 }

    5、闰年判断

    题目内容:

    输入一个1900-2200之间的年份,

    判断这一年是不是闰年,是闰年输出yes,不是则输出no

    闰年判断条件:

    1、能整除4且不能整除100 

    2、能整除400

    如果输入不合法,输出error

    输入样例:

    1900

    输出样例:

    no

     1 #include <stdio.h>
     2 
     3 int main()
     4 {
     5     int y;
     6     int ret = scanf("%d",&y);
     7     if(ret != 1 || y<1900 && y>2200)
     8         printf("error");
     9     else if(y%4==0&&y%100!=0||y%400==0)
    10         printf("yes");
    11     else
    12         printf("no");
    13     return 0;
    14 }

    6、百钱百鸡

    题目内容:

    一只公鸡值5钱,

    一只母鸡值3钱,

    三只小鸡值1钱,

    现在用百钱买百鸡,

    请问公鸡、母鸡、小鸡各多少只?

    列举所有可能,从公鸡数目小到大排列,公鸡相同则按照母鸡递增顺序,公鸡母鸡都相同,则按照小鸡递增顺序

    输出结果:

    a,b,c

    d,e,f

    .....

    (a,d...对应公鸡数量,b,e...对应母鸡数量,c,f...对应小鸡数量)

     1 #include <stdio.h> 
     2 int main()
     3 {
     4     int i,j,k;
     5     for(i=0; i<=100/5; ++i){
     6         for(j=0; j<=100/3; ++j){
     7             k = 100 - i - j; //百鸡 
     8             if(k%3==0 && 100 == k/3 + i*5 + j*3) //百钱 
     9                 printf("%d,%d,%d
    ",i,j,k);
    10         }
    11     }
    12     return 0;
    13 }

    7、猴子摘桃

    题目内容:

    一个猴子摘了些桃子,

    第一天吃掉其中的一半然后多吃了1个,

    第二天照此方法又吃掉了剩下桃子的一半加1个,

    以后每天如此,直到第十天晚上,猴子发现只剩下了1个桃子,

    请问猴子第一天总共摘了多少个桃子?

    并反向打印每天所剩桃子数。

    即a,b,c,d.....,sum

    分别表示第九天剩余桃子,第八天剩余桃子,....,第一天剩余桃子,总桃子数。

    比如,如果总桃子10个,第一天剩余10/2-1=4个,第二天剩余4/2-1=1个,根据题目要求应该输出第一天剩余桃子,总桃子分别为:

    4,10

     1 #include <stdio.h> 
     2 int main()
     3 {
     4     int day = 10, remain = 1;
     5     for(day =10; day>0; --day)
     6     {
     7         remain = 2 * (remain + 1);
     8         if(day!=10)
     9             printf(",");
    10         printf("%d", remain);
    11     }
    12     return 0;
    13 }

    8、回文判断

    题目内容:

    回文测试:输入一30个字符以内的字符串,判断是否为回文;如果是,则打印"true";否则打印"false"。像"aba"这样的从左往右读与从右往左读一致就是回文。

    输入样例1:

    ayzya

    输出样例1:

    true

    输入样例2:

    ayzy

    输出样例2:

    false

     1 #include <stdio.h> 
     2 #include <string.h>
     3 int main()
     4 {
     5     char str[31] = "";
     6     scanf("%s",str);
     7     char *p1=str, *p2 = str+strlen(str)-1;
     8     while(p1<p2 && *p1==*p2){
     9         p1++,p2--;        
    10     }
    11     if(p1>=p2)
    12         printf("true");
    13     else
    14         printf("false");
    15     return 0;
    16 }

    9、设计数字时钟

    题目内容:

    按照下面要求定义一个时钟结构体类型:

    struct clock

    {

        int hour;

        int minute;

        int second;

    };

    typedef struct clock CLOCK;

    然后,编程实现将时钟模拟显示在屏幕上。注意:时钟是24小时的。需要判断输入的数据是否合法。

    输入样例1:

    10,20,3

    输出样例1:

    10:20:03

    输入样例1:

    25,100,200

    输出样例2:

    error

     1 #include <stdio.h> 
     2 #include <string.h>
     3 
     4 struct clock
     5 {
     6     int hour;
     7     int minute;
     8     int second;
     9 };
    10 typedef struct clock CLOCK;
    11 
    12 int main()
    13 {
    14     CLOCK c;
    15     int ret = scanf("%d,%d,%d",&c.hour,&c.minute,&c.second);
    16     if(ret!=3 || c.hour>=24 || c.minute>=60 || c.second>=60){
    17         printf("error");
    18     }
    19     else{
    20         printf("%02d:%02d:%02d",c.hour,c.minute,c.second);
    21     }
    22     return 0;
    23 }

    10、排序

    题目内容:

    接受若干非负整数(数据不重复),当个数超过10个或者遇到负数时停止接受,将这几个整数按升序排列输出,并且奇数在前,偶数在后。

    输出要求,每个数字后输出空格与其他数字隔开,最后一个数字后也有空格

    输入样例1:

    10 9 8 7 6 5 4 3 2 1

    输出样例1:

    1 3 5 7 9 2 4 6 8 10回车

    输入样例2:

    2 3 4 5 -1

    输出样例2:

    3 5 2 4回车

     1 #include <stdio.h> 
     2 #include <string.h>
     3 
     4 void InsertSort(int *arr, int n)
     5 {
     6     int i;
     7     for(i=1; i<n; ++i){/*第0个元素有序,从第1个元素向右无序*/
     8         int j=i-1,key=arr[i];/*保存第i个元素,左边的元素i-1*/
     9         while(j>=0 && key<arr[j]){/*保存的元素key与之前的元素从右向左逐个比较*/
    10             arr[j+1]=arr[j];/*移动(向后赋值)*/
    11             j--;
    12         }
    13         arr[j+1]=key;/*j--退出,恢复正确值j+1*/
    14     }
    15 }
    16 
    17 int main()
    18 {
    19     int i=0,x,flag=0, arr[10]={0};
    20 
    21     scanf("%d",&x);
    22     while(i<10 && x>0){                
    23         arr[i++] = x;
    24         scanf("%d",&x);        
    25     }
    26     
    27     InsertSort(arr,i);
    28     for(int j=0; j<i; ++j){
    29         if(arr[j]%2){
    30             if(flag) printf(" ");
    31             flag = 1;
    32             printf("%d",arr[j]);
    33         }
    34     }
    35         
    36     for(int j=0; j<i; ++j){
    37         if(arr[j]%2==0)
    38             printf(" %d",arr[j]);
    39     }
    40        
    41     return 0;
    42 }

    11、最大整数

    题目内容:

    输入3个整数a,b,c,用指针p=&a,q=&b,请用max指向最大整数并输出。

    输出按照如下格式输出: printf("max=%d ",*pmax);

    输入样例:

    1,2,3 

    输出样例:

    max=3

     1 #include <stdio.h>
     2 int main()
     3 {
     4     int a,b,c,max;
     5     int *pmax = &max;
     6     scanf("%d,%d,%d",&a,&b,&c);
     7     
     8     if(a>b)
     9         max = a;
    10     else
    11         max = b;
    12     if(c>max)
    13         max = c;
    14     printf("max=%d
    ",*pmax);
    15     return 0;
    16 }

    12、删除字符串中连续的重复字符

    题目内容:

    功能:实现删除字符串中连续的重复字符(除字母和数字)。 输入为字符串,将字符串中连续重复的,非字母和数字的字符删去,然后输出处理后的字符串。要求用指针指向输入的字符串进行操作。

    输入格式:

    输入字符串最长50个字符,之后截断,只输出处理后的字符串。 

    输出格式:

    输入样例:

    1+++2==3

    输出样例: 

    1+2=3

     1 #include <stdio.h>
     2 #define N 100
     3 int main()
     4 {
     5     char str[N] = "";
     6     char *p = str;
     7     gets(str);
     8     
     9     while(*p){    
    10         printf("%c",*p++);
    11         while(*p&&!(*p>='0'&&*p<='9'||*p>='a'&&*p<='z'||*p>='A'&&*p<='Z')&&*p==*(p-1))
    12                 p++;    
    13     }
    14     printf("
    ");
    15     return 0;
    16 }

    13、统计输出字符串中的字母个数和数字个数。

    题目内容:

    编写程序,输入一个字符串,分别统计输出该字符串中的字母个数和数字个数。要求用指针指向这个字符串进行处理。

    输入格式:

    字符串

    输出格式:

    英文逗号分隔的2个整数,第一个整数是字母个数,第二个整数的数字个数。

    输入样例:

    the day the month the year 123 

    输出样例:

    21,3

     1 #include <stdio.h>
     2 #define N 100
     3 int main()
     4 {
     5     char str[N] = "";
     6     char *p = str;
     7     int letters = 0, digits = 0;
     8     gets(str);
     9         
    10     while(*p){
    11         if(*p>='0'&&*p<='9')
    12             digits++;
    13         if(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z')
    14             letters++;
    15         p++;
    16     }
    17     printf("%d,%d
    ",letters,digits);
    18 
    19     return 0;
    20 }

    14、比较字符串是否相等(25分)

    题目内容:

    编写程序,输入两个字符串,通过2个指针p和q分别指向这2个字符串,比较字符串是否相等。 要求不使用strcmp函数。

    输入格式:

    string1回车string2回车 

    string1和string2最长为256,可能包含空格 

    输出格式:

    相等输出: equal

    不等输出: unequal

    输入样例:

    string1

    string2

    输出样例:

    unequal

     1 #include <stdio.h>
     2 #define N 300
     3 int main()
     4 {
     5     char str1[N] = "";
     6     char str2[N] = "";
     7     char *p = str1, *q = str2;
     8     
     9     gets(str1);
    10     gets(str2);
    11         
    12     while(*p&&*q&&*p==*q){
    13         p++; q++;
    14     }
    15     
    16     if(!(*p)&&!(*q))
    17         printf("equal
    ");
    18     else
    19         printf("unequal
    ");
    20 
    21     return 0;
    22 }

    15、水仙花数

    设有一个3位数,它的百位数、十位数、个位数的立方和正好等于这个3位数,如153=1+125+27。

    编写函数,返回小于等于传入参数n且满足该条件的三位数(称为水仙花数)的个数。

    (指定函数原型:int find(int n))

     返回值要求:如果传入参数n不是三位数或者在该范围内没有找到,则find返回0,

                        否则返回找到的水仙花数的个数。

     注意:不要在find函数中打印(如调用printf或puts等函数)任何数据,否则视为错误。

    提交的程序需要包含需要的头文件及如下的main函数:

    #include<stdio.h>

    #include<stdlib.h>

    int find(int n);

    int main()

    {

       int n,r;

      r=scanf("%d",&n);

      if(r!=-1){printf("error",return -1;}

      r=find(n);

      printf("%d",r);

      return 0;

    }

    输入1:

      400

    输出1:

      3

    输入2:

      59

    输出2:

     0

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 int find(int n);
     5 int main()
     6 {
     7     int n,r;
     8     r=scanf("%d",&n);
     9     if(r!=1){printf("error"); return 0;}
    10     r=find(n);
    11     printf("%d",r);
    12     return 0;
    13 }
    14 int find(int n){
    15     int cnt = 0;
    16     if(n>=100&&n<=999)
    17     for(int i = 123; i<=n; ++i ){
    18         int a = i%10, b = i/10%10, c = i/100;
    19         if(a*a*a + b*b*b + c*c*c == i) cnt++;
    20     }
    21     return cnt;
    22 }

    16、最小公倍数

    编写程序,从键盘输入5个正整数,然后求出它们的最小公倍数,并显示输出。

    (通过调用对两个正整数求最小公倍数的函数实现)(参考函数原型:int LCM(int x, int y))

    输入输出格式要求:

     编写函数int LCM(int x, int y);返回值为x和y的最小公倍数。

     要求在main函数接收5个正整数,然后通过调用LCM函数最终得到这5个数的最小公倍数,最后输出最小公倍数。

    如果输入数据错误,输出"error"。

    例如:

    输入:2 3 6 9 10

    输出:90

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 int LCM(int x, int y);
     5 int GCD(int x, int y);
     6 int main()
     7 {
     8     /* */
     9     int a,b,c,d,e,ret;
    10     ret = scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
    11     if(ret!=5 || a<=0 || b<=0 || c<=0 || d<=0 || e<=0)
    12         printf("error"); 
    13         else 
    14         printf("%d",LCM(LCM(LCM(LCM(a,b),c),d),e));
    15     return 0;
    16 }
    17 
    18 int LCM(int x, int y){
    19     return x/GCD(x,y)*y; 
    20 }
    21 int GCD(int x, int y){
    22     return y == 0 ? x : GCD(y, x%y); 
    23 }

    17、字符串的拷贝

    编程实现函数:void my_strcpy(char * destination,char * source);

    函数功能:将source指向的字符串拷贝到destination指向的位置。

    注意:使用空格字符来表示字符串的结束。例如source指向位置,依次保存了字符'a',字符'b',字符空格' ',字符'c',则source指向的字符串为"ab"。destionation原来存储的字符串是"xyz tdk",则拷贝后,destionation存储的应该是“ab  tdk”。遇到异常情况,输出"error";否则不要随意输出,会视为错误.

    您的main函数需要读入2个长度不超过80字节的字符串(按行及下面顺序读入source和destionation字符串),然后调用my_strcpy函数,最后用puts函数输出destionation里面存储的字符串。

    例如:

    输入1:

    xyz abc

    a kp

    输出1:

    xyz

    输入2:

    xyz abc

    a kppp

    输出2:

    xyz pp

     1 #include<stdio.h>
     2  
     3 void my_strcpy(char * destination,char * source);
     4 int main()
     5 {
     6     char source[81] = "";
     7     char destination[81]="";
     8     gets(source);
     9     gets(destination);
    10      
    11     my_strcpy(destination,source);  
    12     puts(destination);
    13      
    14     return 0;
    15 }
    16 void my_strcpy(char * destination,char * source){
    17     if(destination == NULL||source ==NULL||source[0] ==' '){
    18         printf("error");
    19         return;
    20     }
    21     char *p1 = destination, *p2 = source;
    22     while((*p1++ = *p2++) != ' ');
    23 }

    18、完成point类

    定义一个点类Point,并定义成员函数double Distance(const & Point) 求两点的距离。

    输入两个点的坐标,创建两个点, 然后调用Point类的Distance方法输出两个点的距离。

    在你的代码中除了实现Point类以外,还需一如下main函数:

    int main(){

        double a,b,c,d;

       cin>>a>>b>>c>>d;

       Point A(a,b),B(c,d);

       cout<<A.Distance(B)<<endl;

       return 0;

    }

    如输入:

    1 2 3 4回车

    输出:

    2.83

     1 #include <iostream>
     2 #include <iomanip>
     3 #include <cmath>
     4 using namespace std;
     5 class Point{
     6     double a,b;
     7 public:
     8     Point(double a, double b){
     9         this->a = a;
    10         this->b = b;
    11     }
    12     double Distance(const Point &p){
    13         return sqrt((a-p.a)*(a-p.a)+(b-p.b)*(b-p.b));
    14     }
    15 };
    16 //
    17 int main()
    18 {
    19     double a,b,c,d;
    20     cin>>a>>b>>c>>d;
    21     
    22     Point A(a,b),B(c,d);
    23     cout<<setprecision(3)<<A.Distance(B)<<endl;
    24     return 0;
    25 }

    19、实现Usr类

    实现User类的构造函数和AddUser方法添加新用户,

    实现int login(char *name,char * pass)方法,该方法接受用户名和密码,

    判断用户名对应的密码是否正确,如果正确返回用户的编号,如果不正确返回-1。

    User类的使用示意如下所示,在你的代码中除了实现User类以外,还需一如下main函数

    int main(){

     char name[10],name1[10],pass[10],pass1[10];

     cin>>name>>pass>>name1>>pass1;

     User user("LiWei","liwei101");

     user.AddUser(name,pass);

     if (user.login(name1,pass1) >=0)

     {

      cout<<"Success Login!"<<endl;

     }

     else{

      cout<<"Login failed!"<<endl;

     }

       return 0;

    }

    例如输入:

    test 1234567 test 123456回车

    输出:

    Login failed!

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 #define N 100
     5 class User{
     6     string name[N];
     7     string pass[N];
     8     static int index;
     9 public:
    10     User(string name,string pass){        
    11         this->name[index] = name;
    12         this->pass[index] = pass;
    13         index++;
    14     }
    15     ~User(){index--;}
    16     void AddUser(string name,string pass){
    17         this->name[index] = name;
    18         this->pass[index] = pass;
    19         index++;
    20     }
    21     int login(string name,string pass){
    22         for(int i=0;  i<=index; ++i)
    23             if(this->name[i] == name && this->pass[i] == pass)
    24                 return 1;
    25         return -1;
    26     }
    27 };
    28 int User::index = 0;
    29 /* class User{
    30     string name;
    31     string pass;
    32 public:
    33     User(string name,string pass){
    34         this->name = name;
    35         this->pass = pass;
    36     }
    37     void AddUser(string name,string pass){
    38         this->name = name;
    39         this->pass = pass;
    40     }
    41     int login(string name,string pass){
    42         if(this->name == name && this->pass == pass)
    43             return 1;
    44         return -1;
    45     }
    46 }; */
    47 
    48 int main()
    49 {
    50     char name[10],name1[10],pass[10],pass1[10];
    51     cin>>name>>pass>>name1>>pass1;
    52 
    53     User user("LiWei","liwei101");
    54 
    55     user.AddUser(name,pass);
    56 
    57     if (user.login(name1,pass1) >=0)
    58     {
    59         cout<<"Success Login!"<<endl;
    60     }
    61     else{
    62         cout<<"Login failed!"<<endl;
    63     }
    64     return 0;
    65 }

    20、实现Sutdent类

    设计一个学生类Student,包含学生学号(最长10位)、姓名(不用支持中文最长12位)、三门课程成绩(成绩是单精度实数类型)等基本信息,

    计算每门课程学生的平均成绩。

    需实现Student的display成员函数,依次输出学号 姓名 和三门课的成绩,每个输出以空格隔开

    成员函数 average1 ,average2 ,average3 ,分别返回三门课的平均成绩。

    Student类的使用方法如下所示,在你的代码中除了实现Student类,还需引入以下代码:

    int main(){

     Student *stu1,*stu2,*stu3;

     char name1[10],name2[10],name3[10];

     char num1[12],num2[12],num3[12];

     int grade1[3],grade2[3],grade3[3];

     cin>>name1>>num1>>grade1[0]>>grade1[1]>>grade1[2];

     cin>>name2>>num2>>grade2[0]>>grade2[1]>>grade2[2];

     cin>>name3>>num3>>grade3[0]>>grade3[1]>>grade3[2];

     stu1 = new Student(name1,num1,grade1[0],grade1[1],grade1[2]);

     stu2 = new Student(name2,num2,grade2[0],grade2[1],grade2[2]);

     stu3 = new Student(name3,num3,grade3[0],grade3[1],grade3[2]);

     stu1->display();

     stu2->display();

     stu3->display();

      cout<<"The average grade of course1:"<<setprecision(4)<<stu2->average1()<<endl;

      cout<<"The average grade of course2:"<<setprecision(4)<<stu2->average2()<<endl;

      cout<<"The average grade of course3:"<<setprecision(4)<<stu2->average3()<<endl;

       return 0;

    }

    上述代码执行时

    输入:

    200906294 LiWeiwei 88 75 91 200902164 ChenHanfu 86 78 93 200908079 ZhanGaolin 94 69 97

    输出:

    200906294 LiWeiwei 88 75 91回车

    200902164 ChenHanfu 86 78 93回车

    200908079 ZhanGaolin 94 69 97回车

    The average grade of course1:89.33回车

    The average grade of course2:67.33回车

    The average grade of course3:93.67回车

     1 #include <iostream>
     2 #include <iomanip>
     3 #include <cmath>
     4 using namespace std;
     5 class Student{
     6     string name, num;
     7     float grade1,grade2,grade3;
     8     static float ave1,ave2,ave3;
     9     static int count;
    10 public:
    11     Student(string name,string num, float grade1,float grade2,float grade3){
    12         this->name = name;
    13         this->num = num;
    14         this->grade1 = grade1;
    15         this->grade2 = grade2;
    16         this->grade3 = grade3;
    17         count++;
    18         ave1 += grade1;
    19         ave2 += grade2;
    20         ave3 += grade3;
    21     }
    22     void display(){
    23         cout<<name<<" "<<num<<" "<<grade1<<" "<<grade2<<" "<<grade3<<endl;        
    24     }
    25     float average1(){
    26         return ave1 /= count;
    27     }    
    28     float average2(){
    29         return ave2 /= count;
    30     }
    31     float average3(){
    32         return ave3 /= count;
    33     }
    34 };
    35 float Student::ave1 = 0.0;
    36 float Student::ave2 = 0.0;
    37 float Student::ave3 = 0.0;
    38 int Student::count = 0;
    39 
    40 int main()
    41 {
    42     Student *stu1,*stu2,*stu3;
    43     char name1[10],name2[10],name3[10];
    44     char num1[12],num2[12],num3[12];
    45     int grade1[3],grade2[3],grade3[3];
    46 
    47     cin>>name1>>num1>>grade1[0]>>grade1[1]>>grade1[2];
    48     cin>>name2>>num2>>grade2[0]>>grade2[1]>>grade2[2];
    49     cin>>name3>>num3>>grade3[0]>>grade3[1]>>grade3[2];
    50     
    51     stu1 = new Student(name1,num1,grade1[0],grade1[1],grade1[2]);
    52     stu2 = new Student(name2,num2,grade2[0],grade2[1],grade2[2]);
    53     stu3 = new Student(name3,num3,grade3[0],grade3[1],grade3[2]);
    54 
    55     stu1->display();
    56     stu2->display();
    57     stu3->display();
    58 
    59      cout<<"The average grade of course1:"<<setprecision(4)<<stu2->average1()<<endl;
    60     /**/cout<<"The average grade of course2:"<<setprecision(4)<<stu2->average2()<<endl;
    61     cout<<"The average grade of course3:"<<setprecision(4)<<stu2->average3()<<endl; 
    62     return 0;
    63 }

    21、形状类

    已知基类ShapeFactory的声明如下:

    const float pi=3.1416;

    class ShapeFactory

    {

    public:

     ShapeFactory(){};

     virtual ~ShapeFactory(){};

     virtual float Circumstance(){return 0;};

    }

      ShapeFactory *Create(float a,float b,float c);

      ShapeFactory *Create(float a,float b,float c,float d);

      ShapeFactory *Create(float r);

    请写出三角形(Triangle)、四边形(Quadrangle)、圆形(Circle)三个派生类,构造函数分别传入三边/四边/半径的长度(不用检查是否符合三角形、矩形、圆的条件,没有异常输出),重写出求周长的函数(Circumstance函数)。

    然后实现基类的Create函数,这里重载的三个Create函数,分别生成三角形、四边形、圆形的对象。

    比如三角形类为Triangle:

    ShapeFactory * ShapeFactory::Create(float a,float b,float c)

    {

     ShapeFactory *p=new Triangle(a,b,c);

     return p;

    }

    如果三角形输入的三边长度是 3 4 5,四边形输入的四条边的长度是2 3 4 7,圆的半径是3,则要求程序运行能够得到如下的提示和输出:

     1 #include <iostream>
     2 using namespace std;
     3 
     4 const float pi=3.1416;
     5 class ShapeFactory
     6 {
     7 public:
     8     ShapeFactory(){};
     9     virtual ~ShapeFactory(){};
    10     virtual float Circumstance(){return 0;};
    11 };
    12 
    13 class Triangle:public ShapeFactory
    14 {
    15     float a,b,c;
    16 public:
    17     Triangle(float a,float b,float c){
    18         this->a = a;
    19         this->b = b;
    20         this->c = c;
    21     }    
    22     float Circumstance(){
    23         return a+b+c;
    24     }
    25 };
    26 
    27 class Quadrangle:public ShapeFactory
    28 {
    29     float a,b,c,d;
    30 public:
    31     Quadrangle(float a,float b,float c,float d){
    32         this->a = a;
    33         this->b = b;
    34         this->c = c;
    35         this->d = d;
    36     }
    37     float Circumstance(){
    38         return a+b+c+d;
    39     }
    40 };
    41 
    42 class Circle:public ShapeFactory
    43 {
    44     float r;
    45 public:
    46     Circle(float r){
    47         this->r = r;
    48     }
    49     float Circumstance(){
    50         return 2*pi*r;
    51     }
    52     
    53 };
    54 int main()
    55 {
    56     float a,b,c,d,r;
    57     cout<<"输入三角形的3边长度:";
    58     cin>>a>>b>>c;
    59     Triangle t(a,b,c);
    60     cout<<"三角形的周长为:"<<t.Circumstance()<<endl;
    61     
    62     cout<<"输入四边形的4边长度:";
    63     cin>>a>>b>>c>>d;
    64     Quadrangle q(a,b,c,d);
    65     cout<<"四边形的周长为:"<<q.Circumstance()<<endl;
    66     
    67     cout<<"输入圆的半径:";
    68     cin>>r;
    69     Circle ci(r);
    70     cout<<"圆的周长为:"<<ci.Circumstance()<<endl;
    71         
    72     return 0;
    73 }

    22、实现带日期的时钟类

    实现带日期的时钟类,具体要求如下:

    已知时钟类的定义如下:

    #include<iostream>

    using namespace std;

    bool leap(int year)

    {

           if(year%400==0||(year%4==0 && year%100!=0))

                  return true;

           return false;

    }

    class Clock{

    public:

     Clock(int h,int m,int s)

     {

      hour =(h>23? 0:h);

      minute = (m>59?0:m);

      second = (s>59?0:s);

     }

     virtual void run()

     {

      second = second+1;

      if (second>59)

      {

       second =0;

       minute+=1;

      }

      if (minute>59)

      {

       minute =0;

       hour+=1;

      }

      if (hour>23)

      {

       hour =0;

      }

     }

     virtual void showTime()

     {

      cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;

     }

     int getHour(){return hour;}

     int getMinute(){return minute;}

     int getSecond(){return second;}

    protected:

     int hour;

     int minute;

     int second;

    };

    日期类定义如下:

    class Date{

    public:

     Date(int y=1996,int m=1,int d=1)

     {

      if (m>12||m<1)

      {

       m=1;

      }

      if(d>days(y,m))

      {

       day = 1;

      }

      day =d;

      year =y;

      month =m;

     };

     int days(int year,int month);

     void NewDay();

     void showTime()

     {

      cout<<year<<"-"<<month<<"-"<<day<<endl;

     }

    protected:

     int year;

     int month;

     int day;

    };

    int main()

    {

           int h,m,s,day,month,year;

           cin>>h>>m>>s>>day>>month>>year;

           ClockWithDate cd1(h,m,s,day,month,year);

           cd1.showTime();

           cout<<"现在运行x秒:";

           int x;

           cin>>x;

           for(int i=0;i<x;++i)

                  cd1.run();

           cd1.showTime();

           return 0;

    }

    需要类外实现Date类的days方法,根据年和月,返回该年该月对应的天数

    实现Date类的NewDay方法,该方法将Date代表的日期增加一天。

    实现ClockWithDate类,它继承至Clock类和Date类,记录时间和日期

    重新实现ClockWithDate类的showTime方法和run方法。

    showTime方法输出当的时间和日期,先输出时间再输出日期。

    run方法每次将现在的时间增加一秒,并且当时间超过23:59:59时,更新日期。

    比如某次程序运行输入当前时间是:1 1 1 7 10 2000(2000年10月7号1点1分1秒),然后输入运行时间x: 5,则程序运行的输入输出如下:

    输入:

    1 1 1 7 10 2000

    5

    输出:

    Now:1:1:1

    2000-10-7

    现在运行x秒:Now:1:1:6

    2000-10-7

      1 #include<iostream>
      2 using namespace std;
      3 
      4 bool leap(int year)
      5 {
      6    if(year%400==0||(year%4==0 && year%100!=0))
      7       return true;
      8    return false;
      9 }
     10 
     11 class Clock
     12 {
     13 public:
     14     Clock(int h,int m,int s)
     15     {
     16         hour =(h>23? 0:h);
     17         minute = (m>59?0:m);
     18         second = (s>59?0:s);
     19     }
     20     virtual void run()
     21     {
     22         second = second+1;
     23         if (second>59)
     24         {
     25             second =0;
     26             minute+=1;
     27         }
     28         if (minute>59)
     29         {
     30             minute =0;
     31             hour+=1;
     32         }
     33         if (hour>23)
     34         {
     35             hour =0;
     36         }
     37     }
     38 
     39     virtual void showTime()
     40     {
     41         cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;
     42     }
     43     int getHour(){return hour;}
     44     int getMinute(){return minute;}
     45     int getSecond(){return second;}
     46 
     47 private:
     48     int hour;
     49     int minute;
     50     int second;
     51 };
     52 
     53 class Date
     54 {
     55 public:
     56     Date(int y=1996,int m=1,int d=1)
     57     {
     58         if (m>12||m<1)
     59         {
     60             m=1;
     61         }
     62         if(d>days(y,m))
     63         {
     64             day = 1;
     65         }
     66         day =d;
     67         year =y;
     68         month =m;
     69     };
     70 
     71     int days(int year,int month){        
     72         int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
     73         int d = days[month];
     74         if(leap(year)&&month==2)
     75             d++;        
     76         return d;
     77     }
     78     void NewDay(){
     79         day++;
     80         if(day>days(year,month))
     81         {
     82             day = 1;
     83             month++;
     84         }
     85         if(month>12){
     86             month = 1;
     87             year++;
     88         } /* */
     89     };
     90     void showTime()
     91     {
     92         cout<<year<<"-"<<month<<"-"<<day<<endl;
     93     }
     94 private:
     95     int year;
     96     int month;
     97     int day;
     98 };
     99 
    100 class ClockWithDate:public Clock,Date
    101 {
    102 public:    
    103     ClockWithDate(int h,int m,int s,int day,int month,int year):Clock(h,m,s),Date(year,month,day){}
    104     void showTime()
    105     {
    106         Clock::showTime();
    107         Date::showTime();
    108     }
    109     virtual void run()
    110     {            
    111         if(getSecond()==59&&getMinute()==59&&getHour()==23){
    112             NewDay();
    113         }
    114         Clock::run();        
    115     }
    116 };
    117 
    118 int main()
    119 {
    120     int h,m,s,day,month,year;
    121     cin>>h>>m>>s>>day>>month>>year;
    122     ClockWithDate cd1(h,m,s,day,month,year);
    123     cd1.showTime();
    124     cout<<"现在运行x秒:";
    125 
    126     int x;
    127     cin>>x;
    128     for(int i=0;i<x;++i)
    129           cd1.run();
    130     cd1.showTime();
    131     return 0;
    132 }
    二、考试题
    1、编写函数输出小于等于n的水仙花数
    设有一个3位数,它的百位数、十位数、个位数的立方和正好等于这个3位数,如153=1+125+27。
    编写函数,找出所有满足该条件的数(称为水仙花数)。
    (参考函数原型:int find(int n))
    输入输出格式要求:
    	编写函数int find(int n);
    	在find里输出所有小于等于n的水仙花数,以逗号分隔
    	返回值要求:如果没有,则find返回0,否则返回找到的水仙花数的个数
    例如:
    n为:400
    输出:153,370,371
    find函数返回3
     1 # include <stdio.h>
     2 int find(int n)
     3 {
     4     int flag=0;
     5     for(int i=100; i<=n; ++i)
     6     {
     7         int a=i%10;
     8         int b = i/10%10;
     9         int c = i/100;
    10         if(a*a*a+b*b*b+c*c*c==i)
    11         {
    12             if(flag)
    13                 printf(",");
    14             printf("%d",i);
    15             flag++;
    16         }            
    17     }
    18     printf("
    ");
    19     return flag;
    20 }
    21 int main()
    22 {
    23     int n;
    24     scanf("%d",&n);
    25     printf("%d
    ",find(n));
    26     return 0;    
    27 }

    2、逆序memcpy

    实现逆序的Memcpy方法。
    void * reversememcpy ( void * destination, const void * source, int num );
    从source所指的内存地址的起始位置开始拷贝num个字节,逆序保存到目标destination所指的内存地址的起始位置中。
    返回destination。
    
    注意为逆序拷贝。source指向位置,依次保存了10,20,30,当num=3时,则逆序拷贝后destination指向的位置应该依次保存30,20,10.

    输入:1,2,3

    输出:3,2,1

    (逆序拷贝,实参指针类型与函数指针类型必须一致,否则只能正序拷贝)

     1 # include <stdio.h>
     2 #define N 100
     3 void * reversememcpy ( void * destination, const void * source, int num );
     4 int main()
     5 {
     6     int arr[N] = {0},num = 3; 
     7     scanf("%d,%d,%d",&arr[0],&arr[1],&arr[2]);
     8     
     9     int *p = (int *)reversememcpy(&arr[num],&arr[0],num);
    10     for(int i=0; i<num;++i)
    11     {
    12         if(i)
    13             printf(",");
    14         printf("%d",p[i]);
    15     }        
    16     return 0;    
    17 }
    18 void *reversememcpy(void *destination,const void *source,int num)
    19 {
    20     int *des=(int*)destination,*src=(int*)source;
    21     des += num-1;
    22     while(num--)
    23     {
    24         *des-- = *src++;  
    25     }
    26     return destination;
    27 }

    memcpy源码(源头目标内存重叠或目标内存不重叠(前后之分),从源头开始复制;源尾目标内存重叠,从源尾开始复制)

     1 void *memcpy(void *dst, const void *src, size_t len)
     2 {
     3     if(NULL == dst || NULL == src){
     4         return NULL;
     5     }
     6     
     7     void *ret = dst;
     8     
     9     if(dst <= src || (char *)dst >= (char *)src + len){
    10         //目标低于等于源或目标高于等于源+len,从源头地址开始复制
    11         while(len--){
    12             *(char *)dst = *(char *)src;
    13             dst = (char *)dst + 1;
    14             src = (char *)src + 1;
    15         }
    16     }else{
    17         //目标高于源并且小于源+len,从源尾地址开始复制
    18         src = (char *)src + len - 1;
    19         dst = (char *)dst + len - 1;
    20         while(len--){
    21             *(char *)dst = *(char *)src;
    22             dst = (char *)dst - 1;
    23             src = (char *)src - 1;
    24         }
    25     }
    26     return ret;
    27 }

    3、扩展String类

    首先输入一个字符串,然后依次将功能显示出来

    输入:testtesttest,3,test,t,a,test,str,t,es

    输出:4,aesaaesaaesa,strstrstr,eseses,tttttt

      1 #include <iostream>
      2 #include <cstring>
      3 #define N 256
      4 using namespace std;
      5 class String
      6 {
      7 protected:
      8     char *mystr;
      9     int len;
     10 public:
     11     String(const char *p)
     12     {
     13         len = strlen(p);
     14         mystr = new char[len+1];
     15         strcpy(mystr,p);
     16     }
     17     ~String()
     18     {
     19         if (mystr !=NULL)
     20         {
     21             delete []mystr;
     22             mystr = NULL;
     23             len = 0;
     24         }
     25     }
     26     void showStr(){cout <<mystr;}
     27     char * GetStr(){return mystr;}
     28     virtual bool IsSubString(const char *str)
     29     {
     30         int i,j;
     31         for (i =0;i<len;i++)
     32         {
     33             int k = i;
     34             for (j =0;str[j] !='';j++,k++)
     35             {
     36                 if (str[j]!= mystr[k]) break;
     37             }
     38             if(str[j] =='') return true;
     39         }
     40         return false;
     41     }
     42 };
     43 
     44 class EditString:public String{
     45 public:
     46     EditString(const char *p):String(p){}
     47     int IsSubString(int start, const char *str);
     48     void EditChar(char s, char d); 
     49     void EditSub(const char * subs,const char *subd); 
     50     void DeleteChar(char ch);  
     51     void DeleteSub(const char * sub);  /* */
     52 
     53 };
     54 int EditString::IsSubString(int start, const char *str)
     55 {
     56     int i=start,j;
     57     for (i =start;i<len;i++)
     58     {
     59         int k = i;
     60         for (j =0;str[j] !='';j++,k++)
     61         {
     62             if (str[j]!= mystr[k]) break;
     63         }
     64         if(str[j] =='') return i;
     65     }
     66     return -1;
     67 }
     68 void EditString::EditChar(char s, char d)
     69 {
     70     char *p = mystr;
     71     while(*p)
     72     {
     73         if(*p==s)
     74             *p = d;
     75         p++;
     76     }
     77 }
     78 void EditString::EditSub(const char * subs,const char *subd)
     79 {
     80     int start=0;
     81     char *temp = new char[len+1];
     82     while(start<len)
     83     {
     84         start=IsSubString(start,subs);
     85         if(start!=-1){
     86             strncpy(temp,mystr,start);
     87             temp[start] = '';
     88             strcat(strcat(temp,subd),&mystr[start+strlen(subs)]);
     89             strcpy(mystr,temp);
     90         }
     91         else{
     92             break;
     93         }
     94         start++;    
     95     }
     96     delete []temp;
     97     len = strlen(mystr);
     98 }
     99 void EditString::DeleteChar(char ch)
    100 {
    101     char *p = mystr,*q=mystr;
    102     while(*q)
    103     {
    104         if(*q!=ch){
    105             *p = *q;
    106             p++;
    107         }
    108         q++;
    109     }
    110     *p = '';
    111     len = strlen(mystr);
    112 }
    113 void EditString::DeleteSub(const char * sub){
    114     int start=0;
    115     char *temp = new char[len+1];
    116     while(start<len)
    117     {
    118         start=IsSubString(start,sub);
    119         if(start!=-1){
    120             strncpy(temp,mystr,start);
    121             temp[start] = '';
    122             strcat(temp,&mystr[start+strlen(sub)]);
    123             strcpy(mystr,temp);
    124         }
    125         else{
    126             break;
    127         }
    128         start++;    
    129     }
    130     delete []temp;
    131     len = strlen(mystr);
    132 }
    133 int main()
    134 {
    135     //testtesttest,3,test,t,a,test,str,t,es
    136     char s[N],s1[N],s2[N],s3[N],s4[N],s5[N];
    137     char c1,c2,c3;
    138     int count;
    139     cin>>s;
    140     /* 1.实现int IsSubString(int start, const char *str); */
    141     //---------------------s1,count,s2
    142     char* p = strtok (s," ,.-");
    143     strcpy(s1,p);
    144     p = strtok (NULL," ,.-");
    145     count = atoi(p);    
    146     p = strtok (NULL," ,.-");
    147     strcpy(s2,p);
    148     EditString es1(s1);
    149     cout<<es1.IsSubString(count,s2)<<",";//4
    150     /* 2.实现EditChar(char s, char d)*/
    151     //---------------------s1,c1,c2
    152     p = strtok (NULL," ,.-");
    153     c1 = *p;
    154     p = strtok (NULL," ,.-");
    155     c2 = *p;
    156     EditString es2(s1);
    157     es2.EditChar(c1,c2);
    158     es2.showStr();  //"aesaaesaaesa"
    159     cout<<",";
    160     /* 3.实现void EditSub(char * subs,char *subd)*/ 
    161     //---------------------s3,s4    
    162     p = strtok (NULL," ,.-");
    163     strcpy(s3,p);
    164     p = strtok (NULL," ,.-");
    165     strcpy(s4,p);
    166     EditString es3(s1);
    167     es3.EditSub(s3,s4);
    168     es3.showStr();//"strstrstr"
    169     cout<<",";
    170     /* 4.实现void DeleteChar(char ch) */
    171     //---------------------c3
    172     p = strtok (NULL," ,.-");
    173     c3 = *p;
    174     EditString es4(s1);
    175     es4.DeleteChar(c3);
    176     es4.showStr();//"eseses"
    177     cout<<",";
    178     /* 5.实现void DeleteSub(char * sub)*/ 
    179     //--------------s5
    180     p = strtok (NULL," ,.-");
    181     strcpy(s5,p);
    182     EditString es5(s1);
    183     es5.DeleteSub(s5);//"tttttt";
    184     es5.showStr();
    185     cout<<endl;
    186     return 0;
    187 }
     1 int main()
     2 {   //testtesttest,3,test,t,a,test,str,t,es
     3     char s[N]="";
     4     gets(s);
     5     EditString str(s);
     6     str.EditChar(',',' ');
     7     strcpy(s,str.GetStr());
     8     char s1[N]="",s2[N]="",s3[N]="",s4[N]="",s5[N]="";
     9     char c1,c2,c3;
    10     int count;
    11     sscanf(s,"%s%d%s %c %c%s%s %c%s",s1,&count,s2,&c1,&c2,s3,s4,&c3,s5);
    12     /* 1.实现int IsSubString(int start, const char *str); */
    13     //---------------------s1,count,s2
    14     EditString es1(s1);
    15     cout<<es1.IsSubString(count,s2)<<",";//4
    16     /* 2.实现EditChar(char s, char d)*/
    17     //---------------------s1,c1,c2
    18     EditString es2(s1);
    19     es2.EditChar(c1,c2);
    20     es2.showStr();  //"aesaaesaaesa"
    21     cout<<",";
    22     /* 3.实现void EditSub(char * subs,char *subd)*/ 
    23     //---------------------s3,s4    
    24     EditString es3(s1);
    25     es3.EditSub(s3,s4);
    26     es3.showStr();//"strstrstr"
    27     cout<<",";
    28     /* 4.实现void DeleteChar(char ch) */
    29     //---------------------c3
    30     EditString es4(s1);
    31     es4.DeleteChar(c3);
    32     es4.showStr();//"eseses"
    33     cout<<",";
    34     /* 5.实现void DeleteSub(char * sub)*/ 
    35     //--------------s5
    36     EditString es5(s1);
    37     es5.DeleteSub(s5);//"tttttt";
    38     es5.showStr();
    39     cout<<endl;
    40     return 0;
    41 }
      1 #include <iostream>
      2 #include <cstring>
      3 using namespace std;
      4 class String{
      5 protected:
      6     char *mystr;
      7     int len;
      8 public:
      9     String(const char *p){
     10         len = strlen(p);
     11         mystr = new char[len+1];
     12         strcpy(mystr,p);
     13     }
     14     ~String(){
     15         if (mystr !=NULL)
     16         {
     17             delete []mystr;
     18             mystr = NULL;
     19             len = 0;
     20         }
     21     }
     22     void showStr(){cout <<mystr;}
     23     char * GetStr(){return mystr;}
     24     virtual bool IsSubString(const char *str){
     25         int i,j;
     26         for (i =0;i<len;i++)
     27         {
     28             int k = i;
     29             for (j =0;str[j] !='';j++,k++)
     30             {
     31                 if (str[j]!= mystr[k]) break;
     32             }
     33             if(str[j] =='') return true;
     34         }
     35         return false;
     36     }
     37 };
     38 
     39 class EditString:public String{
     40 public:
     41     EditString(const char *p):String(p){}
     42     int IsSubString(int start, const char *str);
     43     void EditChar(char s, char d); 
     44     void EditSub(char * subs,char *subd); 
     45 
     46     void DeleteChar(char ch);  
     47     void DeleteSub(char * sub); 
     48 
     49 };
     50 int EditString::IsSubString(int start, const char *str)
     51 {
     52     int i=start,j;
     53     for (i =start;i<len;i++)
     54     {
     55         int k = i;
     56         for (j =0;str[j] !='';j++,k++)
     57         {
     58             if (str[j]!= mystr[k]) break;
     59         }
     60         if(str[j] =='') return i;
     61     }
     62     return -1;
     63 }
     64 void EditString::EditChar(char s, char d)
     65 {
     66     char *p = mystr;
     67     while(*p)
     68     {
     69         if(*p==s)
     70             *p = d;
     71         p++;
     72     }
     73 }
     74 void EditString::EditSub(char * subs,char *subd)
     75 {
     76     int start=0;
     77     char *temp = new char[len+1];
     78     while(start<len)
     79     {
     80         start=IsSubString(start,subs);
     81         if(start!=-1){
     82             strncpy(temp,mystr,start);
     83             temp[start] = '';
     84             strcat(strcat(temp,subd),&mystr[start+strlen(subs)]);
     85             strcpy(mystr,temp);
     86             len = strlen(mystr);
     87         }
     88         else{
     89             break;
     90         }
     91     }
     92     delete []temp;
     93 }
     94 void EditString::DeleteChar(char ch)
     95 {
     96     char *p = mystr,*q=mystr;
     97     while(*q)
     98     {
     99         if(*q!=ch){
    100             *p = *q;
    101             p++;
    102         }
    103         q++;
    104     }
    105     *p = '';
    106     len = strlen(mystr);
    107 }
    108 void EditString::DeleteSub(char * sub){
    109     int start=0;
    110     char *temp = new char[len+1];
    111     while(start<len)
    112     {
    113         start=IsSubString(start,sub);
    114         if(start!=-1){
    115             strncpy(temp,mystr,start);
    116             temp[start] = '';
    117             strcat(temp,&mystr[start+strlen(sub)]);
    118             strcpy(mystr,temp);
    119             len = strlen(mystr);
    120         }
    121         else{
    122             break;
    123         }  
    124     }
    125     delete []temp;
    126 }
    127 void fun(char *s,char str[][256]){
    128     int index = 0;
    129     char *p = s,*q = s;
    130     while(*p)
    131     {
    132         if(*p==','){
    133             *p = '';
    134             strcpy(str[index++],q);
    135             q = p+1;
    136         }
    137         p++;
    138     }
    139     strcpy(str[index++],q);    
    140     /* for(int i=0;i<9;++i){
    141         printf("%s
    ",str[i]);
    142     } */
    143 }
    144 int main()
    145 {   //testt     esttest,3,test,t,a,test,str,t,es
    146     char s[256] = "";
    147     char str[9][256]={0};    
    148     gets(s);
    149     fun(s,str);    
    150     /* 1.实现int IsSubString(int start, const char *str); */
    151     //---------------------s0,count1,s2
    152     EditString es1(str[0]);
    153     cout<<es1.IsSubString(atoi(str[1]),str[2])<<",";//4
    154     /* 2.实现EditChar(char s, char d)*/
    155     //---------------------s0,c3,c4
    156     EditString es2(str[0]);
    157     es2.EditChar(str[3][0],str[4][0]);
    158     es2.showStr();  //"aesaaesaaesa"
    159     cout<<",";
    160     /* 3.实现void EditSub(char * subs,char *subd)*/ 
    161     //---------------------s5,s6    
    162     EditString es3(str[0]);
    163     es3.EditSub(str[5],str[6]);
    164     es3.showStr();//"strstrstr"
    165     cout<<",";
    166     /* 4.实现void DeleteChar(char ch) */
    167     //---------------------c7
    168     EditString es4(str[0]);
    169     es4.DeleteChar(str[7][0]);
    170     es4.showStr();//"eseses"
    171     cout<<",";
    172     /* 5.实现void DeleteSub(char * sub)*/ 
    173     //--------------s8
    174     EditString es5(str[0]);
    175     es5.DeleteSub(str[8]);//"tttttt";
    176     es5.showStr();
    177     cout<<endl;
    178     return 0;
    179 }

    4、完备数

    如果一个数正好是他的所有约数(除了它本身以外)的和,称为完备数,

    如:6,它的约数有1,2,3,并且1+2+3=6.

    请输出n以内的所有完备数(完备数递增输出),每个完备数1行,每行按照下例输出:

    比如某完备数是6,则该行输出:6=1+2+3

    如果输入错误,则输出"error"。

    例如:

    输入:

    40

    输出:

    6=1+2+3空格回车

    28=1+2+4+7+14空格回车

     1 #include<stdio.h>
     2 
     3 int fun(int i)
     4 {
     5     int sum=0;
     6     int t = i-1,j;
     7     int arr[100] = {0};
     8     int index =0;
     9     while(t)
    10     {
    11         
    12         if(i%t==0)
    13         {
    14             arr[index++] = t;
    15             sum += t;
    16             //printf("%d,%d,%d
    ",i,sum,t);
    17         }
    18         t--;
    19     }
    20     if(sum==i){
    21         printf("%d=",i);
    22         for( j=index-1; j>=0; --j)
    23         {
    24             if(j<index-1&&index!=1)
    25                 printf("+");
    26             printf("%d",arr[j]);
    27         }
    28         return 1;
    29     }
    30     return 0;
    31 }
    32 
    33 int main()
    34 {
    35     int n,flag=0;
    36     scanf("%d",&n);
    37    
    38     for(int i=6;i<=n;++i)
    39     {
    40         if(fun(i)){
    41             if(flag>1)
    42                 printf("
    ");
    43             else{
    44                 printf(" 
    ");
    45             }
    46             flag++;
    47         }
    48     }    
    49     return 0;
    50 }

    5、逆序memcpy

    实现逆序的Memcpy方法。

    void * reversememcpy ( void * destination, const void * source, int num );

    从source所指的内存地址的起始位置开始拷贝num个字节,逆序保存到目标destination所指的内存地址的

    起始位置中。返回destination首地址。

    注意为逆序拷贝。比如source指向的位置,依次保存了abcdef,当num=3时,则逆序拷贝后destination指向的

    位置应该依次保存cba.

    输入:abcdef

    输出:cba

    提交的程序包括main函数,具体内容如下:

    #include<stdio.h>
    #include<string.h>
    void * reversememcpy ( void * destination, const void * source, int num );
    int main()
    {
        char source[100],destionation[100];
        int n;
        scanf("%s",source);
        scanf("%d".&n);
        destionation=reversememcpy (destionation,scource,strlen(source),n);
        printf("%s",destionation);
        return 0;
    } 

    注意:目标地址不能为空,源和目标空间首地址差应该大于num。如果num>strlen(source),应该输出error

     1 #include<stdio.h>
     2 #include<string.h>
     3 void * reversememcpy ( void * destination, const void * source, int num )
     4 {
     5     char *des = (char*)destination, *src = (char*)source;
     6     if(strlen(src)<num){
     7         printf("error");
     8         return NULL;
     9     }
    10     
    11     for(int i=0; i<num; ++i)
    12     {
    13         des[num-i-1] = src[i];
    14     }
    15     return destination;
    16 }
    17 
    18 int main()
    19 {
    20     char source[100]="",destination[100]="";
    21     int n; 
    22     gets(source);
    23     scanf("%d",&n);
    24     
    25     reversememcpy (destination,source,n);
    26     printf("%s",destination);
    27     return 0;
    28 }

    6、冒泡排序

    题目内容:

    输入n个整数,用冒泡排序对这n个数进行递增/非递减排序,输出排序后的结果.如果输入不符要求,则输出"error"

    输入格式:第一行是待排序的数据个数n,第二排是逗号分隔的n个正整数

    9

    9,8,7,6,5,4,3,2,1

    输出格式:输出排序后的用逗号分隔的n个数据,最后1个数据后面没有逗号

    1,2,3,4,5,6,7,8,9

     1 #include <stdio.h>
     2 #define N 100
     3 /*冒泡排序--递归*/
     4 void BubbleSort(int *a,int left, int right)
     5 {
     6     if(left<right){
     7         int j,t; 
     8         for(j=right; left<j; j--){
     9             if(a[j-1]>a[j])/*相邻比较*/ 
    10                 t=a[j],a[j]=a[j-1],a[j-1]=t;  
    11         }
    12         BubbleSort(a,j+1,right);/*递归*/
    13     }
    14 }
    15 
    16 int main()
    17 {
    18     int arr[N] ={0};
    19     int t,n;
    20     int ret = scanf("%d",&n);
    21     if(!ret){
    22         printf("error");return 0;
    23     }
    24     
    25     for(int i=0; i<n-1; ++i)
    26     {
    27         int ret = scanf("%d,",&arr[i]);
    28         if(!ret){
    29                 printf("error");return 0;
    30         }
    31     }
    32     scanf("%d,",&arr[n-1]);
    33    
    34     BubbleSort(arr,0,n-1);
    35     for(int i=0; i<n; ++i){
    36         if(i&&n>1)printf(",");
    37         printf("%d",arr[i]);
    38     }
    39     printf("
    ");
    40     
    41     return 0;
    42 }

    7、实现三角形类

    实现一个三角形类 Ctriangle

    该类有一个GetPerimeter方法返回三角形的周长;

    GetArea方法返回三角形的面积;

    该类还提供一个display方法显示三角形的三边长度;

    最终在main函数中生成该类,

    输入三条边的长度(不用考虑三条边不能构成三角形的情况);

    展示三角形的三边长度以及周长和面积

    Ctriangle类的使用如下,在你的代码中除了实现Ctriangle类还需引入一下代码。

    int main() 
    {
        double a, b, c;
        cin >> a >> b >> c;
        Ctriangle T(a, b, c);
        T.display();
        cout << "Perimeter:" <<setprecision(4)<< T.GetPerimeter() << endl;
        cout << "Area:" << setprecision(5) << T.GetArea() << endl;
        system("pause");
        return 0;
    }

    输出格式

    输入:3 4 5

    输出:

    Ctriangle:a=3,b=4,c=5

    Perimeter:12

    Area:6

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <iomanip>
     5 using namespace std;
     6 class Ctriangle
     7 {
     8 protected:
     9     double a,b,c;
    10 public:
    11     Ctriangle(double x,double y,double z)
    12     {
    13        a=x;b=y;c=z;
    14     }
    15     double GetPerimeter()
    16     {
    17         return a+b+c;
    18     }
    19     double GetArea()
    20     {
    21         double p = (a+b+c)/2;
    22         return sqrt(p*(p-a)*(p-b)*(p-c));
    23     }
    24     void display()
    25     {
    26         cout<<"Ctriangle:a="<<a<<",b="<<b<<",c="<<c<<endl;
    27     }
    28 };
    29 
    30 int main()
    31 {
    32     double a, b, c;
    33     cin >> a >> b >> c;
    34     Ctriangle T(a, b, c);
    35     T.display();
    36     cout << "Perimeter:" <<setprecision(4)<< T.GetPerimeter() << endl;
    37     cout << "Area:" << setprecision(5) << T.GetArea() << endl;
    38     system("pause");
    39     return 0;
    40 }
  • 相关阅读:
    Html5 Input 类型
    Html5 部分特性
    Asp.net Mvc4 基于Authorize实现的模块访问权限
    第11天知识点总结
    C# string类型和byte[]类型相互转换
    C#中AppDomain.CurrentDomain.BaseDirectory及各种路径获取方法
    Socket 学习
    C#中的Dictionary字典类介绍
    js判断客户端是pc还是手机
    input type="file" accept="image/*"上传文件慢的问题解决办法
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/11629669.html
Copyright © 2011-2022 走看看