❶ c语言设计案例张传学P196编写结构体链表
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedefstructNode
{
charname[20];
charphone[20];
charcell[20];
charmail[50];
structNode*next;
}Node;
voidshow(Node*h)
{
Node*p;
for(p=h;p!=NULL;p=p->next)
{
printf("%s,%s,%s,%s ",p->name,p->phone,p->cell,p->mail);
}
}
Node*Insert(Node*h)
{
Node*p=(Node*)malloc(sizeof(Node));
scanf("%s%s%s%s",p->name,p->phone,p->cell,p->mail);
if(h==NULL)
{
p->next=NULL;
}
else
{
p->next=h;
}
returnp;
}
Node*find(Node*h,char*name)
{
Node*r=NULL;
while(h)
{
if(strcmp(h->name,name)==0)
{
r=h;
break;
}
h=h->next;
}
returnr;
}
voiddestroy(Node*h)
{
Node*p;
while(h)
{
p=h;
h=h->next;
free(p);
}
}
intmain()
{
Node*h=NULL;
intc=-1;
do
{
printf("chooseafunction: ");
printf("1:insertanewnode ");
printf("2:printallnodes ");
printf("3:findanode ");
printf("4:exit ");
scanf("%d",&c);
switch(c)
{
case1:
h=Insert(h);
break;
case2:
show(h);
break;
case3:
{
Node*r;
charname[20];
scanf("%s",name);
r=find(h,name);
if(r)
printf("%s,%s,%s,%s ",r->name,r->phone,r->cell,r->mail);
elseprintf("notfind ");
break;
}
case4:
break;
default:
printf("unknowcommand ");
break;
}
}while(c!=4);
destroy(h);
return0;
}
❷ C语言结构体编程
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct telephone
{
char name[10];
char telno[20];
};
void search(struct telephone b[], char *x, int n);
int main()
{
int i,n;
struct telephone b[100];
char nane[100];
for(i=0;;i++)
{
printf("Please input name:");
gets(b[i].name);
if(b[i].name[0]=='#')
break;
printf("Please input telephone:");
gets(b[i].telno);
}
n=i;
printf("Please input you want to find name:");
gets(nane);
search(b,&nane[0],n);
return 0;
}
void search(struct telephone b[],char *x,int n)
{
int i;
int find=0;
for(i=0;i<n;i++)
{
if(strcmp(x,b[i].name)==0)
{
printf("the telephone is %s\n",b[i].telno);
find=1;
}
}
if(find==0)
printf("Not found!");
}
❸ c语言编程 结构体:由键盘输入某商场各商品的商品名、价格、销售量,并计算各商品的销售额,最后输出销
1.建立结构体数组
2.建立结构体指针数组
3.初始化内存
4.循环输入
5.按销量成员排序,结果存入结构体指针数组
6.按结构体指针数组输出前十
❹ 谁能提供一个C语言结构体实现链表的例子,代码能直接运行的
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
typedef struct Node
{
int key;
Node * next;
}Node; //定义结构
int main()
{
int *a,n,i;
Node* L0,*p;
scanf("%d",&n);//输入链表长度
a=(int*)malloc(sizeof(int)*n);//给数组a动态分配n个空间
L0=(Node*)malloc(sizeof(Node));
L0->next=NULL; //建立头结点
srand((unsigned)time( NULL ) ); //与rand()函数一起使用
for(i=0;i<n;i++)
{
a[i]=rand()%100; //产生100以内的随机数,也可以自己输入
}
for(i=0;i<n;i++)
{
p=(Node*)malloc(sizeof(Node));
p->key=a[i]; //分配一个结点空间,并赋值
p->next=L0->next;
L0->next=p; //连接单链表,这里是精髓
}
while(L0->next)
{
L0=L0->next;
printf("%d ",L0->key); //遍历单链表
}
return 0;
}
还是自己把链表弄懂吧,很有意思的,也是数据结构的基础内容。
❺ C语言 结构体及其应用
#include <stdio.h>
#include <stdlib.h>
typedef struct strStudent{
int stuID;
int score;
char stuName[20];
};
int main (void)
{
strStudent stu;
scanf("%d",&stu.stuID);
scanf("%d",&stu.score);
scanf("%d",stu.stuName);
printf("%d\t%d\t%s\n",stu.stuID,stu.score,stu.stuName);
return 0;
}
第二个 你自己修改一下
❻ 求一个C语言结构体编程例子。
#include<stdio.h>
structstudent
{
intnum;
charname[20];
intScore1;
intScore2;
intScore3;
}student[2];
intmain()
{
inti,j;
intAverage;
for(i=0;i<3;i++)
{
printf("请输入学生%d的资料: ",i+1);
printf("学号为:");
scanf("%d",&student[i].num);
printf("姓名是:");
scanf("%s",&student[i].name);
printf("第1门成绩是:");
scanf("%d",&student[i].Score1);
printf("第2门成绩是:");
scanf("%d",&student[i].Score2);
printf("第3门成绩是:");
scanf("%d",&student[i].Score3);
printf(" ");
}
printf("学号 姓名 语文 数学 英语 平均分 ");
for(j=0;j<3;j++)
{
Average=(student[j].Score1+student[j].Score2+student[j].Score3)/3;
printf("%d",student[j].num);
printf(" %s",student[j].name);
printf(" %d",student[j].Score1);
printf(" %d",student[j].Score2);
printf(" %d",student[j].Score3);
printf(" %d",Average);
printf(" =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* ");
}
}
❼ C语言问题,就什麽叫结构体引用最好举点例子.
结构体引用,没有这个“专业用词”,但是从你的问题上来看,可以理解你的想法。
引用就是引用的意思,结构体是一种扩展的数据结构。
结构体一般可以根据实际需要设定内部的构造,比如存放商品信息的结构体
struct obj{
char name[20];//名字
int number;//编码
float price;//价钱
};
如上,就定义了一个结构体,但是这东西本身并无实际意义,它只是规定了一种格式。
商店的货品有许多样,比如100种商品。
struct obj commodity[100];这样,就定义了100个实际的空间。用来保存100种商品的信息
而比如我想获取或修改第八个商品的信息就用如下方法。
struct obj temp = commodity[7];//获取了第八个商品的结构体空间
temp.name="xx牌面包";//设定商品名称
temp.number=12345;//设定商品编码
temp.price=3.5f;//设定商品价钱
如上的定义结构体数组,从结构体数组中获取某一具体元素并对内容属性修改的一系列过程
就叫结构体引用拉。
❽ C语言 结构体 怎么定义 使用 举个例子 看看
struct a
{
int m;
char f[20];
....
}
这就是结构体,定义一般放在头文件的开头!
❾ C语言结构体问题
因为你没有理解函数的参数在压栈进入函数时存在拷贝的原理:
void func(struct int_char ex2)函数采用的是值传递方式传递参数,在调用时,函数的参数被拷贝了一份,压栈进入函数,所以在函数内部,在调用
ex2.n+=10;
ex2.c-=1;//应该是ex2.ch-=1;
后,ex2.n的值是15,ex2.ch的值为's'
而当函数返回后,外部的ex2/ex1中的值一直未变化,也就是说你修改的只是函数内部的那份拷贝,用下面的方法可以实现你所想要的值:
方法一:
struct int_char func(struct int_char ex2)
{
ex2.n+=10;
ex2.ch-=1;
return ex2;
}
方法二:
void func(struct int_char *ex2)
{
ex2->n += 10;
ex2->ch -= 1;
}
❿ C语言结构体编程
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char num[10];
int s;
}strec;
int N;
int fun(strec*a,strec*b)
{
int i,j=0;
strec max;
for(i=0;i<N;i++)
{
printf("输入姓名:");
scanf("%s",a[i].num);
printf("输入分数:");
scanf("%d",&a[i].s);
}
max=a[0];
for(i=0;i<N;i++)
if(max.s<a[i].s)
max=a[i];
for(i=0;i<N;i++)
if(max.s==a[i].s)
{ b[j]=a[i];
j++;
}
for(i=0;i<j;i++)
{
printf("%s ",b[i].num);
printf("%d",b[i].s);
printf(" ");
}
return j;
}
void main()
{
strec *a,*b;
int n;
printf("input N:");
scanf("%d",&N);
a=(strec*)malloc(N*sizeof(strec));
b=(strec*)malloc(N*sizeof(strec));
n=fun(a,b);
printf("最高分有%d人 ",n);
}
采纳时多给点分!桥这么多代码不容易啊!