⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calc.c

📁 简单的计算器
💻 C
📖 第 1 页 / 共 3 页
字号:
	printf("\n\tFunctional Profiles:\n");
	printf("\t1.Support Real-Mathematical Operations,ect.:7+5(3^-1+4)*(1+9.44\n");
	printf("\t2.Support Relations Operations,ect.:4+8.45>=3456.1234+233^-2.94\n");
	printf("\t3.Support Save the Expression and Results to File\n");
	printf("\t4.Support Load Expression for Calculate From File\n");
	printf("\t5.Support Humanization Guide Mode\n");
	printf("\t----------------------------------------------------\n");
	printf("\n\tProgrammer:He Tao  ID:0505030321  Nickname:Null\n");
	printf("\n\tFor Further Suggestions and Comments, Please Contact\n");
	printf("\tEmail:HeTaoOS@Gmail.com  QQ:28681290\n");
	printf("\tWelcome to Visit\n");
	printf("\thttp://HeTaoOS.GooglePages.com\n");
	printf("\thttp://Blog.VeryCD.com/hetaoos\n");
	printf("\t----------------------------------------------------\n");
	getch();
}

/* 显示主菜单... */
int Menu_Sel()
{
    char Sel;
    int MaxItem=4;
    printf("\n\t\t             Expression Calculator\n");
    printf("\t\t      ===================================\n");
    printf("\t\t      |  1.[P]ress an Expression        |\n");
    printf("\t\t      |  2.[L]oad Expression From File  |\n");
    printf("\t\t      |  -----------------------------  |\n");
    printf("\t\t      |  3.[C]lear Screen               |\n");
    printf("\t\t      |  4.[A]bout ME                   |\n");
    printf("\t\t      |  0.[E]xit                       |\n");
    printf("\t\t      ===================================\n");
    printf("\t\t      Press a Key to Continue(0-%d):",MaxItem);
    for (;;)
    {
        Sel=getch();                			
        if ((Sel<'0'||Sel>MaxItem+'0')&&Sel!='D'&&Sel!='P'&&Sel!='p'&&Sel!='L'&&Sel!='l'&&Sel!='C'&&Sel!='c'&&
			Sel!='A'&&Sel!='a'&&Sel!='E'&&Sel!='e'&&Sel!='X'&&Sel!='x'&&Sel!=27)              
        {                               			/* 输入范围检查 */
            printf("\n\t\t      Press Error! Please Retry(0-%d):",MaxItem);
        }
        else
            break;
    }
    return Sel-'0';
}

/* 手工输入表达式求值 */
int Do_Press()
{
    char sel;
    int WF=0;                                       /* 文件写入开关 */
    char FN[256];                                   /* 文件名 */					
    char strExp[Max];                               /* 要计算的表达式 */    
    double strRes;                                  /* 计算结果 */	
    int TF=0;                                       /* 真假判断 */
    int strChk_Re=0;
    FILE *fp;                   					
    printf("\nWhether Save The Expression and Result to File(Y/N)?");
    scanf("%s",&sel);                               /* 保存到文件 */	
    if(sel=='Y'||sel=='y')
    {
        WF=1;                   					
        for(;;)
        {
            printf("Press a File Name for Save Log(Press \'`\' to Exit):");
            scanf("%s",FN);
            if(FN[0]=='`')                          /* 设置取消陷阱 */
        	{
                WF=0;
                printf("\nYou Cancelled Save Log Operation!\n");
                break;
        	}
            if (FileChk(FN))                        /* 文件检查 */  
            {
                fp=fopen(FN,"a");
                break;
            }
        }
    }
    for(;;)
    {
        printf("Please Press an Expression(Press a \'`\' to Exit):\n");
        scanf("%s",strExp);
        if(strExp[0]=='`') break;                   /* 设置取消陷阱 */
        strChk_Re=StrChk(strExp);
        if(strChk_Re==0)                        	/* 错误退出 */
        {
            printf("NO Activate Calculation...\n");
        }
        else if(strChk_Re==1)                       /* 表达式计算 */
        {   
            strRes=Operation(strExp);
            StrReplace(strExp,'#','-');
			if((long int)strRes==strRes)			/* 呵呵,去掉小数点 */
			{
				if(Debug) printf("...Conversion Double to Long Int.\n");
				printf("\n%s=%ld\n",strExp,(long int)strRes);
				if(WF) fprintf(fp,"%s=%ld\n",strExp,(long int)strRes);
			}
			else
			{
				printf("\n%s=%f\n",strExp,strRes);
				if(WF) fprintf(fp,"%s=%f\n",strExp,strRes);
			}
        }
        else                                        /* 表达式关系判断 */
        {
            TF=Nexus(strExp);                       /* 调用判断函数判断 */
            StrReplace(strExp,'#','-');
            printf("\n%s --> %s\n",strExp,TF_Info[TF]);
            if(WF) fprintf(fp,"%s --> %s\n",strExp,TF_Info[TF]);
        }
        getch();
    }
    if(WF) fclose(fp);
    return 0;
}

/* 文件导入表达式求值 */
int Do_File()
{
    char iFN[256];                                  /* 输入文件名 */
    char oFN[256];                                  /* 临时文件名 */
    FILE *ifp,*ofp;             	
    double strRes;                                  /* 计算结果 */
    char strExp[Max];                               /* 读取的字符串,也就是要计算的字符串 */
    int TF=0;										/* TRUE FALSE Error 索引*/
	int OutOther=0;									/* 输出到其它文件开关 */
    int strChk_Re;
    for(;;)
    {
        printf("\nPlease Press a File Name to Open(Press a \'`\' to Exit):\n");  /* 输入要打开的文件名 */
        scanf("%s",iFN);
        if(iFN[0]=='`') return 0;
        ifp=fopen(iFN,"r");             			
        if(ifp==0)                                  /* 文件存在检查 */
        {
            printf("File %s NOT Exist or Open Error!\n",iFN);
        }
        else break;
    }
	for(;;)
    {
        printf("\nPlease Press a File Name To Save(Press a \'`\' to Save to %s):\n",iFN);  /* 输入要打开的文件名 */
        scanf("%s",oFN);
        if(oFN[0]=='`')
		{
			strcpy(oFN,iFN);						/* 临时文件名,以及打开它 */
			strcat(oFN,".null");            		
			ofp=fopen(oFN,"w");
			OutOther=0;
			break;
		}
		else
		{
			if (FileChk(oFN))
			{
				ofp=fopen(oFN,"a");             			
				OutOther=1;
				break;
			}
		}
    }

    while(feof(ifp)==0)                             /* 如果没有读到文件尾 */
    {                       						
        fscanf(ifp,"%s\n",strExp);                  /* 逐行读取文件内容 */
        strChk_Re=StrChk(strExp);
        if(strChk_Re==0)                        	/* 合法性检查 */
        {                   						
            printf("Fall Across Illegal Char...\n");
            fprintf(ofp,"%s Include Illegal Char, Can't Calculate!\n",strExp);
        }
        else if(strChk_Re==1)                       /* 表达式计算 */
        {   
            strRes=Operation(strExp);               /* 计算 */
            StrReplace(strExp,'#','-');
			if((long int)strRes==strRes)			/* 呵呵,去掉小数点及其后的 '0'  */
			{
				if(Debug) printf("...Conversion Double to Long Int.\n");
				printf("\n%s=%ld\n",strExp,(long int)strRes);
				fprintf(ofp,"%s=%ld\n",strExp,(long int)strRes);
			}
			else
			{
				printf("\n%s=%f\n",strExp,strRes);      /* 屏幕打印 */
				fprintf(ofp,"%s=%f\n",strExp,strRes);   /* 打印输出到文件 */
			}
        }
        else                                        /* 表达式关系判断 */
        {       	
            TF=Nexus(strExp);
            StrReplace(strExp,'#','-');
            printf("\n%s --> %s\n",strExp,TF_Info[TF]);
            fprintf(ofp,"%s --> %s\n",strExp,TF_Info[TF]);

        }
    }
    fclose(ifp);                                    /* 关闭文件 */
    fclose(ofp);

	if(OutOther)
	{
		printf("\nFile %s Operation Completed and The Result Save to %s...\n",iFN,oFN); 
	}
	else
	{
		remove(iFN);                                /* 删除文件 */
		rename(oFN,iFN);							/* 重命名文件 int rename(const char *oldname, const char *newname); */
		printf("\nFile %s Operation Completed...\n",iFN); 
	}											
     
    getch();
    return 0;
}

/* 文件检查 0 重新输入,1 继续 */
int FileChk(char FN[])
{
    FILE *fp;
    char sel;
	int nCount=0;
	for(;FN[nCount];nCount++)						/* 文件名合法检查 '\' 不好过滤...*/
	{
		if(FN[nCount]=='/'||FN[nCount]==':'||FN[nCount]=='*'||FN[nCount]=='?'||FN[nCount]=='\"'||FN[nCount]=='<'||FN[nCount]=='>'||FN[nCount]=='|')
		{
			printf("File Name %s Include Illegal Char \'%c\',Please Press a New One!\n",FN,FN[nCount]);
			return 0;
		}
	}

    fp=fopen(FN,"r");
    if(fp==0)                               		/* 文件存在检查 */
    {
        fp=fopen(FN,"w");                       	/* 创建文件 */
        if(fp==0)                       			/* 创建检查 */
        {
            printf("File %s Create Failure!\n",FN);
            return 0;                           	/* 不可修复错误 */
        }
        fclose(fp);
        return 1;                       			/* 成功提示 */
    }
    fclose(fp);

    printf("File %s Already Exist,Rewrite or Cancel or Append(Y/N/A)?",FN); /* 文件存在了... */
    scanf("%s",&sel);
    if(sel=='Y'||sel=='y')                  		/* 覆盖 */
    {
        fp=fopen(FN,"w");                       	/* 覆盖检查 */
        if(fp==0)
        {
            printf("File %s Rewrite Failure!\n",FN);
            return 0;
        }
        fclose(fp);
        return 1;
    }
    else if(sel=='A'||sel=='a')                     /* 允许追加 */
    {
        return 1;
    }
    else                        					/* 返回... */
    {
        return 0;
    }
}
/* 关系运算 */
int Nexus(char strIn[])
{
    int nCount=0;                                   /* 循环索引 '>'=1 '<'=2 '='=4 '!'=8 */
    char strTmp;
    char strBuf[Max];
    int iNexus=0;                                   /* 符号代码 */
    double strRes1;                                 /* 分步结果 */
    double strRes2;                                 /* 符号代码 */
    for(;strIn[nCount];nCount++)
    {
        strTmp=strIn[nCount];
        if(strTmp=='>'||strTmp=='<'||strTmp=='='||strTmp=='!')
        {
            Left_f(strIn,strBuf,nCount);            /* 提取字符串 */
			if(StrChk(strBuf)!=1)					/* 二次检查 */
			{
				iNexus=0;
				break;
			}
            strRes1=Operation(strBuf);              /* 存储结果 */

            iNexus=0;                               /* 初始化符号代码 */
            if(strTmp=='>') iNexus++;               /* 符号代码调整 */
            else if(strTmp=='<') iNexus+=2;
            else if(strTmp=='=') iNexus+=4;
			else iNexus+=8;

            strTmp=strIn[nCount+1];                 /* 取下一个字符 */
            if(strTmp=='>'||strTmp=='<'||strTmp=='=')
            {                                       /* 符号代码调整 */
                if(strTmp=='>') iNexus++;       	
                else if(strTmp=='<') iNexus+=2;
                else iNexus+=4;
                nCount++;                           /* 移动索引号 */
            }
            Right_f(strIn,strBuf,strlen(strIn)-nCount-1);
			if(StrChk(strBuf)!=1)					/* 二次检查 */
			{
				iNexus=0;
				break;
			}
            strRes2=Operation(strBuf);
            break;
        }
    }
    switch(iNexus)                          		/* 关系比较 */
    {
    case 1:                                         /* > */
        if(strRes1>strRes2) return 1;
        else return 0;
    case 2:                                         /* < */
        if(strRes1<strRes2) return 1;
        else return 0;
    case 4: case 8:									/* =,== */
        if(strRes1==strRes2) return 1;
        else return 0;
    case 3: case 12:                                /* <> != */
        if(strRes1!=strRes2) return 1;
        else return 0;
    case 5:                                         /* >= */
        if(strRes1>=strRes2) return 1;
        else return 0;
    case 6:                                         /* <= */
        if(strRes1<=strRes2) return 1;
        else return 0;
    default:										/* 出错,唉,如果发现多组关系运算符就... */
        return 2;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -