自动生成简单四则运算的C语言程序

  该程序是在博客园里面找的,具体是谁的找了半天没找到,无法提供它原本的链接。由于自己写的过于简单,且有一些功能暂时无法实现,所以就找了一个来应付作业,望原谅。在这个程序的源码中我改了一个错误的地方,源码中有这样一个随机数发生器的初始化函数的语句:“srand((unsigned)time(NULL))”。srand函数是随机数发生器的初始化函数。但是正确的写法应该是:srand(unsigned( time(NULL)));为了防止随机数每次重复,常常使用系统时间来初始化,即使用time函数来获得系统时间,它的返回值为从 00:00:00 GMT, January 1, 1970 到现在所持续的秒数,然后将time_t型数据转化为(unsigned)型再传给srand函数,即: srand((unsigned) time(&t)); 还有一个经常用法,不需要定义time_t型t变量,即: srand((unsigned) time(NULL)); 直接传入一个空指针,因为你的程序中往往并不需要经过参数获得的t数据。所以在源码中他并没有定义需要的time_t型t变量,导致程序无法运行。改后的代码如下(https://github.com/Jackchenyu/four-arithmetic-operation/tree/Dev):

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

int count_s();
void over();

void main()
{
    int i;
    printf("\n\t\t\t || 欢迎进入小学四则运算系统 ||\n");
    printf("\n\t\t\t1  开始做题\n");
    printf("\n\t\t\t2  退出\n");
    printf("\n\t请输入您的选择: \n");
    scanf("%d",&i);
    if(i==1){
        count_s();
    }
    else if(i==2)
    {
        over();
    }else{
        printf("\n\t输入错误,请重新输入:d%",i);
        return;
    }
}

void over()
{
    printf("\n\t\t欢迎再次使用,谢谢!");
}

int count_s()
{
    int i=0;
     int n=0;
     int x=0;
     int t;
     char a;
     int b, c;
     float result;
     printf("/******请输入要出的题目数量:\n");
     scanf("%d",&n);
     srand((unsigned) time(NULL));
     while(x<n)
     {
            a = rand() % 4;
             b = rand() % 100;
             c = rand() % 100;
         switch(t)
         {
         case 0:
             printf("%d + %d = ?\n", b, c);
             break;
         case 1:
             printf("%d - %d = ?\n", b, c);
             break;
         case 2:
             printf("%d * %d = ?\n", b, c);
             break;
         case 3:
             printf("%d / %d = ?\n", b, c);
             break;
         }
 
         i++;
         while(i>=n)
         {
             printf("\n\t一共 %d 题\n",i);
             printf("\n\t\t继续?[Y/N]\n");
             fflush(stdin);
             scanf("%c",&a);
             if(a=='Y'||a=='y')
             {
                 printf("/*****请输入要出的题目数量\n");
                 scanf("%d",&n);
                 i=0;
                 break;
              }
             printf("欢迎再次使用使用!\n");
             fflush(stdin);
             getchar();
             return 0;
         }
     }
}

如果有什么不对的地方请多多指教。

原文地址:https://www.cnblogs.com/CyJack/p/7560372.html