不明白原理就在線聊
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比麻栗坡網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式麻栗坡網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋麻栗坡地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。
#include stdio.h
#include stdlib.h
#include memory.h
#define m 48
struct shuz{
int id;
int num;
};
int main(void)
{
int i=0;
struct shuz sum[m];
FILE *fp;
if((fp=fopen("a.txt","r"))==NULL)
{
fprintf(stderr,"file open error!");
exit(1);
}
while(fscanf(fp,"%d %d",sum[i].id,sum[i].num)!=EOF)
{
printf("%d %d\n",sum[i].id,sum[i].num);
i++;
if (ferror(fp)) /* 使用ferror函數(shù)來判斷讀取數(shù)據(jù)有無錯(cuò)誤 */
{
fprintf(stderr,"讀取數(shù)據(jù)有錯(cuò)誤!\n");
exit(1);
}
}
int k,j;
for(k=0;ki-1;k++)
{
if(sum[k].id==-1)
continue;
int id=sum[k].id;
for(j=k+1;ji;j++)
{
if(sum[j].id==-1)
continue;
if(sum[j].id==id)
{
sum[k].num+=sum[j].num;
sum[j].id=-1;
}
}
}
printf("%c",'\n');
for(j=0;ji;j++)
{
if(sum[j].id!=-1)
printf("%d %d\n",sum[j].id,sum[j].num);
}
return 0;
}
int qiuhe(int *arry) //求和函數(shù) 和最大不能超過int,如果需要不受限制,則要改進(jìn)存儲(chǔ)方式
{
int sum=0;
for(;*arry!='\0';arry++){
sum+=*arry;
}
return sum;
}
#includecstdio
#includeiostream
using?namespace?std;
struct?Myfloat?{
int?son,mother;
Myfloat(int?x=0,int?y=0)?{
son=y;
mother=x;
}
void?pt()?{
printf("%d/%d",son,mother);
}
};
int?gcd(int?x,?int?y)?{
int?z?=?y;
while(x%y!=0)?{
z?=?x%y;
x?=?y;
y?=?z;
}
return?z;
}
int?lcm(int?x,?int?y){
return?x*y/gcd(x,y);
}
Myfloat?operator?+?(Myfloat?F1,Myfloat?F2)?{
int?bigmother=lcm(F1.mother,F2.mother);
int?bigson=F1.son*bigmother/F1.mother+F2.son*bigmother/F2.mother;
int?biggcd=gcd(bigson,bigmother);
return?Myfloat(bigmother/biggcd,bigson/biggcd);
}
int?main()?{
//try:
Myfloat?a(5,4);?//?4/5
Myfloat?b(6,7);?//?7/6
Myfloat?c=a+b;
c.pt();//?59/30
return?0;
}
利用最大公約數(shù)和最小公倍數(shù)進(jìn)行計(jì)算