zoukankan      html  css  js  c++  java
  • C函数调用

      1 #include<stdio.h>
      2 #include<time.h>
      3 #include<math.h>
      4 #include<stdlib.h>
      5 
      6 int No=-1;        //功能选择标志
      7 
      8 void HomePage();                            //首页
      9 void FunctionSelection();                    //功能选择
     10 void MultiplicationTable();                    //九九乘法表
     11 void YanghuiTriangle();                        //杨辉三角
     12 void IntLeapYear();                            //判断闰年
     13 void LettersPyramid_1();                    //字母金字塔(1)
     14 void LettersPyramid_2();                    //字母金字塔(2)
     15 void GCD_LCM();                                //最大公约数最小公倍数
     16 void Josephus();                            //约瑟夫环
     17 void JudgePrime();                            //判断素数
     18 void NarcissisticNumber();                    //水仙花数
     19 void PrimeFactors();                        //分解质因数
     20 void JudgmentDay();                            //判断某年某月某日是今年的第几天
     21 void Pattern();                                //打印棱形图案
     22 
     23 void main()
     24 {
     25     do{
     26     system("cls");
     27     HomePage();
     28     FunctionSelection();
     29     }while(No!=0);
     30 }
     31 
     32 void FunctionSelection()                    //功能选择
     33 {
     34     switch(No)
     35     {
     36     case 1:MultiplicationTable();break;
     37     case 2:YanghuiTriangle();break;
     38     case 3:IntLeapYear();break;
     39     case 4:LettersPyramid_1();break;
     40     case 5:LettersPyramid_2();break;
     41     case 6:GCD_LCM();break;
     42     case 7:Josephus();break;
     43     case 8:JudgePrime();break;
     44     case 9:NarcissisticNumber();break;
     45     case 10:PrimeFactors();break;
     46     case 11:JudgmentDay();break;
     47     case 12:Pattern();break;
     48     }
     49 
     50 }
     51 
     52 void HomePage()                                //首页
     53 {
     54     char* str_time=NULL;
     55     time_t t;
     56     system("cls");
     57     t=time(0);                        /*获取当前时间*/
     58     str_time=ctime(&t);                /*把日期和时间转换为字符串*/
     59 
     60     printf ("┏ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅┓
    ");
     61     printf ("┇请选择您需要的功能:                                         ┇
    ");
     62     printf ("┣ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅┫
    ");
     63     printf ("┇  九九乘法表请按 …………………………………………1          ┇
    ");
     64     printf ("┇  杨辉三角请按 ……………………………………………2          ┇
    ");
     65     printf ("┇  判断闰年请按 ……………………………………………3          ┇
    ");
     66     printf ("┇  字母金字塔(1)请按  ……………………………………4          ┇
    ");
     67     printf ("┇  字母金字塔(2)请按  ……………………………………5          ┇
    ");
     68     printf ("┇  最大公约数最小公倍数请按 ……………………………6          ┇
    ");
     69     printf ("┇  约瑟夫环请按 ……………………………………………7          ┇
    ");
     70     printf ("┇  判断素数请按 ……………………………………………8          ┇
    ");
     71     printf ("┇  水仙花数请按 ……………………………………………9          ┇
    ");
     72     printf ("┇  分解质因数请按 …………………………………………10         ┇
    ");
     73     printf ("┇  判断某年某月某日是今年的第几天请按 ………………11         ┇
    ");
     74     printf ("┇  打印棱形图案请按 ………………………………………12         ┇
    ");
     75     printf ("┇                                                     退出_0 ┇
    ");
     76     printf ("┗ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅┛
    ");
     77     printf ("                               %s",str_time);
     78     printf ("Please write down the number:");
     79     scanf ("%d",&No);
     80 }
     81 
     82 
     83 
     84 void MultiplicationTable()                    //九九乘法表
     85 {
     86     int i,j;
     87     for(i=1;i<10;i++)
     88     {
     89         for(j=1;j<=i;j++)
     90         {
     91             printf("%d*%d=%-2d ",j,i,i*j);
     92         }
     93         printf("
    ");
     94     }
     95     system("pause");
     96 }
     97 
     98 void YanghuiTriangle()                        //杨辉三角
     99 {
    100     int n;
    101     printf("请先定义杨辉三角的深度(3~13最佳):");
    102     scanf("%d",&n);
    103     //缺少异常机制
    104     int a[30][30];
    105     int i,j,k;
    106     for(i=0;i<n;i++)
    107         for(j=0;j<=i;j++)
    108         {
    109             if(j==0||i==j)    a[i][j]=1;        //对两边的1进行赋值
    110         }
    111     for(i=2;i<n;i++)
    112         for(j=1;j<i;j++)
    113             a[i][j]=a[i-1][j-1]+a[i-1][j];    //数为两肩上的数之和
    114         
    115     for(i=0;i<n;i++)
    116     {
    117         for(k=0;k<n-i;k++)
    118             printf("  ");
    119         for(j=0;j<=i;j++)
    120             printf("%-3d ",a[i][j]);        //输出
    121         printf("
    ");
    122     }
    123     system("pause");
    124 }
    125 
    126 
    127 
    128 void  IntLeapYear()                            //判断闰年
    129 {
    130     int year,leap;
    131     printf("
    请输入年份:");
    132     scanf("%d",&year);
    133     if((year%4==0 && year%100!=0)||(year%400==0))
    134         leap=1;
    135     else leap=0;
    136     if(leap)
    137         printf("%d is ",year);
    138     else
    139         printf("%d is not ",year);
    140     printf("a leap year.
    
    ");
    141     system("pause");
    142 }
    143 
    144 void LettersPyramid_1()                        //字母金字塔(1)
    145 {
    146     int i,j;
    147     for(i=0;i<26;i++)
    148     {    
    149         for(j=0;j<26-i;j++)  printf(" ");
    150         for(j=i;j>=0;j--) printf("%c",'A'+j);
    151         for(j=1;j<i+1;j++) printf("%c",'A'+j);
    152         putchar('
    ');
    153     }
    154     system("pause");
    155 }
    156 
    157 void LettersPyramid_2()                        //字母金字塔(2)
    158 {
    159     int i,j;
    160     for(i=0;i<26;i++)
    161     {
    162         for(j=0;j<25-i;j++) putchar(' ');
    163         for(j=0;j<i;j++) putchar('A'+j);
    164         for(j=i;j>=0;j--) putchar('A'+j);
    165         putchar(10);
    166     }
    167     system("pause");
    168 }
    169 
    170 void GCD_LCM()                            //最大公约数最小公倍数
    171 {
    172     int n,m,nm,t;
    173     printf("请输入两个数:");
    174     scanf("%d%d",&n,&m);
    175     nm=n*m;
    176     if(m>n)
    177     {
    178         t=m;
    179         m=n;
    180         n=t;
    181     }
    182     while(n%m!=0)
    183     {
    184         t=n%m;
    185         n=m;
    186         m=t;
    187     }
    188     printf("最大公约数:%d,最小公倍数:%d
    ",m,nm/m);
    189     system("pause");
    190 }
    191 
    192 
    193 
    194 
    195 void Josephus()                                    //约瑟夫环
    196 {
    197     int m,n,i,j=0,a[100];
    198     int count1=0,count2=0;
    199     printf("请输入人数,和退出序数:");
    200     scanf("%d%d",&n,&m);
    201     for(i=0;i<n;i++)  a[i]=i+1;
    202     i=0;
    203     while(count2<n)
    204     {   
    205         
    206         while(a[i]!=0)
    207         {
    208             count1++;
    209             if(count1==m)
    210             {
    211                 j++;
    212                 printf("第%d个退出序数:%d
    ",j,a[i]);
    213                 a[i]=0;
    214                 
    215                 count1=0;
    216                 count2++;
    217             }
    218             i++;
    219             i=i%n;    
    220         }
    221             i++;
    222             i=i%n;    
    223     }
    224     system("pause");
    225 }
    226 
    227 
    228 
    229 void JudgePrime()                        //判断素数
    230 {
    231     int n,j;
    232     printf("请输入一个数:");
    233     scanf("%d",&n);
    234     for(j=2;j<=sqrt(n);j++)
    235     {
    236         if(n%j==0) break;
    237     }
    238     if(j>sqrt(n)) printf("%d是素数.
    ",n);
    239     else printf("%d不是素数.
    ",n);
    240     system("pause");
    241 }
    242 
    243 
    244 void NarcissisticNumber()                //水仙花数
    245 {
    246     int i,bai,shi,ge;
    247     printf("水仙花数:
    ");
    248     for(i=100;i<=999;i++)
    249     {
    250         bai=i/100;
    251         shi=i/10-bai*10;
    252         ge=i%10;
    253         if(i==bai*bai*bai+shi*shi*shi+ge*ge*ge)
    254             printf("%d=%d*%d*%d+%d*%d*%d+%d*%d*%d
    
    ",i,bai,bai,bai,shi,shi,shi,ge,ge,ge);
    255     }
    256     system("pause");
    257 }
    258 
    259 
    260 
    261 void PrimeFactors()                        //分解质因数
    262 {
    263     int k,n;
    264     printf("(分解质因数)请输入一个数:");
    265     scanf("%d",&n);
    266     printf("%d=",n);
    267     for(k=2;k<=n;k++)
    268     {
    269         while(n%k==0)
    270         {
    271             printf("%d",k);
    272             if(n!=k) printf("*");
    273             n=n/k;
    274         }
    275         
    276     }
    277     printf("
    ");
    278     system("pause");
    279 }
    280 
    281 
    282 void JudgmentDay()                            //判断某年某月某日是今年的第几天
    283 {
    284     int year,month,day,sum=0;
    285     printf("(判断某年某月某日是今年的第几天)
    请输入年 月 日:");
    286     scanf("%d%d%d",&year,&month,&day);
    287     switch(month)
    288     {
    289         case 1:sum=0;break;
    290         case 2:sum=31;break;
    291         case 3:sum=59;break;
    292         case 4:sum=90;break;
    293         case 5:sum=120;break;
    294         case 6:sum=151;break;
    295         case 7:sum=181;break;
    296         case 8:sum=212;break;
    297         case 9:sum=243;break;
    298         case 10:sum=273;break;
    299         case 11:sum=304;break;
    300         case 12:sum=335;break;
    301     }
    302     sum+=day;
    303     if(year%4==0&&year%100!=0||year%400==0) sum++;
    304     printf("
    今天是今年的第%d天.
    
    ",sum);
    305     system("pause");
    306 }
    307 
    308 
    309 void Pattern()                        //打印棱形图案
    310 {
    311     int i,j,k,l;
    312     char a[7][7]={' '};
    313     for(i=0;i<7;i++)
    314     {
    315         j=3;
    316         k=j-i;
    317         l=j+i;
    318         if(i>3)
    319         {
    320             k=i-3;
    321             l=6-k;
    322         }
    323         for(j=k;j<=l;j++)
    324         {
    325             a[i][j]='*';
    326         }
    327     }
    328     for(i=0;i<7;i++)
    329     {
    330         for(j=0;j<7;j++)
    331         {
    332             printf("%c",a[i][j]);
    333         }
    334         printf("
    ");
    335     }
    336     system("pause");
    337 }
  • 相关阅读:
    pig 调试(explain&amp;illerstrate)
    ufldl学习笔记与编程作业:Linear Regression(线性回归)
    EasyUI基础入门之Pagination(分页)
    codevs1052
    1040. Longest Symmetric String (25)
    Appium
    lua实现大数运算
    创建一个Spring的HelloWorld程序
    [Swift]LeetCode495. 提莫攻击 | Teemo Attacking
    [Swift]LeetCode992. K 个不同整数的子数组 | Subarrays with K Different Integers
  • 原文地址:https://www.cnblogs.com/Arvin-9/p/4225067.html
Copyright © 2011-2022 走看看