收藏 分销(赏)

c语言程序.doc

上传人:仙人****88 文档编号:8603675 上传时间:2025-02-21 格式:DOC 页数:13 大小:17.79KB 下载积分:10 金币
下载 相关 举报
c语言程序.doc_第1页
第1页 / 共13页
c语言程序.doc_第2页
第2页 / 共13页


点击查看更多>>
资源描述
试验一 (1)#include<stdio.h> void main() { void tongji(char*,int,int*); int i=0,a[4]={0,0,0,0}; char s[50]; while((s[i]=getchar())!='\n') i++; tongji(s,i,a); printf("daxie:%d xiaoxie:%d shuzi:%d qita:%d ",a[0],a[1],a[2],a[3]); } void tongji(char *p,int j,int *t) { int i=0; while(i<j) { if(*(p+i)<='Z' && *(p+i)>='A') (*(t))++; else if(*(p+i)<='z' && *(p+i)>='a') (*(t+1))++; else if(*(p+i)<=9 && *(p+i)>=0) (*(t+2))++; else (*(t+3))++; i++; } } (2)#include<stdio.h> void main() { void input(); void swap(); int a[10],*p,j; p=a; input(9,p); swap(p,9); for(j=0;j<=9;j++) printf("%d ",a[j]); printf("\n") ; } void input(int s,int *p1) { int i; printf("\n"); for(i=0;i<=s;i++) scanf("%d",p1+i); printf("\n"); } void swap(int *p2,int g) { int *max,*min,i,t; max=(p2+g); min=p2; for(i=0;i<=9;i++) { if(*min>=*(p2+i)) min=(p2+i); if(*max<=*(p2+i)) max=(p2+i); } t=*p2; *p2=*min; *min=t; t=*(p2+g); *(p2+g)=*max; *max=t; } (3)1.3 #include"stdio.h" #include"malloc.h" void main() { int i; float *sc_english,*sc_normal,*sc_final,sum_s=0,avg_s; sc_english=(float*)malloc(10*sizeof(float)); sc_normal=(float*)malloc(10*sizeof(float)); sc_final=(float*)malloc(10*sizeof(float)); for(i=0;i<10;i++) scanf(fp,"%f",sc_english+i); for(i=0;i<10;i++) scanf(fp,"%f",sc_normal+i); for(i=0;i<10;i++) {sum_s+=*(sc_english+i); *(sc_final+i)=*(sc_english+i)+*(sc_normal+i);} avg_s=sum_s/10; printf("Avage score :%.2f\nFinal score:\n",avg_s); for(i=0;i<10;i++) printf("Student %d: %.2f\n",i+1,*(sc_final+i)); free(sc_english),free(sc_normal),free(sc_final); } (4)#include<stdio.h> struct student { long num; char name[50]; float score; }; void main() { void input(); void print(); struct student a[3],*p; int i; p=a; for(i=0;i<=2;i++) input(p,i); for(i=0;i<=2;i++) print(p,i); } void input(struct student *o,int i) { printf("\nxuehao:"); scanf("%ld",&((o+i)->num)); printf("xingming:"); scanf("%s",&((o+i)->name)); scanf("%f",&(o+i)->score); printf("next one:"); } void print(struct student *k,int i) { printf("\nxuehao:%ld ",(k+i)->num); printf("\nxingming:%s",(k+i)->name); printf(" \nchengji:%f",(k+i)->score); } 实验二 (1)#include<stdio.h> struct a { int data[10]; int num; }list; void initiatelist(struct a *o) { o->num=0; } void add(struct a *o,int i) { int j=1; printf("please enter 8 number!"); while(j<=i) { scanf("%d",&(o->data[j])); j++; o->num++; } } void delete1(struct a *o,int i) { int j; if(i>o->num||i<0) { printf("not exist! enter again!"); scanf("%d",&i); } for(j=i+1;j<=o->num;j++) o->data[j-1]=o->data[j]; o->num--; } void main() { struct a *p; int i,j=8; p=&list; initiatelist(p); add(p,j); for(i=1;i<=p->num;i++) printf("%d ",p->data[i]); printf("\nafer delete:\n"); delete1(p,4); for(i=1;i<=p->num;i++) printf("%d ",p->data[i]); printf("\n"); } 实验三 (1)#include<stdio.h> #define null 0 struct node { int data; struct node *next; }; void initiatesl(struct node *h) { h=(struct node *)malloc(sizeof(struct node)); h->next=null; } void shch(struct node **h,int i) { struct node *p; int j=0; p=*h; while(j<=i) { p->next=(struct node*)malloc(sizeof(struct node)); p=p->next; j++; } p->next=null; } void add(struct node **h,int j) { struct node *p; int i=0,a; p=*h; while(i<j) { printf("please enter the number!"); scanf("%d",&a); p->next->data=a; p=p->next; i++; } } int nodelong(struct node **h) { int i=0; struct node *p; p=*h; while(p->next!=null) { p=p->next; i++; } return(--i); } int find(struct node **h,int a) { struct node *p; int i=0; p=*h; while(p->next!=null) { p=p->next; i++; if(p->data==a) return(i); } return(null); } void delete1(struct node **h) { struct node *p,*o,*q; int a; p=*h; q=*h; while(p->next!=null) { p=q->next; q=q->next; a=p->data; while(p->next!=null) { o=p; p=p->next; if(a==p->data) { o->next=p->next; free(p); } } } } void print(struct node **h) { struct node *p; p=*h; while(p->next->next!=null) { p=p->next; printf("%d ",p->data); } } void main() { struct node **p; int long1,a,b; initiatesl(*p); shch(p,7); add(p,7); long1=nodelong(p); printf("the lenth of the list:%d\n",long1); print(p); printf("\n"); a=find(p,3); b=find(p,7); if(a==null) printf("not exit!\n"); else printf("the place of 3 is:%d ",a); printf("\n"); if(b==null) printf("the place of 7 is not exit!\n"); else printf("the place of 7 is:%d ",b); delete1(p); print(p); printf("\n"); } 实验四 (1)#include<stdio.h> struct shuju { int data; struct shuju *next; } (struct shuju *)shengcheng() { struct shuju *h,*s,*y; int i=1; h=(struct shuju *)malloc(sizeof(struct shuju)); s=h; while(i<=8) { printf("\nplease enter a interger:\n"): scanf("%d",&(s->data)); y=(struct shuju *)malloc(sizeof(struct shuju)); s->next=y; s=y; } s->next=null; return(h); } int chazhao(struct shuju *y,int n) { while(y->next!=null) { if(y->data=n) { return(n); } y=y->next } return(-1); } void print(struct shuju *p) { while(p->next!=null) { printf("%d ",p->data); p=p->next; } } void main() { struct shuju *h; int n,i; h=shengcheng; print(*h); printf("\n"); printf("please enter a interger whitch you want to choose:\n"); scanf("%d",&n); i=chazhao(n); if(i=-1) printf("do not exit!\n"); else printf("yes!"); } (2)#include <stdio.h> struct element {int k;}; typedef struct element s[]; int f(b,j,n ) s b;int j,n; {int low,high,mid; low=0; high=n-1; while(low<=high) { mid=(low+high)/2; if(b[mid].k==j) {printf("searching success"); return(mid); } else if(b[mid].k<j) low=mid-1; else high=mid-1; } printf("searching failed"); return(-1); } void main() {int a[8]={3,10,13,17,40,43,50,70}; f(a,43,8); f(a,5,8); } 实验五 (1) #include"stdio.h" int sortinsert(int *,int len); int main() { int i,a[10]; if(!(input=fopen("5.txt","r"))) printf("Cannot find 5.txt\n"); for(i=0;i<10;i++) scanf(input,"%d",a+i); printf("Orignal list:\n"); for(i=0;i<10;i++) printf("%d ",*(a+i)); sortinsert(a,10); printf("\nInsert sort:\n"); for(i=0;i<10;i++) printf("%d ",*(a+i)); printf("\n"); return 0; } int sort_insert(int *a,int len) { int t,i,j; for(i=0;i<len-1;i++) { t=*(a+i+1); j=i; while(j>-1&&t<*(a+j)) { *(a+j+1)=*(a+j); j--; } *(a+j+1)=t; } return 1; } 13 / 13
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服