819代码

  1 #include <iostream>
  2 using namespace std;
  3 class Student
  4 {public:
  5   void get_value()
  6    {cin>>num>>name>>sex;}
  7   void display( )
  8     {cout<<"num: "<<num<<endl;
  9      cout<<"name: "<<name<<endl;
 10      cout<<"sex: "<<sex<<endl;}
 11  private :
 12    int num;
 13    char name[10];
 14    char sex;
 15 };   
 16 
 17 class Student1: public Student
 18  {public:
 19    void get_value_1()
 20     {get_value();
 21      cin>>age>>addr;}
 22    void display_1()
 23    {   cout<<"age: "<<age<<endl;          //引用派生类的私有成员,正确。
 24        cout<<"address: "<<addr<<endl;}    //引用派生类的私有成员,正确。
 25   private:
 26        int age;
 27        char addr[30];
 28  };
 29  
 30 int  main()
 31  {Student1 stud1;
 32   stud1.get_value_1();
 33   stud1.display();
 34   stud1.display_1();
 35   system("pause");
 36   return 0;
 37 } 
 38 
 39 
 40 
 41 #include <stdio.h>
 42 #include <stdlib.h>
 43 #include <string.h>
 44 #define N 3
 45 #define LEN (Student*) malloc(sizeof(Student))
 46 /* 学生数据结构 */
 47 typedef struct node
 48 {
 49     char num[20];//
 50     char name[15];
 51     int score[N];
 52     int sum;
 53     double ave;
 54     struct node *next;
 55 } Student;
 56  
 57 /* 头指针 */
 58 Student *head = NULL;
 59 /* 临时指针 */
 60 Student *tmp = NULL;
 61 /* 课程名称 */
 62 char CLASSNAME[N][30] = {"大物", "高数", "c语言"};
 63 /* 命令开关 */
 64 int SWITCH[16] = {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 65 /* 函数声明 */
 66 int Menu();
 67 Student* Init();
 68 int CheckNUM(char*);
 69 int CheckName(char*);
 70 int CheakScore(int score);
 71 int Same_NUM(char*);
 72 void InputNodeInfo(Student*);
 73 void OutputNodeInfo(Student*);
 74 Student* SearchFrontNode(Student*);
 75 void DeleteNode(Student*);
 76 void InsertBefore();
 77 void InputList();
 78 Student* SearchID(char*);
 79 Student* SearchName(char*);
 80 void SearchDeleteNode();
 81 void OutList();
 82 void SearchPrintNode();
 83 void Compute();
 84 int CmpID(Student*, Student*, int);
 85 int CmpSum(Student*, Student*, int);
 86 int CmpScore(Student*, Student*, int);
 87 Student* SearchMaxNode(int (*cmp)(Student*, Student*, int), int);
 88 Student* Sort(int (*cmp)(Student*, Student*, int), int);
 89 void OutputToFile(FILE*, Student*, int);
 90 void InsertAfter(Student*);
 91 void SaveToFile();
 92 void LoadFile();
 93 void CopyFile();
 94 void InsertToFile();
 95 void FreeList(Student* p);
 96 void Stat();
 97 void Quit();
 98  
 99 /* 主函数 */
100 int main()
101 {
102     int n;
103  
104     while (1)
105     {
106         n = Menu();
107         {
108             if (n == 1 || n == 15 || SWITCH[1])
109             {
110                 switch (n)
111                 {
112                         /* 执行初始化 */
113                     case 1:
114                         head = Init();
115                         printf("LOOK...初始化成功\n");
116                         break;
117                         /* 创建链表 ,输入学生信息*/
118                     case 2:
119                         InputList();
120                         break;
121                         /* 查找学号或姓名删除信息 */
122                     case 3:
123                         SearchDeleteNode();
124                         break;
125                         /* 输出全部学生信息 */
126                     case 4:
127                         system("cls");
128                         OutList();
129                         break;
130                         /* 按姓名查找学生信息*/
131                     case 5:
132                         SearchPrintNode();
133                         break;
134                         /* 保存到文件 */
135                     case 6:
136                         SaveToFile();
137                         break;
138                         /* 从文件中读取学生信息*/
139                     case 7:
140                         if (SWITCH[6])
141                         {
142                             head = Init();
143                             LoadFile();
144                         }
145                         else
146                         {
147                             printf("当前文件未保存\n");
148                         }
149                         break;
150                         /* 计算所有学生的总分和平均分 */
151                     case 8:
152                         Compute();
153                         SWITCH[8] = 1;
154                         printf("计算完毕\n");
155                         break;
156                         /* 插入一个学生信息到链表 */
157                     case 9:
158                         InsertBefore();
159                         SWITCH[6] = 0;
160                         SWITCH[8] = 0;
161                         break;
162                         /* 复制文件 */
163                     case 10:
164                         CopyFile();
165                         break;
166                         /* 排序,按总分排序并打印学生信息 */
167                     case 11:
168                         if (SWITCH[8])
169                         {
170                             head = Sort(CmpSum, 0);
171                             system("cls");
172                             OutList();
173                         }
174                         else
175                         {
176                             printf("请先计算总分!\n");
177                         }
178                         break;
179                         /* 尾部添加一个学生信息到文件中 */
180                     case 12:
181                         InsertToFile();
182                         SWITCH[6] = 0;
183                         printf("尾部添加完毕!\n");
184                         break;
185                         /* 按学号搜索..学生信息*/
186                     case 13:
187                         if (SWITCH[8])
188                         {
189                             head = Sort(CmpID, 0);
190                             system("cls");
191                             OutList();
192                         }
193                         else
194                         {
195                             printf("请先计算总分!\n");
196                         }
197                         break;
198                         /* 分类汇总 */
199                     case 14:
200                         system("cls");
201                         Stat();
202                         break;
203                         /* 结束 */
204                     case 15:
205                         Quit();
206                         break;
207                     default:
208                         printf("无效命令!\n");
209                         fflush(stdin);
210                 }
211                 system("pause");
212             }
213             else
214             {
215                 printf("你必须首先初始化!\n");
216                 system("pause");
217             }
218         }
219     }
220  
221     system("pause");
222     return 0;
223 }
224  
225 /* 菜单 */
226 int Menu()
227 {
228     int n;
229     system("cls");
230     fflush(stdin);
231     printf("*********************************************************************\n");
232     printf("*********************************************************************\n");
233     printf("【01】 初始化........\n");
234     printf("【02】 输入学生信息\n");
235     printf("【03】 查找学号或姓名删除信息\n");
236     printf("【04】 输出全部学生信息\n");
237     printf("【05】 按姓名查找学生信息\n");
238     printf("【06】 保存到文件\n");
239     printf("【07】 从文件中读取学生信息\n");
240     printf("【08】 计算所有学生的总分和平均分\n");
241     printf("【09】 插入一个学生信息到链表中\n");
242     printf("【10】 复制文件\n");
243     printf("【11】 按总分排序并打印学生信息\n");
244     printf("【12】 尾部添加一个学生信息到文件中\n");
245     printf("【13】 按学号搜索..学生信息\n");
246     printf("【14】 分类汇总\n");
247     printf("【15】 退出\n");
248     printf("********************************************************************\n");
249     printf("请输入命令编号: ");
250     scanf("%d", &n);
251     return n;
252 }
253  
254 /* 初始化 */
255 Student* Init()
256 {
257     int i;
258     Student *head;
259     head = LEN;
260     head->next = NULL;
261  
262     /* 命令开关初始化 */
263     for (i = 1; i < 16; i++)
264     {
265         SWITCH[i] = 0;
266     }
267  
268     SWITCH[1] = 1;
269     SWITCH[6] = 1;
270     return head;
271 }
272  
273 /* 检查学号 */
274 int CheckNUM(char* s)
275 {
276     int i;
277  
278     if (strlen(s) == 0 || strlen(s) > 10) return 0;
279  
280     for (i = 0; i < strlen(s); i++)
281     {
282         if (s[i] < '0' || s[i] > '9') return 0;
283     }
284  
285     return 1;
286 }
287  
288 /* 检查姓名 */
289 int CheckName(char* s)
290 {
291     int i;
292  
293     if (strlen(s) == 0 || strlen(s) > 15) return 0;
294  
295     for (i = 0; i < strlen(s); i++)
296     {
297         if (!(s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z')) return 0;
298     }
299  
300     return 1;
301 }
302  
303 /* 检查分数 */
304 int CheakScore(int score)
305 {
306     if (score > 100 || score <= 0) return 0;
307     return 1;
308 }
309  
310 /* 检查相同学号 */
311 int Same_NUM(char* s)
312 {
313     Student *p = head->next;
314     while(p != NULL)
315     {
316         if (strcmp(s, p->num) == 0) return 1;
317         p = p->next;
318     }
319     return 0;
320 }
321  
322 /* 给p指向的节点输入信息 */
323 void InputNodeInfo(Student* p)
324 {
325     fflush(stdin);
326  
327     /* 学号 */
328     printf("\n请输入学号: ");
329     do
330     {
331         gets(p->num);
332  
333         if (!CheckNUM(p->num))
334         {
335             printf("数据不标准,请重新输入学号: ");
336         }
337         else if (Same_NUM(p->num))
338         {
339             printf("检测到此学号存在,请重新输入: ");
340         }
341     }while (!(CheckNUM(p->num) && !Same_NUM(p->num)));
342  
343     /* 姓名 */
344     printf("请输入姓名: ");
345     do
346     {
347         gets(p->name);
348          if (!CheckName(p->name))
349         {
350             printf("数据不标准,请重新输入姓名: ");
351         }
352     }
353     while (!CheckName(p->name));
354  
355     /* 成绩 */
356     int i;
357     for (i = 0; i < N; i++)
358     {
359         printf("请输入 %s 成绩: ", CLASSNAME[i]);
360         do
361         {
362             fflush(stdin);
363             scanf("%d", &p->score[i]);
364  
365             if (!CheakScore(p->score[i]))
366             {
367                 printf("数据不标准,请重新输入 %s 成绩: ", CLASSNAME[i]);
368             }
369         }
370         while (!CheakScore(p->score[i]));
371     }
372  
373     /* 总分及平均分 */
374     p->sum = -1;
375     p->ave = -1;
376 }
377  
378 /* 输出p指向节点的信息 */
379 void OutputNodeInfo(Student* p)
380 {
381     int i;
382     printf("\n");
383     printf("姓名: %s\n", p->name);
384     printf("学号: %s\n", p->num);
385  
386     for (i = 0; i < N; i++)
387     {
388         printf("%s 成绩: %d\n", CLASSNAME[i], p->score[i]);
389     }
390  
391     /* 计算过才输出 */
392     if (SWITCH[8]) printf("总分: %d\n", p->sum);
393     if (SWITCH[8]) printf("平均分: %.2lf\n", p->ave);
394 }
395  
396 /* 返回r的前一个节点 */
397 Student* SearchFrontNode(Student* r)
398 {
399     Student *p = head;
400     while (p->next != r) p = p->next;
401     return p;
402 }
403  
404 /* 删除r指向的节点 */
405 void DeleteNode(Student* r)
406 {
407     Student *p = SearchFrontNode(r);
408     p->next = r->next;
409 }
410  
411 /* 头插法插入节点 */
412 void InsertBefore()
413 {
414     Student *s = LEN;
415     InputNodeInfo(s);
416     s->next = head->next;
417     head->next = s;
418 }
419  
420 /* 输入链表 */
421 void InputList()
422 {
423     int n;
424     printf("有多少个学生信息要输入? ");
425     scanf("%d", &n);
426  
427     while (n--)
428     {
429         InsertBefore();
430     }
431 }
432  
433 /* 按学号查找 */
434 Student* SearchID(char* num)
435 {
436     Student *p = head->next;
437  
438     while (p != NULL)
439     {
440         if (strcmp(p->num, num) == 0) break;
441         p = p->next;
442     }
443  
444     return p;
445 }
446  
447 /* 按姓名查找 */
448 Student* SearchName(char* name)
449 {
450     Student *p = head->next;
451  
452     while (p != NULL)
453     {
454         if (strcmp(p->name, name) == 0) break;
455         p = p->next;
456     }
457  
458     return p;
459 }
460  
461 /* 按学号或姓名查找删除节点 */
462 void SearchDeleteNode()
463 {
464     Student *p;
465     fflush(stdin);
466     char str[20];
467     char sure[20];
468  
469     /* 输入合法性判断 */
470     printf("请输入你要删除的学生的 姓名 或 学号: ");
471     do
472     {
473         gets(str);
474  
475         if (!(CheckNUM(str) || CheckName(str)))
476         {
477             printf("数据不标准,请重新输入姓名或学号: ");
478         }
479     }
480     while (!(CheckNUM(str) || CheckName(str)));
481  
482     /* 判断是姓名还是学号 */
483     if (str[0] >= '0' && str[0] <= '9')
484     {
485         p = SearchID(str);
486  
487         if (p == NULL)
488         {
489             printf("对不起,找不到这个学号\n");
490         }
491         else
492         {
493             OutputNodeInfo(p);
494             printf("确认删除? (输入\"y\"确认,任意键取消): ");
495             if (strcmp(gets(sure), "y") == 0)
496             {
497                 DeleteNode(p);
498                 printf("删除成功\n");
499                 SWITCH[6] = 0;
500             }
501             fflush(stdin);
502         }
503     }
504     else
505     {
506         p = SearchName(str);
507  
508         if (p == NULL)
509         {
510             printf("对不起,找不到这个姓名\n");
511         }
512         else
513         {
514             OutputNodeInfo(p);
515             printf("确认删除? (输入\"y\"确认,任意键取消): ");
516             if (strcmp(gets(sure), "y") == 0)
517             {
518                 DeleteNode(p);
519                 printf("删除成功!\n");
520                 SWITCH[6] = 0;
521              }
522             fflush(stdin);
523         }
524     }
525 }
526  
527 /* 输出链表 */
528 void OutList()
529 {
530     Student *p = head->next;
531  
532     /* 空表处理 */
533     if (p == NULL)
534     {
535         printf("暂无学生信息!\n");
536     }
537  
538     while (p != NULL)
539     {
540         OutputNodeInfo(p);
541         p = p->next;
542     }
543 }
544  
545 /* 按姓名查找记录并打印 */
546 void SearchPrintNode()
547 {
548     Student *p = head->next;
549     int ok = 1; 
550     char name[20];
551     fflush(stdin);
552  
553     /* 姓名合法性判断 */
554     printf("请输入你要查找的学生姓名: ");
555     do
556     {
557         gets(name);
558  
559         if (!CheckName(name))
560         {
561             printf("数据不标准,请重新输入姓名: ");
562         }
563     }
564     while (!CheckName(name));
565  
566     /* 按姓名查找节点 */
567     while (p != NULL)
568     {
569         if (strcmp(p->name, name) == 0)
570         {
571             ok = 0;
572             OutputNodeInfo(p);
573         }
574         p = p->next;
575     }
576  
577     if (ok)
578     {
579         printf("对不起,找不到这个姓名\n");
580     }
581 }
582  
583 /* 计算总分和均分 */
584 void Compute()
585 {
586     int i;
587     Student *p = head->next;
588  
589     while (p != NULL)
590     {
591         int sum = 0;
592  
593         for (i = 0; i < N; i++)
594         {
595             sum += p->score[i];
596         }
597  
598         p->sum = sum;
599         p->ave = sum * 1.0 /N;
600         p = p->next;
601     }
602 }
603  
604 /* 比较学号 */
605 int CmpID(Student* a, Student* b, int k)
606 {
607     return strcmp(a->num, b->num);
608 }
609  
610 /* 比较总分 */
611 int CmpSum(Student* a, Student* b, int k)
612 {
613     return b->sum - a->sum;
614 }
615  
616 /* 比较各科分数 */
617 int CmpScore(Student* a, Student* b, int k)
618 {
619     return b->score[k] - a->score[k];
620 }
621  
622 /* 选择最大元素 */
623 Student* SearchMaxNode(int (*cmp)(Student* a, Student* b, int k), int k)
624 {
625     Student *p = head->next;
626     Student *max = p;
627  
628     while (p != NULL)
629     {
630         if (cmp(p, max, k) < 0)
631         {
632             max = p;
633         }
634         p = p->next;
635     }
636  
637     return max;
638 }
639  
640 /* 排序 */
641 Student* Sort(int (*cmp)(Student* a, Student* b, int k), int k)
642 {
643     Student *newhead = LEN;
644     Student *p = newhead;
645     Student *max;
646  
647     while (head->next != NULL)
648     {
649         max = SearchMaxNode(cmp, k);
650         p->next = max;
651         DeleteNode(max);
652         p = p->next;
653     }
654  
655     /* 表尾处理 */
656     p->next = NULL;
657     return newhead;
658 }
659  
660 
661 /* 将s插入链表尾部 */
662 void InsertAfter(Student* s)
663 
664 {
665     Student *p = head;
666  
667     while (p->next != NULL) p = p->next;
668  
669     s->next = NULL;
670     p->next = s;
671 }
672  
673 /* 保存到文件 */
674 void SaveToFile()
675 {    
676     /* 处理尾部添加表尾情况 */
677     if (SWITCH[12])
678     {
679         InsertAfter(tmp);
680     }
681  
682     FILE *fp;
683     int i;
684     Student *p; 
685     char file[20];
686     fflush(stdin);
687     printf("请输入要保存的文件名: ");
688     gets(file);
689  
690     if ((fp = fopen(file, "wt")) == NULL)
691     {
692         printf("写文件错误.......!\n");
693         return;
694     }
695    for(p = head->next;p!=NULL;p=p->next)
696      fprintf(fp,"%s %s \n",p->name,p->num);
697        for(i=0;i<2;i++)
698         {
699          fprintf(fp,"%d\n",p->score[i]);
700          }  
701     printf("文件保存成功!\n");
702     fclose(fp);
703     SWITCH[6] = 1;
704  
705     /* 处理尾部添加情况 */
706     if (SWITCH[12])
707     {
708         DeleteNode(tmp);
709          SWITCH[12] = 0;
710     }
711 }
712  
713 /* 从文件中读入记录 */
714 void LoadFile()
715 {
716     int i;
717     FILE *fp;
718     char file[20];
719     fflush(stdin);
720     printf("请输入文件名: ");
721     gets(file);
722  
723     if ((fp = fopen(file, "rt")) == NULL)
724     {
725         printf("对不起,无法打开文件!\n");
726         return;
727     }
728  
729     /* 文件未结束时读入数据 */
730     while (!feof(fp))
731     {
732         Student *s = LEN;
733         fscanf(fp, "%s", s->name);
734         fscanf(fp, "%s", s->num);
735  
736         for (i = 0; i < N; i++)
737         {
738             fscanf(fp, "%d", &s->score[i]);
739         }
740  
741         s->next = head->next;
742         head->next = s;
743     }
744  
745     printf("文件读取成功!\n");
746     fclose(fp);
747 }
748  
749 /* 复制文件 */
750 void CopyFile()
751 {
752     FILE *fp1, *fp2;
753     char ch, file1[20], file2[20];
754     fflush(stdin);
755     /* 读入源文件 */
756     printf("请输入源文件名: ");
757     gets(file1);
758  
759     if ((fp1 = fopen(file1, "rb")) == NULL)
760     {
761         printf("对不起,无法打开文件!\n");
762         return;
763     }
764  
765     /* 读入目标文件 */
766     printf("请输入目标文件名: ");
767     gets(file2);
768  
769     if ((strcmp(file1, file2) == 0) || ((fp2 = fopen(file2, "wb")) == NULL))
770     {
771         printf("对不起,无法创建文件!\n");
772         return;
773     }
774  
775     /* 逐个字符拷贝 */
776     while (!feof(fp1))
777     {
778         ch = fgetc(fp1);
779  
780         if (ch != EOF)
781             fputc(ch, fp2);
782     }
783  
784     fclose(fp1);
785     fclose(fp2);
786     printf("文件拷贝成功!\n");
787 }
788  
789 /* 尾部添加记录到文件中 */
790 void InsertToFile()
791 {
792     tmp = LEN;
793     InputNodeInfo(tmp);
794     SWITCH[12] = 1;
795 }
796  
797 /* 分类统计 */
798 void Stat()
799 {
800     int i, j, n = 0;
801     int sum[N] = {0};
802     Student *p = head->next;
803  
804     if (p == NULL)
805     {
806         printf("暂无学生信息,无法统计\n");
807         return;
808     }
809  
810     /* 统计各科总分 */
811     while (p != NULL)
812     {
813         /* 记录学生总数 */
814         n++;
815  
816         for (i = 0; i < N; i++)
817         {
818             sum[i] += p->score[i];
819         }
820  
821         p = p->next;
822     }
823  
824     /* 各科分别输出 */
825     for (i = 0; i < N; i++)
826     {
827         printf("%s 总均分: %.2lf\n", CLASSNAME[i], sum[i] * 1.0 / n);
828         head = Sort(CmpScore, i);
829         j = 0;
830         p = head->next;
831  
832         while (p != NULL)
833         {
834             j++;
835             printf("第%d名 %s %d\n", j, p->name, p->score[i]);
836             p = p->next;
837         }
838  
839         printf("\n");
840     }
841 }
842  
843 /* 释放链表 */
844 void FreeList(Student* p)
845 {
846     if (p->next != NULL)
847     {
848         FreeList(p->next);
849     }
850     free(p);
851 }
852  
853 /* 退出 */
854 void Quit()
855 {
856     if (!SWITCH[6])
857     {
858         printf("请先保存文件!\n");
859         return;
860     }
861     if (head != NULL)
862     {
863         FreeList(head);
864     }    
865     exit(0);
866 }
867 
868 
869 #include <iostream>        //用VC++时改为∶ #include <iostream.h>
870 using namespace std;      //用VC++时为取消此行
871 class Complex
872  {public:
873    Complex(){real=0;imag=0;}
874    Complex(double r,double i){real=r;imag=i;}
875    Complex operator+(Complex &c2);
876    Complex operator+(int &i);
877    friend Complex operator+(int&,Complex &);
878    void display();
879   private:
880    double real;
881    double imag;
882  };
883  
884 Complex Complex::operator+(Complex &c)
885 {return Complex(real+c.real,imag+c.imag);}
886  
887 Complex Complex::operator+(int &i)
888 {return Complex(real+i,imag);}
889 
890 void Complex::display()
891 {cout<<"("<<real<<","<<imag<<"i)"<<endl;}
892 
893 Complex operator+(int &i,Complex &c)
894 {return Complex(i+c.real,c.imag);}
895 
896 int main()
897 {Complex c1(3,4),c2(5,-10),c3;
898  int i=5;
899  c3=c1+c2;
900  cout<<"c1+c2=";
901  c3.display();
902  c3=i+c1;
903  cout<<"i+c1=";
904  c3.display();
905  c3=c1+i;
906  cout<<"c1+i=";
907  c3.display();
908  system("pause");
909  return 0;
910 }
911 
912 
913 #include <iostream>
914 using namespace std;
915 class Complex
916  {public:
917    Complex(){real=0;imag=0;}
918    Complex(double r,double i){real=r;imag=i;}
919    double get_real();
920    double get_imag();
921    void display();
922   private:
923    double real;
924    double imag;
925  };
926  
927 double Complex::get_real()
928 {return real;}
929 
930 double Complex::get_imag()
931 {return imag;}
932 
933 void Complex::display()
934 {cout<<"("<<real<<","<<imag<<"i)"<<endl;}
935 
936 Complex operator + (Complex &c1,Complex &c2)
937 {
938  return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
939 }
940 
941 int main()
942 {Complex c1(3,4),c2(5,-10),c3;
943  c3=c1+c2;
944  cout<<"c3=";
945  c3.display();
946  system("pause");
947  return 0;
948 }
949 
950 #include <iostream>
951 using namespace std;
952 class Complex
953  {public:
954    Complex(){real=0;imag=0;}
955    Complex(double r){real=r;imag=0;}
956    Complex(double r,double i){real=r;imag=i;}
957    operator double(){return real;}
958    void display();
959   private:
960    double real;
961    double imag;
962  };
963    
964 void Complex::display()
965 {cout<<"("<<real<<", "<<imag<<")"<<endl;}
966 
967 int main()
968 {Complex c1(3,4),c2;
969  double d1;
970  d1=2.5+c1;
971  cout<<"d1="<<d1<<endl;
972  c2=Complex(d1);
973  cout<<"c2=";
974  c2.display();
975  system("pause");
976  return 0;
977 }

 

posted @ 2013-08-19 21:11  herizai007  阅读(516)  评论(0编辑  收藏  举报