佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1184|回复: 3

C++高手帮忙下

[复制链接]
发表于 28-8-2013 03:08 PM | 显示全部楼层 |阅读模式
我的code基本上是可以work了得
但是我要加array去,可以算几个人的BMI在同一时间,和可以选择measure unit (meter,inches)
但是这两个东西我写来写去弄不出来,
谁可以帮帮忙? TT
  1. #include <iostream>
  2. using namespace std;
  3. void displayMenu()
  4. {
  5.    cout << "*-------------------------------------*" << endl;
  6.    cout << "| Golden Years Financial Inc.         |" << endl;
  7.    cout << "|=====================================|" << endl;
  8.    cout << "| Select:                             |" << endl;
  9.    cout << "| 1 => Display Data                   |" << endl;
  10.    cout << "| 2 => Insert Data                    |" << endl;
  11.    cout << "|-------------------------------------|" << endl;
  12.    cout << "| 3 => Calculate Standard BMI         |" << endl;
  13.    cout << "| 4 => Clear Data                     |" << endl;
  14.    cout << "|-------------------------------------|" << endl;
  15.    cout << "| Q => Quit                           |" << endl;
  16.    cout << "*-------------------------------------*" << endl;
  17.    cout << endl;
  18.    cout << "Choice: ";
  19. }

  20. void DisplayData( string &name, float &weight, float &height,float &bmi )
  21. {
  22.    cout << endl;
  23.    cout << "Name = " <<name<< endl;
  24.    cout << "Weight = " <<weight<< endl;
  25.    cout << "Height = " <<height<< endl;
  26.    cout << endl;

  27. }


  28. void InsertData( string &name, float &weight, float &height,float &bmi )
  29. {
  30.         cout << endl;
  31.         cout<<"Please Enter Your Name\n";
  32.         cin >>name;

  33.     cout <<"Enter your weight in Kilograms (KG) : ";
  34.         cin >> weight;

  35.     cout <<"\nEnter your height in Meters (M) : ";
  36.         cin >> height;
  37.        
  38.         cout<<"You are now able to calculate your BMI."<<endl;
  39. }



  40. void Calculate( string &name, float &weight, float &height,float &bmi )
  41. {
  42.      
  43.         bmi = weight/(height*height);
  44.         cout<<"Your BMI is: "<<bmi<<endl;
  45.         if (bmi <= 18.5)
  46.     cout << "You are under weight."<<endl;
  47.     else if ((bmi > 18.5 ) && (bmi < 25))
  48.     cout << "Your weight is in the normal range."<<endl;
  49.     else if (bmi >= 25)
  50.     cout << "You are overweight."<<endl;

  51.        
  52.        
  53.        
  54.        
  55.        
  56. }
  57. void setPrecision ()
  58. {
  59.     cout.setf(ios::fixed);
  60.     cout.setf(ios::showpoint);
  61.     cout.precision(1);
  62. }
  63. void DebugCalculator(string name, float weight, float height)
  64. {
  65.      if(name == "null" && weight==0 && height ==0 )
  66.      cout<<"Please Insert Your Data Before You Calculate Your BMI Or Your Data is Invalid."<<endl;  


  67. }
  68. int main()
  69. {
  70.    string name = "null";
  71.    float weight = 0 ;
  72.    float height = 0;
  73.    char choice;
  74.    bool invalid = true;
  75.    
  76.    do
  77.    {
  78.      float bmi;
  79.      {
  80.        displayMenu();
  81.        cin >> choice;
  82.        choice = toupper(choice);
  83.        switch (choice)
  84.        {
  85.            case '1' : DisplayData(name, weight, height, bmi);
  86.                       break;
  87.            case '2' : InsertData( name, weight, height, bmi);
  88.                       break;
  89.            case '3' : DebugCalculator(name,weight,height);
  90.                       setPrecision();
  91.                       Calculate( name, weight, height, bmi);
  92.                       break;
  93.            case '4' : DisplayData(name="null", weight=0,height=0,bmi);
  94.                       cout << "Your record have been deleted, please insert your data again." << endl;
  95.                       break;
  96.            case 'Q' : invalid = false;
  97.                       cout<<"Thanks for using"<<endl;
  98.                       break;
  99.            default : cout << "Invalid selection, try again!" << endl;
  100.                     
  101.        }
  102.      }
  103.    }while(invalid);
  104.    cout << "Good bye and have a nice day!" << endl;
  105.    cout << endl;
  106.    system("PAUSE");
  107.    return 0;
  108. }
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 1-9-2013 12:51 PM | 显示全部楼层
我以前assignment 写过类似的。。。。还是用pointer 把 data store 在 text file 里。。。。有空的话我就把它找出来给你。。。
回复

使用道具 举报

 楼主| 发表于 1-9-2013 12:57 PM | 显示全部楼层
johnng86 发表于 1-9-2013 12:51 PM
我以前assignment 写过类似的。。。。还是用pointer 把 data store 在 text file 里。。。。有空的 ...

感谢你!!
回复

使用道具 举报

发表于 2-9-2013 03:41 PM | 显示全部楼层
反正收着也没什么用,你就拿去自己慢慢研究吧
  1. #include<iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <iomanip>
  6. using namespace std;
  7. int i=0;
  8. #define p_ause system("pause")
  9. //declare the "pause" system

  10. struct component
  11. {
  12.         string code;
  13.         string name;
  14.         double cost;
  15.         int qty;
  16.         double s_price;
  17. }product[100];


  18. void enter_data(int,int);
  19. void main_menu();//show all the commands
  20. void check_dgt(string*,double&);//check value is digit number or not
  21. void data();
  22. void new_data();
  23. void exit();
  24. void edit();
  25. void disp_all();
  26. void del();
  27. void data_shifting(int);
  28. //Add Two new function.=)
  29. void disp_selection();
  30. void disp_1_data();
  31. void store_data();

  32. int main()
  33. {
  34.         data();
  35.        
  36.         // disp_all();
  37.         string c;
  38.         int a=2;
  39.         cout<<endl<<endl;
  40.        
  41.         //Print the title
  42.         cout<<setw(20)<<" "<<setfill('=')<<setw(38)<<""<<setfill(' ')<<endl<<endl;
  43.         cout<<setw(20)<<""<<"====Universiti Tunku abdul Rahman====="<<endl<<endl;
  44.         cout<<setw(20)<<""<<"=========Programming Techniques=======\n\n";
  45.         cout<<setw(20)<<" "<<setfill('=')<<setw(38)<<""<<setfill(' ')<<endl<<endl;
  46.         while(1)
  47.         {
  48.                 cout<<setw(15)<<""<<"\nAre You Want To Enter Command Mode?(Y/N): ";
  49.                 cin>>c;
  50.                 //ask user enter yes ot not
  51.                 if (c=="y" || c=="Y")main_menu();
  52.                 else if (c=="n" || c=="N")        exit();
  53.                 else cout<<setw(20)<<""<<"\aPlease enter Y or N correctly!"<<endl;
  54.                
  55.         }
  56.         return 0;
  57. }


  58. void main_menu()
  59. {
  60.         //declare the variable
  61.         double n;
  62.         string cmd;

  63.         //using whlie() statement to continue run the program
  64.         while(1)
  65.         {
  66.                 //print the menu
  67.                 system("CLS");//Clear the previous output screen
  68.                 cout<<endl<<endl;
  69.                 cout<<setw(15)<<" "<<setfill('_')<<setw(55)<<" "<<setfill(' ')<<endl<<endl;
  70.                 cout<<setw(15)<<""<<"The Command available is below:"<<endl;
  71.                 cout<<setw(15)<<" "<<setfill('_')<<setw(55)<<" "<<setfill(' ')<<endl<<endl;
  72.                 cout<<setw(15)<<""<<"(1) New( Add new item )"<<endl;
  73.                 cout<<setw(15)<<""<<"(2) Edit( Specify an etem and edit it )"<<endl;
  74.                 cout<<setw(15)<<""<<"(3) Delete( Specify an etem and delete it )"<<endl;
  75.                 cout<<setw(15)<<""<<"(4) Display( Display a specify an item and delete it )"<<endl;
  76.                 cout<<setw(15)<<""<<"(5) Exit( Quite the program )"<<endl;
  77.                 cout<<setw(15)<<" "<<setfill('_')<<setw(55)<<" "<<setfill(' ')<<endl<<endl;
  78. loop_m:
  79.                 cout<<setw(15)<<""<<"Select Your Command: ";
  80.                 cin>>cmd;
  81.                 ;
  82.                
  83.                 //pass the memory address to chr
  84.                 check_dgt(&cmd,n);
  85.                 n=int(n);
  86.                 //check chr is digit or not, and assign the value to n
  87.                 if (n!=-1)
  88.                 {
  89.                         if(n==1) new_data();
  90.                         else if (n==2)edit();
  91.                         else if (n==3){del();}
  92.                         else if (n==4)disp_selection();
  93.                         else if (n==5)exit();
  94.                 }

  95.                 else goto loop_m;//go back to loo_m
  96.                
  97.         }

  98. }
  99. void check_dgt(string *s,double& n)
  100. {
  101.         char c[20];
  102.         char * cstr;
  103.         for(int i=0;i<s->length();i++)
  104.         {
  105.                 if(isdigit(s->at(i)) || s->at(i)=='.')
  106.                 {
  107.                                  cstr = new char [s->size()+1];
  108.                                  strcpy (cstr, s->c_str());
  109.                        
  110.                         n=atof(cstr);
  111.                        
  112.                        
  113.                 }
  114.                 else if(isalpha(s->at(i)))
  115.                 {
  116.                         //if 's' is alphabet, then return n=-1;
  117.                         cout<<setw(15)<<""<<"Please don't enter alphabet!"<<endl;
  118.                         n=-1;
  119.                         return;
  120.                 }
  121.                 else
  122.                 {
  123.                         cout<<setw(15)<<""<<"Please enter correctly command!"<<endl;
  124.                         n=-1;
  125.                         return;
  126.                 }
  127.         }
  128. }

  129. void data()
  130. {
  131.         string s;
  132.         ifstream infile("Data.txt");

  133.         //get all the datas from the texr.txt files
  134.         if(infile.is_open())
  135.         {
  136.                 while(!infile.eof())
  137.                 {
  138.                         //store the datas
  139.                         getline(infile,product[i].name,'$');
  140.                         infile>>product[i].code;
  141.                         infile>>product[i].cost;
  142.                         infile>>product[i].qty;
  143.                         infile>>product[i].s_price;
  144.                         getline(infile,s);
  145.                         i++;

  146.                 }
  147.         }       
  148.         else
  149.                 cout<<"Error opening file!"<<endl;
  150.         infile.close();
  151. }

  152. void new_data()
  153. {
  154.         string c;
  155.         int update=0;

  156.         system("CLS");//Clear the previous output screen
  157.         cout<<endl<<endl;
  158.         cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
  159.         cout<<setw(30)<<""<<"====Add New Data===="<<endl;
  160.         cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;

  161.         while(i<100)
  162.         {
  163.                 fflush(stdin);
  164.                 enter_data(update,i);
  165.                 i++;//incerment the i by 1 for next data
  166. loop_new2:
  167.                 cout<<setw(15)<<""<<"Are you want to continue add data?(Y/N): ";
  168.                 cin>>c;
  169.                 if (c=="y" || c=="Y")continue;
  170.                 else if (c=="n" || c=="N")return;
  171.                 else
  172.                 {
  173.                         cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
  174.                         goto loop_new2;
  175.                 }

  176.         }

  177.        
  178. }

  179. void exit()
  180. {
  181.         system("cls");
  182.         cout<<endl<<endl;
  183.         cout<<setw(25)<<" "<<setfill('*')<<setw(34)<<" "<<setfill(' ')<<endl;
  184.         cout<<setw(25)<<""<<"* Thanks For Using This Program *"<<endl;
  185.         cout<<setw(25)<<" "<<setfill('*')<<setw(34)<<" "<<setfill(' ')<<endl;

  186.         exit(1);
  187. }

  188. void edit()
  189. {
  190.         double n;
  191.         int j,choose,update;
  192.         string name,type_edit,cmd,s;
  193.         update=1;
  194.        
  195.         system("CLS");//Clear the previous output screen
  196.         cout<<endl<<endl;
  197.         cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
  198.         cout<<setw(30)<<""<<"=====Edit  Data====="<<endl;
  199.         cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
  200. loop_edit1:       
  201.         cout<<setw(15)<<"Enter the component name: ";
  202.         fflush(stdin);
  203.         getline(cin,name,'\n');
  204.         for(j=0;j<100;j++)
  205.         {
  206.                 if(product[j].name==name)
  207.                 {
  208.                         cout<<setw(15)<<""<<"The \'"<<name<<"\' is FOUND."<<endl;
  209.                         cout<<setw(15)<<""<<"Component details:"<<endl;
  210.                         cout<<setfill('_')<<setw(72)<<" "<<setfill(' ')<<endl;
  211.                         //cout<<left<<setw(25)<<"Name"<<setw(10)<<"Code"<<setw(10)<<"Cost"<<setw(10)<<"Quantity"<<setw(10)<<"Selling Price"<<endl;
  212.                         cout<<left<<setw(25)<<"(1)Name"<<setw(10)<<"(2)Code"<<setw(10)<<"(3)Cost"<<setw(15)<<"(4)Quantity"
  213.                                 <<setw(15)<<"(5)Selling Price"<<endl;
  214.                         cout<<setfill('_')<<setw(72)<<" "<<setfill(' ')<<endl;
  215.                         cout<<left<<setw(25)<<product[j].name<<setw(10)<<fixed<<setw(10)<<product[j].code
  216.                                         <<setprecision(2)<<setw(10)<<product[j].cost <<setprecision(0)<<setw(15)<<product[j].qty <<setw(10)<<setprecision(2)
  217.                                         <<product[j].s_price <<endl;
  218.                        
  219.                         cout<<setfill('_')<<setw(72)<<" "<<setfill(' ')<<endl;
  220.                        
  221.                                
  222.                                 cout<<endl;
  223.                                 while(1)
  224.                                 {
  225.                                 cout<<setw(15)<<"Which part of element you want edit?(6 for all part // 0 to exit): ";
  226.                                
  227.                                 cin>>cmd;
  228.                                 check_dgt(&cmd,n);
  229.                                 choose=int(n);
  230.                        
  231.                        
  232.                         if(choose==1)
  233.                         {
  234.                                 fflush(stdin);
  235.                                 cout<<setw(15)<<""<<"Name:";
  236.                                 getline(cin,type_edit,'\n');
  237.                                 product[j].name =type_edit;

  238.                         }
  239.                                 else if(choose==2)
  240.                                 {
  241.                                         fflush(stdin);
  242.                                         cout<<setw(15)<<""<<"Code:";
  243.                                         getline(cin,type_edit,'\n');
  244.                                         product[j].code=type_edit;
  245.                                 }
  246.                                 else if(choose==3)
  247.                                 {
  248. loop_cost:
  249.                                         cout<<setw(15)<<""<<"Cost:";
  250.                                         cin>>s;
  251.                                         check_dgt(&s,n);
  252.                                         if(n!=-1)
  253.                                                 product[j].cost=n;
  254.                                 else
  255.                                         goto loop_cost;
  256.                                 }
  257.                                 else if(choose==4)
  258.                                 {
  259. loop_qty:
  260.                                         cout<<setw(15)<<""<<"Quantity:";
  261.                                         cin>>s;
  262.                                         check_dgt(&s,n);
  263.                                         if (n!=-1)
  264.                                                 product[j].qty =int(n);
  265.                                         else
  266.                                                 goto loop_qty;                                       
  267.                                 }
  268.                                 else if(choose==5)
  269.                                 {
  270. loop_price:
  271.                                         cout<<setw(15)<<""<<"Selling Price:";
  272.                                         cin>>s;
  273.                                         check_dgt(&s,n);
  274.                                         if (n!=-1)
  275.                                                 product[j].s_price =n;
  276.                                         else
  277.                                                 goto loop_price;
  278.                                                
  279.                                   }
  280.                                 else if (choose==6)
  281.                                 {
  282.                                         fflush(stdin);
  283.                                         enter_data(update,j);
  284.                                
  285.                                 }
  286.                                 else if(choose==0)goto outside;
  287.                                 else cout<<"Please enter correctly!"<<endl;
  288.                                 };
  289.                 }
  290.                
  291.         }
  292. outside:
  293.         if(j==100)
  294.         {
  295.                 cout<<setw(15)<<""<<"The \'"<<name<<"\' no found."<<endl;
  296.         }
  297.                 loop_edit2:
  298.                 cout<<setw(15)<<""<<"Are you want to continue edit next element?(Y/N): ";
  299.                 cin>>cmd;
  300.                 if (cmd=="y" || cmd=="Y") goto loop_edit1;
  301.                 else if (cmd=="n" || cmd=="N")
  302.                 {
  303.                         store_data();
  304.                         return;
  305.                 }
  306.                 else
  307.                 {
  308.                         cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
  309.                         goto loop_edit2;
  310.                 }

  311. }

  312. void enter_data(int update,int j)
  313. {
  314.         //store the data from user
  315.         string code,name,s;
  316.         double n;
  317.         component* p=new component;
  318.         cout<<"Number ("<<i<<"): "<<endl;
  319.         cout<<setw(15)<<""<<"Code: ";
  320.         getline(cin,code,'\n');
  321.         cout<<setw(15)<<""<<"Name: ";
  322.         getline(cin,name,'\n');
  323. loop_cost:
  324.         cout<<setw(15)<<""<<"Cost: ";
  325.         cin>>s;
  326.         check_dgt(&s,n);
  327.         if (n!=-1)
  328.         {
  329.                 p->cost =n;
  330.                
  331.         }
  332.         else
  333.                 goto loop_cost;
  334. loop_qty:
  335.         cout<<setw(15)<<""<<"Quantity: ";
  336.         cin>>s;
  337.         check_dgt(&s,n);
  338.         if (n!=-1)
  339.         {
  340.                 p->qty =int(n);
  341.                
  342.         }
  343.         else
  344.                 goto loop_qty;
  345. loop_price:
  346.         cout<<setw(15)<<""<<"Selling Price: ";
  347.         cin>>s;
  348.         check_dgt(&s,n);
  349.         if (n!=-1)
  350.         {
  351.                 p->s_price =n;
  352.                
  353.         }
  354.         else
  355.                 goto loop_price;
  356.         //store the data
  357.         p->name =name;
  358.         p->code =code;
  359.         if(update==0)
  360.         {
  361.                 p->name +="$";
  362.                 ofstream fileout("Data.txt",ios::out|ios::ate|ios::app);
  363.                 if(fileout.is_open ())
  364.                 {
  365.                         fileout<<left<<setw(25)<<p->name<<setw(10)<<fixed<<setprecision(2)<<setw(10)<<p->code
  366.                                         <<setw(10)<<p->cost<<setprecision(0)<<setw(10)<<p->qty<<setw(10)<<setprecision(2)
  367.                                         <<p->s_price <<endl;
  368.                
  369.                         fileout.close();
  370.                 }
  371.                 else cout<<"Error opening file"<<endl;
  372.         }
  373.         else if(update==1)
  374.         {
  375.                 product[j].name =p->name;
  376.                 product[j].code =p->code ;
  377.                 product[j].cost =p->cost ;
  378.                 product[j].qty =p->qty ;
  379.                 product[j].s_price =p->s_price ;
  380.                 store_data();
  381.         }
  382.         delete p;
  383. }
  384. void disp_selection()
  385. {
  386.         double n;
  387.         string cmd;
  388. loop_dis0:
  389.         system("CLS");//Clear the previous output screen
  390.         cout<<endl<<endl;
  391.         cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
  392.         cout<<setw(30)<<""<<"====Display Datas==="<<endl;
  393.         cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
  394.         cout<<setw(15)<<""<<"1) Display One Data."<<endl;
  395.         cout<<setw(15)<<""<<"2) Display All The Datas."<<endl;
  396.         cout<<setw(15)<<""<<"3) Exit To Command Mode."<<endl;
  397. loop_dis1:
  398.         cout<<setw(15)<<""<<"Select Your Command: ";
  399.         cin>>cmd;
  400.         cout<<endl;
  401.         //pass the memory address to chr
  402.         check_dgt(&cmd, n);
  403.         //check chr is digit or not, and assign the value to n
  404.         if (n!=-1)
  405.         {
  406.                 if (n==1)
  407.                 {
  408.                         disp_1_data();

  409.                 }
  410.                 else if (n==2) disp_all();
  411.                 else if (n==3) return;
  412.                 else goto loop_dis1;//go back to loo_m
  413.         }
  414.         else goto loop_dis1;//go back to loo_m
  415.         goto loop_dis0;
  416. //*************************************************************

  417. }


  418. void disp_1_data()
  419. {
  420.         component* pp=product;
  421.         string s,code,cost,price,qty;
  422.         double n;
  423.         int j,choose;
  424.         string name,type_edit,c;
  425.         //control all the output position
  426.         cout.setf(ios::fixed);
  427.         cout.setf(ios::left);
  428. loop_dis1:
  429.         cout<<setw(15)<<"Enter the component name: ";
  430.         fflush(stdin);
  431.         getline(cin,name);
  432.         ifstream filein("Data.txt");
  433.         if(filein.is_open())
  434.         {
  435.                 j=0;
  436.                 while(!filein.eof())
  437.                 {
  438.                         fflush(stdin);
  439.                         getline(filein,(pp+j)->name ,'$');
  440.                         filein>>(pp+j)->code;
  441.                         filein>>(pp+j)->cost;
  442.                         filein>>(pp+j)->qty;
  443.                         filein>>(pp+j)->s_price;
  444.                         if(name==((pp+j)->name.substr(0,name.length())))
  445.                         {
  446.                                 cout<<setw(15)<<""<<"The \'"<<name<<"\' is FOUND."<<endl;
  447.                                 cout<<setw(13)<<" "<<setfill('_')<<setw(30)<<" "<<setfill(' ')<<endl;
  448.                                 cout<<setw(15)<<""<<"Component details:"<<endl;
  449.                                 cout<<setw(13)<<" "<<setfill('_')<<setw(30)<<" "<<setfill(' ')<<endl;
  450.                                 cout<<setw(15)<<""<<setw(15)<<"Code"<<setw(10)<<(pp+j)->code<<endl;
  451.                                 cout<<setw(15)<<""<<setw(15)<<"Name"<<setw(10)<<(pp+j)->name<<endl;
  452.                                 cout<<setw(15)<<""<<setw(15)<<"Cost"<<"RM"<<setprecision(2)<<setw(10)<<(pp+j)->cost<<endl;
  453.                                 cout<<setw(15)<<""<<setw(15)<<"Quantity"<<setw(10)<<(pp+j)->qty<<endl;
  454.                                 cout<<setw(15)<<""<<setw(15)<<"Selling"<<"RM"<<setprecision(2)<<setw(10)<<(pp+j)->s_price<<endl;
  455.                                 cout<<setw(13)<<" "<<setfill('_')<<setw(30)<<" "<<setfill(' ')<<endl;
  456.                                 cout<<endl<<endl;
  457.                                 break;
  458.                         }
  459.                         getline(filein,c,'\n');
  460.                         j++;
  461.                 }
  462.         }       
  463.         if(filein.eof())
  464.         {
  465.                 cout<<setw(15)<<""<<"The \'"<<name<<"\' no found."<<endl;
  466.         }
  467. loop_dis2:
  468.                 cout<<setw(15)<<""<<"Are you want to continue check other elements?(Y/N): ";
  469.                 cin>>c;
  470.                 if (c=="y" || c=="Y") goto loop_dis1;
  471.                 else if (c=="n" || c=="N")return;
  472.                 else
  473.                 {
  474.                         cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
  475.                         goto loop_dis2;
  476.                 }
  477.                 filein.close();
  478. }
  479. void disp_all()
  480. {
  481.        
  482.         int j=0;
  483.         string s;
  484.         component* p=new component[100];
  485.         ifstream filein("Data.txt");
  486.         if(filein.is_open())
  487.         {
  488.                 while(!filein.eof())
  489.                 {
  490.                         getline(filein,(p+j)->name,'$');
  491.                         filein>>(p+j)->code ;
  492.                         filein>>(p+j)->cost ;
  493.                         filein>>(p+j)->qty ;
  494.                         filein>>(p+j)->s_price ;
  495.                         getline(filein,s,'\n');
  496.                         j++;
  497.                        
  498.                 }
  499.         }
  500.         else cout<<"Error file opening"<<endl;
  501.         filein.close();
  502.         j--;
  503.         system("CLS");//Clear the previous output screen
  504.         cout<<right<<setfill('_')<<setw(70)<<" "<<setfill(' ')<<endl;
  505.         cout<<left<<setw(25)<<"Name"<<setw(10)<<"Code"<<setw(10)<<"Cost"<<setw(10)<<"Quantity"<<setw(10)<<"Selling Price"<<endl;
  506.         cout<<right<<setfill('_')<<setw(70)<<" "<<setfill(' ')<<endl;
  507.        
  508.         for(int k=0;k<j;k++)
  509.                 {
  510.                                 cout<<left<<setw(25)<<(p+k)->name <<setw(10)<<fixed<<setprecision(2)
  511.                                         <<setw(10)<<(p+k)->code<<"RM" <<setw(10)<<(p+k)->cost <<setprecision(0)
  512.                                         <<setw(10)<<(p+k)->qty <<"RM"<<setw(10)<<setprecision(2)
  513.                                         <<(p+k)->s_price <<endl;
  514.                 }
  515.         cout<<right<<setfill('_')<<setw(70)<<" "<<setfill(' ')<<endl;
  516.         delete []p;
  517.         p_ause;
  518.        
  519. }

  520. void del()
  521. {
  522.         string cmd;
  523.         int j;
  524.         string name;
  525.        
  526.         system("CLS");//Clear the previous output screen
  527.         cout<<endl<<endl;
  528.         cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
  529.         cout<<setw(30)<<""<<"====Delete  Data===="<<endl;
  530.         cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
  531. loop_del1:

  532.         cout<<setw(15)<<""<<"Enter the component name: ";
  533.         //****************************************
  534.         fflush(stdin);
  535.         getline(cin,name);
  536.        
  537.        
  538.         for(j=0;j<100;j++)
  539.         {
  540.                 //search the component name
  541.                 if(product[j].name==name)
  542.                 {
  543.                         cout<<setw(15)<<""<<"The \'"<<name<<"\' is FOUND."<<endl;
  544.                        
  545.                         loop_del3:
  546.                         cout<<"Are you sure you want to delete this component in data(Y/N): ";
  547.                         cin>>cmd;

  548.                         if (cmd=="y" || cmd=="Y")
  549.                         {
  550.                                 data_shifting(j);
  551.                                 break;
  552.                         }
  553.                         else if (cmd=="n" || cmd=="N")break;
  554.                         else
  555.                         {
  556.                                 cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
  557.                                 goto loop_del3;
  558.                         }
  559.                 }
  560.         }
  561.         if(j==100)
  562.         {
  563.                 cout<<setw(15)<<""<<"The \'"<<name<<"\' no found."<<endl;
  564.         }
  565.         loop_del2:
  566.         cout<<setw(15)<<""<<"Are you want to continue delete other data?(Y/N): ";
  567.         cin>>cmd;
  568.         if (cmd=="y" || cmd=="Y") goto loop_del1;
  569.         else if (cmd=="n" || cmd=="N")return;
  570.         else
  571.         {
  572.                 cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
  573.                 goto loop_del2;
  574.         }
  575. }

  576. void data_shifting(int s)
  577. {
  578.         for(int j=s;j<i;j++)
  579.         {
  580.                 //Shift the data after user deleted it
  581.                 product[j].code=product[j+1].code;
  582.                 product[j].cost=product[j+1].cost;
  583.                 product[j].name =product[j+1].name;
  584.                 product[j].qty =product[j+1].qty;
  585.                 product[j].s_price =product[j+1].s_price;
  586.         }::i--;//"i" decrease 1 after deleted 1 data
  587.         store_data();
  588.        
  589. }




  590. void store_data()
  591. {
  592.         string name;
  593.         ofstream outfile("Data.txt");
  594.         if(outfile.is_open())
  595.         {
  596.                 for(int k=0;k<i-1;k++)
  597.                 {
  598.                         //add '$' to every 'name' at the end
  599.                         //this can easily use the getline function to get the full name of the component from text.txt file
  600.                         name=product[k].name+"$";
  601.                         outfile<<left<<setw(25)<<name<<setw(10)<<fixed<<setw(10)<<product[k].code
  602.                                         <<setprecision(2)<<setw(10)<<product[k].cost <<setprecision(0)<<setw(10)<<product[k].qty <<setw(10)<<setprecision(2)
  603.                                         <<product[k].s_price <<endl;
  604.                 }
  605.         }
  606.         else cout<<"Error opening file"<<endl;//if opening file have any problem will perform this
  607.         outfile.close();
  608. }
复制代码
本帖最后由 johnng86 于 2-9-2013 03:47 PM 编辑

评分

参与人数 1人气 +5 收起 理由
devilfromheaven + 5 大神啊!!感谢!!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 21-9-2025 05:15 AM , Processed in 0.140160 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表