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

2024-12-14 13:25:51
推荐回答(3个)
回答1:

#include
#include
using namespace std;

int main()
{
char tmp;
cout<<"Please input some words(The amounts less 200 is OK else will lose words):"<
ofstream fout("test.txt");
while(cin.get(tmp))
{
if(tmp=='\n')
break;
if(tmp<='z'&&tmp>='a')
tmp=tmp-32;
fout< }
fout.close();

ifstream fin("test.txt");
char ch[200];
fin.getline(ch,200);
cout< fin.close();
return 0;
}
干嘛非要用数组呢,c++的string字符串类型不更安全。

回答2:

#include
class Arr
{
public:
Arr(){x=0;y=0;p=NULL;}
~Arr()
{
for(int i=0;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 p[i]=new int[n];

for (i=0;i {
for(j=0;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 c1[i]=new int[b.getY()];

a1=a.getP();
b1=b.getP();
for (i=0;i {
for (j=0;j {
for (k=0;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:"< cin>>x1>>y1;
a.inputArr(x1,y1);
cout<<"input the row and column of second array:"< cin>>x2>>y2;
b.inputArr(x2,y2);
if(a.getY()==b.getX())
{
c=new int*[a.getX()];
for (i=0;i c[i]=new int[b.getY()];

c=multiArr(a,b);
cout<<"the result is:"< for (i=0;i {
for(int j=0;j cout< cout< }
}
else
cout<<"the two array don't multiplicate!"< return 0;
}

回答3:

#include
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 p[i]=new int[n];

for (i=0;i {
for(j=0;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 c1[i]=new int[b.getY()];

a1=a.getP();
b1=b.getP();
for (i=0;i {
for (j=0;j {
for (k=0;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:"< cin>>x1>>y1;
a.inputArr(x1,y1);
cout<<"input the row and column of second array:"< cin>>x2>>y2;
b.inputArr(x2,y2);
if(a.getY()==b.getX())
{
c=new int*[a.getX()];
for (i=0;i c[i]=new int[b.getY()];

c=multiArr(a,b);
cout<<"the result is:"< for (i=0;i {
for(int j=0;j cout< cout< }
}
else
cout<<"the two array don't multiplicate!"< return 0;
}