主题:请教一下大虾,这个程序哪里错了
题目:
顺序表的合并、归并;链表的合并、归并;4*4二维数组对角求积;猴子数大王;二叉树的建立及求结点数的递归算法.
任务:请给以上若干程序编写用户界面程序,使其能用菜单选择相应内容的运行,并保证正确的输出
用C语言实现
#include"iostream.h"
#include"windows.h"
#include"顺序表的合并.cpp"
#include"顺序表的归并.cpp"
#include"链表的合并.cpp"
#include"链表的归并.cpp"
#include"猴子数大王.cpp"
#include"4*4二维数组对角线求积.cpp"
#include"二叉树的建立及求结点数的递归算法.cpp"
int menu_select(){
system("cls");
int i;
cout<<" 1.顺序表的合并 "<<endl;
cout<<" 2.顺序表的归并 "<<endl;
cout<<" 3.链表的合并 "<<endl;
cout<<" 4.链表的归并 "<<endl;
cout<<" 5.猴子数大王 "<<endl;
cout<<" 6.4*4二维数组对角线求积 "<<endl;
cout<<" 7.二叉树的建立及求结点数的递归算法 "<<endl;
cout<<" 0.退出系统 "<<endl;
cout<<" 版权所有-祝奎 "<<endl;
cout<<"请选择(0-7) :";
for( ; ; ){
cin>>i;
if(i<0||i>7)
cout<<"输入有误,请从新输入(0-7):";
else
break;
}
return i;
}
void main(){
int i=1;
while(i){
switch(menu_select())
{
case 1:
system("cls");
cout<<" 顺序表的合并 "<<endl;
main1();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 2:
system("cls");
cout<<" 顺序表的归并 "<<endl;
main2();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 3:
system("cls");
cout<<" 链表的合并 "<<endl;
main3();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 4:
system("cls");
cout<<" 链表的归并 "<<endl;
main4();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 5:
system("cls");
cout<<" 猴子数大王 "<<endl;
main5();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 6:
system("cls");
cout<<" 4*4二维数组对角线求积 "<<endl;
cout<<"20051183052 20051183052 20051183052 "<<endl;
main6();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 7:
system("cls");
cout<<" 二叉树的建立及求结点数的递归算法 "<<endl;
main7();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 0:
system("cls");
cout<<" 谢谢你的使用,再见!"<<endl;
i=0;
break;
}
}
}
main1()
{int LA[9]={3,5,8,11},LB[7]={2,6,8,9,11,15,20},LC[11],t,i,j,m,n;
m=0,n=4;
clrscr ();
/* for(i=0;i<7;i++,m=0)
{for(j=0;j<4;j++)
if(LB[i]!=LA[j]) m++;
if(m==4) (LA[n++]=LB[i]);
}
for(i=0;i<n;i++)
printf("%d",LA[i]);*/
for(i=0;i<4;i++)
LC[i]=LA[i];
for(i=4,j=0;j<7;i++,j++)
LC[i]=LB[j];
for(i=1;i<11;i++)
for(j=0;j<11-i;j++)
if(LC[j]>=LC[j+1])
{t=LC[j+1];LC[j+1]=LC[j];LC[j]=t;}
for(i=0;i<11;i++)
printf("%d",LC[i]);
}
#include<stdio.h>
void main2()
{
int i,j,t;
int la[4],lb[7],lc[11];
for(i=0;i<=3;i++)
{
printf("la[%d]=",i);
scanf("%5d",&la[i]);
lc[i]=la[i];
}
printf("\n");
for(i=4;i<=10;i++)
{
j=i-4;
printf("lb[%d]=",j);
scanf("%5d",&lb[j]);
lc[i]=lb[j];
}
printf("\nlc:");
for(j=0;j<10;j++)
for(i=0;i<10-j;i++)
if(lc[i]>lc[i+1])
{
t=lc[i];
lc[i]=lc[i+1];
lc[i+1]=t;
}
for(i=0;i<=10;i++)
printf("%5d",lc[i]);
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#define n 0
typedef struct Lnode
{
int data;
struct Lnode *next;
}Lnode,*linklist;
linklist create_linklist()
{
linklist l,s,r;
int x;
l=(Lnode*)malloc(sizeof(Lnode));
l->next=NULL;
r=l;
scanf("%d",&x);
while(x!=n)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->data=x;
s->next = r->next;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return l;
}
void list_linklist(linklist A)
{
linklist p;
p=A->next;
while(p!=NULL)
{
printf("%d \n",p->data);
p=p->next;
}
printf("/n");
}
linklist merg(linklist A ,linklist B)
{
linklist c;
Lnode *p,*q,*s,*r;
c = (Lnode*)malloc(sizeof(Lnode));
c->next=NULL;
p=A->next;
q=B->next;
c=A;
r=c;
while(p&&q)
{
if(p->data<q->data)
{
s=p;
p=p->next;
}
else
{
s=q;
q=q->next;
}
r->next=s;
r=s; //从A,B表中选择较小的插入到C表中去
}
if(p==NULL)
{
p=q;
}
r->next=q;
r->next = (q!=NULL? q:p);
return c;
}
void main3()
{
linklist A,B,C;
printf("输入链表A的内容(从小到大输入):\n");
A=create_linklist();
printf("输入链表B的内容(从小到大输入):\n");
B=create_linklist();
printf("链表A为:\n");
list_linklist(A);
printf("链表B为:\n");
list_linklist(B);
C=merg(A,B);
printf("合并后的表为:\n");
list_linklist(C);
system("pause");
}
typedef struct node
{ int data;
struct node *next;
}Lnode,*Linklist;
Linklist next list(int n)
{ linklist head,p,q;
int t=1;
head=new Lnode;
p=head;
q=p;
p->next=NULL;
for(i=1;i<=n;i++)
{ p=new Lnode;
cin>>p->data;
q-.next=p;
q=p;
p->next=NULL;i++
}
if(t>n)cout<<"错误"<<endl;
else return head;
}
Linklist merge(linklist la,linklist lb)
{linklist p,q;
p=la-.next;q=lb->next;
while9p-.next0p=p->next;
p->next=q;
return la;
}
void display(linklist lc)
{ linklist p;
p=lc-.next;
while(p){cout<<p->data<<"";
p=p->next;}
}
void main4()
{linklist p,q,r;
int n;
cout<<"其输入n值:"<<endl;
cout<<"n=";
cin>>n;
p=creat listr(n);
display(p);
cout<<endl<<"a输入结束:"<<endl;
cout<<"请再次输入n值:"<<endl;
cout<<"n=";
cin>>n;
q=creat list(n);
display(q);
r=mege(p,q);
cout<<"链表a,b合并后:"<<endl;
display(r);
cout<<endl;
}
#include "stdio.h"
typedef struct node{
int data;
struct node *next;
}node;
void init_list(node *first,int m){
node *p,*q;
int i;
p=first;
p->data=1;
for(i=2;i<=m;i++){
q=(node*)malloc(sizeof(node));
p->next=q;
p=q;
p->data=i;
p->next=first;
}
}
void main5(){
int i,m,n;
node *first,*p,*q;;
printf("please input the total number of monkeys:");
scanf("%d",&m);
printf("please input a number :");
scanf("%d",&n);
first=(node*)malloc(sizeof(node));
first->next=first;
init_list(first,m);
q=(node*)malloc(sizeof(node));
p=(node*)malloc(sizeof(node));
p=first;
while(p->next!=p){
for(i=1;i<n;i++){
if(i==n-1)
q=p;
p=p->next;
}
p=p->next;
q->next=p;
}
printf("The king is %d",p->data);
}
#define N 4
main6()
{
int a[N][N],i,j;
long int s=1;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
scanf("%d",&a[i][j]);
for(i=0;i<N;i++)
s*=a[i][i]*a[3-i][i];
printf("%d",s);
}
#include "stdio.h"
#define NULL 0
#define M 9
typedef struct node{
int data;
struct node *lchild,*rchild;
}node,*Bitree;
void C(Bitree *T)
{ char ch;
ch=getchar();
if(ch=='0') (*T)=NULL;
else{
(*T)=(node*)malloc(sizeof(node));
(*T)->data=ch;
C(&(*T)->lchild);
C(&(*T)->rchild);
}
}
int Node(Bitree T)
{ if(T)
if ((T->lchild==NULL)&&(T->rchild==NULL))
return 1;
else return Node(T->lchild)+Node(T->rchild)+1;
else return 0;
}
int Countleaf(Bitree bt){
if(bt==NULL) return 0;
if(bt->lchild==NULL&&bt->rchild==NULL)
return 1;
return(Countleaf(bt->lchild)+Countleaf(bt->rchild));
}
main7()
{int total,i;
Bitree bt;
C(&bt);
total=Node(bt);
i=Countleaf(bt);
printf("total=%5d,leaf=%5d",total,i);
/* char preod[M],inod[M];*//* Bitree root;
gets(preod);
gets(inod);
Rebitree(preod,inod,M-1,&root);
total=Node(root);
i=Countleaf(root);*/
}
/*void CreateBitree(char preod[],char inod[],int i,int j,int k,int h,Bitree *T)
{ int m;
(*T)=(node*)malloc(sizeof(node));
(*T)->data=preod[i];
m=k;
whlie(inod[m]!=preod[i])
m++;
if(m==k)
(*T)->lchild=NULL;
else
CreateBitree(preod,inod,i+1,i+m-k,k,m-1,&((*T)->lchild));
if(m==h)
(*T)->rchild=NULL;
else
CreateBitree(preod,inod,i+m-k+1,j,m+1,h,&((*T)->rchild));
}
void Rebitree(char preod[],char inod[],int n,Bitree root){
if(n<=0)
root=NULL;
else
CreateBitree(preod,inod,1,n,1,n,&root);
}*/
顺序表的合并、归并;链表的合并、归并;4*4二维数组对角求积;猴子数大王;二叉树的建立及求结点数的递归算法.
任务:请给以上若干程序编写用户界面程序,使其能用菜单选择相应内容的运行,并保证正确的输出
用C语言实现
#include"iostream.h"
#include"windows.h"
#include"顺序表的合并.cpp"
#include"顺序表的归并.cpp"
#include"链表的合并.cpp"
#include"链表的归并.cpp"
#include"猴子数大王.cpp"
#include"4*4二维数组对角线求积.cpp"
#include"二叉树的建立及求结点数的递归算法.cpp"
int menu_select(){
system("cls");
int i;
cout<<" 1.顺序表的合并 "<<endl;
cout<<" 2.顺序表的归并 "<<endl;
cout<<" 3.链表的合并 "<<endl;
cout<<" 4.链表的归并 "<<endl;
cout<<" 5.猴子数大王 "<<endl;
cout<<" 6.4*4二维数组对角线求积 "<<endl;
cout<<" 7.二叉树的建立及求结点数的递归算法 "<<endl;
cout<<" 0.退出系统 "<<endl;
cout<<" 版权所有-祝奎 "<<endl;
cout<<"请选择(0-7) :";
for( ; ; ){
cin>>i;
if(i<0||i>7)
cout<<"输入有误,请从新输入(0-7):";
else
break;
}
return i;
}
void main(){
int i=1;
while(i){
switch(menu_select())
{
case 1:
system("cls");
cout<<" 顺序表的合并 "<<endl;
main1();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 2:
system("cls");
cout<<" 顺序表的归并 "<<endl;
main2();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 3:
system("cls");
cout<<" 链表的合并 "<<endl;
main3();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 4:
system("cls");
cout<<" 链表的归并 "<<endl;
main4();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 5:
system("cls");
cout<<" 猴子数大王 "<<endl;
main5();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 6:
system("cls");
cout<<" 4*4二维数组对角线求积 "<<endl;
cout<<"20051183052 20051183052 20051183052 "<<endl;
main6();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 7:
system("cls");
cout<<" 二叉树的建立及求结点数的递归算法 "<<endl;
main7();
cout<<"需要继续选择吗?如果需要请输入(1-7),如果不需要请输入0:";
cin>>i;
break;
case 0:
system("cls");
cout<<" 谢谢你的使用,再见!"<<endl;
i=0;
break;
}
}
}
main1()
{int LA[9]={3,5,8,11},LB[7]={2,6,8,9,11,15,20},LC[11],t,i,j,m,n;
m=0,n=4;
clrscr ();
/* for(i=0;i<7;i++,m=0)
{for(j=0;j<4;j++)
if(LB[i]!=LA[j]) m++;
if(m==4) (LA[n++]=LB[i]);
}
for(i=0;i<n;i++)
printf("%d",LA[i]);*/
for(i=0;i<4;i++)
LC[i]=LA[i];
for(i=4,j=0;j<7;i++,j++)
LC[i]=LB[j];
for(i=1;i<11;i++)
for(j=0;j<11-i;j++)
if(LC[j]>=LC[j+1])
{t=LC[j+1];LC[j+1]=LC[j];LC[j]=t;}
for(i=0;i<11;i++)
printf("%d",LC[i]);
}
#include<stdio.h>
void main2()
{
int i,j,t;
int la[4],lb[7],lc[11];
for(i=0;i<=3;i++)
{
printf("la[%d]=",i);
scanf("%5d",&la[i]);
lc[i]=la[i];
}
printf("\n");
for(i=4;i<=10;i++)
{
j=i-4;
printf("lb[%d]=",j);
scanf("%5d",&lb[j]);
lc[i]=lb[j];
}
printf("\nlc:");
for(j=0;j<10;j++)
for(i=0;i<10-j;i++)
if(lc[i]>lc[i+1])
{
t=lc[i];
lc[i]=lc[i+1];
lc[i+1]=t;
}
for(i=0;i<=10;i++)
printf("%5d",lc[i]);
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#define n 0
typedef struct Lnode
{
int data;
struct Lnode *next;
}Lnode,*linklist;
linklist create_linklist()
{
linklist l,s,r;
int x;
l=(Lnode*)malloc(sizeof(Lnode));
l->next=NULL;
r=l;
scanf("%d",&x);
while(x!=n)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->data=x;
s->next = r->next;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return l;
}
void list_linklist(linklist A)
{
linklist p;
p=A->next;
while(p!=NULL)
{
printf("%d \n",p->data);
p=p->next;
}
printf("/n");
}
linklist merg(linklist A ,linklist B)
{
linklist c;
Lnode *p,*q,*s,*r;
c = (Lnode*)malloc(sizeof(Lnode));
c->next=NULL;
p=A->next;
q=B->next;
c=A;
r=c;
while(p&&q)
{
if(p->data<q->data)
{
s=p;
p=p->next;
}
else
{
s=q;
q=q->next;
}
r->next=s;
r=s; //从A,B表中选择较小的插入到C表中去
}
if(p==NULL)
{
p=q;
}
r->next=q;
r->next = (q!=NULL? q:p);
return c;
}
void main3()
{
linklist A,B,C;
printf("输入链表A的内容(从小到大输入):\n");
A=create_linklist();
printf("输入链表B的内容(从小到大输入):\n");
B=create_linklist();
printf("链表A为:\n");
list_linklist(A);
printf("链表B为:\n");
list_linklist(B);
C=merg(A,B);
printf("合并后的表为:\n");
list_linklist(C);
system("pause");
}
typedef struct node
{ int data;
struct node *next;
}Lnode,*Linklist;
Linklist next list(int n)
{ linklist head,p,q;
int t=1;
head=new Lnode;
p=head;
q=p;
p->next=NULL;
for(i=1;i<=n;i++)
{ p=new Lnode;
cin>>p->data;
q-.next=p;
q=p;
p->next=NULL;i++
}
if(t>n)cout<<"错误"<<endl;
else return head;
}
Linklist merge(linklist la,linklist lb)
{linklist p,q;
p=la-.next;q=lb->next;
while9p-.next0p=p->next;
p->next=q;
return la;
}
void display(linklist lc)
{ linklist p;
p=lc-.next;
while(p){cout<<p->data<<"";
p=p->next;}
}
void main4()
{linklist p,q,r;
int n;
cout<<"其输入n值:"<<endl;
cout<<"n=";
cin>>n;
p=creat listr(n);
display(p);
cout<<endl<<"a输入结束:"<<endl;
cout<<"请再次输入n值:"<<endl;
cout<<"n=";
cin>>n;
q=creat list(n);
display(q);
r=mege(p,q);
cout<<"链表a,b合并后:"<<endl;
display(r);
cout<<endl;
}
#include "stdio.h"
typedef struct node{
int data;
struct node *next;
}node;
void init_list(node *first,int m){
node *p,*q;
int i;
p=first;
p->data=1;
for(i=2;i<=m;i++){
q=(node*)malloc(sizeof(node));
p->next=q;
p=q;
p->data=i;
p->next=first;
}
}
void main5(){
int i,m,n;
node *first,*p,*q;;
printf("please input the total number of monkeys:");
scanf("%d",&m);
printf("please input a number :");
scanf("%d",&n);
first=(node*)malloc(sizeof(node));
first->next=first;
init_list(first,m);
q=(node*)malloc(sizeof(node));
p=(node*)malloc(sizeof(node));
p=first;
while(p->next!=p){
for(i=1;i<n;i++){
if(i==n-1)
q=p;
p=p->next;
}
p=p->next;
q->next=p;
}
printf("The king is %d",p->data);
}
#define N 4
main6()
{
int a[N][N],i,j;
long int s=1;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
scanf("%d",&a[i][j]);
for(i=0;i<N;i++)
s*=a[i][i]*a[3-i][i];
printf("%d",s);
}
#include "stdio.h"
#define NULL 0
#define M 9
typedef struct node{
int data;
struct node *lchild,*rchild;
}node,*Bitree;
void C(Bitree *T)
{ char ch;
ch=getchar();
if(ch=='0') (*T)=NULL;
else{
(*T)=(node*)malloc(sizeof(node));
(*T)->data=ch;
C(&(*T)->lchild);
C(&(*T)->rchild);
}
}
int Node(Bitree T)
{ if(T)
if ((T->lchild==NULL)&&(T->rchild==NULL))
return 1;
else return Node(T->lchild)+Node(T->rchild)+1;
else return 0;
}
int Countleaf(Bitree bt){
if(bt==NULL) return 0;
if(bt->lchild==NULL&&bt->rchild==NULL)
return 1;
return(Countleaf(bt->lchild)+Countleaf(bt->rchild));
}
main7()
{int total,i;
Bitree bt;
C(&bt);
total=Node(bt);
i=Countleaf(bt);
printf("total=%5d,leaf=%5d",total,i);
/* char preod[M],inod[M];*//* Bitree root;
gets(preod);
gets(inod);
Rebitree(preod,inod,M-1,&root);
total=Node(root);
i=Countleaf(root);*/
}
/*void CreateBitree(char preod[],char inod[],int i,int j,int k,int h,Bitree *T)
{ int m;
(*T)=(node*)malloc(sizeof(node));
(*T)->data=preod[i];
m=k;
whlie(inod[m]!=preod[i])
m++;
if(m==k)
(*T)->lchild=NULL;
else
CreateBitree(preod,inod,i+1,i+m-k,k,m-1,&((*T)->lchild));
if(m==h)
(*T)->rchild=NULL;
else
CreateBitree(preod,inod,i+m-k+1,j,m+1,h,&((*T)->rchild));
}
void Rebitree(char preod[],char inod[],int n,Bitree root){
if(n<=0)
root=NULL;
else
CreateBitree(preod,inod,1,n,1,n,&root);
}*/