寒假作业三题解

编程题(请使用C语言或者C++完成以下题目):

继续完成作业二的编程题。
优化架构,思考代码的拓展性,比如我需要增加其他功能,如选择,循环语句怎么办。
思考:可以参考现有的编程语言,把这些语言的内容加入。如选择、循环语句、函数、或者扩大数字范围,支持负数等。

添加输出负数操作:

直接对负数进行另外操作,在操作之前,输出“负”,之后将原本的数字变成正数,进行和原来相关的,计数和输出操作:

 if(sum<0)
    {
    	printf("负");
	    sum=0-sum; 
    }

字符简化转变:

为缩小函数长度,直接使用纯数字的方式去表示汉字,与以前使用assic码同理:

int changeA(int num)
{
    if (num == -63)return 0;
    if (num == -46)return 1;
    if (num == -74)return 2;
    if (num == -56)return 3;
    if (num == -53)return 4;
    if (num == -50)return 5;
    if (num == -63)return 6;
    if (num == -58)return 7;
    if (num == -80)return 8;
    if (num == -66)return 9;
    if (num == -54)return 10;
}

简单的测试:

样例一

整数 钱包 等于 零
钱包 增加 一
钱包 减少 二
看看 钱包

样例二

整数 钱包 等于 六
钱包 增加 一
钱包 减少 九
看看 钱包

代码主体:

#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
int changeA(int num)
{
    if (num == -63)return 0;
    if (num == -46)return 1;
    if (num == -74)return 2;
    if (num == -56)return 3;
    if (num == -53)return 4;
    if (num == -50)return 5;
    if (num == -63)return 6;
    if (num == -58)return 7;
    if (num == -80)return 8;
    if (num == -66)return 9;
    if (num == -54)return 10;
}
void changeB(int number)
{
    if (number == 0)printf("零");
    else if (number == 1)printf("一");
    else if (number == 2)printf("二");
    else if (number == 3)printf("三");
    else if (number == 4)printf("四");
    else if (number == 5)printf("五");
    else if (number == 6)printf("六");
    else if (number == 7)printf("七");
    else if (number == 8)printf("八");
    else if (number == 9)printf("九");
    else if (number == 10)printf("十");
}
int main()
{
    int caozuo(char caozuo[20]);
    int changeA(int num);
    void changeB(int number);
    int sum, putness, ones, twos, countness, bianL;
    char a[20], b[20], c[20], d[20];
    scanf("%s %s %s %s", a, b, c, d);
    countness = strlen(d);
    if (countness == 4)
    {
        bianL = d[0];
        if (bianL != -54)sum = changeA(bianL) * 10;
        if (bianL == -54)sum = 10 + changeA(d[2]);
    }
    if (countness == 6)
    {
        bianL = d[0];
        sum = changeA(bianL) * 10;
        bianL = d[4];
        sum += changeA(bianL);
    }
    if (countness == 2)sum = changeA(d[0]);
    while (1)
    {
        scanf("%s ", a);
        if (strcmp(a, "看看") == 0)break;
        scanf("%s %s", b, c);
        putness = caozuo(b);
        if (putness)sum += changeA(c[0]);
        else sum -= changeA(c[0]);
    }
    if(sum<0)
    {
    	printf("负");
		sum=0-sum; 
	}
    if (sum <= 10&&sum>0)changeB(sum);
    if (sum >= 20)
    {
        twos = sum % 10;
        ones = (sum / 10) % 10;
        changeB(ones);
        printf("十");
        changeB(twos);
    }
    if (sum > 10 && sum < 20)
    {
        printf("十");
        twos = sum % 10;
        changeB(twos);
    }
    system("pause");
}
int caozuo(char caozuo[20])
{
    if (strcmp(caozuo, "减少") == 0)return 0;
    else return 1;
}
posted @ 2020-02-16 17:32  南海蛟龙  阅读(99)  评论(0编辑  收藏  举报