Esempio n. 1
0
int calc_main()
{
    char cmd[1005];
    char history[HIST][1005];
    char einfo[20][30]={
"",
"",
"矩阵太大",
"矩阵宽度不一致",
"矩阵长度不一致",
"矩阵大小不一致",
"除数不能为0",
"底数必须为正数",
"必须为正方矩阵",
"越界错",
"矩阵长度必须为1",
"表达式出错",
"操作数必须不为矩阵",
"该矩阵无法求逆",
"变量名称错",
"变量数目过多",
"矩阵输入错误",
"单步值不能为0",
"表达式出错",
""};
    int y,x,res,i,ch,hi,oldmode;
    oldmode=uinfo.mode;
    modify_user_mode(CALC);
    for(i=0;i<HIST;i++) history[i][0]=0;
    res = get_var("res");
    set_var(vars+get_var("%pi"), Pi);
    set_var(vars+get_var("%e"), exp(1));
    clear();
    outline("欢迎使用超级计算器1.0        作者:[email protected]\n");
    outline("输入exit退出,输入help帮助\n\n");
    while(1) {
        hi=0;
        getyx(&y, &x);
        cmd[0]=0;
        do{
            UPDOWN = true;
            ch = multi_getdata(y, x, scr_cols-1, "> ", cmd, 995, 13, 0,0 );
            UPDOWN = false;
            if(ch==-KEY_UP) {
                if(hi==HIST) cmd[0]=0;
                else strncpy(cmd, history[hi], 1000);
                hi++; if(hi>HIST) hi=0;
            } else if(ch==-KEY_DOWN) {
                hi--; if(hi<0) hi=HIST;
                if(hi==HIST) cmd[0]=0;
                else strncpy(cmd, history[hi], 1000);
            }
        } while(ch<0);
        y = y-1+ch;
        if(y>=scr_lns) y = scr_lns-1;
        move(y, 0);
        outline("\n");
        if(!cmd[0]) continue;
        if(!strncasecmp(cmd, "exit", 5)) break;
        if(!strncasecmp(cmd, "quit", 5)) break;
        for(i=HIST-1;i>0;i--)
            strncpy(history[i],history[i-1],1000);
        strncpy(history[0],cmd,1000);
        if(!strncasecmp(cmd, "help", 5)||!strncasecmp(cmd, "?", 2)) {
            outline("变量: 1到6个字母,例如x=3\n");
            outline("常量: %pi, %e\n");
            outline("矩阵: [3,4;5,6] a(3:4,1:5:2) 1:5:0.5\n");
            outline("函数: sin,cos,tan,asin,acos,atan,log,exp,ln,fact,\n");
            outline("      sinh,cosh,tanh,asinh,acosh,atanh\n");
            outline("      abs,sign,sqr,sqrt,round,floor,ceil\n");
            outline("      det,inv\n");
            outline("操作: + - * / ^ '(转置) .*(矩阵乘) ./(矩阵除) \n");
            continue;
        }
        if(strchr(cmd, '=')) {
            i=strchr(cmd, '=')-cmd;
            if(i<=0||!check_var_name(cmd, i)) {
                calcerr=19;
                goto checkcalcerr;
            }
            cmd[i]=0;
            res = get_var(cmd);
            i++;
        }
        else {
            res = get_var("res");
            i=0;
        }
        eval(vars+res, cmd+i, 0, strlen(cmd+i)-1);
checkcalcerr:
        if(calcerr) {
            outline(einfo[calcerr]);
            outline("\n");
            calcerr=0;
            continue;
        }
        else
            print_var(vars+res);
    }
    for(i=0;i<vart;i++)
        del(vars+i);
    modify_user_mode(oldmode);
    return 0;
}
Esempio n. 2
0
/** Run Tests for NetCDF-4 Functions.
 *
 * @param argc argument count
 * @param argv array of arguments
 */
int
main(int argc, char **argv)
{
    int verbose = 1;
    
    /** Zero-based rank of processor. */
    int my_rank;

    /** Number of processors involved in current execution. */
    int ntasks;

    /** Specifies the flavor of netCDF output format. */
    int iotype;

    /** Different output flavors. */
    int format[NUM_NETCDF_FLAVORS] = {PIO_IOTYPE_PNETCDF, 
				      PIO_IOTYPE_NETCDF,
				      PIO_IOTYPE_NETCDF4C,
				      PIO_IOTYPE_NETCDF4P};

    /** Names for the output files. */
    char filename[NUM_NETCDF_FLAVORS][NC_MAX_NAME + 1] = {"test_names_pnetcdf.nc",
							  "test_names_classic.nc",
							  "test_names_serial4.nc",
							  "test_names_parallel4.nc"};
	
    /** Number of processors that will do IO. In this test we
     * will do IO from all processors. */
    int niotasks;

    /** Stride in the mpi rank between io tasks. Always 1 in this
     * test. */
    int ioproc_stride = 1;

    /** Number of the aggregator? Always 0 in this test. */
    int numAggregator = 0;

    /** Zero based rank of first processor to be used for I/O. */
    int ioproc_start = 0;

    /** The dimension IDs. */
    int dimids[NDIM];

    /** Array index per processing unit. */
    PIO_Offset elements_per_pe;

    /** The ID for the parallel I/O system. */
    int iosysid;

    /** The ncid of the netCDF file. */
    int ncid = 0;

    /** The ID of the netCDF varable. */
    int varid;

    /** Storage of netCDF-4 files (contiguous vs. chunked). */
    int storage;

    /** Chunksizes set in the file. */
    size_t my_chunksize[NDIM];
    
    /** The shuffle filter setting in the netCDF-4 test file. */
    int shuffle;
    
    /** Non-zero if deflate set for the variable in the netCDF-4 test file. */
    int deflate;

    /** The deflate level set for the variable in the netCDF-4 test file. */
    int deflate_level;

    /** Non-zero if fletcher32 filter is used for variable. */
    int fletcher32;

    /** Endianness of variable. */
    int endianness;

    /* Size of the file chunk cache. */
    size_t chunk_cache_size;

    /* Number of elements in file cache. */
    size_t nelems;

    /* File cache preemption. */
    float preemption;

    /* Size of the var chunk cache. */
    size_t var_cache_size;

    /* Number of elements in var cache. */
    size_t var_cache_nelems;

    /* Var cache preemption. */    
    float var_cache_preemption;
    
    /** The I/O description ID. */
    int ioid;

    /** A buffer for sample data. */
    float *buffer;

    /** A buffer for reading data back from the file. */
    int *read_buffer;

    /** The decomposition mapping. */
    PIO_Offset *compdof;

    /** Return code. */
    int ret;

    /** Index for loops. */
    int fmt, d, d1, i;
    
#ifdef TIMING    
    /* Initialize the GPTL timing library. */
    if ((ret = GPTLinitialize ()))
	return ret;
#endif    
    
    /* Initialize MPI. */
    if ((ret = MPI_Init(&argc, &argv)))
	MPIERR(ret);

    /* Learn my rank and the total number of processors. */
    if ((ret = MPI_Comm_rank(MPI_COMM_WORLD, &my_rank)))
	MPIERR(ret);
    if ((ret = MPI_Comm_size(MPI_COMM_WORLD, &ntasks)))
	MPIERR(ret);

    /* Check that a valid number of processors was specified. */
    if (!(ntasks == 1 || ntasks == 2 || ntasks == 4 ||
	  ntasks == 8 || ntasks == 16))
	fprintf(stderr, "Number of processors must be 1, 2, 4, 8, or 16!\n");
    if (verbose)
	printf("%d: ParallelIO Library example1 running on %d processors.\n",
	       my_rank, ntasks);

    /* keep things simple - 1 iotask per MPI process */    
    niotasks = ntasks; 

    /* Initialize the PIO IO system. This specifies how
     * many and which processors are involved in I/O. */
    if ((ret = PIOc_Init_Intracomm(MPI_COMM_WORLD, niotasks, ioproc_stride,
				   ioproc_start, PIO_REARR_SUBSET, &iosysid)))
	ERR(ret);

    /* Describe the decomposition. This is a 1-based array, so add 1! */
    elements_per_pe = X_DIM_LEN * Y_DIM_LEN / ntasks;
    if (!(compdof = malloc(elements_per_pe * sizeof(PIO_Offset))))
	return PIO_ENOMEM;
    for (i = 0; i < elements_per_pe; i++) {
	compdof[i] = my_rank * elements_per_pe + i + 1;
    }
	
    /* Create the PIO decomposition for this test. */
    if (verbose)
	printf("rank: %d Creating decomposition...\n", my_rank);
    if ((ret = PIOc_InitDecomp(iosysid, PIO_FLOAT, 2, &dim_len[1], (PIO_Offset)elements_per_pe,
			       compdof, &ioid, NULL, NULL, NULL)))
	ERR(ret);
    free(compdof);

    /* How many flavors will we be running for? */
    int num_flavors = 0;
    int fmtidx = 0;
#ifdef _PNETCDF
    num_flavors++;
    format[fmtidx++] = PIO_IOTYPE_PNETCDF;
#endif
#ifdef _NETCDF
    num_flavors++;
    format[fmtidx++] = PIO_IOTYPE_NETCDF;
#endif
#ifdef _NETCDF4
    num_flavors += 2;
    format[fmtidx++] = PIO_IOTYPE_NETCDF4C;
    format[fmtidx] = PIO_IOTYPE_NETCDF4P;
#endif
    
    /* Use PIO to create the example file in each of the four
     * available ways. */
    for (fmt = 0; fmt < num_flavors; fmt++) 
    {
	/* Create the netCDF output file. */
	if (verbose)
	    printf("rank: %d Creating sample file %s with format %d...\n",
		   my_rank, filename[fmt], format[fmt]);
	if ((ret = PIOc_createfile(iosysid, &ncid, &(format[fmt]), filename[fmt],
				   PIO_CLOBBER)))
	    ERR(ret);
	
	/* Define netCDF dimensions and variable. */
	if (verbose)
	    printf("rank: %d Defining netCDF metadata...\n", my_rank);
	for (d = 0; d < NDIM; d++) {
	    if (verbose)
		printf("rank: %d Defining netCDF dimension %s, length %d\n", my_rank,
		       dim_name[d], dim_len[d]);
	    if ((ret = PIOc_def_dim(ncid, dim_name[d], (PIO_Offset)dim_len[d], &dimids[d])))
		ERR(ret);
	}

	/* Check the dimension names. */
	if ((ret = check_dim_names(my_rank, ncid, verbose)))
	    ERR(ret);

	/* Define a global attribute. */
	int att_val = 42;
	if ((ret = PIOc_put_att_int(ncid, NC_GLOBAL, ATT_NAME, NC_INT, 1, &att_val)))
	    ERR(ret);

	/* Check the attribute name. */
	if ((ret = check_att_name(my_rank, ncid, verbose)))
	    ERR(ret);

	/* Define a variable. */
	if ((ret = PIOc_def_var(ncid, VAR_NAME, PIO_FLOAT, NDIM, dimids, &varid)))
	    ERR(ret);

	/* Check the variable name. */
	if ((ret = check_var_name(my_rank, ncid, verbose)))
	    ERR(ret);

	if ((ret = PIOc_enddef(ncid)))
	    ERR(ret);

	/* Close the netCDF file. */
	if (verbose)
	    printf("rank: %d Closing the sample data file...\n", my_rank);
	if ((ret = PIOc_closefile(ncid)))
	    ERR(ret);

	/* Put a barrier here to make verbose output look better. */
	if ((ret = MPI_Barrier(MPI_COMM_WORLD)))
	    MPIERR(ret);

    }
	
    /* Free the PIO decomposition. */
    if (verbose)
	printf("rank: %d Freeing PIO decomposition...\n", my_rank);
    if ((ret = PIOc_freedecomp(iosysid, ioid)))
	ERR(ret);
	
    /* Finalize the IO system. */
    if (verbose)
	printf("rank: %d Freeing PIO resources...\n", my_rank);
    if ((ret = PIOc_finalize(iosysid)))
	ERR(ret);

    /* Finalize the MPI library. */
    MPI_Finalize();

#ifdef TIMING    
    /* Finalize the GPTL timing library. */
    if ((ret = GPTLfinalize ()))
	return ret;
#endif    
    

    return 0;
}
Esempio n. 3
0
void eval(struct var_struct * p, char * s, int l, int r)
{
    int i,j,n;
    char op[9]=";, +-*/^E";
    char buf[1000];
    while(s[l]==' '&&l<=r) l++;
    while(s[r]==' '&&l<=r) r--;
    makesure(l<=r,11);
    while((s[l]=='('&&s[r]==')'&&get_rl(s,r,l)==l)||
        (s[l]=='['&&s[r]==']'&&get_rl3(s,r,l)==l)) {
        l++; r--;
        while(s[l]==' '&&l<=r) l++;
        while(s[r]==' '&&l<=r) r--;
    }
    if(check_var_name(s+l, r-l+1)||(s[l]=='%'&&check_var_name(s+l+1, r-l))) {
        strncpy(buf, s+l, 1000);
        buf[r-l+1]=0;
        i=get_var(buf);
        copy_var(vars+i, p);
        return;
    }
    if(check_var_double(s+l, r-l+1)) {
        double f;
        strncpy(buf, s+l, 1000);
        buf[r-l+1]=0;
        f = atof(buf);
        set_var(p, f);
        return;
    }
    if(check_var_array(s+l, r-l+1)) {
        double f1,f2,f3;
        strncpy(buf, s+l, 1000);
        buf[r-l+1]=0;
        i=check_var_array(s+l,r-l+1);
        if(buf[0]==':'&&!buf[1]) {
            p->p=0;
            return;
        }
        else if(i==1) {
            sscanf(buf, "%lf:%lf", &f1, &f2);
            if(f1<f2) f3=1; else f3=-1;
        }
        else {
            sscanf(buf, "%lf:%lf:%lf", &f1, &f2, &f3);
            if(fabs(f3)<MINIMUM) {
                calcerr=17;
                return;
            }
            if((f2-f1)/f3<0) f3=-f3;
        }
        if(f3>0)
            makesize(p, 1, (int)((f2-f1+MINIMUM)/f3)+1);
        else
            makesize(p, 1, (int)((f2-f1-MINIMUM)/f3)+1);
        makesure(1,0);
        i=0;
        do{
            p->p[0][i]=f1;
            f1+=f3;
            i++;
            if(i>p->width) break;
        }while((f1-MINIMUM<=f2&&f3>0)||(f1+MINIMUM>=f2&&f3<0));
        return;
    }
    i=l;
    while(isalpha(s[i])&&i<=r) i++;
    if(i>l&&s[i]=='('&&s[r]==')'&&get_rl(s,r,l)==i) {
        struct var_struct u,v;
        u.p=0; v.p=0;
        strncpy(buf, s+l, 1000);
        buf[i-l]=0;
        j=0;
        while(funcname[j][0]) {
            if(!strncasecmp(funcname[j],buf,10)) break;
            j++;
        }
        if(funcname[j][0]) {
            eval(&u, s, i+1, r-1);
            makesure(1,0);
            take_func(p, &u, j);
            del(&u);
        }
        else {
            int k;
            char* kk;
            j=get_var(buf);
            strncpy(buf, s+i+1, 1000);
            buf[r-1-i]=0;
            kk=strchr(buf, ',');
            if(!kk) {
                calcerr=18;
                return;
            }
            k=kk-buf;
            eval(&u, buf, 0, k-1);
            makesure(1,0);
            eval(&v, buf, k+1, strlen(buf)-1);
            makesure(1,0);
            selmatrix(vars+j, &u, &v, p);
            del(&u);
            del(&v);
        }
        return ;
    }
    for(j=0;j<9;j++) {
        n=r;
        do{
            if(toupper(s[n])==op[j]) {
                struct var_struct m1,m2,m3;
                m1.p=0; m2.p=0; m3.p=0;
                if(n>l&&s[n-1]=='.'&&(j==4||j==5||j==6)) eval(&m1,s,l,n-2);
                else eval(&m1,s,l,n-1);
                makesure(1,0);
                eval(&m2,s,n+1,r);
                makesure(1,0);
                switch(j) {
                    case 0:
                        link_h(&m1, &m2, p);
                        break;
                    case 1:
                    case 2:
                        link_w(&m1, &m2, p);
                        break;
                    case 3:
                        add_var(&m1, &m2, p);
                        break;
                    case 4:
                        sub_var(&m1, &m2, p);
                        break;
                    case 5:
                        if(n>l&&s[n-1]=='.') domatrix(&m1,&m2,p,4);
                        else mul_var(&m1, &m2, p);
                        break;
                    case 6:
                        if(n>l&&s[n-1]=='.') domatrix(&m1,&m2,p,5);
                        else {
                            inverse(&m3, &m2);
                            mul_var(&m1, &m3, p);
                            del(&m3);
                        }
                        break;
                    case 7:
                        if(n>l&&s[n-1]=='.') domatrix(&m1,&m2,p,6);
                        else {
                            makesure(is_single_var(&m1)&&is_single_var(&m2),12);
                            makesure((**(m1.p))>0,7);
                            set_var(p, exp(log(**(m1.p))*(**(m2.p))));
                        }
                        break;
                   case 8:
                        makesure(is_single_var(&m1)&&is_single_var(&m2),12);
                        set_var(p, exp(log(10)*(**(m2.p)))*(**m1.p));
                        break;
                }
                del(&m1);
                del(&m2);
                return;
            }
            if(s[n]==')') n=get_rl(s,n,l);
            if(s[n]==']') n=get_rl3(s,n,l);
            n--;
        }while(n>=l);
    }
    if(s[r]=='\'') {
        struct var_struct m;
        m.p = 0;
        eval(&m, s, l, r-1);
        makesure(1,0);
        reverse(&m,p);
        del(&m);
        return;
    }
    calcerr=18;
}