我如何使用参数 [英] How Do I Use Parameters
本文介绍了我如何使用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < stdio.h >
#include < string.h >
int funcA( char * ch);
void main()
{
FILE * fp;
char ch [ 100 ];
char ch2 [ 10 ];
char c;
int i = 0 ;
int a = 0 ;
int num = 0 ; //
// int i = 0;
int j = 0 ;
int k = 10 ;
int sum = 0 ;
fp = fopen( a.txt, rt);
while (feof(fp)== 0 )
{
c = fgetc(fp);
/ *
if(c =='\ 0')
{
// ch [i] ='\0';
中断
}
* /
// 否则
// {
ch [i] = c ; //
// }
printf( %c 跨度>,CH [1]);
i ++;
num ++;
}
printf( \ n%d \ n,num- 1 );
while ( 1 ){
printf( \ n);
for (i = j,a = 0 ; i< k,a>< 10;我++中,++){>
if (i> = num)
{
// funcA(ch2);
printf( :%d\\\
跨度>,FuncA的行(CH 2));
退出( 0 );
}
else
{
ch2 [a] = ch [i];
printf( %c,ch [i]);
}
}
printf( :%d \ n跨度>,FuncA的行(CH 2));
// printf(%d,su);
j + = 10 ;
k + = 10 ;
}
fclose(fp);
return 0 ;
}
int funcA( char * ch){
< span class =code-keyword> int i = 0 ;
int sum = 0 ;
printf( ();
for (i = 0 ; i< 10; i ++)
{ if (ch [i]< = 0 )
{ break ;}
sum + = ch [i];
printf( %d,ch [i]);
}
printf( ) 跨度>);
// printf(:%d \ n,sum);
返回 sum;
}
解决方案
你真的只需要一个char数组,比如char C [10]和一个用于计数的int n。
然后一个函数(或memset)使全部为零,然后读取下一个10个字符。
fread函数应该读取最多10个字符并将n设置为数字字符读取。
对于ASCII和,函数应如下所示:
int sumchars( char * arr, int n){}
然后使用for循环,就像你必须加起来一样。
另一种方法是使你的char数组有11个元素并始终将最后一个元素设置为零。
然后,你的方法可以sum char,而* arr不等于零。
Hii every one .. I have a code and now i have to use index or parameters in this code... can anybody help me.. my code is... first read a file and divide 10 elemets in first array and next 10 elements in second aaray.. After that change character into ascii value and sum of the ascii value... please help me ...
#include <stdio.h>
#include <string.h>
int funcA(char *ch);
void main()
{
FILE *fp;
char ch[100];
char ch2[10];
char c;
int i=0;
int a=0;
int num=0;//
//int i=0;
int j=0;
int k=10;
int sum=0;
fp=fopen("a.txt","rt");
while(feof(fp)==0)
{
c=fgetc(fp);
/*
if(c=='\0')
{
//ch[i]='\0';
break
}
*/
//else
//{
ch[i]=c;//
//}
printf("%c",ch[i]);
i++;
num++;
}
printf("\n%d\n",num-1);
while(1){
printf("\n");
for(i=j,a=0;i<k,a><10;i++,a++){>
if(i>=num)
{
//funcA(ch2);
printf(":%d\n",funcA(ch2));
exit(0);
}
else
{
ch2[a]=ch[i];
printf("%c",ch[i]);
}
}
printf(":%d\n",funcA(ch2));
//printf("%d",su);
j+=10;
k+=10;
}
fclose(fp);
return 0;
}
int funcA(char *ch){
int i=0;
int sum=0;
printf("(");
for(i=0;i<10;i++)
{if(ch[i]<=0)
{ break;}
sum+=ch[i];
printf("%d",ch[i]);
}
printf(")");
//printf(": %d\n",sum);
return sum;
}
解决方案
You really only need one char array, say char C[10] and an int n for counting.
Then one function (or memset) to make all zero, before reading up to next 10 chars.
The fread function should read in max 10 chars and set n to number of chars read.
For ASCII sum, the function should look like:
int sumchars(char* arr, int n){}
Then use for loop like you have to add up the chars.
Another way is to make your char array with 11 elements and always set last element to zero.
Then, your method can sum chars while *arr is not equal to zero.
这篇关于我如何使用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文