demo

demo1:

#include <stdio.h>

int main(void)
{
    double number1=0.0;
    double number2=0.0;
    char operation=0;
    double result=0.0;
    printf("\nEnter the calculation\n");
    scanf("%lf %c %lf",&number1,&operation,&number2);
    switch(operation)
    {
        case '+':
        result=number1+number2;
        break;
        case '-':
        result=number1-number2;
        break;
        case '*':
        result=number1*number2;
        break;
        case '/':
        if(number2==0)
        {
            printf("\n\naDivision by zero error!\n");
            break;
        }
        result=number1/number2;
        break;
        case '%':
        if((long)number2==0)
        {
            printf("\n\naDivision by zero error!\n");
            break;
        }
        result=(long)number1%(long)number2;
        break;
        default:
        printf("\ntheir is not support the operation!\n");
    }
    printf("%.2lf %c %.2f = %.2lf",number1,operation,number2,result);
    return 0;
}

取摩运算要求运算符两端都是整数,所以要先将两个double转换称为long类型

demo2:

#include <stdio.h>

int main(void)
{
    double Fahrenheit=0.0;
    double centigrade=0.0;
    double temp;
    char type;
    printf("\nInput type what's you want to transform to and variable(F or C),example(F12.3)\n");
    scanf("%c %lf",&type,&temp);
    switch(type)
    {
        case 'C':
        centigrade=temp;
        Fahrenheit=(centigrade-32)*5/9;
        printf("\ncentigrade %.2lf to Fahrenheit,get %.2f.\n",centigrade,Fahrenheit);
        break;
        case 'F':
        Fahrenheit=temp;
        centigrade=Fahrenheit*1.8+32;
        printf("\nFahrenheit %.2lf to centigrade,get %.2.\n",Fahrenheit,centigrade);
        break;
        default:
        printf("\nInput error:not this convert!");
    }
    return 0;
}

 

#include <stdio.h> /*for input and output*/
#include <ctype.h> /*for toupper() function*/
#include <stdbool.h> /*for bool,true,false*/
#include <time.h> /*for time() function*/
#include <stdlib.h> /*for rand() and srand()*/

int main(void)
{
    bool correct=true;
    char another_game='Y';/*records id another game is to be played*/
    int  counter=0; /* number of sequences enter successfully */
    int sequence_length=0;
    time_t seed=0; /* Seed value for random number sequence */
    int number=0; /*Stores an input digit */
    time_t now=0;
    int time_taken=0;
    
    /*describe how the game is played ;this is game discription*/
    printf("\nTo play Simple Simon,");
    printf("watch the screen for a sequence of digits.");
    printf("\nWatch carefully,as the digits are only displayd for a second!");
    printf("\nWhen computer will remove them,and then prompt you ");
    printf("to enter the same sequence.");
    printf("\nGood luck!\nPress Enter to play\n");
    
    scanf("%c",&another_game);
    
    do
    {
        correct=true;
        counter=0; /* Initialize count of number of successful tries */
        sequence_length=2; /*Initialize length of a dight sequence */
        time_taken=clock(); /* Record current time at start of game */
        while(correct)
        {   
            sequence_length+=counter++%3 ==0;/*理解一下,count如果是3的倍数余数得到的是0,否则。。。,然后和0进行等于判断,然后将true和false转为0或者1再加*/
            seed=time(NULL); 
            now=clock();
            srand((unsigned int)seed);
            for(int i=1;i<=sequence_length;i++)
            {
                printf("%d",rand() % 10);/*实现输出一系列长度的0~9的数原来是这样实现的。巧劲*/
            }
            /* wait one second and clear the screen*/
            for(;clock()-now<CLOCKS_PER_SEC;)
            {
                /*这个也用的好,我之前的想法是使用if语句,只有满足条件才会走下面的语句。我太嫩了啊!*/
            }
            /* overwrite the digit sequence */ 
            srand((unsigned int)seed); /* restart the random sequence */
               printf("\r"); 
               for(int i=1;i<sequence_length;i++)
               printf(" ");/* Output two spaces */
               if(counter==1){ /* only output for the first try */
                   printf("\nNow you enter the sequence - don't forgrt the spaces\n");
               }else{
                   printf("\r");  /* back to beginning of the line */
               }
            for(int i=1;i<=sequence_length;i++)/*这验证步骤也很。。,因为使用同一个种子值得到的序列数是一样的,所以将随机数还原,然后再使用之前的种子值依次得到序列中的对应位置的值与输入的值进行比较*/
            {
                scanf("%d",&number);
                if(number !=rand()%10)
                  {
                      correct =false;
                      break;
                  }
               } 
               printf("%s\n",correct ? "Correct!" :"Wrong!");
        
        }
        /* Calculate total time to play the game in seconds */
        time_taken=(clock()-time_taken)/CLOCKS_PER_SEC;//进行了多少秒
        printf("\n\n your score is %d",--counter*100/time_taken); 
        fflush(stdin);//清除缓存 
        printf("\nDo you want to play again (y/n)? ");
        scanf("%c",&another_game);
    }while(toupper(another_game)=='Y');
    return 0;
}

 

posted @ 2016-04-26 11:03  guodaxia  阅读(168)  评论(0编辑  收藏  举报