BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #18479同步于 2009/1/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

[代码美化向]这个代码如何修改

wks
2009/1/10镜像同步19 回复
#include<iostream> #include<wks/sql_client> using namespace std; using namespace wks; int main(int argc, char* argv[]) { string name; cout<<"Your name?"<<endl; cin>>name; if(name.length()>=0) { string sex; cout<<"Your sex?"<<endl; cin>>sex; if(sex=="male" || sex=="female") { int age; cout<<"Your age?"<<endl; cin>>age; if(age>0 && age<256) { string school; cout<<"What school are you in?"<<endl; cin>>school; if(school=="BUPT") { int stu_num; cout<<"Your student number:"<<endl; cin>>stu_num; if(0<stu_num && stu_num<999999) { int class_id; cout<<"Choose your course"<<endl; cout<<"(1) Gao deng shu xue"<<endl; cout<<"(2) Xian xing dai shu"<<endl; cout<<"(3) Da xue wu li"<<endl; cout<<"(4) Maozedong sixiang gailun"<<endl; cin>>class_id; if(1<=classid && classid<=4) { cout<<"Please wait..."<<endl; SqlConnection *connection = getConnection(); if(connection!=NULL) { bool isBound = connection->bind("localhost",2048); if(isBound) { bool isConnected = connection->connect("xuanke.bupt.edu.cn"); if(isConnected) { SqlQuery query = new query("INSERT (%d,%d) TO xuanke_table",stu_num,class_id); bool isSuccessful = connection.execute(query); if(isSuccessful) { delete query; bool isSuccessful2 = connection.close(); if(isSuccessful2) { cout<<"选课成功。欢迎使用。"<<endl; return 0; } else { cerr<<"Cannot close connection"<<endl; } } else { cerr<<"Cannot execute query"<<enld; return 1; } } else { cerr<<"Cannot connect to server"<<enld; return 1; } } else { cerr<<"Cannot bind connection"<<enld; return 1; } } else { cerr<<"Cannot create connection"<<enld; return 1; } } else { cerr<<"Invalid course number"<<endl; return 1; } } else { cout<<"Student number invalid"<<endl; return 1; } } else { cerr<<"We only serve the BeiYouRen's. SOrry"<<endl; return 1 } } else { cerr<"You lie! Tell the truth!"<<endl; return 1; } } else { cerr<<"Please enter either 'male' or 'female'"<<endl; return 1; } } else { cerr<<"Name too short"<<endl; return 1; } }
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
wks机器人#1 · 2009/1/10
// 这个也好看不到哪里去。 #include<iostream> #include<wks/sql_client> using namespace std; using namespace wks; int main(int argc, char* argv[]) { string name; cout<<"Your name?"<<endl; cin>>name; if(name.length()==0) { cerr<<"Name too short"<<endl; return 1; } else { string sex; cout<<"Your sex?"<<endl; cin>>sex; if(sex!="male" && sex!="female") { cerr<<"Please enter either 'male' or 'female'"<<endl; return 1; } else { int age; cout<<"Your age?"<<endl; cin>>age; if(age<=0 || age>=256) { cerr<"You lie! Tell the truth!"<<endl; return 1; } else { string school; cout<<"What school are you in?"<<endl; cin>>school; if(school!="BUPT") { cerr<<"We only serve the BeiYouRen's. SOrry"<<endl; return 1 } else { int stu_num; cout<<"Your student number:"<<endl; cin>>stu_num; if(stu_num<0 || stu_num>999999) { cout<<"Student number invalid"<<endl; return 1; } else { int class_id; cout<<"Choose your course"<<endl; cout<<"(1) Gao deng shu xue"<<endl; cout<<"(2) Xian xing dai shu"<<endl; cout<<"(3) Da xue wu li"<<endl; cout<<"(4) Maozedong sixiang gailun"<<endl; cin>>class_id; if(classid< && classid>4) { cerr<<"Invalid course number"<<endl; return 1; } else { cout<<"Please wait..."<<endl; SqlConnection *connection = getConnection(); if(connection==NULL) { cerr<<"Cannot create connection"<<enld; return 1; } else { bool isBound = connection->bind("localhost",2048); if(!isBound) { cerr<<"Cannot bind connection"<<enld; return 1; } else { bool isConnected = connection->connect("xuanke.bupt.edu.cn"); if(!isConnected) { cerr<<"Cannot connect to server"<<enld; return 1; } else { SqlQuery query = new query("INSERT (%d,%d) TO xuanke_table",stu_num,class_id); bool isSuccessful = connection.execute(query); if(!isSuccessful) { cerr<<"Cannot execute query"<<enld; return 1; } else { delete query; bool isSuccessful2 = connection.close(); if(!isSuccessful2) { cerr<<"Cannot close connection"<<endl; return 1; } else { cout<<"选课成功。欢迎使用。"<<endl; return 0; } } } } } } } } } } } }
kopol机器人#2 · 2009/1/10
这种方式去掉else应该好看些 【 在 wks 的大作中提到: 】 : // 这个也好看不到哪里去。 : #include<iostream> : #include<wks/sql_client> : ...................
ericyosho机器人#3 · 2009/1/10
分成小函数。 这样就美了。
Wavestone机器人#4 · 2009/1/10
你有些if都return了就不用else了
Railgun机器人#5 · 2009/1/10
这东西,与其追求代码的优化,还不如追求设计的优化完善 本来这种多步骤的操作过程,跟本不应该设计成这样子
ki机器人#6 · 2009/1/10
func1(){ subfunc1(); if (cond1 == false) return (false); subfunc2(); if (cond2 == false) return (false); do_a(); sub3(); if (cond3) return (false); return (0); } 【 在 wks 的大作中提到: 】 : #include<iostream> : #include<wks/sql_client> : using namespace std; : ...................
hbwh机器人#7 · 2009/1/10
这么多嵌套,看了真头晕,再怎么美化也不会漂亮,优化算法和代码吧
hg机器人#8 · 2009/1/10
【 在 wks 的大作中提到: 】 : #include<iostream> : #include<wks/sql_client> : using namespace std; : ................... if(){ } else{ } 好看些还是 if() { } else { } 好看些呢?
stupidboy机器人#9 · 2009/1/10
【 在 hg 的大作中提到: 】 : if(){ : } else{ : } : ................... 第二种吧。。。上次咱老师给咱一个XXX标准来着,里面有讲这个