学习C++过程中有问题的作业,记下以便日后修改完善

第四章  类

运算符重载(问题不会利用友元)

#include<iostream>
using namespace std;
class Complex{
public:
 Complex operator++(int);
 /*Complex operator++();*/
 Complex operator/=(const Complex &);
 Complex operator*=(const Complex &);
 Complex operator+=(const Complex &);
 Complex operator-(const Complex &);
 Complex(double r=0,double i=0):real(r),image(i){}
 void show(){
  cout<<real<<"+"<<image<<"i"<<endl;
 }
private:
 double real;
 double image;
};
Complex Complex::operator++(int){
 this->real++;
 this->image++;
 return *this;
}
//Complex Complex::operator++(){
// this->real++;
// this->image++;
// return *this;
//}
Complex Complex::operator/=(const Complex &c){
 double x;
 x=this->real;
 this->real=(x*c.real+this->image*c.image)/(c.real*c.real+c.image*c.image);
 this->image=(this->image*c.real-x*c.image)/(c.real*c.real+c.image*c.image);
 return *this;
}
Complex Complex::operator*=(const Complex &c){
 double x;
 x=this->real;
 this->real=x*c.real-this->image*c.image;
 this->image=x*c.image+this->image*c.real;
 return *this;
}
Complex Complex::operator+=(const Complex &c){
 Complex t;
 this->real+=c.real;
 this->image+=c.image;
 return *this;
}
Complex Complex::operator-(const Complex & c){
 Complex t;
 t.real=this->real-c.real;
 t.image=this->image-c.image;
 return t;
}
int main(){
 Complex c1(2,3),c2(1,4),c3;
 cout<<"c1=";
 c1.show();
 cout<<"c2=";
 c2.show();
 cout<<"c3=";
 c3.show();
 cout<<"(c3=c1-c2)=";
 c3=c1-c2;
 c3.show();
 /*cout<<"(c1+=c2)=";
 c1+=c2;
 c1.show();
 cout<<"(c2*=c1)=";
 c2*=c1;
 c2.show();*/
 /*c1/=c2;
 cout<<"(c1/=c2)=";
 c1.show();*/
 cout<<"c1++=";
 c1++.show();
 return 0;
}

第五章  数组与指针

标准c++string类(问题无法理解,无从下手)

#include<iostream>
using namespace std;
const int n=21;
class mystring{
public:
 bool operator==(mystring &);
 bool operator>(mystring &);
 bool operator<(mystring &);
 mystring &operator+=(char * s);
 mystring &operator+(char * s);
 mystring &operator=(char * s);
 char operator[](int);
 mystring(){
  last=0;
  maxsize=n;
  str[0]='\0';
 }//当c字符串过长时,初始化要采用结尾处理
 mystring(char *s){
  last=-1;
  maxsize=n;
  do{
   last++;
   str[last]=s[last];
  }while(s[last]!='\0' && last<maxsize-1);
  str[last]='\0';
 }
 ~mystring(){}    //析构函数

 void show(){
  cout<<str<<'\t'; }
private:
 char str[n];
 int maxsize;
 int last;
 
};
char mystring::operator[](int){
 int i;
 cin>>i;
 return i;
}
mystring & mystring::operator=(char *s){//这里返回值为引用,不调用复制构造函数
 last=-1;
 do{
  last++;
  str[last]=s[last];
 }while(s[last]!='\0' && last<maxsize-1);
 str[last]='\0';
 return *this;
}
mystring &mystring::operator+(char *s){
 mystring t;
 last=-1;
 do{
  last ++;
  t.str[last]=str[last]+s[last];
 }while(s[last]!='\0' && last<maxsize-1);
 str[last]='\0';
 return t;
}
mystring &mystring::operator+=(char *s){
 last=-1;
 do{
  last ++;
  str[last]+=s[last];
 }while(s[last]!='\0' && last<maxsize-1);
 str[last]='\0';
 return *this;
}
bool mystring::operator<(mystring & ms){
 int i=0,k;
 do{
  k=str[i]-ms.str[i];
  i++;
 }while(k==0 && i<last && i<ms.last);
 if(k<0) return true;
 if(i==last && i!=ms.last) return true;
 return false;
}
bool mystring::operator>(mystring & ms){
 int i=0,k;
 do{
  k=ms[last]-str[last];
  i++;
 }while(k==0 && i<last && i<ms.last);
 if(k<0) return true;
 if(i==last && i!=ms.last) return true;//此处不太理解!!
 return false;
}
bool mystring::operator==(mystring & ms){
 int i=0,k;
 do{
  k=str[i]-ms.str[i];
  i++;
 }while( k==0 && i<last && i<ms.last);
 if(k==0) return true;
 if(i==last && i!=ms.last) return true;
 return false;
}
int main(){
 mystring  str0;
 mystring();
 str0.show();
 return 0;
}//无法理解

关于日历的制作(只能对已知的日期进行编程)

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
 int year,month,day;
 year=2012;
 month=2;
 cout<<setw(16)<<year<<"年"<<month<<"月"<<endl;
 cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)<<"四"<<setw(4)<<"五"<<setw(4)<<"六"<<endl;
 for(day=1;day<=29;day++)
 {
  if(day==1)
   cout<<setw(16)<<day;
  else
   cout<<setw(4)<<day;
  if((day+3)%7==0)
   cout<<endl;
 }
}

杨辉三角(虽然程序正确,但对循环还是不太熟练)

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
 int n[10][10],i,j,k;
 for(i=0;i<10;i++)
  for(j=0;j<=i;j++)
   if(j==0 || j==i)
    n[i][j]=1;
   else
    n[i][j]=n[i-1][j-1]+n[i-1][j];
 
 for(i=0;i<10;i++)
 {
  for(k=10;k>=i;k--)
  cout<<"  ";
  for(j=0;j<=i;j++)
   cout<<setw(4)<<n[i][j];
  cout<<endl;
 }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
作业车间调度问题是一个经典的组合优化问题,主要是解决在多个作业需要在多个机器上进行处理时,如何安排作业的顺序和分配机器的问题,以最小化完成所有作业的时间。 在C++中,可以使用贪心算法、动态规划等算法来解决作业车间调度问题。以下是一个使用贪心算法的C++示例代码: ```cpp #include <iostream> #include <algorithm> using namespace std; // 定义作业结构体 struct Job { int id; // 作业编号 int machine; // 机器编号 int time; // 作业处理时间 }; // 按照作业处理时间从小到大排序 bool cmp(Job a, Job b) { return a.time < b.time; } // 计算作业完成时间 int calcFinishTime(Job jobs[], int n) { int finishTime[n]; int maxTime = 0; for (int i = 0; i < n; i++) { int machine = jobs[i].machine; int time = jobs[i].time; if (i == 0) { finishTime[i] = time; } else { finishTime[i] = finishTime[i-1] + time; } maxTime = max(maxTime, finishTime[i]); } return maxTime; } // 贪心算法解决作业车间调度问题 int scheduleJobs(Job jobs[], int n, int m) { sort(jobs, jobs+n, cmp); // 按照作业处理时间排序 int machineTime[m] = {0}; // 记录每个机器的可用时间 for (int i = 0; i < n; i++) { int machine = jobs[i].machine; int time = jobs[i].time; jobs[i].id = i + 1; // 给作业编号 machineTime[machine] += time; // 更新机器可用时间 } sort(jobs, jobs+n, cmp); // 按照作业处理时间排序 return calcFinishTime(jobs, n); // 计算作业完成时间 } int main() { int n = 5; // 作业数量 int m = 3; // 机器数量 Job jobs[] = {{0, 0, 2}, {0, 1, 4}, {0, 2, 3}, {0, 1, 1}, {0, 2, 4}}; // 作业列表 int finishTime = scheduleJobs(jobs, n, m); // 计算作业完成时间 cout << "最短完成时间:" << finishTime << endl; return 0; } ``` 该示例代码中,先按照作业处理时间从小到大排序,然后根据贪心算法,将作业分配到可用时间最短的机器上处理。最后计算所有作业完成时间,输出最短完成时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值