zoukankan      html  css  js  c++  java
  • 19、显示表达式的运算形式

    显示表达式的运算形式

    1、显示表达式1*2+3*4+...+9*10的运算表示形式 

    源程序代码如下:

    /*
        2017年6月7日22:54:51
        功能:实现1*2+3*4+...+9*10表达式的操作
    
    */
    #include "stdio.h"
    #include "string.h"
    
    char a[100];
    int i, j = 0;
    int main()
    {
        for(i = 1; i <= 10; i++)
        {
            if(i % 2 != 0)
            {
                a[j++] = i +'0';
                a[j++] = '*';
            }
            else if(i % 2 == 0 && i != 10)
            {
                a[j++] = i +'0';
                a[j++] = '+';
            }
            else 
            {
                a[j++] = 1 + '0';
                a[j++] = '0';
            }
    
        }
        a[j] = '';
        puts(a);
        return 0;
    }
    /*
        总结:
        在VC++6.0中显示的结果:
        ——————————————————
        1*2+3*4+5*6+7*8+9*10
        ——————————————————
    
    */
    

    2、显示表达式1*2+3*4+...+99*100的运算表示形式 

     源程序代码如下:

    /*
        2017年6月7日22:44:12
        功能:显示表达式1*2+3*4+...+99*100的表示形式
    */
    
    #include"stdio.h"
    #include"string.h"
    
    int main()
    {
        char seq[10000] = "alex";
        int i, j = 0, first, second;
        for (i = 1; i <= 100; i++)
        {
            if (i < 10)
                if (i % 2 != 0)
                    seq[j++] = '0' + i,seq[j++] = '*';
                else
                    seq[j++] = '0' + i,seq[j++] = '+';
            else if (i >= 10 && i <= 99)
            {
                first = i % 10;
                second = (i - first) / 10;
                if (i % 2 != 0)
                    seq[j++] = '0' + second,seq[j++] = '0' + first,seq[j++] = '*';
                else
                    seq[j++] = '0' + second,seq[j++] = '0' + first,seq[j++] = '+';
            }
            else if (i == 100)
                seq[j++] = '0' + 1,seq[j++] = '0',seq[j] = '0';
            
        }
        puts(seq);
        return 0;
    }
    
    
    /*
        总结:
        在VC++6.0中显示的结果:
        ——————————————————————————————————
        1*2+3*4+5*6+7*8+9*10+11*12+13*14+15*16+17*18+19*20+21*22+23*24+25*26
        +27*28+29*30+31*32+33*34+35*36+37*38+39*40+41*42+43*44+45*46+47*48+
        49*50+51*52+53*54+55*56+57*58+59*60+61*62+63*64+65*66+67*68+69*70+
        71*72+73*74+75*76+77*78+79*80+81*82+83*84+85*86+87*88+89*90+91*92+
        93*94+95*96+97*98+99*100
    
    
        ——————————————————————————————————
    */ 

    3、显示表达式1*2+3*4+...+99*100的运算表示形式(采取交互的形式)

    程序源代码如下:

    /*
        2017年6月8日08:03:38
        功能:采取交互的形式输出以下的表达式
    */
    #include "stdio.h"
    #include "string.h"
    
    int main()
    {
        char seq[1000];
        int i, j = 0, first, second, num;
        printf("please input a odd number: ");
        scanf("%d",&num);
        if(num <= 10)
        {
            for (i = 1; i <= num; i++)
            {
                 if (i % 2 != 0)
                    seq[j++] = '0' + i,seq[j++] = '*';
                 else if(i % 2 == 0 && i != num)
                    seq[j++] = '0' + i,seq[j++] = '+';
                 else if (i == num)     
                    seq[j++] = '0' + 1,seq[j++] = '0';
            }
        }
        else if(num > 10 && num < 100)
        {
            for (i = 1; i <= num; i++)    
            {
                if (i < 10)
                     if (i % 2 != 0)
                         seq[j++] = '0' + i,seq[j++] = '*';
                     else
                         seq[j++] = '0' + i,seq[j++] = '+';
                 else if (i >= 10 && i < num)
                 {
                     first = i % 10;
                     second = (i - first) / 10;
                     if (i % 2 != 0)
                         seq[j++] = '0' + second,seq[j++] = '0' + first,seq[j++] = '*';
                     else if (i % 2 == 0 && i != num)
                         seq[j++] = '0' + second,seq[j++] = '0' + first,seq[j++] = '+';
                 }
                 else if (i == num)
                     seq[j++] = '0' + second ,seq[j++] = '0' + (first + 1) ;
            }
    
        }
        else if(num == 100)
        {
            for (i = 1; i <= 100; i++)    
            {
                if (i < 10)
                     if (i % 2 != 0)
                         seq[j++] = '0' + i,seq[j++] = '*';
                     else
                         seq[j++] = '0' + i,seq[j++] = '+';
                 else if (i >= 10 && i < 100)
                 {
                     first = i % 10;
                     second = (i - first) / 10;
                     if (i % 2 != 0)
                         seq[j++] = '0' + second,seq[j++] = '0' + first,seq[j++] = '*';
                     else
                         seq[j++] = '0' + second,seq[j++] = '0' + first,seq[j++] = '+';
                 }
                 else if (i == 100)
                     seq[j++] = '0' + 1,seq[j++] = '0',seq[j++] = '0';
            }
        }
        seq[j] = '';
        puts(seq);
    }
    /*
        总结:
        在VC++6.0中显示的结果:
        -----------------------------------------------------------------
        please input a odd number: 98
        1*2+3*4+5*6+7*8+9*10+11*12+13*14+15*16+17*18+19*20+21*22+23*24+25*26+27*28+29*30
        +31*32+33*34+35*36+37*38+39*40+41*42+43*44+45*46+47*48+49*50+51*52+53*54+55*56+5
        7*58+59*60+61*62+63*64+65*66+67*68+69*70+71*72+73*74+75*76+77*78+79*80+81*82+83*
        84+85*86+87*88+89*90+91*92+93*94+95*96+97*98
    
        please input a odd number: 100
        1*2+3*4+5*6+7*8+9*10+11*12+13*14+15*16+17*18+19*20+21*22+23*24+25*26+27*28+29*30
        +31*32+33*34+35*36+37*38+39*40+41*42+43*44+45*46+47*48+49*50+51*52+53*54+55*56+5
        7*58+59*60+61*62+63*64+65*66+67*68+69*70+71*72+73*74+75*76+77*78+79*80+81*82+83*
        84+85*86+87*88+89*90+91*92+93*94+95*96+97*98+99*100
        -----------------------------------------------------------------
        注意:将整数型数据转化成字符型数据的格式
                char m;
                int n;
                m = n + '0';(像10、100之类的数据除外)
    */
    

     

  • 相关阅读:
    EDA cheat sheet
    numpy.bincount()
    非负矩阵分解的两种方法简析
    Python列表解析和字典解析
    Pandas使用groupby()时是否会保留顺序?
    Reduce pandas memory size
    Understanding the Transform Function in Pandas
    What’s up with the Graph Laplacian
    如何在github上下载单个文件夹
    TCP回射服务器修订版(ubuntu 18.04)
  • 原文地址:https://www.cnblogs.com/wxt19941024/p/6960878.html
Copyright © 2011-2022 走看看