如何修复这些语法错误? [英] How do I fix these syntax errors ?

查看:101
本文介绍了如何修复这些语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求编写一个应该执行以下操作的程序;

- 使用数组和指针构建二进制搜索树。存储在节点上的值是实数。

- 以通常的3种方式遍历二叉树:inorder,pre order和post oreder。



所以在直接编写整个程序之前,我想至少只使用指针编写一个。然后,如果它工作,我会添加requierd行,以便代码可以使用指针或数组构建BST,具体取决于用户的选择。



下面是我试过的代码,问题是我不知道如何修复case语句中函数的语法错误。



我尝试过:



  #include  < span class =code-preprocessor> <   iostream   >  
使用 命名空间标准;

struct node {
float info;
节点*左;
节点*权利;
};

typedef node * tree; // 用于存储根节点的地址。

void build(node * t, float x){
if (t == NULL){
t = new 节点;
t - > info = x;
t - > left = NULL;
t - > right = NULL;
}
其他 {
if (t - > info > = x)
build(t - > left,x);
else
build(t-> right,x);
}
}


void inorder(tree t){
if (t - > left!= NULL)
inorder(t - > left);
cout<< t - > info<< ;
if (t - > right!= NULL)
inorder(t - > right);
}


void 预订(树t){
if (t!= NULL){
cout<< t - > info<< ;
预购(t - >左);
预购(t - >右);
}
}

void postorder(tree t){
if (t - > left!= NULL)
postorder(t - > left);
if (t - > right!= NULL)
postorder(t - > right);
cout<< t - > info<< ;
}

int main(){
b:cout<< 选择以下操作<< ENDL;
cout<< 1.构建二进制搜索树<< ENDL;
cout<< 2.按顺序遍历树<< ENDL;
cout<< 3.按预先的顺序遍历树<< ENDL;
cout<< 4.按邮政顺序遍历树<< ENDL;
cout<< 5.退出<< ENDL;

float s;

int choice;
cin>>选择;

switch (选择){
case 1
build(node *,s);
break ;
case 2
inorder(node *);
break ;
case 3
preorder(node *);
break ;
case 4
postorder(node *);
break ;
case 5
goto a;
break ;
默认
goto b;
break ;

}

a: return 0 ;
}

解决方案

您正在切换案例中调用函数。这些需要传递现有的和类型匹配的参数。



示例:

  float  s =  1  .0f; 
// 结构节点的实例
节点头;
// 初始化
head.info = 0 ;
head.left = NULL;
head.right = NULL;
// 也可以在一行中将所有标准设置为零:
// node head = {0};
// 初始化树(node *)实例
tree root =& head;

// 参数的类型为node *和float
建立(& head,s);
// 参数的类型为树(节点*)
inorder(root) );


I was asked to write a prgram that should do the following;
- build a binary search tree using arrays and pointers. The values stored at the nodes are real numbers.
- Travers the binary tree in the usual 3 ways: inorder, pre order and post oreder.

So before directly going to write the whole program, I wanted to at least write one using only pointers. Then if it works, I would add the requierd lines so that the code can build the BST either using pointers or an array depending on the users choice.

Down belowe is the code I've tried, and the probleme is that I don't know how to fix the syntax errors in the functions in the case statements.

What I have tried:

#include <iostream>
using namespace std;

struct node {
	float info;
	node * left;
	node * right;
	};

typedef node *tree;    // to store the address of the root node.

void build(node *t, float x){
	if (t == NULL){
		t = new node;
		t -> info = x;
		t -> left =   NULL;
		t -> right = NULL;
    }
	else{
		if (t -> info >= x)
			build (t -> left, x);
		else
			build(t-> right, x);
	}
}


void inorder(tree t){
	if (t -> left != NULL)
		inorder (t -> left);
	cout << t -> info <<" ";
	if (t -> right != NULL)
		inorder (t -> right);
}


void preorder(tree t){
	if (t != NULL){
		cout << t -> info <<" ";
		preorder(t -> left);
		preorder(t -> right);
	}
}

void postorder(tree t){
	if (t -> left != NULL)
		postorder(t -> left);
    if (t -> right != NULL)
    	postorder(t -> right);
    cout << t -> info <<" ";
}

int main() {
b:	cout << "Choose among the following actions" << endl;
	cout << "1. Build a binary search tree"<< endl;
	cout << "2. Traverse the tree in order"<< endl;
	cout << "3. Traverse the tree in pre order"<< endl;
	cout << "4. Traverse the tree in post order"<< endl;
	cout << "5. Quit"<< endl;

	float s;

	int choice;
	cin >> choice;

	switch(choice){
	case 1:
		build(node*,s);
		break;
	case 2:
		inorder(node*);
		break;
	case 3:
		preorder(node*);
		break;
	case 4:
		postorder(node*);
		break;
	case 5:
		goto a;
		break;
	default :
		goto b;
		break;

	}

a:	return 0;
}

解决方案

You are calling functions from within your switch cases. These require passing existing and type matching parameters.

Examples:

float s = 1.0f;
// An instance of struct node
node head;
// Initialise it
head.info = 0;
head.left = NULL;
head.right = NULL;
// May do it also in a single line setting all mebers to zero: 
//node head = { 0 };
// An initialised tree (node*) instance
tree root = &head;

// Parameters are of type node* and float
build(&head, s);
// Parameter is of type tree (node*)
inorder(root);


这篇关于如何修复这些语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆