FILE *fp;//将生成的一千道题写入该文件
int produce(int n);//生成一系列随机数,代表一个表达式里的数字和运算符、括号,存在int数组里该函数核心代码:
sum = rand() %7+4;//运算符数量 至少4个sum += sum +1;for(j =0; j < sum; j++){if(j%2==0) a[j]= rand()%50+1;elsea[j]= -1* (rand()%4+1); }if(n ==1){ mid= (rand()%4)*2+1; a[mid]= -5; }
a[]存放数字,每个数字表示一个数或一个运算符、括号。
int trans(int sum,int n);//将int数组里的数转化为字符来表示表达式,存放在字符型数组里,以写入文件或输出到屏幕int transfor(int n);//中缀转后缀表达式,嵌套调用int isp(int a)和int icp(int a);该函数核心代码:
while(i <n){if(a[i] >=0){//如果是数字放入b数组b[kb++] =a[i]; i++; }else{ g= stack[bottom-1];if(isp(g) <icp(a[i])){ stack[bottom]=a[i]; bottom++; i++; }elseif(isp(g) >icp(a[i])){ bottom--; b[kb++] =stack[bottom]; }else{ bottom--;if(stack[bottom] == -6)//-6为(i++; } } }
b[]存放后缀表达式double solve(int n);//解后缀表达式该函数核心代码:
for(i =0; i < n; i++){if(b[i] >=0) stack[top++] = (double)b[i];else{ y= stack[--top]; x= stack[--top];switch(b[i]){case-1:x = x + y;break;case-2:x = x - y;break;case-3:x = x * y;break;case-4:x = x / y;break;case-5:x = pow(x,y);break; } stack[top++] =x; } }
int produce(int n);//生成一系列随机数,代表一个表达式里的数字和运算符、括号,存在int数组里该函数核心代码:
sum = rand() %7+4;//运算符数量 至少4个sum += sum +1;for(j =0; j < sum; j++){if(j%2==0) a[j]= rand()%50+1;elsea[j]= -1* (rand()%4+1); }if(n ==1){ mid= (rand()%4)*2+1; a[mid]= -5; }
a[]存放数字,每个数字表示一个数或一个运算符、括号。
int trans(int sum,int n);//将int数组里的数转化为字符来表示表达式,存放在字符型数组里,以写入文件或输出到屏幕int transfor(int n);//中缀转后缀表达式,嵌套调用int isp(int a)和int icp(int a);该函数核心代码:
while(i <n){if(a[i] >=0){//如果是数字放入b数组b[kb++] =a[i]; i++; }else{ g= stack[bottom-1];if(isp(g) <icp(a[i])){ stack[bottom]=a[i]; bottom++; i++; }elseif(isp(g) >icp(a[i])){ bottom--; b[kb++] =stack[bottom]; }else{ bottom--;if(stack[bottom] == -6)//-6为(i++; } } }
b[]存放后缀表达式double solve(int n);//解后缀表达式该函数核心代码:
for(i =0; i < n; i++){if(b[i] >=0) stack[top++] = (double)b[i];else{ y= stack[--top]; x= stack[--top];switch(b[i]){case-1:x = x + y;break;case-2:x = x - y;break;case-3:x = x * y;break;case-4:x = x / y;break;case-5:x = pow(x,y);break; } stack[top++] =x; } }
心得:我在结对项目中学到了如何把显示的部分放在文件中去表达,以及中缀后缀表达式的相关问题。