读音为xiang的字:C程序上机考试设计题(附源代码

来源:百度文库 编辑:九乡新闻网 时间:2024/03/28 16:52:12
C程序上机考试设计题(附源代码)  

1.按要求设计一个简单的计数器程序,能够按键盘输入的操作符和整数,做到简单的加减乘除运算并输出结果。

#include
int main()
{
int a=0,b=0;
char ch;
printf("请输入表达式1、运算符、表达式2:\n");
scanf("%d%c%d",&a,&ch,&b);
switch(ch)
{
case '+': printf("%d+%d=%d\n",a,b,a+b);break;
case '-': printf("%d-%d=%d\n",a,b,a-b);break;
case '*': printf("%d*%d=%d\n",a,b,a*b);break;
case '/': printf("%d/%d=%d\n",a,b,a/b);break;
default: printf("字符错误!");
}
return 0;
}

2.按下列要求编制一个程序。

 

1.函数input()输入5个学生的4门功课成绩

2.函数total()求出每个学生的总分,并输出

3.函数average()求出每门功课的平均分,并输出。

4.在主函数main()中对上面函数进行调用以实现程序功能

#include

#define N 5

#define M 4

float score[N][M];

float total_stu[N],a_cour[M];

void input()

{

       int i,j;

       for(i=0;i

       {

              printf("\ninput score of student %2d:\n",i+1);

              for(j=0;j

              scanf("%f",&score[i][j]);

       }

}

void average()

{

       int i,j;

       float s;

       for(j=0;j

       {

              s=0;

              for(i=0;i

              s+=score[i][j];

              a_cour[j]=s/(float)N;

       }

       for(j=0;j

       printf("%-5.2f  ",a_cour[j]);

}

void total()

{

       int i,j;

       float sum;

       for(i=0;i

       {

              sum=0;

       for(j=0;j

       sum=sum+score[i][j];

       total_stu[i]=sum;

       }

    for(i=0;i

    printf("%5.2f ",total_stu[i]);

}

void main()

{

       input();

       printf("output everystudents'total score:\n");

       printf("  No1    No2    No3    No4    No5\n");

       total();

       printf("\n");

       printf("output everycourse's average score:\n");

       printf("  cour1  cour2  cour3   cour4\n");

       average();

}

   

3.编程查找指定字符在字符串中第一次出现的位置。若找到则从该字符开始打印余下的字符串,找不到,则打印“No Found”.要求在主程序中输入被处理的字符串和指定的字符,查找指定字符在字符串中第一次出现的位置用函数find(s,c)来完成。

 

#include

#include

#define Maxsize 100

void main()

{

       void find(char a[],char);

       char str[80];

       char p;

       printf("please enter a string:\n");

       gets(str);

       printf("please enter an charater:\n");

    p=getchar();

       find(str,p);

}

void find(char a[Maxsize],char M)

{

       int length,i;

       length=strlen(a);

       for(i=0;i

       {

              if(a[i]= =M)

              break;

       }

       if(i= =length)

       puts("No Found.\n");

       else

       for(;i

       printf("%c",a[i]);

}

4.设计一个在字符串中删除指定字符的C程序。要求在主函数中输入被处理的字符串和指定的字符,调用函数find(s,c)查找并返回指定字符在字符串中第一次出现的位置,然后在主函数里作删除并输出处理后的字符串。若字符串中不存在指定字符,则原串输出。

 

#include

#include

void main()

{

       int find(char str[],char C);

       int i,j;

       char str1[80],ch;

       gets(str1);

       ch=getchar();

       printf("The charater '%c' appears for the first time is the %dth charater.\n",ch,find(str1,ch)+1);

        for(i=j=0;str1[i]!='\0';i++)  

      if(str1[i]!=ch)  

      str1[j++]=str1[i];  

      str1[j]='\0';

      printf("now the string turns:\n");

      printf("%s",str1);

}

int find(char str[80],char C)

{

       int length ,i;

       length=strlen(str);

       for(i=0;i

       if(str[i]= =C)

       break;

       return i;

}

 

5.编程实现从键盘为一个6×6 整型数组赋值,并将每一行的最小值和最大值及其下标显示出来(注:书上是分开求最小和最大的,为方便,我把它和在一起了,如果设计时分开的话,稍加改动就OK了)

 

#include

void main()

{

       int i,j,min_col=0,min_row=0,max_col=0,max_row=0;

    int min,max;

       int matr[6][6];

       puts("please enter the maxtrix's number:");

       for(i=0;i<6;i++)

       for(j=0;j<6;j++)

       scanf("%d",&matr[i][j]);

    for(i=0;i<6;i++)

       {

              min=matr[i][0];

           max=matr[i][0];

           for(j=0;j<6;j++)

           {

           

              if(matr[i][j]

            {

              min=matr[i][j];

              min_col=i;

              min_row=j;

            }

              if(matr[i][j]>max)

            {

               max=matr[i][j];

               max_col=i;

               max_row=j;

            }

           }

           printf("The %d line's mininum and maxinum are %d and %d,with their position:[%d][%d]and[%d][%d]\n",i+1,min,max,

       min_col,min_row,max_col,max_row); 

        }

}

6.编写程序完成从键盘为一个6*6整型二维数组输入数据,求出其最大元素的下标后输出。

#include

#define M 6

#define N 6

void main()

{

       int arr[M][N];

       int i,j,max_col,max_row;

       printf("enter a 6*6 array:\n");

       for(i=0;i

       for(j=0;j

       scanf("%d",&arr[i][j]);

       max_col=0;

       max_row=0;

       for(i=0;i

       {

           for(j=0;j

           {

              if(arr[i][j]>arr[max_col][max_row])

              {

                     max_col=i;

                     max_row=j;

              }

           }

       }

       printf(“col=%d,row=%d,max=%d\n”,max_col,max_row,arr[max_col][max_row]);

}

7.编写程序将1-100之间的所有不能被7整除的数打印出来。

 #include

void main()

{

       int i,count=0;

       for(i=1;i<=100;i++)

       {

              if(i%7!=0)

           printf("%-5d",i);

              count++;

       if(count%10= =0)

       printf("\n");

       }

}

      

8.编写函数pi,其功能是根据以下近似公式求圆周率的值。

 

#include

#include

void main()

{

       double pi(int);

       int n;

       printf("enter an integer number:\n");

       scanf("%d",&n);

       printf("the pi's similar value:%f",pi(n));

}

double pi(int t)

{

       int i;

       double s=1;

       for(i=2;i<=t;i++)

       s=s+1.0/(i*i);

       return (sqrt(6*s));

}

9.编写一个程序,求表达式1+(1+2)+(1+2+3)+……+(1+2+…+10)的值。

 

#include

#define N 10

void main()

{

       int i,j;

       int s=0;

       for(i=1;i<=N;i++)

       for(j=1;j<=i;j++)

       s=s+j;

       printf("%d",s);

}

10.编写一个程序,求分数序列2/1,3/2,5/3,8/5,13/8,……的前20项之和。

#include

void main()

{

 int i,t,n=20;

 float a=2,b=1,s=0;

 for(i=1;i<=n;i++)

 {

   s=s+a/b;

   t=a;

   a=a+b;

   b=t;

}

printf(“sum=%9.6f\n”,s);

}