zoukankan      html  css  js  c++  java
  • c++输入输出流读取文件

    1. #include<stdio.h>
    2. #include<string.h>
    3. #include "fstream"
    4. using namespace std;
    5. ifstream fin("read.txt");
    6. ofstream fout("result1.txt");
    7. void zero(char *ch, int len)
    8. {
    9. int i;
    10. for(i=0; i<len; ++i)
    11. {
    12. ch[i] -= '0';
    13. }
    14. }
    15. int main()
    16. {
    17. char a[1000], b[1000];
    18. char c[1002];
    19. int alen, blen, i, j, k, T, m=1, yes;
    20. //scanf("%d",&T);
    21. fin>>T;
    22. getchar();
    23. while( T-- )
    24. {
    25. //scanf("%s", &a);
    26. //scanf("%s", &b);
    27. fin>>a>>b;
    28. //printf("Case %d: ", m++);
    29. //printf("%s + %s = ", a, b);
    30. fout<<"Case %d:"<<endl<<m++<<a<<" + "<<b<<" = ";
    31. alen = strlen(a);
    32. blen = strlen(b);
    33. for(i=0; i<1002; ++i)
    34. {
    35. c[i] = 0;
    36. }
    37. zero(a, alen);
    38. zero(b, blen);
    39. k = yes = 0;
    40. for(i=alen-1, j=blen-1; (i>=0)&&(j>=0); ++k, --i, --j )
    41. {
    42. if( (a[i]+b[j]+c[k]) >= 10 )
    43. {
    44. c[k+1] = 1;
    45. }
    46. c[k] = (a[i]+b[j]+c[k])%10 ;
    47. }
    48. for( ; i>=0; ++k, --i)
    49. {
    50. if( a[i]+c[k] >=10 )
    51. {
    52. c[k+1] = 1;
    53. }
    54. c[k] = (a[i]+c[k])%10;
    55. }
    56. for( ; j>=0; ++k, --j)
    57. {
    58. if( b[j]+c[k] >=10 )
    59. {
    60. c[k+1] = 1;
    61. }
    62. c[k] = (b[j]+c[k])%10;
    63. }
    64. if( c[k]==0 )
    65. {
    66. --k;
    67. }
    68. for(i=k; i>=0; --i)
    69. {
    70. if( c[i] !=0 )
    71. {
    72. yes = 1;
    73. break;
    74. }
    75. }
    76. if( yes!=0 )
    77. {
    78. for(; k>=0; --k)
    79. {
    80. //printf("%d", c[k]);
    81. fout<<c[k];
    82. }
    83. }
    84. else
    85. {
    86. //printf("0");
    87. fout<<"0";
    88. }
    89. if( T!=0 )
    90. {
    91. //printf(" ");;
    92. fout<<endl<<endl;
    93. }
    94. else
    95. {
    96. //printf(" ");
    97. fout<<endl;
    98. }
    99. }
    100. return 0;
    101. }





    附件列表

    • 相关阅读:
      捡到一本<C++ Reference>
      题目1008:最短路径问题
      题目1014:排名
      题目1080:进制转换
      题目1081:递推数列
      题目1086:最小花费
      题目1076:N的阶乘
      题目1035:找出直系亲属
      在Mac上搭建Jenkins环境
      获取鼠标点击UGUI,先对于特定物体的相对坐标
    • 原文地址:https://www.cnblogs.com/sober-reflection/p/bab7914b01809729a42d3d17089ba91e.html
    Copyright © 2011-2022 走看看