范例01-081
选票统计的
输入10个选片的姓
统计出每个所得的个数
#include "stdio.h"
struct candidate
{
char name[20];
int count;
} cndt[3] =
{{"wang",0},{"zhang",0},{"chen",0}};
void main()
{
int i,j;
char Ctname[20];
for(i=1;i<=10;i++)
{
scanf("%s",&Ctname);
for(j=0;j<3;j++)
{
if(strcmp(Ctname,cndt[j].name)==0)
cndt[j].count++;
}
}
for(i=0;i<3;i++)
{
printf("%s:%d\n",cndt[i].name,cndt[i].count);
}
}
范例
02
输出成绩最大--082--???
#include "stdio.h"
struct student
{
int num;
char name[20];
float score;
};
void main()
{
int i,m;
float maxScore;
struct student stu[5] =
{
{"01","liming",60},
{"02","uu",70.56},
{"03","bravery",88.90},
{"04","sam",99.01},
{"05","test",89.04},
};
printf("%s",stu[0].name);
m=0;
maxScore = stu[0].score;
for(i=1;i<5;i++)
{
if(stu[i].score > maxScore)
{
maxScore = stu[i].score;
m=i;
}
//
printf("i:%d\n",i);
}
printf("m is:%d\n",m);
printf("the maxScoreis:%f\n",maxScore);
printf("the maxScore stu is:%c\n",stu[m].name);
printf("the maxScore stu number is:%d\n",stu[m].num);
}
范例03
求平均数 -083
#include
void main()
{
struct stu_score
{
int mid;
int end;
int ave;
}score;
printf("please input score(midterm and end and
end of term)");
scanf("%d,%d",&score.mid,&score.end);
score.ave = (score.mid + score.end)/2;
printf("average = %d\n",score.ave);
}
实例05 比较输入数的大小 -084
#include "stdio.h"
#define N 5
struct order
{
int num;
int con;
}a[20];
main()
{
int i,j;
for(i=0;i
{
scanf("%d",&a[i].num);
a[i].con=0;
}
for(i=N-1;i>=1;i--)
{
for(j=i-1;j>=0;j--)
if(a[i].num < a[j].num)
a[j].con++;
else
a[i].con++;
}
printf("the order is:\n");
for(i=0;i
printf("==\n",a[i].num,a[i].con);
}
实例06 信息查询
-085
#include
#include
#define MAX 101
struct aa
{
char name[15];
char tel[15];
};
int readin(struct aa *a)
{
int i=0,n=0;
while(1)
{
scanf("%s",a[i].name);
if(!strcmp(a[i].name,"#"))
break;
scanf("%s",a[i].tel);
i++;
n++;
}
return n;
}
void search(struct aa *b,char *x,int n)
{
int i=0;
while(1)
{
if(!strcmp(b[i].name,x))
{
printf("name:%s tel:%s\n",b[i].name,b[i].tel);
break;
}
else
i++;
n--;
if(n==0)
{
printf("not found !\n");
break;
}
}
}
main()
{
struct aa s[MAX];
int num;
char name[15];
num = readin(s);
printf("input the name:");
scanf("%s",name);
search(s,name,num);
}
llll
5522255
bbbb
112444
ttttt
889900
#
输入3个手机号
然后
以#号结束
input the name:llll
//搜索这个名字的人的手机号
name:llll tel:5522255
实例07 -086
计算开机时间
#include "stdio.h"
struct time
{
int hour;
int minute;
int second;
}t;
main()
{
FILE *fp;
fp = fopen("Time","r");
fread(&t,sizeof(struct time),1,fp);
while(!kbhit())
{
rewind(fp);
sleep(1);
fread(&t,sizeof(struct time),1,fp);
if(t.second == 59)
{
t.minute = t.minute+1;
if(t.minute == 60)
{
t.hour = t.hour+1;
t.minute = 0;
}t.second =0;
}
else
else
t.second = t.second+1;
printf("%d:%d:%d\n",t.hour,t.minute,t.second);
fp = fopen("Time","w");
fwrite(&t,sizeof(struct time),1,fp);
fclose(fp);
}
}
出错
mac:c-calculation loveuu$ gcc -o struct07
struct07.c
Undefined symbols for architecture
x86_64:
"_kbhit", referenced
from:
_main in ccZ46QqX.o
ld: symbol(s) not found for architecture
x86_64
collect2: ld returned 1 exit status
实例08 链表
--087
#include "stdio.h"
#include
struct LNode
{
int data;
struct LNode *next;
};
struct LNode *create(int n)
{
int i;
struct LNode *head,*p1,*p2;
int a;
head = NULL;
printf("input the intgers:\n");
for(i=n;i>0;--i)
{
p1 = (struct LNode)malloc(sizeof(struct LNode));
scanf("%d",&a);
p1->data=a;
if(head ==NULL)
{
head = p1;
p2=p1;
}
else
{
p2->next = p1;
}
}
p2->next = NULL;
return head;
}
void main()
{
int n;
struct LNode *q;
printf("Input the count of the nodes you want to
create:");
scanf("%d",&n);
q = create(n);
printf("the result is :\n");
while(q)
{
printf("%d ",q->data);
q=q->next;
}getch();
}
加载中,请稍候......