C++程序设计 从键盘上输入一行字符串,存入一个字符数组,然后输出该字符串

并把该字符串中的小写字母转换为大写字母,输出大文件test.txt中,然后从文件中读取字符串并显示出来

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char tmp;
cout<<"Please input some words(The amounts less 200 is OK else will lose words):"<<endl;

ofstream fout("test.txt");
while(cin.get(tmp))
{
if(tmp=='\n')
break;
if(tmp<='z'&&tmp>='a')
tmp=tmp-32;
fout<<tmp<<flush;
}
fout.close();

ifstream fin("test.txt");
char ch[200];
fin.getline(ch,200);
cout<<ch<<endl;
fin.close();
return 0;
}
干嘛非要用数组呢,c++的string字符串类型不更安全。
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-11
#include <iomanip>
class Arr
{
public:
Arr(){x=0;y=0;p=NULL;}
~Arr()
{
for(int i=0;i<x;i++)
delete[] p[i];
delete[] p;
}
int getX();
int getY();
int **getP();
void inputArr(int x,int y);
private:
int x,y;
int **p;
};
void Arr::inputArr(int m,int n)
{
int i,j;
x=m;
y=n;
p=new int*[m];
for (i=0;i<m;i++)
p[i]=new int[n];

for (i=0;i<x;i++)
{
for(j=0;j<y;j++)
cin>>p[i][j];
}
}
int Arr::getX()
{
return x;
}
int Arr::getY()
{
return y;
}
int** Arr::getP()
{
return p;
}
int** multiArr(Arr a,Arr b)
{
int i,j,k;
int **a1,**b1,**c1;
c1=new int*[a.getX()];
for (i=0;i<a.getX();i++)
c1[i]=new int[b.getY()];

a1=a.getP();
b1=b.getP();
for (i=0;i<a.getX();i++)
{
for (j=0;j<b.getY();j++)
{
for (k=0;k<a.getY();k++)
{
c1[i][j]=a1[i][k]*b1[k][j];
}
}
}
return c1;
}
int main(int argc, char* argv[])
{
Arr a,b;
int **c;
int x1,y1,x2,y2,i;
cout<<"input the row and column of first array:"<<endl;
cin>>x1>>y1;
a.inputArr(x1,y1);
cout<<"input the row and column of second array:"<<endl;
cin>>x2>>y2;
b.inputArr(x2,y2);
if(a.getY()==b.getX())
{
c=new int*[a.getX()];
for (i=0;i<a.getX();i++)
c[i]=new int[b.getY()];

c=multiArr(a,b);
cout<<"the result is:"<<endl;
for (i=0;i<a.getX();i++)
{
for(int j=0;j<b.getY();j++)
cout<<setw(6)<<c[i][j];
cout<<endl;
}
}
else
cout<<"the two array don't multiplicate!"<<endl;
return 0;
}
第2个回答  2011-11-11
#include <iomanip>
class Arr
{
public:
Arr(){x=0;y=0;p=NULL;}
int getX();
int getY();
int **getP();
void inputArr(int x,int y);
private:
int x,y;
int **p;
};
void Arr::inputArr(int m,int n)
{
int i,j;
x=m;
y=n;
p=new int*[m];
for (i=0;i<m;i++)
p[i]=new int[n];

for (i=0;i<x;i++)
{
for(j=0;j<y;j++)
cin>>p[i][j];
}
}
int Arr::getX()
{
return x;
}
int Arr::getY()
{
return y;
}
int** Arr::getP()
{
return p;
}
int** multiArr(Arr a,Arr b)
{
int i,j,k;
int **a1,**b1,**c1;
c1=new int*[a.getX()];
for (i=0;i<a.getX();i++)
c1[i]=new int[b.getY()];

a1=a.getP();
b1=b.getP();
for (i=0;i<a.getX();i++)
{
for (j=0;j<b.getY();j+)
{
for (k=0;k<a.getY();k++)
{
c1[i][j]=a1[i][k]*b1[k][j];
}
}
}
return c1;
}
int main(int argc, char* argv[])
{
Arr a,b;
int **c;
int x1,y1,x2,y2,i;
cout<<"input the row and column of first array:"<<endl;
cin>>x1>>y1;
a.inputArr(x1,y1);
cout<<"input the row and column of second array:"<<endl;
cin>>x2>>y2;
b.inputArr(x2,y2);
if(a.getY()==b.getX())
{
c=new int*[a.getX()];
for (i=0;i<a.getX();i++)
c[i]=new int[b.getY()];

c=multiArr(a,b);
cout<<"the result is:"<<endl;
for (i=0;i<a.getX();i++)
{
for(int j=0;j<b.getY();j++)
cout<<setw(6)<<c[i][j];
cout<<endl;
}
}
else
cout<<"the two array don't multiplicate!"<<endl;
return 0;
}
相似回答