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

📄 viprintf.cpp

📁 此为VISA数据读取转换程序
💻 CPP
字号:
#include "STDARG.H"
#include "string.h"
#include "iostream.h"
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>


void viPrintf(char * vi,char *Fmt,...) 
{
	
    char* buf=new char;
    *buf='1';
	//strcat(buf,"##");
	char tempForm[20];
	tempForm[0]='%';
	char Form[50];
	//unsigned char k[4];
	int count =1;
	//unsigned long j;
	//int *i,f;
	va_list arg_ptr; 
	va_start(arg_ptr,Fmt);
    char rulesCH[20]="  ";
	char tempBuff[20];
	int r=0;
	while(((*Fmt) !='\n') && ((*Fmt) != ' ') && ((*Fmt)!='\0'))
	{
		rulesCH[r]=*Fmt;
		Fmt++;
		r++;
	}
	switch(*(Fmt))
	{
	case '\n':
		{
			count++;
		    rulesCH[r]='\n';
		    break;
		}
	case ' ':
		{
			Fmt++;
			rulesCH[r]=' ';
			switch(*Fmt)
			{
			case '%':
				{
					//数据转换
					int i=0;
					strcpy(Form,Fmt);
					while((Form[i]!='\0') && (Form[i] !='\n'))
					{
						int len=1;
                        i++;
						while((Form[i]!='%') && (Form[i]!='\0'))
						{
							tempForm[0]='%';
							tempForm[len]=Form[i];
							len++;
							i++;
						}
						/*if(Form[i]=='%')
							i++;*/
  						tempForm[len]='\0';
						if(tempForm[1]==',')
						{
							//作数组处理
							//int data_count=0;
							char *Eptr;
							Eptr = strchr(tempForm, '*');
							if(Eptr)
							{
							
								char tempFormA[]="%";
								tempForm[len-1]=',';
								strcat(tempFormA,&tempForm[3]);
								char *arrayptr=arg_ptr;
								arg_ptr+=sizeof(int);
								int arrayDCnt=*arg_ptr;
								count+=arrayDCnt;
								int t=0;
								for(t=0;t<arrayDCnt;t++)
								{		
									if(tempForm[len-2]=='d')
									{
										int *p=(int *)(*(int *)arrayptr);
										vsprintf(tempBuff,tempFormA,(char *)(p+t));
									}
									else
									{
										double *p=(double *)(*(int *)arrayptr);
										vsprintf(tempBuff,tempFormA,(char *)(p+t));
									}
									strcat(buf,tempBuff);
								}
							}
							else
							{
								char data_countCH[3];
								int l=0;
								
								while((tempForm[l+2]>='0') && (tempForm[l+2])<='9')
								{
									data_countCH[l]=tempForm[l+2];
									l++;
								}
								data_countCH[l]='\0';
								char tempFormArray[2]="%";
								tempForm[len-1]=',';
								strcat(tempFormArray,&tempForm[l+2]);
								int data_count=atoi(data_countCH);
								count+=data_count;
								for(int j=0;j<data_count;j++)
								{
									
									//char temp[2];
									//itoa(*(p+j),temp,10);
									if(tempForm[len-2]=='d')
									{
										int *p=(int *)(*(int *)arg_ptr);
										vsprintf(tempBuff,tempFormArray,(char *)(p+j));
									}
									else
									{
										double *p=(double *)(*(int *)arg_ptr);
										vsprintf(tempBuff,tempFormArray,(char *)(p+j));
									}
								
									strcat(buf,tempBuff);
								}
							}
							arg_ptr+=sizeof(int);
							
						}
						else
						{
							//普通数处理
							switch(tempForm[len-2])
							{
							case 'd':
								{
									count++;
									char *Eptr;
									Eptr = strchr(tempForm, '*');
									if(Eptr)
									{
										//宽度由参数确定
										char *dataCH=arg_ptr;
										arg_ptr+=sizeof(int);
										int fieldD=*(arg_ptr);
										char tempstr[10];
										itoa(fieldD, tempstr, 10);
										char tempFormDField[2];
										tempFormDField[0]='%';
										tempFormDField[1]='\0';
										//tempFormDField[1]=
										strcat(tempFormDField,tempstr);
										strcat(tempFormDField,&tempForm[2]);
										vsprintf(tempBuff,tempFormDField,dataCH);
										strcat(buf,tempBuff);
										arg_ptr+=sizeof(int);

									}
									else
									{
										vsprintf(tempBuff,tempForm,arg_ptr);
										strcat(buf,tempBuff);
										arg_ptr+=sizeof(int);
									}
									break;
								}
							case 'f':
								{
									count++;
									char *Eptr;
									Eptr = strchr(tempForm, '*');
									if(Eptr)
									{
										//精度由参数确定
										char *dataCH=arg_ptr;
										arg_ptr+=sizeof(double);
										int fieldD=*(arg_ptr);
										char tempstr[10];
										itoa(fieldD, tempstr, 10);
										char tempFormDField[3];
										tempFormDField[0]='%';
										tempFormDField[1]='.';
										tempFormDField[2]='\0';
										//tempFormDField[1]=
										strcat(tempFormDField,tempstr);
										strcat(tempFormDField,&tempForm[3]);
										vsprintf(tempBuff,tempFormDField,dataCH);
										strcat(buf,tempBuff);
										arg_ptr+=sizeof(int);
									}
									else
									{
										count++;
										vsprintf(tempBuff,tempForm,arg_ptr);
										strcat(buf,tempBuff);
										arg_ptr+=sizeof(double);
									}
									break;
								}
							case 'c':
								{
									count++;
									vsprintf(tempBuff,tempForm,arg_ptr);
									strcat(buf,tempBuff);
									arg_ptr+=sizeof(char *);
									break;

								}
							case 's':
								{
									char tempFormS[10];
									count++;
									char *Eptr;
									Eptr = strchr(tempForm, '*');
									if(Eptr)
									{
										char *dataCH=arg_ptr;
										arg_ptr+=sizeof(char *);
										int strCount=*arg_ptr;
										vsprintf(tempBuff,"%s,\n",dataCH);
										memcpy(tempFormS, tempBuff, strCount);
										tempFormS[strCount]='\0';
										strcat(buf,tempFormS);
										arg_ptr+=sizeof(int);
									}
									else
									{
										if((tempForm[1]>='0')&&(tempForm[1]<='9'))
										{
											char charcount[3];//此处的3表示最多可以指定3位数的字符个数
											for(int j=0;j<len-3;j++)
											{
												charcount[j]=tempForm[j+1];
											}
											int charcnt=atoi(charcount);
											vsprintf(tempBuff,tempForm,(char *)arg_ptr);
											memcpy(tempFormS, tempBuff, charcnt);
											tempFormS[charcnt-1]='\0';
											strcat(buf,tempFormS);
											strcat(buf,",");
											arg_ptr+=sizeof(char *);
											
										}
										else
										{
											vsprintf(tempBuff,tempForm,(char *)arg_ptr);
											strcat(buf,tempBuff);
											arg_ptr+=sizeof(char *);
										}
									}
									break;
									
								}
							default:
								{
									//尚未实现增强型
									printf("the function has not implatation!");
								}
							}
						}
					}
					break;
					
				}
			default:
				{
					strcat(buf,Fmt);
					//直接复制
					strcat(rulesCH," ");

				}
			}

		}
	
	}

	//char countCH[2]=atoi(count);
	//buf[1]=count;
	va_end(arg_ptr);

	
}

int main(int argc, char* argv[])
{
	int ar[]={6,4,5,6,7};
	double dr[]={2.4,3.5,6.5,8.6};
	char *s="i come here!";
	char ch='r';
	int m=16;
	int d=3;
	float i=(float)90.8934;
	//sizeof(ar);
	//viPrintf("vi","RST* \n");
	//viPrintf("vi","RST* 50\n");
	viPrintf("vi","MEAS:VOLT:AC? %4s,%c,%Lf\n",s,ch,i);
	//viPrintf("vi","MEAS:VOLT:AC? %3s\n",s);
	//viPrintf("vi","MEAS:VOLT:AC? %,*hd\n",ar,d);
	//viPrintf("vi","MEAS:VOLT:AC? %,*f\n",dr,d);
	//viPrintf("vi","MEAS:VOLT:AC? %,5d\n",ar);
    //viPrintf("vi","MEAS:VOLT:AC? %,4f\n",dr);
	/*printf("%s",buf);
    printf("%s",rulesCH);*/
	return 0;
}

⌨️ 快捷键说明

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