原创 Labwindows/cvi8.5 实现了计算器的基本功能

2010-6-12 13:59 4065 6 6 分类: 软件与OS

//*********************************************************
// Labwindows/cvi8.5 实现了计算器的基本功能
// 使用 Edit---Menu Bar...直接创建菜单
// 取消了语音的功能
// szlihongtao
// 2010-06-12
// 参照 王建新的<Labwindows/cvi测试技术及工程应用>的光盘
// 稍有改写
//*********************************************************
#include <utility.h>
#include <formatio.h>
#include <ansi_c.h>      
#include <cvirte.h>
#include <userint.h>
#include "toolbox.h"
#include "calc.h"
//*********************************************************
static int panelHandle,aboutpanelhandle;
static int menubarhandle;


static char str1[50];     // 参数1的字符串
static char str2[50];     // 参数2的字符串
static double strvalue1; // 参数1的值
static double strvalue2; // 参数2的值
static double strvalue3; // 运算结果
static int flag;   // 加减乘除的标志
//*********************************************************
void setvalue (void);
//*********************************************************
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "calc.uir", PANEL)) < 0)
  return -1;
 DisplayPanel (panelHandle);


 menubarhandle = GetPanelMenuBar (panelHandle);   // 菜单栏的句柄
 //SmartRead_InitialAuth (panelHandle, -1, -1, -1, "", "","", "");


 RunUserInterface ();
 DiscardPanel (panelHandle);
 return 0;
}
//*********************************************************
// 面板的函数
//*********************************************************
int CVICALLBACK panelcb (int panel, int event, void *callbackData,
  int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_RIGHT_CLICK:   // 弹出式的菜单
   RunPopupMenu (menubarhandle, MENUBAR_EDIT, panelHandle, eventData1, eventData2, 0, 0, 0, 0);
   break;
  case EVENT_CLOSE:
   //SmartRead_Close();
   QuitUserInterface (0);
   break;
 }
 return 0;
}
//*********************************************************
// 清除显示
//*********************************************************
int CVICALLBACK clear (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
  {
   str1[0]='\0';
   str2[0]='\0';
   strvalue1=0;
   strvalue2=0;
   strvalue3=0;
   flag=0;
   SetCtrlVal (panelHandle, PANEL_STRING, "");
   //SmartRead_Speak("清零", 11, 0, -1, -1, 1, "");
   break;
  }
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_7 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
  {   // 读取控件的标题显示值
   GetCtrlAttribute (panelHandle, PANEL_CMD_7, ATTR_LABEL_TEXT, labeltext);
   // labeltext[0]=55='7';  labeltext[0]=0='\0';


   //SmartRead_Speak("7", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
  }
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_8 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_8, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("8", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_9 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_9, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("9", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_4 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_4, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("4", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;        
}
//*********************************************************
int CVICALLBACK cmd_5 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_5, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("5", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_6 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_6, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("6", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_1 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_1, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("1", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_2 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_2, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("2", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_3 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_3, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("3", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_0 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_0, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak("0", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_dot (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char labeltext[2];
 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlAttribute (panelHandle, PANEL_CMD_DOT, ATTR_LABEL_TEXT, labeltext);
   //SmartRead_Speak(".", 11, 0, -1, -1, 1, "");
   strcat (str1, labeltext);
   setvalue ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_div (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   //SmartRead_Speak("除以", 11, 0, -1, -1, 1, "");
   GetCtrlVal (panelHandle, PANEL_STRING, str1);
   Fmt (&strvalue1, "%f<%s", str1);
      str1[0]='\0';
   SetCtrlVal (panelHandle, PANEL_STRING, "");
   flag=4;     // 4表示触发
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_mul (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   //SmartRead_Speak("乘以", 11, 0, -1, -1, 1, "");
   GetCtrlVal (panelHandle, PANEL_STRING, str1);
   Fmt (&strvalue1, "%f<%s", str1);
      str1[0]='\0';
   SetCtrlVal (panelHandle, PANEL_STRING, "");
   flag=3;    // 3表示乘法
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_minus (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   //SmartRead_Speak("减去", 11, 0, -1, -1, 1, "");
   GetCtrlVal (panelHandle, PANEL_STRING, str1);
   Fmt (&strvalue1, "%f<%s", str1);
      str1[0]='\0';
   SetCtrlVal (panelHandle, PANEL_STRING, "");
   flag=2;     // 2表示减法
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_plus (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
  {
   //SmartRead_Speak("加上", 11, 0, -1, -1, 1, "");
   GetCtrlVal (panelHandle, PANEL_STRING, str1);
   Fmt (&strvalue1, "%f<%s", str1);
      str1[0]='\0';
   SetCtrlVal (panelHandle, PANEL_STRING, "");
   flag=1;    // 1表示加法
   break;
  }
 }
 return 0;
}
//*********************************************************
 int CVICALLBACK cmd_sqrt (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   //SmartRead_Speak("开方等于", 11, 0, -1, -1, 1, "");
   Delay (1.5);
   GetCtrlVal (panelHandle, PANEL_STRING, str1);
   Fmt (&strvalue1, "%f<%s", str1);
      str1[0]='\0';
   SetCtrlVal (panelHandle, PANEL_STRING, "");
   strvalue2=sqrt(strvalue1);
   Fmt (str2, "%s<%f", strvalue2);
   SetCtrlVal (panelHandle, PANEL_STRING, str2);
   //SmartRead_Speak(str2, 11, 0, -1, -1, 1, "");
   str2[0]='\0';
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_percent (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   //SmartRead_Speak("等于", 11, 0, -1, -1, 1, "");
   Delay (1);
   GetCtrlVal (panelHandle, PANEL_STRING, str1);
   Fmt (&strvalue1, "%f<%s", str1);
      str1[0]='\0';
   SetCtrlVal (panelHandle, PANEL_STRING, "");
   strvalue2=100*(strvalue1);
   Fmt (str2, "%s<%f", strvalue2);
   strcat (str2, "%");
   SetCtrlVal (panelHandle, PANEL_STRING, str2);
   //SmartRead_Speak(str2, 11, 0, -1, -1, 1, "");
   str2[0]='\0';
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_daoshu (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   //SmartRead_Speak("倒数等于", 11, 0, -1, -1, 1, "");
   Delay (1.5);
   GetCtrlVal (panelHandle, PANEL_STRING, str1);
   Fmt (&strvalue1, "%f<%s", str1);
      str1[0]='\0';
   SetCtrlVal (panelHandle, PANEL_STRING, "");
   strvalue2=1/(strvalue1);
   Fmt (str2, "%s<%f", strvalue2);
   SetCtrlVal (panelHandle, PANEL_STRING, str2);
   //SmartRead_Speak(str2, 11, 0, -1, -1, 1, "");
   str2[0]='\0';
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK cmd_equal (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   //SmartRead_Speak("等于", 11, 0, -1, -1, 1, "");


   GetCtrlVal (panelHandle, PANEL_STRING, str1);
   Fmt (&strvalue2, "%f<%s", str1);
   //SetCtrlVal (panelHandle, PANEL_STRING, "");
   switch (flag)
   {
    case 1:
    {
     strvalue3=strvalue1+strvalue2;
     break;
    }
    case 2:
    {
     strvalue3=strvalue1-strvalue2;
     break;
    }
    case 3:
    {
     strvalue3=strvalue1*strvalue2;
     break;
    }
    case 4:
    {
     strvalue3=strvalue1/strvalue2;
     break;
    }
    default:
     break;
   }
   Fmt (str2, "%f", strvalue3);
   SetCtrlVal (panelHandle, PANEL_STRING, str2);
   //SmartRead_Speak(str2, 11, 0, -1, -1, 1, "");
      str1[0]='\0';
   str2[0]='\0';
   break;
 }
 return 0;
}
//*********************************************************
void CVICALLBACK editcopy (int menuBar, int menuItem, void *callbackData,
  int panel)
{
    char strings[50];
 GetCtrlVal (panelHandle, PANEL_STRING, strings);
/*
Copies a NULL terminated text string onto the system clipboard.
将字符串拷贝进入系统的剪贴板
*/ 
 ClipboardPutText (strings);    
}
//*********************************************************
void CVICALLBACK editexit (int menuBar, int menuItem, void *callbackData,
  int panel)
{
 QuitUserInterface (0);
}
//*********************************************************
// 界面总是显示在最前面
//*********************************************************
void CVICALLBACK editalwaystop (int menuBar, int menuItem, void *callbackData,
  int panel)
{
 static int topflag;   // 显示在最前方
 int val_float;
 
 if (topflag)
 {
  val_float=VAL_FLOAT_NEVER;
  topflag=0;
 }
 else     
 {
  val_float=VAL_FLOAT_ALWAYS;
  topflag=1;
 }    
 // Specifies whether the panel's window floats above all non-floating windows.   
 SetPanelAttribute (panelHandle, ATTR_FLOATING,val_float );
 
 // 设定菜单项前面是否打勾
 SetMenuBarAttribute (menubarhandle, MENUBAR_EDIT_EDITALWATYSTOP, ATTR_CHECKED, topflag);
}
//*********************************************************
void CVICALLBACK helpabout (int menuBar, int menuItem, void *callbackData,
  int panel)
{
 unsigned long availablevirtual;
 unsigned long availablepagefile;
 unsigned long availablephysical;
 unsigned long totalvirtal;
 unsigned long totalpagefile;
 unsigned long totalphysical;
 unsigned long percentload;
 char tp[50];


 aboutpanelhandle = LoadPanel (panelHandle, "calc.uir", PANEL_ABOU);
 InstallPopup (aboutpanelhandle);
 // 获得系统的内存状态
 GetMemoryInfo (&percentload, &totalphysical, &totalpagefile, &totalvirtal, &availablephysical, &availablepagefile, &availablevirtual);


 totalphysical=totalphysical/1000000;
 Fmt (tp, "%s<%d MB", totalphysical);  // or Fmt (tp, "%d MB", totalphysical);
/* 
You may omit the target specifier, in which case the target is assumed to be %s (string).
当目标为字符串的时候,可以忽略 %s<  这个描述符
Fmt (tp, "%d MB", totalphysical);  


%d Indicates that the source or target is an integer.
%s Indicates that the source or target is a string. Numeric values may be converted into ASCII form using the %s target specifier.
*/   


 SetCtrlVal (aboutpanelhandle, PANEL_ABOU_TEXTMSG_MEMO, tp);
}
//*********************************************************
void setvalue (void)
{
 SetCtrlVal (panelHandle, PANEL_STRING, "");
 SetCtrlVal (panelHandle, PANEL_STRING, str1);
}
//*********************************************************
int CVICALLBACK ok (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   DiscardPanel (aboutpanelhandle);
   break;
 }
 return 0;
}
//*********************************************************
//*********************************************************
//*********************************************************

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
6
关闭 站长推荐上一条 /3 下一条