【新人迷惑】怎样实现元素从结构体赋值到线性表?

_Looong_ 2016-10-20 05:20:19
想将结构体中的值赋到线性表 再在线性表中合并
程序如下 曾想借助结构指针 但错误更多 修改数次不得其解
报错5个 皆以标出
感谢~~~~~
#include<malloc.h>
#include<iostream>
#include<stdio.h>
#include<string>
#include<stdlib.h>


using namespace std;

#define N 5
#define T 2
typedef struct
{
int data[50];
int length;

}sqlist;
struct student
{
int num;
string name;
float score1;
float score2;
float score3;
}stu[2];

void creatlist(sqlist *l,stu[i]) ; //error1:syntax error : identifier 'stu'
void DispList(sqlist *);
void unionlist(sqlist *,sqlist *,sqlist *&);


int main()
{
int i;
sqlist *sqa,*sqb,*sqc;

cout<<"请输入学生信息:/n 学号 姓名 分数1 分数2 "<<endl;
cin>>stu[0].num>>stu[0].name>>stu[0].score1>>stu[0].score2;
creatlist(sqa,stu[0]); //error2: 'creatlist' : function does not take 2 parameters
cout<<"请输入学生信息:/n 学号 姓名 分数1 分数3 "<<endl;
cin>>stu[0].num>>stu[0].name>>stu[0].score1>>stu[0].score3;
creatlist(sqa,stu[1]); //error3: 'creatlist' : function does not take 2 parameters

unionlist(sqa,sqb,sqc);
DispList(sqc);
return 0;
}

void creatlist(sqlist *l,stu[i]) //error4: syntax error : identifier 'stu'
{
int i;
l=(sqlist *)malloc(sizeof(sqlist));
l->data[0]=stu[i].num;
l->data[1]=stu[i].name; //error5: no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no a
l->data[2]=stu[i].score1;
l->data[3]=stu[i].score2;
}

void DispList(sqlist *l)
{
int i;
for(i=0;i<l->length;i++)
{
printf("%d ",l->data[i]);
}
printf("\n");
}
void unionlist(sqlist *a,sqlist *b,sqlist *&c)
{
c=(sqlist *)malloc(sizeof(sqlist));
int i,j=0,k=0,l=0;
c->length=0;
for(i=0;i<a->length;i++)
{
c->data[i]=a->data[i];
c->length++;
}
while(j<b->length)
{

c->data[i]=b->data[j];
i++;
c->length++;

}
}




...全文
421 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
_Looong_ 2016-10-20
  • 打赏
  • 举报
回复
再次改进后 没有error和warning 但是程序一运行就停止 #include<malloc.h> #include<iostream> #include<stdio.h> #include<string> #include<stdlib.h> using namespace std; typedef struct { string data[50]; int length; }sqlist; struct student { int num; string name; float score1; float score2; float score3; }; student stu[2]; void creatlist(sqlist *l,struct student stu) ; void DispList(sqlist *l); void unionlist(sqlist *,sqlist *,sqlist *&); void main() { sqlist *sqa=NULL, *sqb=NULL, *sqc; cout<<"请输入学生信息: 学号 姓名 分数1 分数2 "<<endl; cin>>stu[0].num>>stu[0].name>>stu[0].score1>>stu[0].score2; creatlist(sqa,stu[0]); cout<<"请输入学生信息:/n 学号 姓名 分数1 分数3 "<<endl; cin>>stu[1].num>>stu[1].name>>stu[1].score1>>stu[1].score3; creatlist(sqb,stu[1]); unionlist(sqa,sqb,sqc); DispList(sqc); } void creatlist(sqlist *l,struct student stu) { l=(sqlist *)malloc(sizeof(sqlist)); l->data[0]=stu.num; l->data[1]=stu.name; l->data[2]=stu.score1; l->data[3]=stu.score2; } void DispList(sqlist *l) { int i; for(i=0;i<l->length;i++) { printf("%d ",l->data[i]); } printf("\n"); } void unionlist(sqlist *a,sqlist *b,sqlist *&c) { c=(sqlist *)malloc(sizeof(sqlist)); int i,j=0,k=0,l=0; c->length=0; for(i=0;i<a->length;i++) { c->data[i]=a->data[i]; c->length++; } while(j<b->length) { c->data[i]=b->data[j]; i++; c->length++; } }
_Looong_ 2016-10-20
  • 打赏
  • 举报
回复
改进后没有error了 但是报warning 说sqa与sqb “used without having been initialized”?? #include<malloc.h> #include<iostream> #include<stdio.h> #include<string> #include<stdlib.h> using namespace std; typedef struct { string data[50]; int length; }sqlist; struct student { int num; string name; float score1; float score2; float score3; }; student stu[2]; void creatlist(sqlist *l,struct student stu) ; void DispList(sqlist *); void unionlist(sqlist *,sqlist *,sqlist *&); int main() { sqlist *sqa,*sqb,*sqc; cout<<"请输入学生信息:/n 学号 姓名 分数1 分数2 "<<endl; cin>>stu[0].num>>stu[0].name>>stu[0].score1>>stu[0].score2; creatlist(sqa,stu[0]); cout<<"请输入学生信息:/n 学号 姓名 分数1 分数3 "<<endl; cin>>stu[1].num>>stu[1].name>>stu[1].score1>>stu[1].score3; creatlist(sqb,stu[1]); unionlist(sqa,sqb,sqc); DispList(sqc); return 0; } void creatlist(sqlist *l,struct student stu) { l=(sqlist *)malloc(sizeof(sqlist)); l->data[0]=stu.num; l->data[1]=stu.name; l->data[2]=stu.score1; l->data[3]=stu.score2; } void DispList(sqlist *l) { int i; for(i=0;i<l->length;i++) { printf("%d ",l->data[i]); } printf("\n"); } void unionlist(sqlist *a,sqlist *b,sqlist *&c) { c=(sqlist *)malloc(sizeof(sqlist)); int i,j=0,k=0,l=0; c->length=0; for(i=0;i<a->length;i++) { c->data[i]=a->data[i]; c->length++; } while(j<b->length) { c->data[i]=b->data[j]; i++; c->length++; } }
paschen 2016-10-20
  • 打赏
  • 举报
回复
这样改:

#include<malloc.h>    
#include<iostream>  
#include<stdio.h>
#include<string> 
#include<stdlib.h> 


using namespace std; 

#define N 5
#define T 2
typedef struct  
{  
	int data[50];  
	int length;  

}sqlist; 
struct student   
{  
	int num;  
	string name; 
	float score1;    
	float score2;
	float score3;
}stu[2];

void creatlist(sqlist *l,struct student)  ;                          //error1:syntax error : identifier 'stu'
void DispList(sqlist *);  
void unionlist(sqlist *,sqlist *,sqlist *&);  


int main()  
{ 
	int i;
	sqlist *sqa,*sqb,*sqc;  

	cout<<"请输入学生信息:/n 学号 姓名 分数1 分数2 "<<endl;
	cin>>stu[0].num>>stu[0].name>>stu[0].score1>>stu[0].score2; 
	creatlist(sqa,stu[0]);                                  //error2: 'creatlist' : function does not take 2 parameters
	cout<<"请输入学生信息:/n 学号 姓名 分数1 分数3 "<<endl;
	cin>>stu[0].num>>stu[0].name>>stu[0].score1>>stu[0].score3; 
	creatlist(sqa,stu[1]);                                  //error3: 'creatlist' : function does not take 2 parameters

	unionlist(sqa,sqb,sqc);
	DispList(sqc);  
	return 0;
}

void creatlist(sqlist *l,struct student stu)                            //error4: syntax error : identifier 'stu'
{ 
	int i;
	l=(sqlist *)malloc(sizeof(sqlist));  
	l->data[0]=stu.num;
	l->data[1]=stoi(stu.name);                             //error5: no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no a
	l->data[2]=stu.score1;
	l->data[3]=stu.score2;
}

void DispList(sqlist *l)  
{  
	int i;  
	for(i=0;i<l->length;i++)  
	{  
		printf("%d ",l->data[i]);  
	}  
	printf("\n");  
}  
void unionlist(sqlist *a,sqlist *b,sqlist *&c)  
{  
	c=(sqlist *)malloc(sizeof(sqlist));  
	int i,j=0,k=0,l=0;  
	c->length=0;  
	for(i=0;i<a->length;i++)  
	{  
		c->data[i]=a->data[i];  
		c->length++;  
	}  
	while(j<b->length)  
	{  

		c->data[i]=b->data[j];  
		i++;  
		c->length++;  

	}
}


linglangtan 2016-10-20
  • 打赏
  • 举报
回复
creatlist 这个函数的第二个参数你写错了,stu[i]不是结构体的名称,应该看成一个变量。 刚刚开始,建议还是按照标准的格式写这些结构体吧。 struct student { int num; string name; float score1; float score2; float score3; }; student stu[2]; 还有基础很重要。

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧