作业一 计算器程序,编写一个可以进行加减乘除运算的程序。
解法1.
#include <stdio.h> void main() { float a,b,c; char d1; scanf("%f%c%f",&a,&d1,&b); if(d1=='/'&&b==0) printf("error "); else { if(d1=='-') c=a-b; else if(d1=='+') c=a+b; else if(d1=='*') c=a*b; else if(d1=='/') c=a/b; printf("%g%c%g=%g ",a,d1,b,c); } }
解法2.
#include<stdio.h> void main() { float a,b,c; char ch; scanf("%f%c%f",&a,&ch,&b); switch(ch) { case'+':c=a+b;printf("%f+%f=%f ",a,b,c);break; case'-':c=a-b;printf("%f-%f=%f ",a,b,c);break; case'*':c=a*b;printf("%f*%f=%f ",a,b,c);break; case'/':if (b!=0) c=a/b,printf("%f/%f=%f ",a,b,c); else printf("error ");break; default:printf("not included "); } }
作业二.编写一个程序,可以输出倒三角形,直角三角形,正三角形,菱形,反平行四边形,平行四边形六种图形。
解法:
1 #include <stdio.h> 2 void main() 3 {int a,i,j,n,m; 4 printf("please enter number 1-6 1,倒三角形 2,直角三角形 3,正三角形 4,菱形 5,反平行四边形 6,平行四边形 "); 5 scanf("%c",&a); 6 printf("please enter n,m "); 7 switch(a) 8 {case'1': 9 scanf("%d,%d",&n,&m); 10 for(i=0;i<n;i++) 11 {for(j=0;j<i;j++) 12 printf(" "); 13 for(j=0;j<2*n-1-2*i;j++) 14 printf("*"); 15 printf(" "); 16 };break; 17 case'2': 18 scanf("%d,%d",&n,&m); 19 for(i=1;i<=n;i++) 20 {for(j=1;j<=i;j++) 21 printf("*"); 22 printf(" "); 23 };break; 24 case'3': 25 scanf("%d,%d",&n,&m); 26 for(i=1;i<=n;i++) 27 {for(j=1;j<=n-i;j++) 28 printf(" "); 29 for(j=1;j<=2*i-1;j++) 30 printf("*"); 31 printf(" ");};break; 32 case'4': 33 scanf("%d,%d",&n,&m); 34 if(n%2==0) 35 printf("error "); 36 else 37 {for(i=1;i<=(n+1)/2;i++) 38 {for(j=1;j<=(n+1)/2-i;j++) 39 printf(" "); 40 for(j=1;j<=2*i-1;j++) 41 printf("*"); 42 printf(" "); 43 }; 44 for(i=0;i<=(n-1)/2;i++) 45 {for(j=0;j<=i;j++) 46 printf(" "); 47 for(j=0;j<n-2-2*i;j++) 48 printf("*"); 49 printf(" ");}}break; 50 case'5': 51 scanf("%d,%d",&n,&m); 52 for(i=1;i<=n;i++) 53 {for(j=1;j<i;j++) 54 printf(" "); 55 for(j=1;j<=m;j++) 56 printf("*"); 57 printf(" ");}break; 58 case'6': 59 scanf("%d,%d",&n,&m); 60 for(i=0;i<n;i++) 61 {for(j=1;j<n-i;j++) 62 printf(" "); 63 for(j=0;j<m;j++) 64 printf("*"); 65 printf(" ");}break; 66 default: 67 printf("not include ");break; 68 } 69 }
作业三.(1)编写一个程序,往一个从小到大排列的数组中插入任意的数字,要求插入数字后数组还是要从小到大排列。
解法:
#include <stdio.h> void main() { int i,t,h; int a[11]; printf("输入10位从小到大排列的数组 "); for(i=0;i<10;i++) scanf("%d",&a[i]); printf(" "); printf("输入你想插入的数字 "); scanf("%d",&h); a[10]=h; for(i=0;i<10;i++) if(a[i]>a[10]) {t=a[i]; a[i]=a[10]; a[10]=t; } printf("输出地结果是: "); for(i=0;i<11;i++) printf("%d",a[i]); printf(" "); }
作业三(2)编写一个程序,可以用来判断一个字符数组是否为回文结构(如“1234321“)。
解法:
#include <stdio.h> #include <string.h> void main() { int i,j,f,k; char string[100]; printf("输入你想判断的字符数组 "); gets(string); k=strlen(string); for(i=0,j=k-1,f=0;i<j;i++,j--) if(string[i]!=string[j]) { printf("这个字符数组不是回文结构 "); f=1;break;} if(f==0) printf("这个字符数组是回文结构 "); }
作业四 编写一个程序,用于判断一年中的某一天是这一年的第几天。
解法1.
#include <stdio.h> int main() { int number(int x,int y,int z); int a,b,c,d; printf("输入你要判断的日期,格式为年 月 日 "); scanf("%d%d%d",&a,&b,&c); d=number(a,b,c); printf("这一天是当年的第%d天 ",d); } int number(int x,int y,int z) { int i,n; int a[12]; int sum=0; if((x%4!=0)||(x%100==0&&x%400!=0)) {int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; for(i=0;i<y-1;i++) sum=sum+a[i];} else {int a[12]={31,29,31,30,31,30,31,31,30,31,30,31}; for(i=0;i<y-1;i++) sum=sum+a[i];} n=sum+z; return(n); }
解法2.
#include <stdio.h> int main() { int number(int x,int y,int z); int a,b,c,d; printf("输入你要判断的日期,格式为年 月 日 "); scanf("%d%d%d",&a,&b,&c); d=number(a,b,c); printf("这一天是当年的第%d天 ",d); } int number(int x,int y,int z) { int n; int m; {if((x%4!=0)||(x%100==0&&x%400!=0)) m=28; else m=29;} if(y<=2) n=(y-1)*31+z; else if(y>=8) {if(y%2==0) n=m+z+184+(y-8)*30+(y-8)/2; else n=m+z+184+(y-8)*30+(y-7)/2;} else {if(y%2==0) n=m+z+31+(y-3)*30+(y-2)/2; else n=m+z+31+(y-3)*30+(y-3)/2; } return(n); }
作业五 编写一个程序,要求输入一个五位整数,输出倒序后的五位整数。
解法:
#include <stdio.h> int main() { int reorder( int array[5]); int s,t; printf("输入一个要倒序的五位整数: "); scanf("%d",&t); int a[5]={t/10000%10,t/1000%10,t/100%10,t/10%10,t%10}; s=reorder(a); printf("倒序后的整数是: "); printf("%d",s); } int reorder( int array[5]) { int m,t; int n; for(m=0;m<3;m++) {t=array[m]; array[m]=array[4-m]; array[4-m]=t; } n=array[0]*10000+array[1]*1000+array[2]*100+array[3]*10+array[4]; return(n); }
作业六 编写双链表的程序。
解法:
#include<stdio.h> #include<malloc.h> #define NULL 0 #define LEN sizeof(struct student) struct student { long num; float score; struct student * plot; struct student * next; }; int n; struct student*creat(void) { struct student*head; struct student*p1,*p2; n=0; p1=p2=(struct student*)malloc(LEN); scanf("%1d,%f",&p1->num,&p1->score); head=NULL; while(p1->num!=0) { n=n+1; if(n==1){head=p1;p1->plot=head;} else {p2->next=p1;p1->plot=p2;} p2=p1; p1=(struct student*)malloc(LEN); scanf("%1d,%f",&p1->num,&p1->score); } p2->next=NULL; return(head); }
作业七 编写一个程序,用来判断字符串中符号是否配对。
解法:
#include <stdio.h> struct node { char b[100]; int top; }match; int main() { char a[100]; int i; printf("请输入一个不长于100个字符的算式: "); scanf("%s",a); match.top=-1; for(i=0;i<100;i++) { if(a[i]=='('||a[i]=='['||a[i]=='{') { match.top++; match.b[match.top]=a[i]; } else if(a[i]==')') { if(match.b[match.top]=='(') match.top--; else { printf("符号不配对。"); return (0); } } else if(a[i]==']'||a[i]=='}') { if(match.b[match.top]==a[i]-2) match.top--; else { printf("符号不配对。"); return (0); } } else if(a[i]=='