c语言上机

一、程序填空题 请补充函数proc,该函数的功能是按条件删除一个字符串指定字符一半的数目,具体要求如下:如果该字符串所包含的指定字符的个数是奇数,则不删除,如果其数目是偶数,则删除原串后半部分的指定字符。其中,str指向原字符串,删除后的字符串存放在b所指的数组中,c中存放指定的字符。例如,当str输入“abcabcabcab”,c=‘b’时,b的输出为“abcabcaca”;如果str的输入为“abcabcabca”,则b的输出为“abcabcabca”。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数proc的横线上填入所编写的若干表达式或语句。 试题程序: #include <stdlib.h> #include <stdio.h> #include <conio.h> #define M 80 void proc(char str〔〕,char b〔〕,char c) { int i=0,j=0; int n=0; int m=0; while(str〔i〕!=‘\0’) { if(str〔i〕==c) n++; i++; } 【1】; if(n%2) { while(str〔j〕!=‘\0’) { b〔j〕=str〔j〕; j++; } b〔j〕=‘\0’; } else { while(str〔i〕!=‘\0’) { b〔j++〕=str〔i〕; if(str〔i〕==c) m++; if((m>n/2)&&(str〔i〕==c)) 【2】; i++; } 【3】; } } void main() { char str〔M〕,b〔M〕; char c; system("CLS"); printf("Enter the string:\n"); gets(str); printf("Enter the character of the string deleted:"); scanf("%c",&c); proc(str,b,c); printf("The new string is : %s\n",b); } 二、程序改错题 下列给定的程序中,proc函数的功能是:将str所指字符串中每个单词的最后一个字母改成大写(这里的“单词”是指有空格隔开的字符串)。例如,若输入:How do you do则输出:HoW dO yoU dO请修改程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> #include <stdio.h> void proc(char *str) { int k=0; for ( ;*str;str++) if (k) { /*******found*******/ if (str==' ') { k=0; /*******found*******/ *str=toupper ( *(str-1)); } } else k=1; } void main() { char chrstr〔64〕; int d; system("CLS"); printf("\nPlease enter an English sentence within 63 letters: "); gets(chrstr); d=strlen(chrstr); chrstr〔d〕=' '; chrstr〔d+1〕=0; printf("\nBofore changing:\n %s",chrstr); proc(chrstr); printf("\nAfter changing:\n %s",chrstr); } 三、程序设计题 请编写函数proc,函数的功能是求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。例如,若二维数组中的值为: 0 1 2 3 4 5 9 7 4 5 4 3 8 3 6 3 5 6 8 7 则函数值为59。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数proc的花括号中填入所编写的若干语句。 试题程序: #include <stdlib.h> #include <conio.h> #include <stdio.h> #define M 4 #define N 5 int proc( int a 〔M〕〔N〕) { } void main() { int aa〔M〕〔N〕={{ 0,1,2,3,4},{5,9,7,4,5}, {4,3,8,3,6},{3,5,6,8,7}}; int i, j, y; system("CLS"); printf ("The original data is:\n "); for (i=0; i<M;i++) { for (j=0; j<N;j++) printf("%6d ",aa〔i〕〔j〕); printf("\n "); } y=proc(aa); printf("\nThe sun: %d\n ",y); printf("\n "); }

第1个回答  2008-06-13
#include<stdio.h>
long func(int N)
{int i;
long m=0;
for(i=1;i<=N;i++)
m=m+i;
return m;}

void main()
{int N,i;
long sum=0;
scanf("%d",&N);
for(i=1;i<=N;i++)
sum=sum+func(i);
printf("%d",sum);}

func函数的作用是求1到n的累加和,
若想让func求l+(l+2)+(l+2+3)+(1+2+3+4)
+……+(1+2+3+4+……+n)的和,则如下

#include<stdio.h>
long func(int N)
{int i,j;
long m=0;
for(i=1;i<=N;i++)
for(j=1;j<=i;j++)
m=m+j;
return m;}

void main()
{int N;
long sum=0;
scanf("%d",&N);
sum=func(i);
printf("%d",sum);}
第2个回答  2008-06-13
写代码,要注意换行,增加代码的可读性!

这种不是代码的,更应该换行的啊!
第3个回答  2008-06-19
#include<stdio.h>
long func(int N)
{int i;
long m=0;
for(i=1;i<=N;i++)
m=m+i;
return m;}

void main()
{int N,i;
long sum=0;
scanf("%d",&N);
for(i=1;i<=N;i++)
sum=sum+func(i);
printf("%d",sum);}本回答被提问者采纳
第4个回答  2008-06-13
也不调整一个题目格式,这看起来太头疼了
相似回答