input.txt中的内容为: 11 12 22 55 66
用C++编写程序:将文件中的数字相加并输出在另一文件
2.
由用户输入N并输出图形,如:若N=4,则输出
4444444
4333334
4322234
4321234
4322234
4333334
4444444
第2个画圈圈还有点意思
[CODE]#include <iostream>
using namespace std;
void display(int **pi,int n){
for (int i = 0;i < n;++i){
for (int j = 0;j < n;++j)
cout << pi[i][j];
cout << endl;
}
}
void draw(int num){
int n = (num-1)*2+1;
int i,j,t1,t2,k;
int **pi = new int*[n];
for (k = 0;k < n;++k)
pi[k] = new int[n];
for (int cnt = num;cnt > 0;--cnt){
i = j = num-cnt;
t1 = (cnt-1)*2+1+(num-cnt);
t2 = num-cnt;
for (;j < t1;++j)
pi[i][j] = cnt;
for (--j;i < t1;++i)
pi[i][j] = cnt;
for (--i;j >= t2;--j)
pi[i][j] = cnt;
for (++j;i >= t2;--i)
pi[i][j] = cnt;
}
display(pi,n);
for (k = 0;k < n;++k)
delete [] pi[k];
delete [] pi;
}
int main(){
draw(4);
system("pause");
return 0;
}[/CODE]
[此贴子已经被作者于2007-8-26 2:42:51编辑过]
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifs;
ofstream ofs;
int a, sum=0;
ifs.open("input.txt");
ofs.open("output.txt");
while(ifs>>a)
{
sum+=a;
}
ofs<<sum;
ifs.close();
ofs.close();
return 0;
}