查看: 1184|回复: 3
|
C++高手帮忙下
[复制链接]
|
|
我的code基本上是可以work了得
但是我要加array去,可以算几个人的BMI在同一时间,和可以选择measure unit (meter,inches)
但是这两个东西我写来写去弄不出来,
谁可以帮帮忙? TT- #include <iostream>
- using namespace std;
- void displayMenu()
- {
- cout << "*-------------------------------------*" << endl;
- cout << "| Golden Years Financial Inc. |" << endl;
- cout << "|=====================================|" << endl;
- cout << "| Select: |" << endl;
- cout << "| 1 => Display Data |" << endl;
- cout << "| 2 => Insert Data |" << endl;
- cout << "|-------------------------------------|" << endl;
- cout << "| 3 => Calculate Standard BMI |" << endl;
- cout << "| 4 => Clear Data |" << endl;
- cout << "|-------------------------------------|" << endl;
- cout << "| Q => Quit |" << endl;
- cout << "*-------------------------------------*" << endl;
- cout << endl;
- cout << "Choice: ";
- }
- void DisplayData( string &name, float &weight, float &height,float &bmi )
- {
- cout << endl;
- cout << "Name = " <<name<< endl;
- cout << "Weight = " <<weight<< endl;
- cout << "Height = " <<height<< endl;
- cout << endl;
- }
- void InsertData( string &name, float &weight, float &height,float &bmi )
- {
- cout << endl;
- cout<<"Please Enter Your Name\n";
- cin >>name;
- cout <<"Enter your weight in Kilograms (KG) : ";
- cin >> weight;
- cout <<"\nEnter your height in Meters (M) : ";
- cin >> height;
-
- cout<<"You are now able to calculate your BMI."<<endl;
- }
- void Calculate( string &name, float &weight, float &height,float &bmi )
- {
-
- bmi = weight/(height*height);
- cout<<"Your BMI is: "<<bmi<<endl;
- if (bmi <= 18.5)
- cout << "You are under weight."<<endl;
- else if ((bmi > 18.5 ) && (bmi < 25))
- cout << "Your weight is in the normal range."<<endl;
- else if (bmi >= 25)
- cout << "You are overweight."<<endl;
-
-
-
-
-
- }
- void setPrecision ()
- {
- cout.setf(ios::fixed);
- cout.setf(ios::showpoint);
- cout.precision(1);
- }
- void DebugCalculator(string name, float weight, float height)
- {
- if(name == "null" && weight==0 && height ==0 )
- cout<<"Please Insert Your Data Before You Calculate Your BMI Or Your Data is Invalid."<<endl;
- }
- int main()
- {
- string name = "null";
- float weight = 0 ;
- float height = 0;
- char choice;
- bool invalid = true;
-
- do
- {
- float bmi;
- {
- displayMenu();
- cin >> choice;
- choice = toupper(choice);
- switch (choice)
- {
- case '1' : DisplayData(name, weight, height, bmi);
- break;
- case '2' : InsertData( name, weight, height, bmi);
- break;
- case '3' : DebugCalculator(name,weight,height);
- setPrecision();
- Calculate( name, weight, height, bmi);
- break;
- case '4' : DisplayData(name="null", weight=0,height=0,bmi);
- cout << "Your record have been deleted, please insert your data again." << endl;
- break;
- case 'Q' : invalid = false;
- cout<<"Thanks for using"<<endl;
- break;
- default : cout << "Invalid selection, try again!" << endl;
-
- }
- }
- }while(invalid);
- cout << "Good bye and have a nice day!" << endl;
- cout << endl;
- system("PAUSE");
- return 0;
- }
复制代码 |
|
|
|
|
|
|
|
发表于 1-9-2013 12:51 PM
|
显示全部楼层
我以前assignment 写过类似的。。。。还是用pointer 把 data store 在 text file 里。。。。有空的话我就把它找出来给你。。。.gif) |
|
|
|
|
|
|
|

楼主 |
发表于 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
|
显示全部楼层
反正收着也没什么用,你就拿去自己慢慢研究吧.gif) - #include<iostream>
- #include <string>
- #include <cstdlib>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- int i=0;
- #define p_ause system("pause")
- //declare the "pause" system
- struct component
- {
- string code;
- string name;
- double cost;
- int qty;
- double s_price;
- }product[100];
- void enter_data(int,int);
- void main_menu();//show all the commands
- void check_dgt(string*,double&);//check value is digit number or not
- void data();
- void new_data();
- void exit();
- void edit();
- void disp_all();
- void del();
- void data_shifting(int);
- //Add Two new function.=)
- void disp_selection();
- void disp_1_data();
- void store_data();
- int main()
- {
- data();
-
- // disp_all();
- string c;
- int a=2;
- cout<<endl<<endl;
-
- //Print the title
- cout<<setw(20)<<" "<<setfill('=')<<setw(38)<<""<<setfill(' ')<<endl<<endl;
- cout<<setw(20)<<""<<"====Universiti Tunku abdul Rahman====="<<endl<<endl;
- cout<<setw(20)<<""<<"=========Programming Techniques=======\n\n";
- cout<<setw(20)<<" "<<setfill('=')<<setw(38)<<""<<setfill(' ')<<endl<<endl;
- while(1)
- {
- cout<<setw(15)<<""<<"\nAre You Want To Enter Command Mode?(Y/N): ";
- cin>>c;
- //ask user enter yes ot not
- if (c=="y" || c=="Y")main_menu();
- else if (c=="n" || c=="N") exit();
- else cout<<setw(20)<<""<<"\aPlease enter Y or N correctly!"<<endl;
-
- }
- return 0;
- }
- void main_menu()
- {
- //declare the variable
- double n;
- string cmd;
- //using whlie() statement to continue run the program
- while(1)
- {
- //print the menu
- system("CLS");//Clear the previous output screen
- cout<<endl<<endl;
- cout<<setw(15)<<" "<<setfill('_')<<setw(55)<<" "<<setfill(' ')<<endl<<endl;
- cout<<setw(15)<<""<<"The Command available is below:"<<endl;
- cout<<setw(15)<<" "<<setfill('_')<<setw(55)<<" "<<setfill(' ')<<endl<<endl;
- cout<<setw(15)<<""<<"(1) New( Add new item )"<<endl;
- cout<<setw(15)<<""<<"(2) Edit( Specify an etem and edit it )"<<endl;
- cout<<setw(15)<<""<<"(3) Delete( Specify an etem and delete it )"<<endl;
- cout<<setw(15)<<""<<"(4) Display( Display a specify an item and delete it )"<<endl;
- cout<<setw(15)<<""<<"(5) Exit( Quite the program )"<<endl;
- cout<<setw(15)<<" "<<setfill('_')<<setw(55)<<" "<<setfill(' ')<<endl<<endl;
- loop_m:
- cout<<setw(15)<<""<<"Select Your Command: ";
- cin>>cmd;
- ;
-
- //pass the memory address to chr
- check_dgt(&cmd,n);
- n=int(n);
- //check chr is digit or not, and assign the value to n
- if (n!=-1)
- {
- if(n==1) new_data();
- else if (n==2)edit();
- else if (n==3){del();}
- else if (n==4)disp_selection();
- else if (n==5)exit();
- }
- else goto loop_m;//go back to loo_m
-
- }
- }
- void check_dgt(string *s,double& n)
- {
- char c[20];
- char * cstr;
- for(int i=0;i<s->length();i++)
- {
- if(isdigit(s->at(i)) || s->at(i)=='.')
- {
- cstr = new char [s->size()+1];
- strcpy (cstr, s->c_str());
-
- n=atof(cstr);
-
-
- }
- else if(isalpha(s->at(i)))
- {
- //if 's' is alphabet, then return n=-1;
- cout<<setw(15)<<""<<"Please don't enter alphabet!"<<endl;
- n=-1;
- return;
- }
- else
- {
- cout<<setw(15)<<""<<"Please enter correctly command!"<<endl;
- n=-1;
- return;
- }
- }
- }
- void data()
- {
- string s;
- ifstream infile("Data.txt");
- //get all the datas from the texr.txt files
- if(infile.is_open())
- {
- while(!infile.eof())
- {
- //store the datas
- getline(infile,product[i].name,'$');
- infile>>product[i].code;
- infile>>product[i].cost;
- infile>>product[i].qty;
- infile>>product[i].s_price;
- getline(infile,s);
- i++;
- }
- }
- else
- cout<<"Error opening file!"<<endl;
- infile.close();
- }
- void new_data()
- {
- string c;
- int update=0;
- system("CLS");//Clear the previous output screen
- cout<<endl<<endl;
- cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
- cout<<setw(30)<<""<<"====Add New Data===="<<endl;
- cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
- while(i<100)
- {
- fflush(stdin);
- enter_data(update,i);
- i++;//incerment the i by 1 for next data
- loop_new2:
- cout<<setw(15)<<""<<"Are you want to continue add data?(Y/N): ";
- cin>>c;
- if (c=="y" || c=="Y")continue;
- else if (c=="n" || c=="N")return;
- else
- {
- cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
- goto loop_new2;
- }
- }
-
- }
- void exit()
- {
- system("cls");
- cout<<endl<<endl;
- cout<<setw(25)<<" "<<setfill('*')<<setw(34)<<" "<<setfill(' ')<<endl;
- cout<<setw(25)<<""<<"* Thanks For Using This Program *"<<endl;
- cout<<setw(25)<<" "<<setfill('*')<<setw(34)<<" "<<setfill(' ')<<endl;
- exit(1);
- }
- void edit()
- {
- double n;
- int j,choose,update;
- string name,type_edit,cmd,s;
- update=1;
-
- system("CLS");//Clear the previous output screen
- cout<<endl<<endl;
- cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
- cout<<setw(30)<<""<<"=====Edit Data====="<<endl;
- cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
- loop_edit1:
- cout<<setw(15)<<"Enter the component name: ";
- fflush(stdin);
- getline(cin,name,'\n');
- for(j=0;j<100;j++)
- {
- if(product[j].name==name)
- {
- cout<<setw(15)<<""<<"The \'"<<name<<"\' is FOUND."<<endl;
- cout<<setw(15)<<""<<"Component details:"<<endl;
- cout<<setfill('_')<<setw(72)<<" "<<setfill(' ')<<endl;
- //cout<<left<<setw(25)<<"Name"<<setw(10)<<"Code"<<setw(10)<<"Cost"<<setw(10)<<"Quantity"<<setw(10)<<"Selling Price"<<endl;
- cout<<left<<setw(25)<<"(1)Name"<<setw(10)<<"(2)Code"<<setw(10)<<"(3)Cost"<<setw(15)<<"(4)Quantity"
- <<setw(15)<<"(5)Selling Price"<<endl;
- cout<<setfill('_')<<setw(72)<<" "<<setfill(' ')<<endl;
- cout<<left<<setw(25)<<product[j].name<<setw(10)<<fixed<<setw(10)<<product[j].code
- <<setprecision(2)<<setw(10)<<product[j].cost <<setprecision(0)<<setw(15)<<product[j].qty <<setw(10)<<setprecision(2)
- <<product[j].s_price <<endl;
-
- cout<<setfill('_')<<setw(72)<<" "<<setfill(' ')<<endl;
-
-
- cout<<endl;
- while(1)
- {
- cout<<setw(15)<<"Which part of element you want edit?(6 for all part // 0 to exit): ";
-
- cin>>cmd;
- check_dgt(&cmd,n);
- choose=int(n);
-
-
- if(choose==1)
- {
- fflush(stdin);
- cout<<setw(15)<<""<<"Name:";
- getline(cin,type_edit,'\n');
- product[j].name =type_edit;
- }
- else if(choose==2)
- {
- fflush(stdin);
- cout<<setw(15)<<""<<"Code:";
- getline(cin,type_edit,'\n');
- product[j].code=type_edit;
- }
- else if(choose==3)
- {
- loop_cost:
- cout<<setw(15)<<""<<"Cost:";
- cin>>s;
- check_dgt(&s,n);
- if(n!=-1)
- product[j].cost=n;
- else
- goto loop_cost;
- }
- else if(choose==4)
- {
- loop_qty:
- cout<<setw(15)<<""<<"Quantity:";
- cin>>s;
- check_dgt(&s,n);
- if (n!=-1)
- product[j].qty =int(n);
- else
- goto loop_qty;
- }
- else if(choose==5)
- {
- loop_price:
- cout<<setw(15)<<""<<"Selling Price:";
- cin>>s;
- check_dgt(&s,n);
- if (n!=-1)
- product[j].s_price =n;
- else
- goto loop_price;
-
- }
- else if (choose==6)
- {
- fflush(stdin);
- enter_data(update,j);
-
- }
- else if(choose==0)goto outside;
- else cout<<"Please enter correctly!"<<endl;
- };
- }
-
- }
- outside:
- if(j==100)
- {
- cout<<setw(15)<<""<<"The \'"<<name<<"\' no found."<<endl;
- }
- loop_edit2:
- cout<<setw(15)<<""<<"Are you want to continue edit next element?(Y/N): ";
- cin>>cmd;
- if (cmd=="y" || cmd=="Y") goto loop_edit1;
- else if (cmd=="n" || cmd=="N")
- {
- store_data();
- return;
- }
- else
- {
- cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
- goto loop_edit2;
- }
- }
- void enter_data(int update,int j)
- {
- //store the data from user
- string code,name,s;
- double n;
- component* p=new component;
- cout<<"Number ("<<i<<"): "<<endl;
- cout<<setw(15)<<""<<"Code: ";
- getline(cin,code,'\n');
- cout<<setw(15)<<""<<"Name: ";
- getline(cin,name,'\n');
- loop_cost:
- cout<<setw(15)<<""<<"Cost: ";
- cin>>s;
- check_dgt(&s,n);
- if (n!=-1)
- {
- p->cost =n;
-
- }
- else
- goto loop_cost;
- loop_qty:
- cout<<setw(15)<<""<<"Quantity: ";
- cin>>s;
- check_dgt(&s,n);
- if (n!=-1)
- {
- p->qty =int(n);
-
- }
- else
- goto loop_qty;
- loop_price:
- cout<<setw(15)<<""<<"Selling Price: ";
- cin>>s;
- check_dgt(&s,n);
- if (n!=-1)
- {
- p->s_price =n;
-
- }
- else
- goto loop_price;
- //store the data
- p->name =name;
- p->code =code;
- if(update==0)
- {
- p->name +="$";
- ofstream fileout("Data.txt",ios::out|ios::ate|ios::app);
- if(fileout.is_open ())
- {
- fileout<<left<<setw(25)<<p->name<<setw(10)<<fixed<<setprecision(2)<<setw(10)<<p->code
- <<setw(10)<<p->cost<<setprecision(0)<<setw(10)<<p->qty<<setw(10)<<setprecision(2)
- <<p->s_price <<endl;
-
- fileout.close();
- }
- else cout<<"Error opening file"<<endl;
- }
- else if(update==1)
- {
- product[j].name =p->name;
- product[j].code =p->code ;
- product[j].cost =p->cost ;
- product[j].qty =p->qty ;
- product[j].s_price =p->s_price ;
- store_data();
- }
- delete p;
- }
- void disp_selection()
- {
- double n;
- string cmd;
- loop_dis0:
- system("CLS");//Clear the previous output screen
- cout<<endl<<endl;
- cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
- cout<<setw(30)<<""<<"====Display Datas==="<<endl;
- cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
- cout<<setw(15)<<""<<"1) Display One Data."<<endl;
- cout<<setw(15)<<""<<"2) Display All The Datas."<<endl;
- cout<<setw(15)<<""<<"3) Exit To Command Mode."<<endl;
- loop_dis1:
- cout<<setw(15)<<""<<"Select Your Command: ";
- cin>>cmd;
- cout<<endl;
- //pass the memory address to chr
- check_dgt(&cmd, n);
- //check chr is digit or not, and assign the value to n
- if (n!=-1)
- {
- if (n==1)
- {
- disp_1_data();
- }
- else if (n==2) disp_all();
- else if (n==3) return;
- else goto loop_dis1;//go back to loo_m
- }
- else goto loop_dis1;//go back to loo_m
- goto loop_dis0;
- //*************************************************************
- }
- void disp_1_data()
- {
- component* pp=product;
- string s,code,cost,price,qty;
- double n;
- int j,choose;
- string name,type_edit,c;
- //control all the output position
- cout.setf(ios::fixed);
- cout.setf(ios::left);
- loop_dis1:
- cout<<setw(15)<<"Enter the component name: ";
- fflush(stdin);
- getline(cin,name);
- ifstream filein("Data.txt");
- if(filein.is_open())
- {
- j=0;
- while(!filein.eof())
- {
- fflush(stdin);
- getline(filein,(pp+j)->name ,'$');
- filein>>(pp+j)->code;
- filein>>(pp+j)->cost;
- filein>>(pp+j)->qty;
- filein>>(pp+j)->s_price;
- if(name==((pp+j)->name.substr(0,name.length())))
- {
- cout<<setw(15)<<""<<"The \'"<<name<<"\' is FOUND."<<endl;
- cout<<setw(13)<<" "<<setfill('_')<<setw(30)<<" "<<setfill(' ')<<endl;
- cout<<setw(15)<<""<<"Component details:"<<endl;
- cout<<setw(13)<<" "<<setfill('_')<<setw(30)<<" "<<setfill(' ')<<endl;
- cout<<setw(15)<<""<<setw(15)<<"Code"<<setw(10)<<(pp+j)->code<<endl;
- cout<<setw(15)<<""<<setw(15)<<"Name"<<setw(10)<<(pp+j)->name<<endl;
- cout<<setw(15)<<""<<setw(15)<<"Cost"<<"RM"<<setprecision(2)<<setw(10)<<(pp+j)->cost<<endl;
- cout<<setw(15)<<""<<setw(15)<<"Quantity"<<setw(10)<<(pp+j)->qty<<endl;
- cout<<setw(15)<<""<<setw(15)<<"Selling"<<"RM"<<setprecision(2)<<setw(10)<<(pp+j)->s_price<<endl;
- cout<<setw(13)<<" "<<setfill('_')<<setw(30)<<" "<<setfill(' ')<<endl;
- cout<<endl<<endl;
- break;
- }
- getline(filein,c,'\n');
- j++;
- }
- }
- if(filein.eof())
- {
- cout<<setw(15)<<""<<"The \'"<<name<<"\' no found."<<endl;
- }
- loop_dis2:
- cout<<setw(15)<<""<<"Are you want to continue check other elements?(Y/N): ";
- cin>>c;
- if (c=="y" || c=="Y") goto loop_dis1;
- else if (c=="n" || c=="N")return;
- else
- {
- cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
- goto loop_dis2;
- }
- filein.close();
- }
- void disp_all()
- {
-
- int j=0;
- string s;
- component* p=new component[100];
- ifstream filein("Data.txt");
- if(filein.is_open())
- {
- while(!filein.eof())
- {
- getline(filein,(p+j)->name,'$');
- filein>>(p+j)->code ;
- filein>>(p+j)->cost ;
- filein>>(p+j)->qty ;
- filein>>(p+j)->s_price ;
- getline(filein,s,'\n');
- j++;
-
- }
- }
- else cout<<"Error file opening"<<endl;
- filein.close();
- j--;
- system("CLS");//Clear the previous output screen
- cout<<right<<setfill('_')<<setw(70)<<" "<<setfill(' ')<<endl;
- cout<<left<<setw(25)<<"Name"<<setw(10)<<"Code"<<setw(10)<<"Cost"<<setw(10)<<"Quantity"<<setw(10)<<"Selling Price"<<endl;
- cout<<right<<setfill('_')<<setw(70)<<" "<<setfill(' ')<<endl;
-
- for(int k=0;k<j;k++)
- {
- cout<<left<<setw(25)<<(p+k)->name <<setw(10)<<fixed<<setprecision(2)
- <<setw(10)<<(p+k)->code<<"RM" <<setw(10)<<(p+k)->cost <<setprecision(0)
- <<setw(10)<<(p+k)->qty <<"RM"<<setw(10)<<setprecision(2)
- <<(p+k)->s_price <<endl;
- }
- cout<<right<<setfill('_')<<setw(70)<<" "<<setfill(' ')<<endl;
- delete []p;
- p_ause;
-
- }
- void del()
- {
- string cmd;
- int j;
- string name;
-
- system("CLS");//Clear the previous output screen
- cout<<endl<<endl;
- cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
- cout<<setw(30)<<""<<"====Delete Data===="<<endl;
- cout<<setw(30)<<" "<<setfill('=')<<setw(21)<<" "<<setfill(' ')<<endl;
- loop_del1:
- cout<<setw(15)<<""<<"Enter the component name: ";
- //****************************************
- fflush(stdin);
- getline(cin,name);
-
-
- for(j=0;j<100;j++)
- {
- //search the component name
- if(product[j].name==name)
- {
- cout<<setw(15)<<""<<"The \'"<<name<<"\' is FOUND."<<endl;
-
- loop_del3:
- cout<<"Are you sure you want to delete this component in data(Y/N): ";
- cin>>cmd;
- if (cmd=="y" || cmd=="Y")
- {
- data_shifting(j);
- break;
- }
- else if (cmd=="n" || cmd=="N")break;
- else
- {
- cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
- goto loop_del3;
- }
- }
- }
- if(j==100)
- {
- cout<<setw(15)<<""<<"The \'"<<name<<"\' no found."<<endl;
- }
- loop_del2:
- cout<<setw(15)<<""<<"Are you want to continue delete other data?(Y/N): ";
- cin>>cmd;
- if (cmd=="y" || cmd=="Y") goto loop_del1;
- else if (cmd=="n" || cmd=="N")return;
- else
- {
- cout<<setw(15)<<""<<"\aPlease enter Y or N correctly!\n"<<endl;
- goto loop_del2;
- }
- }
- void data_shifting(int s)
- {
- for(int j=s;j<i;j++)
- {
- //Shift the data after user deleted it
- product[j].code=product[j+1].code;
- product[j].cost=product[j+1].cost;
- product[j].name =product[j+1].name;
- product[j].qty =product[j+1].qty;
- product[j].s_price =product[j+1].s_price;
- }::i--;//"i" decrease 1 after deleted 1 data
- store_data();
-
- }
- void store_data()
- {
- string name;
- ofstream outfile("Data.txt");
- if(outfile.is_open())
- {
- for(int k=0;k<i-1;k++)
- {
- //add '$' to every 'name' at the end
- //this can easily use the getline function to get the full name of the component from text.txt file
- name=product[k].name+"$";
- outfile<<left<<setw(25)<<name<<setw(10)<<fixed<<setw(10)<<product[k].code
- <<setprecision(2)<<setw(10)<<product[k].cost <<setprecision(0)<<setw(10)<<product[k].qty <<setw(10)<<setprecision(2)
- <<product[k].s_price <<endl;
- }
- }
- else cout<<"Error opening file"<<endl;//if opening file have any problem will perform this
- outfile.close();
- }
复制代码 本帖最后由 johnng86 于 2-9-2013 03:47 PM 编辑
|
评分
-
查看全部评分
|
|
|
|
|
|
| |
本周最热论坛帖子
|