Object-Oriented Programming 1 winter operations program title

This work belongs courses 2020 Object-Oriented Programming (Fuzhou University - School of Mathematics and Computer Science)
Where the job requires Object-oriented programming winter operations 1
The target job 1. Install the C ++ development environment (see appendix)
2. Complete the quiz, practice questions and programming questions
3. Publish blog
Text of the job Object-Oriented Programming 1 winter operations Quiz
Object-Oriented Programming 1 winter operations practice topic
Object-Oriented Programming 1 winter operations program title
Other references Have been in essay questions, practice questions marked

Programming problem

1. Topics requirements:

By the Chinese character "integer", "wallet", "look", "increase", "decrease" to achieve the vernacular programming. Note that the total purse in the range of zero to 99 .

2. Decomposition requirements:

Understood by reading the title, this question comprises three aspects:
(1) the definition of an integer variable "wallet", and initializes;
(2) the input "up", "down" command, to change the total wallet;
(3) by "look wallet", the output of the total purse.

3. thinking process and doubts

First of all, I intend to be divided into four, the main function, the function is responsible for the total purse initialization function is responsible for the total increase or decrease in the wallet and the wallet function of total output constituted.
The following is the initial total purse function code words:

int beginning(int wallet,char d[]){
    if(strcmp(d,"零")==0) return 0;  //这里使用strcmp函数时,当字符串相同时,返回值应为0,一开始
    else if(strcmp(d,"一")==0) return 1;    //以为是1,故得不出结果,一定要注意!!
    else if(strcmp(d,"二")==0) return 2;
    else if(strcmp(d,"三")==0) return 3;
    else if(strcmp(d,"四")==0) return 4;
    else if(strcmp(d,"五")==0) return 5;
    else if(strcmp(d,"六")==0) return 6;
    else if(strcmp(d,"七")==0) return 7;
    else if(strcmp(d,"八")==0) return 8;
    else if(strcmp(d,"九")==0) return 9;
    else if(strcmp(d,"十")==0) return 10;
    else return -1;
    }    

Here I take the enumeration method, but the code will seem very lengthy, there is no other easier way to do that?
For part of the function responsible for the increase or decrease in the total purse, I used the judgment in the main function, if the string c is "increase" is performed

wallet+=calculate(wallet,d);

Similarly, if c is the string "reduce", is performed

wallet-=calculate(wallet,d);

Originally began as a plan to increase and decrease, respectively, to write a function, but a judge later found the if statement, more concise, here is my main function of the code:

int calculate(int wallet,char d[]){
      if(strcmp(d,"零")==0) return 0;
      else if(strcmp(d,"一")==0) return 1;
      else if(strcmp(d,"二")==0) return 2;
      else if(strcmp(d,"三")==0) return 3;
      else if(strcmp(d,"四")==0) return 4;
      else if(strcmp(d,"五")==0) return 5;
      else if(strcmp(d,"六")==0) return 6;
      else if(strcmp(d,"七")==0) return 7;
      else if(strcmp(d,"八")==0) return 8;
      else if(strcmp(d,"九")==0) return 9;
        else if(strcmp(d,"十")==0) return 10;
      else return -1;
}

For the function of this part of the output of the total purse, first I intend to discuss three cases in function carve, then enumerating 1-10 kinds of situations in each case, and then found that doing this too long, therefore write the a function responsible 1-10 characters output, while three cases in total output in the wallet function call this function. as follows:

void num(int i){
    if(i==0) printf("零");
    else if(i==1) printf("一");
    else if(i==2) printf("二");
    else if(i==3) printf("三");
    else if(i==4) printf("四");
    else if(i==5) printf("五");
    else if(i==6) printf("六");
    else if(i==7) printf("七");
    else if(i==8) printf("八");
    else if(i==9) printf("九");
    else if(i==10) printf("十");
}
void sum(int wallet){
    int a,b;
    if(wallet<=10){num(wallet);}
    else if(wallet<20){
        a=wallet%10;
        printf("十");
        num(a);
    }
    else{
        a=wallet/10;
        b=wallet%10;
        num(a);
        printf("十");
       if(b!=0) num(b); /*这一步我认为比较重要,因为假如钱包总额
                                      正好被十整除,按照我们中文习惯,是不会
                                      打出零的,比如20->二十而非二十零,故我
                                      认为这步判断不可或缺。*/
    }
}

For the main function section, the code below:

int main() {
    char a[100],b[100],c[100],d[100];
    int wallet=0,i,flag=0;
    scanf("%s %s %s %s",a,b,c,d);
    if(strcmp(a,"整数")!=0||strcmp(c,"等于")!=0) flag=1;
    for(i=1;flag==0;i++){
        if(i==1) wallet=beginning(wallet,d);
        else {
            scanf("%s",a);
            if(strcmp(a,b)!=0&&strcmp(a,"看看")!=0) break;
            scanf("%s",c);
            if(strcmp(a,"看看")==0){
                sum(wallet);
                break;}
            scanf("%s",d);
         if(strcmp(c,"增加")==0) wallet+=calculate(wallet,d);
         if(strcmp(c,"减少")==0) wallet-=calculate(wallet,d);
        }
    }
   return 0;
}

Total code is as follows:

#include <stdio.h>
#include <string.h>
int beginning(int wallet,char d[]){
    if(strcmp(d,"零")==0) return 0;
    else if(strcmp(d,"一")==0) return 1;
    else if(strcmp(d,"二")==0) return 2;
    else if(strcmp(d,"三")==0) return 3;
    else if(strcmp(d,"四")==0) return 4;
    else if(strcmp(d,"五")==0) return 5;
    else if(strcmp(d,"六")==0) return 6;
    else if(strcmp(d,"七")==0) return 7;
    else if(strcmp(d,"八")==0) return 8;
    else if(strcmp(d,"九")==0) return 9;
    else if(strcmp(d,"十")==0) return 10;
    else return -1;
}
int calculate(int wallet,char d[]){
      if(strcmp(d,"零")==0) return 0;
      else if(strcmp(d,"一")==0) return 1;
      else if(strcmp(d,"二")==0) return 2;
      else if(strcmp(d,"三")==0) return 3;
      else if(strcmp(d,"四")==0) return 4;
      else if(strcmp(d,"五")==0) return 5;
      else if(strcmp(d,"六")==0) return 6;
      else if(strcmp(d,"七")==0) return 7;
      else if(strcmp(d,"八")==0) return 8;
      else if(strcmp(d,"九")==0) return 9;
      else if(strcmp(d,"十")==0) return 10;
      else return -1;
}
void num(int i){
    if(i==0) printf("零");
    else if(i==1) printf("一");
    else if(i==2) printf("二");
    else if(i==3) printf("三");
    else if(i==4) printf("四");
    else if(i==5) printf("五");
    else if(i==6) printf("六");
    else if(i==7) printf("七");
    else if(i==8) printf("八");
    else if(i==9) printf("九");
    else if(i==10) printf("十");
}
void sum(int wallet){
    int a,b;
    if(wallet<=10){num(wallet);}
    else if(wallet<20){
        a=wallet%10;
        printf("十");
        num(a);
    }
    else{
        a=wallet/10;
        b=wallet%10;
        num(a);
        printf("十");
       if(b!=0) num(b);
    }
}

int main() {
    char a[100],b[100],c[100],d[100];
    int wallet=0,i,flag=0;
    scanf("%s %s %s %s",a,b,c,d);
    if(strcmp(a,"整数")!=0||strcmp(c,"等于")!=0) flag=1;
    for(i=1;flag==0;i++){
        if(i==1) wallet=beginning(wallet,d);
        else {
            scanf("%s",a);
            if(strcmp(a,b)!=0&&strcmp(a,"看看")!=0) break;
            scanf("%s",c);
            if(strcmp(a,"看看")==0){
                sum(wallet);
                break;}
            scanf("%s",d);
         if(strcmp(c,"增加")==0) wallet+=calculate(wallet,d);
         if(strcmp(c,"减少")==0) wallet-=calculate(wallet,d);
        }
    }
   return 0;
}

4. Test Case




5. doubts and solve

  • 在解决这一题,我使用枚举的方法将0-10全部写出来,是否有更简便的方法可以替换这种枚举的方法;
  • 在这一过程中,我尝试了用scanf函数与gets函数,并比较了两者的不同,可以在今后的学习中分情况不同从而进行使用
  • 本次使用了strcmp函数,并且注意到了,当两个字符串相同时,其返回值为0,而并非1,这以后一定会注意!!

Guess you like

Origin www.cnblogs.com/lvhang/p/12233520.html
Recommended