對一個人的轉變

題目:實現兩個方程的相加。要求用字符數組實現。

#include <iostream.h>
#include <string.h>
void main()
{
 char a1[20],b1[20];
 char a2[20],b2[20];
 int i=0,j=0,length,k,flag1=0,flag2=0,add=1,n=0,l;
 signed int count=0,count1=0,count2=0;
    cout<<"請輸入第一個方程式:";
 cin>>a1;
    cout<<"請輸入第二個方程式:";
 cin>>b1;
 while(a1[i]!='=')i++;
    length=strlen(a1);
 a1[i]='/0';
     while(i<length)
 {
   i++;
  a2[j]=a1[i];
     j++;
 
  }
 a2[j]='/0';
 i=0;
 j=0;
while(b1[i]!='=')i++;
    length=strlen(b1);
 b1[i]='/0';
 while(i<length)
 {
  i++;
  b2[j]=b1[i];
 j++;
 }
 b2[j]='/0';
 length=strlen(a1);
 for(k=0;k<length;k++)
 {
  if((a1[k]>='a'&&a1[k]<='z')||(a1[k]>='A'&&a1[k]<='Z'))n++;
 }
 cout<<"相加後的方程爲:";
i=0;
j=0;
if(n==0)
{
 if(a1[0]=='-')
 {length=strlen(a1);
  for(k=1;k<length;k++)count1=count1*10+a1[k]-'0';
 count1=0-count1;
 }
 else
 {length=strlen(a1);
  for(k=0;k<length;k++)count1=count1*10+a1[k]-'0';
 }
 if(b1[0]=='-')
 {length=strlen(b1);
  for(k=1;k<length;k++)count2=count2*10+b1[k]-'0';
 count2=0-count2;
 }
 else {length=strlen(b1);
  for(k=0;k<length;k++)count2=count2*10+b1[k]-'0';
 }
 count=count1+count2;
 cout<<count;
}
else{

for(l=0;l<n;l++)
{ count=0;
count1=0;
count2=0;
  while(!((a1[i]>='a'&&a1[i]<='z')||(a1[i]>='A'&&a1[i]<='Z')))i++;
  while(!((b1[j]>='a'&&b1[j]<='z')||(b1[j]>='A'&&b1[j]<='Z')))j++;
  if(flag1==0)
  {if(a1[0]=='-')
 {
  for(k=1;k<i;k++)count1=count1*10+a1[k]-'0';
 count1=0-count1;
 }
 else
 {
  for(k=0;k<i;k++)count1=count1*10+a1[k]-'0';
 }
 if(b1[0]=='-')
 {
  for(k=1;k<j;k++)count2=count2*10+b1[k]-'0';
  count2=0-count2;
 }

if(count1==0)count1=1;
if(count2==0)count2=1;
 else {
  for(k=0;k<j;k++)count2=count2*10+b1[k]-'0';
 }
}
  else{
for(k=flag1;k<i;k++)count1=count1*10+a1[k]-'0';
for(k=flag2;k<j;k++)count2=count2*10+b1[k]-'0';
if(count1==0)count1=1;
if(count2==0)count2=1;}
if(add==1){
 count=count1+count2;
 cout<<count;}
if(add==0){
 count=count1-count2;
 if(count!=0){
 if(count>0)cout<<a1[flag1-1];
 else{
  cout<<b1[flag2-1];
  count=-count;
 }
 cout<<count;}
}
if(count!=0)cout<<a1[i];
i++;
j++;
if(a1[i]==b1[j])
{add=1;
cout<<a1[i];
}
else add=0;
flag1=i+1;
flag2=j+1;
}
}

cout<<"=";
flag1=0;flag2=0;count1=0;count2=0;add=1,n=0,count=0;
length=strlen(a2);
 for(k=0;k<length;k++)
 {
  if((a2[k]>='a'&&a2[k]<='z')||(a2[k]>='A'&&a2[k]<='Z'))n++;}
i=0;
j=0;
if(n==0)
{
 if(a2[0]=='-')
 {length=strlen(a2);
  for(k=1;k<length;k++)count1=count1*10+a2[k]-'0';
 count1=0-count1;
 }
 else
 {length=strlen(a2);
  for(k=0;k<length;k++)count1=count1*10+a2[k]-'0';
 }
 if(b2[0]=='-')
 {length=strlen(b2);
  for(k=1;k<length;k++)count2=count2*10+b2[k]-'0';
 count2=0-count2;
 }
 else {length=strlen(b2);
  for(k=0;k<length;k++)count2=count2*10+b2[k]-'0';
 }
 count=count1+count2;
 cout<<count;
}
else{
for(l=0;l<n;l++)
{ count=0;
count1=0;
count2=0;
  while(!((a2[i]>='a'&&a2[i]<='z')||(a2[i]>='A'&&a2[i]<='Z')))i++;
  while(!((b2[j]>='a'&&b2[j]<='z')||(b2[j]>='A'&&b2[j]<='Z')))j++;
  if(flag1==0)
{
 if(a2[0]=='-')
 {
  for(k=1;k<i;k++)count1=count1*10+a2[k]-'0';
 count1=0-count1;
 }
 else
 {
  for(k=0;k<i;k++)count1=count1*10+a2[k]-'0';
 }
 if(b2[0]=='-')
 {
  for(k=1;k<j;k++)count2=count2*10+b2[k]-'0';
 count2=0-count2;
 }
 else {
  for(k=0;k<j;k++)count2=count2*10+b2[k]-'0';
 }
}
  else{
for(k=flag1;k<i;k++)count1=count1*10+a2[k]-'0';
for(k=flag2;k<j;k++)count2=count2*10+b2[k]-'0';
if(count1==0)count1=1;
if(count2==0)count2=1;}
if(add==1){
 count=count1+count2;
 cout<<count;}
if(add==0){
 count=count1-count2;
 if(count!=0){
 if(count>0)cout<<a2[flag1-1];
 else{
  cout<<b2[flag2-1];
  count=-count;
 }
 cout<<count;}
}
if(count!=0)cout<<a2[i];
i++;
j++;
if(a2[i]==b2[j])
{add=1;
cout<<a2[i];
}
else add=0;
flag1=i+1;
flag2=j+1;
}
}
cout<<endl;
}

 

 

上面是我班一個女生寫出來的代碼,我居然願意把她的代碼打印下來並花了半小時候的時間去理清她的思想。看了她寫的代碼後我很是感嘆:這纔是我們的差距,她的思維縝密,想到的可能性和實現性都比我的全面,這就是處理問題的一種思考和能力。以前從來沒有好好的想過她,今天看了她的代碼,才知道自己以前爲什麼不重視她,全因爲是妒忌!今天這種妒忌就成了佩服!

由此我就在想,之偶爾感嘆身邊的環境不盡人意是不是也是主觀的在排斥些什麼東西!我應該學會更從容的去對待身邊的人和事纔對!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章