❶ 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);
}
採納時多給點分!橋這么多代碼不容易啊!