零零散散学了很多概念上的一些皮毛,这些以前在学校的时候也知悉,只不过没有时间去倒腾怎么一步一个脚印去编写代码。这就是所谓的行而上学论吧。正所谓实践才是检验真理的唯一标准,所以最重要的还是要去实现想法。现在就从一些小程序,比如计算器开始吧,这是我们平常比较常见的小应用程序。经常用,但就是不知道怎么去实现的。
1、最简易的计算器
主要是用的控制台应用程序构建项目,所以主要显示在出现的黑框框上。
这里我用的IDE是cfree,每更改一段代码,总是会弹出以下错误:Access violation at adress 004BFA14 in module "CPPIDE.exe",read of adress 0000000C.这是什么意思呢?从字面上的理解是非法访问了模块CPPIDE.exe的 004BFA14地址,所以实际读到的地址是0000000C。那怎么解决这个问题呢?再仔细查看cfree中的CPPIDE.exe,这个文件不就是cfree的可行性文件吗,晕死,难道又要重装Cfree?估计是的。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
int MoveCursor(int x,int y);
int main()
{
// float a,b;
// char c;
//MoveCursor(1,2);
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" 1 2 3 + - \n");
printf(" \n");
printf(" 4 5 6 * / \n");
printf(" \n");
printf(" 7 8 9 = \x1b \n");
printf(" \n");
printf(" 0 . End \n");
printf(" \n");
MoveCursor(1,2);//将光标设置在计算器的前面
//{
char ch;
int a,b;
printf("Enter formula:");
scanf("%d%c%d",&a,&ch,&b);
switch(ch)
{
case '+':printf("%d+%d=%d\n",a,b,a+b);break;
case '-':printf("%d-%d=%d\n",a,b,a-b);break;
case '*':printf("%d*%d=%d\n",a,b,a*b);break;
case '/':printf("%d/%d=%d\n",a,b,a/b);
}
system("pause"); //为了生成release版本停留界面
//}
}
int MoveCursor(int x,int y)
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD ord;
ord.X=x;
ord.Y=y;
SetConsoleCursorPosition(handle,ord);
return 1;
}
实际上这个简易计算器没有调用windows的API函数,就只是简单的模拟了计算器的形式,实现了加、减、乘、除等简易计算。需要注意的是,在生成release版本的时候要在程序尾添加system("pause"),主要是为了界面能够停留结果,以让我们看清楚结果。