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

_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++;

}
}




...全文
479 4 打赏 收藏 转发到动态 举报
写回复
用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]; 还有基础很重要。
源码链接: https://pan.quark.cn/s/a4b39357ea24 ### Ubuntu 用户磁盘空间分配与 Linux 硬盘分区方案深入剖析 #### 标题与描述中的核心知识要点说明 本文件将系统性地阐释在 Ubuntu 操作系统中如何为用户科学地分配磁盘空间,并深入分析适用于 Linux 的硬盘分区策略。文件不仅会介绍基础的目录结构及其功能,还会提供具体的分区指导以及在实际操作过程中需要留意的事项。 #### 目录结构及其功能说明 - **/bin**:系统常用基础命令的存储位置。 - **/boot**:包含启动时所需文件,例如内核镜像和初始化脚本等。 - **/dev**:存放设备文件。 - **/etc**:存储系统配置信息。 - **/home**:用户个人目录,每个用户拥有独立的子目录。 - **/lib**:存放运行二进制文件所需的共享库。 - **/mnt**:用于临时挂载其他文件系统的目录,可用于挂载软盘或 CD-ROM 设备。 - **/proc**:虚拟文件系统目录,存储当前运行内核的相关信息。 - **/root**:超级用户(root)的主目录。 - **/sbin**:系统管理程序的存放位置。 - **/tmp**:临时文件存储的目录。 - **/usr**:存放只读数据,包括应用程序的可执行文件和文档等。 - **/var**:存储可变数据文件,例如日志文件和邮件队列等。 - **/X11R6**:X Window 系统的安装路径。 #### 硬盘分区策略分析 文件中强调,在 Linux 环境下进行硬盘分区时需要综合考量多个因素,以确保系统的稳定运行和性能表现。以下是一些关键的分区建议: 1. **/boot ...
代码转载自:https://pan.quark.cn/s/4f1937b2c732 Quartus Prime是由Altera公司(现已被Intel公司所收购)研发的一款集成化电子设计自动化软件,其主要应用领域在于Field-Programmable Gate Array(现场可编程门阵列)以及Complex Programmable Logic Device(复杂可编程逻辑器件)的设计、仿真、综合、布局布线以及编程等任务。在本案例中,所提及的为Quartus Prime 17.1版本的安装程序,该程序内含器件库文件及其他相关组件,适用于Windows 10 64位操作系统环境,并且已有相关测试验证其可用性。 **1. Quartus Prime概述** Quartus Prime是Altera/Intel公司FPGA设计流程的核心工具,它提供了一个集成化的开发平台,支持从设计概念到硬件实现的完整过程。该软件不仅涵盖了逻辑综合、时序分析、仿真、物理设计等基础功能,还包含了高速接口IP核、嵌入式处理器系统、内存资源管理等多种高级特性。 **2. Quartus Prime 17.1版本的新增功能** - QSYS系统构建工具的优化:17.1版本可能对QSYS系统进行了升级,使得系统集成更为便捷,支持更多的外部设备和处理器模型。 - 性能提升:可能增强了综合和布局布线的效率,降低了设计的功耗和面积。 - 新器件支持:可能会增加对最新发布的Altera FPGA和CPLD产品的支持,如Arria 10、Cyclone 10等系列产品。 - 调试功能的改进:可能强化了硬件调试工具,如JTAG调试和软件调试器的性能。 **3. 器件库文件** 器件库文件是Quartu...

33,316

社区成员

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

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