如何在C中添加int? [英] How do I add an int in C?

查看:115
本文介绍了如何在C中添加int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C中有一个代码,我需要添加更多东西。检查告诉我你是否可以帮助我。





 #include< stdio.h> 
#include< limits.h>
int 进程(FILE * f){

int a ,b;
int total = 1 ; // 添加a大于b的次数

/ * 现在我想在每次发现
a数量大于另一个$ b $时添加总计++ b我在哪里需要添加它?
因此,当我获得输出时,例如9行,以显示我在
结束的过程中新行中的数字10 - 因为int bb的
起始值是1并且将数字
10保存在名为out.txt的新文件中* /

如果(f == 0 return 0 ;
if (fscanf(f, % d,& a)!= 1 return INT_MIN;
if ((b = process(f))== INT_MIN) return a;
如果(a > b) return printf( %d>%d,现在总数为%d \ n,a,b,total),a;
返回 b;
}
int main( void ){
FILE * fp = fopen( xxx.txt r);
过程(fp);
fclose(fp);

return 0 ;
}

解决方案

如果我理解你的话,你需要改变这个:

< pre lang =c#> if (a > b)返回 printf( %d>%d,现在总数为%d \ n ,a,b,total),a;

对此:

 如果(a >  b)
{
总++;
return printf( %d> %d和total现在是%d \ n,a,b,total);
}



这个:

  int  total =  1 ;  //  添加a大于b的次数 

对此:

 静态  int  total =  1 ;  //  添加a大于b的次数 





刚才注意到代码中的行尾垃圾... [/ edit]






你不需要使用递归调用来实现这个功能...



只需读取文件行,然后提取字符并检查您需要哪个...



如果这个程序你应该工作然后找到该区域当递归函数调用返回到其初始阶段并显示total时。



此外,您需要确保将total变量声明为全局值..或整数指针..





谢谢,

Ullas


I have a code in C and I need to add omething more. Check to tell me if you can help me.


#include <stdio.h> 
#include <limits.h> 
int process( FILE *f ) { 
    
	int a, b; 
	int total = 1;						//adding how many times "a" was larger than "b"
	
	/*  and now I want to add total++  every time 
		a number found that is greater than another
		Where do I need to add it? 
		So when I get output with eg 9 lines to show me in the
		end of the process the number 10 in a new line -cause the 
		start value of the int total is 1 and to save the number
		10 in a new file called out.txt */
    if ( f == 0 ) return 0; 
    if ( fscanf( f, "%d", &a ) != 1 ) return INT_MIN; 
    if ( (b= process( f )) == INT_MIN ) return a; 
    if ( a > b ) return printf( "%d > %d and total now is %d\n", a, b, total ), a; 
    return b; 
} 
int main( void ) { 
    FILE *fp= fopen( "xxx.txt", "r" ); 
    process( fp ); 
    fclose( fp ); 
	
    return 0; 
}

解决方案

If I understand you right, you need to change this:

if ( a > b ) return printf( "%d > %d and total now is %d\n", a, b, total ), a;

To this:

if ( a > b )
{
total++;
return printf( "%d > %d and total now is %d\n", a, b, total );
}


And this:

int total = 1;                      //adding how many times "a" was larger than "b"

To this:

static int total = 1;                       //adding how many times "a" was larger than "b"



[edit]Just noticed the end of line rubbish in your code...[/edit]


Hi,

You don't need to use recursive call for this functionality...

Just the read the line of file and then extract the character and check in which was you need to...

If this the program you should work then find the area when the recursive function call was returned to its initial stage and display the "total" their.

Also you need to make sure that the "total" variable was declared as global value.. or integer pointer..


Thanks,
Ullas


这篇关于如何在C中添加int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆