编译原理实验源代码共10次实验.docx
- 文档编号:27642662
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:50
- 大小:26.73KB
编译原理实验源代码共10次实验.docx
《编译原理实验源代码共10次实验.docx》由会员分享,可在线阅读,更多相关《编译原理实验源代码共10次实验.docx(50页珍藏版)》请在冰豆网上搜索。
编译原理实验源代码共10次实验
一、实验1:
学生成绩分析程序
#include
#include
#definem5
#defineNULL0
typedefstructstnode
{
charid;
charname[16];
intcls[5];
floatave;
structstnode*next;
}students;
students*head;
intn;
voidaverage()//求平均成绩函数
{
inti,j;
floatsum,aver;
students*p;
printf("\n\t**********数学平均成绩***********\n");
for(i=0;i<1;i++)//i<1只求数学平均成绩;i { j=0; sum=0; p=head; while(p->next) { sum=sum+p->cls[i]; p=p->next; j++; } aver=sum/j; printf("\n\t数学平均成绩: %5.2f\n",aver); } } voidnopass()//不及格学生函数 { inti,t; students*p; p=head;//从第一个节点开始查找 printf("\n\t**********有两门或两门以上课程不及格的学生**********\n"); printf("\n\t****学号**数学**物理**英语**语文**体育**平均成绩****\n"); while(p->next)//最后一个节点无数据,不用输出 { i=0; t=0; while(i { if(p->cls[i]<60) { t++; } i++; if(t>=2)//判断是否满足两门以上 { printf("\t%7d",p->id); for(i=0;i printf("%6d",p->cls[i]); printf("%8.2f\n",p->ave); i=m;//用于跳出while语句 } } p=p->next;//查找下一个学生 } } voidgood()//优秀学生函数 { students*p; p=head; inti,j,t=0; printf("\n\t***优秀(平均成绩大于85分或全部课程80分以上)学生***\n"); printf("\n\t****学号**数学**物理**英语**语文**体育**平均成绩****\n"); while(p->next) { if(p->ave>85.0)//判断是否是大于85分 { printf("\t%7d",p->id); for(i=0;i printf("%6d",p->cls[i]); printf("%8.2f\n",p->ave); t++;//累计优秀学生个数 } else { i=0; j=0; while(i { if(p->cls[i]>80) j++; i++; } if(j==5)//符合优秀条件的输出 { printf("\t%7d",p->id); for(i=0;i printf("%6d",p->cls[i]); printf("%8.2f\n",p->ave); t++;//累计优秀学生个数 } } p=p->next; } if(t<=0)//如果不存在优秀学生时 printf("\t\t\t无成绩优秀学生! \n"); } voidmain() { students*p,*q; inti,j; floatsum; printf("\n\t\t***************欢迎进入学生成绩管理系统***************\n"); head=newstudents; q=head; for(i=0;i<3;i++)//i规定了学生人数,为了调试方便,本例取3人 { printf("\n\t请输入第%d位学生的学号,姓名(以回车分隔): ",i+1); p=q; scanf("%d\n",&p->id); scanf("%s",&p->name); j=0; printf("\t请输入第%d位学生的数学成绩: ",i+1); scanf("%d",&p->cls[j]); j++; printf("\t请输入第%d位学生的物理成绩: ",i+1); scanf("%d",&p->cls[j]); j++; printf("\t请输入第%d位学生的英语成绩: ",i+1); scanf("%d",&p->cls[j]); j++; printf("\t请输入第%d位学生的语文成绩: ",i+1); scanf("%d",&p->cls[j]); j++; printf("\t请输入第%d位学生的体育成绩: ",i+1); scanf("%d",&p->cls[j]); q=newstudents; q->next=NULL; p->next=q; } p=head; while(p->next) { sum=0; for(j=0;j sum=sum+p->cls[j]; p->ave=sum/m; p=p->next; } average(); nopass(); good(); } 二、实验2: 多项式求和-1 #include #defineMAXLEN100 typedefstruct {intdata[MAXLEN]; intlast; }SeqList; //多项式相加 voidadd_List(SeqListA,SeqListB,SeqList*C) {inti; C->last=A.last>B.last? A.last: B.last; for(i=0;i<=C->last;i++) C->data[i]=A.data[i]+B.data[i]; } //显示多项式 voidshow_list(SeqListC) {inti; for(i=C.last;i>=1;i--) if(C.data[i]) printf("\(%dx^%d\)+",C.data[i],i); printf("\(%dx^%d\)\n",C.data[0],0); } //生成一个多项式 voidcreate_list(SeqList*D) {intn,i; printf("请按多项式X的最高次数: "); scanf("%d",&n); for(intk=99;k>=0;k--) D->data[k]=0; printf("请按多项式X的次数由大到小输入系数,缺少项用0补齐\n"); for(i=n;i>=0;i--) {printf("输入X^%d项的系数: ",i); scanf("%d",&D->data[i]); } D->last=n; } voidmain()//主程序 {SeqListA,B,C; printf("生成多项式A(x): \n"); create_list(&A); printf("A(x)="); show_list(A); printf("生成多项式B(x): \n"); create_list(&B); printf("B(x)="); show_list(B); printf("多项式A(x)和B(x)的和"); add_List(A,B,&C); printf("C(x)="); show_list(C); } 三、实验3: 后缀表达式求值-1 #include #include structnode//栈结构声明 { intdata;//数据域 structnode*next;//指针域 }; typedefstructnodestacklist;//链表类型 typedefstacklist*link;//链表指针类型 linkoperand=NULL;//操作数栈指针 linkpush(linkstack,intvalue)//进栈 { linknewnode;//新结点指针 newnode=newstacklist;//分配新结点 if(! newnode) { printf("分配失败! "); returnNULL; } newnode->data=value;//创建结点的内容 newnode->next=stack; stack=newnode;//新结点成为栈的开始 returnstack; } linkpop(linkstack,int*value)//出栈 { linktop;//指向栈顶 if(stack! =NULL) { top=stack;//指向栈顶 stack=stack->next;//移动栈顶指针 *value=top->data;//取数据 deletetop;//吸收结点 returnstack;//返回栈顶指针 } else *value=-1; } intempty(linkstack)//判栈空 { if(stack! =NULL) return1; else return0; } intisoperator(charop)//判运算符 { switch(op) { case'+': case'-': case'*': return1;//是运算符,返回1 case'/': return0;//不是运算符,返回0 } } intgetvalue(intop,intoperand1,intoperand2)//计算表达式值 { switch((char)op) { case'*': return(operand1*operand2); case'/': return(operand1/operand2); case'+': return(operand1+operand2); case'-': return(operand1-operand2); } } voidmain()//主函数 { charexp[100]; intoperand1=0;//定义操作数1 intoperand2=0;//定义操作数2 intresult=0;//定义操作结果 intpos=0; printf("\t\n请输入后缀表达式: "); gets(exp);//读取表达式 printf("\t\n\n后缀表达式[%s]的计算结果是: ",exp); while(exp[pos]! ='\0'&&exp[pos]! ='\n')//分析表达式字符串 { if(isoperator(exp[pos]))//是运算符,取两个操作数 { operand=pop(operand,&operand1); operand=pop(operand,&operand2); operand=push(operand,getvalue(exp[pos],operand1,operand2));//计算结果入栈 } else operand=push(operand,exp[pos]-48);//是操作数,压入操作数栈 pos++;//移到下一个字符串位置 } operand=pop(operand,&result);//弹出结果 printf("%d\n",result);//输出 } 四、实验3: 后缀表达式求值-2 #include #defineMaxlen88 typedefstruct {chardata[Maxlen]; inttop; }opstack; typedefstruct {floatdata[Maxlen]; inttop; }stack; voidtrans(charstr[],charexp[])//求后缀表达式 {opstackop; charch; inti=0,t=0; op.top=-1; ch=str[i]; i++; while(ch! ='\0') {switch(ch) {case'(': op.top++;op.data[op.top]=ch; break; case')': while(op.data[op.top]! ='(') {exp[t]=op.data[op.top]; op.top--; t++; } op.top--; break; case'+': case'-': while(op.top! =-1&&op.data[op.top]! ='(') {exp[t]=op.data[op.top]; op.top--; t++; } op.top++; op.data[op.top]=ch; break; case'*': case'/': while(op.top=='/'||op.top=='*') {exp[t]=op.data[op.top]; op.top--; t++; } op.top++; op.data[op.top]=ch; break; case'': //输入为空格,则跳过 break; default: while(ch>='0'&&ch<='9') {exp[t]=ch; t++; ch=str[i]; i++; } i--; exp[t]=''; t++; } ch=str[i]; i++; } while(op.top! =-1) {exp[t]=op.data[op.top]; t++; op.top--; } exp[t]='\0'; } floatcompvalue(charexp[])//后缀表达式求值 {stackst; floatd; charch; intt=0; st.top=-1; ch=exp[t]; t++; while(ch! ='\0') {switch(ch) {case'+': st.data[st.top-1]=st.data[st.top-1]+st.data[st.top]; st.top--; break; case'-': st.data[st.top-1]=st.data[st.top-1]-st.data[st.top]; st.top--; break; case'*': st.data[st.top-1]=st.data[st.top-1]*st.data[st.top]; st.top--; break; case'/': if(st.data[st.top]! =0) {st.data[st.top-1]=st.data[st.top-1]/st.data[st.top]; st.top--;} else printf("\n\t表达式中有除数为零,本次计算无效! \n"); break; default: d=0; while(ch>='0'&&ch<='9') {d=10*d+ch-'0'; ch=exp[t]; t++; } st.top++; st.data[st.top]=d; } ch=exp[t]; t++; } returnst.data[st.top]; } voidmain() { charstr[Maxlen],exps[Maxlen]; printf("\n请输入一个整数算术(只包含+,-,*,/和括号)表达式: "); gets(str); printf("\n原算术表达式为: %s\n",str); trans(str,exps); printf("\n其后缀表达式为: %s\n",exps); printf("\n其运算的结果为: %g\n\n",compvalue(exps)); } 五、实验4: 循环队列的实现和运算 #include #defineMAXLEN10 typedefstruct {intdata[MAXLEN];//定义数据的类型 intfront,rear;//定义队头、队尾指针 }csequeue; csequeueq; voidIniQueue()//初始化队列 {q.front=q.rear=MAXLEN-1; } voidInQueue()//入队函数 {intx; printf("\n\t\t输入一个入队的整数数据: "); scanf("%d",&x); if(q.front==(q.rear+1)%MAXLEN) {printf("\n\t\t队满,不能入队! \n");return;} q.rear=(q.rear+1)%MAXLEN; q.data[q.rear]=x; printf("\n\t\t入队成功! \n"); } voidOutsequeue()//出队函数 {if(q.front==q.rear) {printf("\n\t\t此队列为空! ");return;}//队空不能出队 else {q.front=(q.front+1)%MAXLEN; printf("\n\t\t出队元素为: %d\n",q.data[q.front]);//输出队头元素 return; } } voidShowQueue()//显示函数 {intk=q.front; if(k==q.rear) {printf("\n\t\t此队列为空! \n");return;} printf("\n\t\t此队列元素为: "); do {k=(k+1)%MAXLEN; printf("%4d",q.data[k]); }while(k! =q.rear); printf("\n"); } intlength() {intk; k=(q.rear-q.front+MAXLEN)%MAXLEN; returnk; } voidmain()//主函数 {inti=1; intchoice; IniQueue(); while(i) { printf("\n\t\t循环队列\n"); printf("\n\t\t***********************************************"); printf("\n\t\t*1----------进队*"); printf("\n\t\t*2----------出队*"); printf("\n\t\t*3----------显示*"); printf("\n\t\t*4----------求队列长度*"); printf("\n\t\t*0----------返回*"); printf("\n\t\t***********************************************"); printf("\n\n\t\t请选择菜单号: "); scanf("%d",&choice); switch(choice) { case1: InQueue();break; case2: Outsequeue();break; case3: ShowQueue();break; case4: printf("\n\t\t队列长度为: %d\n",length());break; case0: i=0;break; } } } 六、实验5字符串分割处理 #include #include #include voidsplit1()//空格分割法 { chars[1000]; charline[255][255]; printf("请输入一组字符串: "); gets(s); inti=0,n=0,k; do {k=0;while(s[n]=='')n++; for(;s[n]! ='\0'&&s[n]! =''&&s[n]! ='\n';n++,k++) line[i][k]=s[n]; line[i][k]='\0'; i++; }while(s[n]! ='\0'); printf("分割后的字符串数组: \n"); intj; for(j=0;j puts(line[j]); system("pause"); } intoperators(charop) { switch(op) { case'+': case'-': case'*': case'/': case'=': case'.': return1;//是运算符
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 实验 源代码 10