My code passed all the test cases above, but I am getting WA. Can anybody help?
Code: Select all
#pragma warning(disable:4786)
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
struct type{
string name;
char name2[20];
double cost;
double sell;
int quantity;
};
bool operator < (const type &a,const type &b){
if(a.name<b.name) return true;
else return false;
}
map <string,int> mymap;
vector <type> v;
int obj_count;
double expence,earn,loss,profit;
void add(char name[],double buy,double sell){
type temp;
strcpy(temp.name2,name);
temp.name=name;
temp.cost=buy;
temp.sell=sell;
temp.quantity=0;
mymap[temp.name]=obj_count++;
v.push_back(temp);
}
void remove(char name[]){
string temp=name;
int mark=mymap[temp];
loss+=v[mark].cost * v[mark].quantity;
v.erase(v.begin()+mark);
obj_count--;
//remapping
for(int i=mark;i<obj_count;i++){
mymap[v[i].name]=mymap[v[i].name]-1;
}
}
void buy(char name[],int amount){
string temp=name;
int index=mymap[temp];
v[index].quantity+=amount;
}
void _sell(char name[],int amount){
string temp=name;
int index=mymap[temp];
earn+=v[index].sell * amount;
expence+=v[index].cost * amount;
v[index].quantity-=amount;
}
void report(){
int i;
double sum=0.;
profit=earn - expence - loss;
earn=0.;
expence=0.;
loss=0.;
sort(v.begin()+1,v.end());
//remapping
for(i=1;i<obj_count;i++){
mymap[v[i].name]=i;
}
printf(" INVENTORY REPORT\n");
printf("Item Name Buy At Sell At On Hand Value\n");
printf("--------- ------ ------- ------- -----\n");
for(i=1;i<obj_count;i++){
printf("%-15s%5.2lf%13.2lf%13d%13.2lf\n",v[i].name2,v[i].cost,v[i].sell,v[i].quantity,v[i].quantity * v[i].cost);
sum+= v[i].quantity * v[i].cost;
}
printf("------------------------\n");
printf("Total value of inventory%35.2lf\n",sum);
printf("Profit since last report%35.2lf\n",profit);
}
int main(){
//freopen("330.in","r",stdin);
bool flag=false;
char line[1005],cmnd[50];
char name[50];
double cost,sell;
expence=0.,earn=0.,loss=0.;
type dummy;
dummy.name="";
dummy.name2[0]=0;
dummy.cost=0.;
dummy.quantity=0;
dummy.sell=0.;
v.push_back(dummy);
int quantity;
obj_count=1;
while(gets(line)){
if(line[0]=='*') break;
sscanf(line,"%s",cmnd);
if(!strcmp(cmnd,"new")) {
sscanf(line,"%s %s %lf %lf",cmnd,name,&cost,&sell);
add(name,cost,sell);
}
else if(!strcmp(cmnd,"delete")) {
sscanf(line,"%s %s",cmnd,name);
remove(name);
}
else if(!strcmp(cmnd,"buy")) {
sscanf(line,"%s %s %d",cmnd,name,&quantity);
buy(name,quantity);
}
else if(!strcmp(cmnd,"sell")) {
sscanf(line,"%s %s %d",cmnd,name,&quantity);
_sell(name,quantity);
}
else if(!strcmp(cmnd,"report")) {
if(!flag) flag=true;
else printf("\n");
report();
}
}
return 0;
}