C代码与方法

  1 //-----------------------------1
  2 #include<stdio.h>
  3 void main()
  4 {
  5     char str1[30]="Very Good!",str2[30];
  6     char *p1,*p2;
  7     p1=str1;
  8     p2=str2;
  9     printf("复制前str1数组存放的串为:%s\n",str1);
 10     while (*p1!='\0')
 11     {
 12         *p2=*p1;
 13         p1++;
 14         p2++;
 15 
 16     }
 17     *p2='\0';//此行不可去
 18     printf("复制后str2数组存放的串为:%s\n",str2);
 19 }
 20 //------------------------------------------2
 21 #include<stdio.h>
 22 void main()
 23 {
 24     int i,a[6];
 25     int *p[6];
 26     printf("input array a[6]:\n");
 27     for(i=0;i<6;i++)
 28     {
 29         scanf("%d",&a[i]);
 30         p[i]=&a[i];
 31     }
 32     printf("地址\t值\n");
 33     for(i=0;i<6;i++)
 34     {
 35         printf("%x ",p[i]);//%x以16进制位输出
 36         printf("%d\n",*p[i]);
 37     }
 38 }
 39 //---------------------------------3
 40 #include<string.h>
 41 #include<stdio.h>
 42 void main()
 43 {
 44     char *course[4]={"English","Mathematics","Data structure","Internet"};
 45     char *temp;
 46     int i,j;
 47     printf("before sort:\n");
 48     for(i=0;i<4;i++)
 49         puts(course[i]);
 50     for(i=0;i<4;i++)//冒泡排序
 51         for(j=i+1;j<4;j++)
 52         {
 53             if(strcmp(course[i],course[j])>0)
 54             {
 55                 temp=course[i];
 56                 course[i]=course[j];
 57                 course[j]=temp;
 58             }
 59         }
 60         printf("\nsfter sort:\n");
 61         for(i=0;i<4;i++)
 62             puts(course[i]);
 63 }
 64 //--------------------------4
 65 #include<stdio.h>
 66 void main()
 67 {
 68     void swap(int *i,int *j);
 69     int a,b;
 70     printf("input a,b:\n");
 71     scanf("%d%d",&a,&b);
 72     printf("交换前:\n");
 73     printf("a=%d,b=%d",a,b);
 74     swap(&a,&b);
 75     printf("交换后:\n");
 76     printf("a=%d,b=%d\n",a,b);
 77 }
 78 void swap(int *i,int *j)
 79 {
 80     int temp;
 81     temp=*i;
 82     *i=*j;
 83     *j=temp;
 84 }
 85 //----------------------------5
 86 #include<stdio.h>
 87 void main()
 88 {
 89     char *str_cat(char *s1,char *s2);
 90     char str1[30],str2[10];
 91     printf("input str1(目标串),str2:\n");
 92     gets(str1);
 93     gets(str2);
 94     printf("str1:\n");
 95     puts(str_cat(str1,str2));
 96 }
 97 char *str_cat(char *s1,char *s2)//* 属于 char,指向函数的指针,给该函数一个地址
 98 {
 99     char *p1=s1,*p2=s2;
100     while (*p1!='\0')
101         p1++;
102     while(*p2!='0')
103         *p1++=*p2++;
104     *p1='\0';
105     return s1;
106 }
107 //-------------------------------------6
108 #include<stdio.h>
109 void main()
110 {
111     int max(int x,int y);
112     int (*p)(int,int);//定义函数指针
113     int a,b,c;
114     p=max;//给p赋初值,将函数max的入口地址赋给指针变量p,是p指向max函数
115     printf("input a,b:\n");
116     scanf("%d%d",&a,&b);
117     c=(*p)(a,b);//调用*p就是调用max,该语句与c=max(a,b);等价
118     printf("a=%d,b=%d,max=%d\n",a,b,c);
119 }
120 int max(int x,int y)
121 {
122     return(x>y?x:y);
123 }
124 //-------------------------------7
125 #include<stdio.h>
126 #define N 5
127 struct student
128 {
129     int num;//学号
130     char name[20];//姓名
131     float score[3];//三门课的成绩
132 };
133 void average(struct student stu[N])//定义求平均值函数
134 {
135     int i,j;
136     float aver[N];//存放每个学生三门课的平均值
137     for(i=0;i<N;i++)
138     {
139         aver[i]=0;
140         for(j=0;j<3;j++)
141             aver[i]=aver[i]+stu[i].score[j];//求总分
142         aver[i]=aver[i]/3;//求每个学生的平均值
143      }
144 printf("output information:\n");//输出每个学生的信息
145 for(i=0;i<N;i++)
146 {
147         printf("num=%d,name=%s,",stu[i].num,stu[i].name);//与下面的输出成一行输出
148         for(j=0;j<3;j++)
149             printf("score=%f,",stu[i].score[j]);
150         printf("average=%f",aver[i]);
151         printf("\n");
152 }
153 }
154 void main()
155 {
156     struct student stu[N];//定义结构体数组stu,每个元素都是内部变量,仅在main函数内有效
157 
158     int i,j;
159     printf("input information:\n");
160     printf("学号,姓名,三门课的成绩:\n");
161     for(i=0;i<N;i++)
162     {
163         scanf("%d%s",&stu[i].num,&stu[i].name);
164         for(j=0;j<3;j++)
165             scanf("%f",&stu[i].score[j]);
166     }
167     average(stu);//调用average函数,实参为结构体数组名,此为地址传递
168 }
169 //------------------------8
170 #include<stdio.h>
171 void main()
172 {
173     int max(int x,int y);
174     int (*p)(int,int);//定义函数指针
175     int a,b,c;
176     p=max;//给p赋初值,将函数max的入口地址赋给指针变量p,是p指向max函数
177     printf("input a,b:\n");
178     scanf("%d%d",&a,&b);
179     c=(*p)(a,b);//调用*p就是调用max,该语句与c=max(a,b);等价
180     printf("a=%d,b=%d,max=%d\n",a,b,c);
181 }
182 int max(int x,int y)
183 {
184     return(x>y?x:y);
185 }
186 //-------------------------------9
187 #include<stdio.h>
188 #define N 5
189 struct student
190 {
191     int num;//学号
192     char name[20];//姓名
193     float score[4];//三门课的成绩与平均分,score[3]存放平均分
194 };
195 struct student stu[N];
196 void input()
197 {
198     int i,j;
199     printf("input information:\n");
200     printf("学号,姓名,三门课的成绩:\n");
201     for(i=0;i<N;i++)
202     {
203         scanf("%d%s",&stu[i].num,stu[i].name);
204         for(j=0;j<3;j++)
205             scanf("%f",&stu[i].score[j]);
206     }
207 }
208 void average()
209 {
210     int i,j;
211     for(i=0;i<N;i++)
212     {
213         stu[i].score[3]=0;
214         for(j=0;j<3;j++)
215             stu[i].score[3]=stu[i].score[3]+stu[i].score[j];
216         stu[i].score[3]=stu[i].score[3];
217     }
218 }
219 void output()
220 {
221     int i,j;
222     printf("output information:\n");
223     for(i=0;i<N;i++)
224     {
225         printf("num=%d,name=%s,",stu[i].num,stu[i].name);
226         for(j=0;j<3;j++)
227             printf("score=%f",stu[i].score[j]);
228         printf(",average=%f",stu[i].score[3]);
229         printf("\n");
230     }
231 }
232 void main()
233 {
234     input();
235     average();
236     output();
237 }
238 //---------------------10
239 #include<stdio.h>
240 #define N 4
241 struct student
242 {
243     int num;
244     char name[20];
245     int score;
246 }s[N],t;
247 void main()
248 {
249     int i,j;
250     printf("学号,姓名,成绩:\n");
251     for(i=0;i<N;i++)
252         scanf("%d%s%d",&s[i].num,s[i].name,&s[i].score);
253 
254     for(i=1;i<N;i++)
255         for(j=0;j<N-i;j++)
256             if(s[j].score>s[j+1].score)//一个分数之差,换了一整套的方法
257             {
258                 t=s[j];
259                 s[j]=s[j+1];
260                 s[j=1]=t;
261             }
262             printf("按成绩排序输出:\n");
263             for(i=0;i<N;i++)
264                 printf("%d %s %d\n",s[i].num,s[i].name,s[i].score);
265 }

 

posted @ 2013-06-11 22:56  herizai  阅读(239)  评论(0编辑  收藏  举报