📄 public.c
字号:
#include"public.h"
#include"db.h"
/**********************************************************************
功能:定长接收字符串
参数:窗口指针 字符串 最大长度 模式
返回值:详见后面注明
***********************************************************************/
int GetString( WINDOW *win,char str[],int maxLen,int type)
{
int ch,i,len,k=0,m=0,n=0,check=2;
int row,col;
int cur_row,cur_col;
len=strlen(str);
touchwin( win );
wrefresh( win );
getyx( win,row,col );
i = col;
col = 0;
noecho();
cbreak(); //开启预处理模式
keypad( win,true ); //开启键盘响应
while(1)
{
ch = wgetch( win );
for(n=2;n<=9;)//判断功能键 f2-f9 相应返回值3-10;
{
n++;
if(ch==KEY_F(n-1))
{
return n;
}
}
if( ( i>=maxLen || len>=maxLen ) && ch!=KEY_BACKSPACE && ch!=10 && ch!=KEY_LEFT && ch!=9&& ch!=27
&& ch!=KEY_RIGHT && ch!=KEY_UP && ch!=KEY_DOWN && ch!=KEY_NPAGE && ch!=KEY_PPAGE )//控制长度
continue;
else if ( ch == KEY_UP ) //向上键
{
str[len] = '\0';
return 1 ;
}
else if ( ch == KEY_DOWN ) //向下键
{
str[len] = '\0';
return 2 ;
}
else if ( ch ==338 ) //下页键
{
str[len]='\0';
return 11;
}
else if ( ch == 339 ) //上页键
{
str[len]='\0';
return 12;
}
else if(ch ==9)//TAB
{
str[len]='\0';
return 13;
}
else if(ch==27)//ESC
{
str[len]='\0';
return 14;
}
else if(ch==262)//Home
{
str[len]='\0';
return 15;
}
else if(ch==360)//End
{
str[len]='\0';
return 16;
}
else if ( ch == 260 ) //向左键
{
getyx( win,cur_row,cur_col );
if ( cur_col == col )
continue;
else
{
cur_col-- ;
i-- ;
wmove( win,cur_row,cur_col );
touchwin( win );
wrefresh( win );
}
}
else if ( ch == 261 ) //向右键
{
getyx( win,cur_row,cur_col );
if ( cur_col == col+len )
continue;
else
{
cur_col++ ;
i++ ;
wmove( win,cur_row,cur_col );
touchwin( win );
wrefresh( win );
}
}
else if ( ch == 10 ) //回车键
{
str[len]='\0';
break;
}
else if ( ch ==263 ) //退格键
{
if ( len==0 || i==0 )
continue;
getyx( win,cur_row,cur_col );
if ( i<len )
{
k = i;
while(k<len)
{
str[k-1] = str[k];
k++;
}
}
else
{
wprintw( win,"\b \b");
}
str[len-1]='\0';
if ( i<len )
{
wclear( win );
if ( type==0)
{
n = strlen( str );
m = 0;
while( m<n )
{
wprintw( win,"*" ); //加密显示
m++;
}
}
else
wprintw( win,"%s",str );
wmove( win,cur_row,cur_col-1 );
}
i--;
len--;
wrefresh( win );
}
else if(isprint(ch)) //判断字符c是否为可打印字符
{
if ( i<len )//在字符串的中间插入
{
getyx( win,cur_row,cur_col );
k = len;
while(i<k)
{
str[k]=str[k-1];
k--;
}
str[i]=ch;
}
else//在字符串的末尾插入
{
str[len]=ch;
if ( type==0 )
{
wprintw( win,"*" );
}
else
wprintw( win,"%c",ch );
}
str[len+1]='\0';
if ( i<len )
{
wclear( win );
if ( type==0)
{
n = strlen( str );
m = 0;
while( m<n )
{
wprintw( win,"*" );
m++;
}
}
else
wprintw( win,"%s",str );
wmove( win,cur_row,cur_col+1 );
}
i++;
len++;
wrefresh( win );
}
}
return 0;
}
/**********************************************************************
功能:读取数据库配置文件
参数:用户名 密码
返回值:无
***********************************************************************/
void get_connect(char *sql_name,char *sql_password)
{
FILE * fp;
char temp[500];
char * str[10];
int i = 0, j = 0, k = 0;
fp = fopen("connect.txt","r");
if(fp==NULL)
{
exit(1);
}
while (fgets(temp,500,fp) != NULL )
{
if(temp[0]=='\n'|| temp[0]=='\r' || temp[0]=='#')
continue;
else
{
str[i] = (char *) malloc(sizeof(temp));
int len=strlen(temp);
if(temp[len-1]=='\n' || temp[len-2]=='\n' || temp[len-1]=='\r'|| temp[len-2]=='\r')
{
temp[len-2]='\0';
}
strcpy(str[i],temp);
i++;
}
}
for(j=0;j<i;j++)
{
TrimSpace(str[j]);
}
k=0;
while(str[0][k]!=' ')
{
k++;
}
str[0][k]='\0';
strcpy(sql_name,&str[0][k+1]);
TrimSpace(sql_name);
k=0;
while(str[1][k]!=' ')
{
k++;
}
str[1][k]='\0';
strcpy(sql_password,&str[1][k+1]);
TrimSpace(sql_password);
fclose(fp);
for(j=0;j<i;j++)
{
free(str[j]);
}
}
/**********************************************************************
功能:获取时间
参数:字符串
返回值:无
***********************************************************************/
void get_time(char *str)
{
struct tm *ptm;
time_t cur_time;
time(&cur_time);
ptm=localtime(&cur_time);
sprintf(str,"%04d%02d%02d%02d%02d%02d",ptm->tm_year+1900,ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
}
/**********************************************************************
功能:去除左右空格
参数:字符串
返回值:无
***********************************************************************/
void TrimSpace(char *str)
{
int i=0,len ;
len = strlen( str );
while(i<len)
{
if ( str[i] == ' ' )
{
i++;
continue;
}
else
break;
}
strcpy( str,str+i );
i = strlen(str)-1;
while(i>0)
{
if ( str[i] == ' ' )
{
i--;
continue;
}
else
break;
}
str[i+1] = '\0';
}
/**********************************************************************
功能:检验字符串是否定长全数字
参数:字符串 长度
返回值:0 正确 2 长度不够 1 非法字符
***********************************************************************/
int check_str(char *str,int num)
{
int i,len;
len=strlen(str);
if(len<num)
{
return 2;
}
for(i=0;i<len;i++)
{
if(isdigit(str[i]))
continue;
else
return 1;
}
return 0;
}
/**********************************************************************
功能:检验字符串是否为小数
参数:字符串
返回值:0 正确 2 (格式错误) 1 非法字符
***********************************************************************/
int check_float(char *str)
{
int len,i,count=0;
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='.'||isdigit(str[i]))
{
if(str[i]=='.')
{
count++;
}
continue;
}
else
{
return 1;//非法字符
}
}
if(count>=2)
{
return 2;//小数点超出2个(格式错误)
}
else if(count==1)
{
if(str[0]=='.')
{
return 2;//第一是小数点(格式错误)
}
if(str[len-2]=='.'||str[len-3]=='.')
{
return 0;
}
else
{
return 3;//小数点后面不止2位小数
}
}
else
{
return 0;
}
}
/**********************************************************************
* 4个比较函数 (相同返回 0,否则返回1) *
***********************************************************************/
int God_Compare( void *a,void *b )
{
if(strcmp(((GOD*)a)->bar_code,((GOD*)b)->bar_code )==0)
{
return 0;
}
else
{
return 1;
}
}
int Goods_Compare( void *a,void *b )
{
if(strcmp(((GOODS *)a)->bar_code,((GOODS *)b)->bar_code )==0)
{
return 0;
}
else
{
return 1;
}
}
int Account_Compare( void *a,void *b )
{
if(strcmp(((STAFFS *)a)->staff_id,((STAFFS *)b)->staff_id)==0)
{
return 0;
}
else
{
return 1;
}
}
int Sale_Compare( void *a,void *b )
{
if(strcmp(((SALE *)a)->sale_id,((SALE *)b)->sale_id)==0)
{
return 0;
}
else
{
return 1;
}
}
/**********************************************************************
功能:通用打印函数
参数:窗口指针 链表类指针 起始NODE指针 反白位置NODE针 模式
type:1 后台商品信息 2 后台账户信息 3 后台销售信息 0 前台收银商品信息
***********************************************************************/
void Print_Data( WINDOW *win,C_CLASS LINKLIST *link,NODE *start,NODE *cur,int type)
{
int i=0,k,num;
NODE *p;
int counts;
float money=0.0;
char str[5];
// char temp_goods[31],temp_spec[17],temp_unit[17];
if ( !start->data ) //无数据
{
return ;
}
werase( win );
for( p=start;p && i<15;p=p->next )
{
if(type==0)
{
k=link->get_index_by_key(link,p->data,(int (*)(void *,void *))God_Compare);
money=((GOD *)p->data)->sale_price;
num=((GOD *)p->data)->num;
TrimSpace(((GOD *)p->data)->goods_name);
TrimSpace(((GOD *)p->data)->spec);
TrimSpace(((GOD *)p->data)->unit);
}
if(type==1)
{
k=link->get_index_by_key(link,p->data,(int (*)(void *,void *))Goods_Compare );
TrimSpace(((GOODS *)p->data)->goods_name);
TrimSpace(((GOODS *)p->data)->spec);
TrimSpace(((GOODS *)p->data)->unit);
}
if(type==2)
{
k=link->get_index_by_key(link,p->data,(int (*)(void *,void *))Account_Compare);
TrimSpace(((STAFFS *)p->data)->staff_name);
TrimSpace(((STAFFS *)p->data)->staff_pwd);
TrimSpace(((STAFFS *)p->data)->remark);
}
if(type==3)
{
k=link->get_index_by_key(link,p->data,(int (*)(void *,void *))Sale_Compare);
TrimSpace(((SALE *)p->data)->sale_date);
if(((SALE *)p->data)->sale_state==1)
{
sprintf(str,"%s","挂单");
}
else if(((SALE *)p->data)->sale_state==0)
{
sprintf(str,"%s","售出");
}
else
{
sprintf(str,"%s","退货");
}
}
if ( p==cur )
{
if(type==0)
{
wattron( win,A_REVERSE|A_BOLD);
wprintw( win,"%-6d%-10s%-25s%-8s%-8s%-8.2f%-5d%7.2f\n",k,((GOD *)p->data)->bar_code,((GOD *)p->data)->goods_name,((GOD *)p->data)->spec,((GOD *)p->data)->unit,((GOD *)p->data)->sale_price,((GOD *)p->data)->num,money*num);
wattroff( win,A_REVERSE|A_BOLD);
}
if(type==1)
{
wattron(win,A_BLINK|COLOR_PAIR(7));
wprintw(win,"%-5d%-9s%-20s%-7s%-7s%-9.2f%-9.2f%-6d%5.2f\n",k,((GOODS *)p->data)->bar_code,((GOODS *)p->data)->goods_name,((GOODS *)p->data)->spec,((GOODS *)p->data)->unit,((GOODS *)p->data)->sale_price,((GOODS *)p->data)->purchase_price,((GOODS *)p->data)->stock,((GOODS *)p->data)->discount);
wattroff(win,A_BLINK|COLOR_PAIR(7));
}
if(type==2)
{
wattron(win,A_BLINK|COLOR_PAIR(7));
wprintw(win,"%-6d%-9s%-18s%-15s%-20s%5d\n",k,((STAFFS *)p->data)->staff_id,((STAFFS *)p->data)->staff_name,((STAFFS *)p->data)->staff_pwd,((STAFFS *)p->data)->remark,((STAFFS *)p->data)->staff_type);
wattroff(win,A_BLINK|COLOR_PAIR(7));
}
if(type==3)
{
wattron(win,A_BLINK|COLOR_PAIR(7));
wprintw(win,"%-6d%-22s%-8s%-10s%-12s%-6s%10.2f\n",k,((SALE *)p->data)->sale_id,((SALE *)p->data)->trans_id,((SALE *)p->data)->staff_id,((SALE *)p->data)->sale_date,str,((SALE *)p->data)->sale_money);
wattroff(win,A_BLINK|COLOR_PAIR(7));
}
}
else
{
if(type==0)
{
wprintw( win,"%-6d%-10s%-25s%-8s%-8s%-8.2f%-5d%7.2f\n",k,((GOD *)p->data)->bar_code,((GOD *)p->data)->goods_name,((GOD *)p->data)->spec,((GOD *)p->data)->unit,((GOD *)p->data)->sale_price,((GOD *)p->data)->num,money*num);
}
if(type==1)
{
wprintw(win,"%-5d%-9s%-20s%-7s%-7s%-9.2f%-9.2f%-6d%5.2f\n",k,((GOODS *)p->data)->bar_code,((GOODS *)p->data)->goods_name,((GOODS *)p->data)->spec,((GOODS *)p->data)->unit,((GOODS *)p->data)->sale_price,((GOODS *)p->data)->purchase_price,((GOODS *)p->data)->stock,((GOODS *)p->data)->discount);
}
if(type==2)
{
wprintw(win,"%-6d%-9s%-18s%-15s%-20s%5d\n",k,((STAFFS *)p->data)->staff_id,((STAFFS *)p->data)->staff_name,((STAFFS *)p->data)->staff_pwd,((STAFFS *)p->data)->remark,((STAFFS *)p->data)->staff_type);
}
if(type==3)
{
wprintw(win,"%-6d%-22s%-8s%-10s%-12s%-6s%10.2f\n",k,((SALE *)p->data)->sale_id,((SALE *)p->data)->trans_id,((SALE *)p->data)->staff_id,((SALE *)p->data)->sale_date,str,((SALE *)p->data)->sale_money);
}
}
// k++;
i++;
}
wrefresh( win );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -