Assignment 6

1. 分析如下程序的结果(数组与指针的关系)

#include<iostream>

using namespace std;

void main()

{

    char s1[5]={'m', 'n', 'p', 'q','\0'};

    char *ps=s1;

   cout<<s1<<'\n';      //mnpq

    cout<<s1+2<<'\n';        //pq输出地址之后的数字统统输出

    cout<<*s1<<*(s1+1)<<s1[2]<<endl;     //mnp

    cout<<*ps<<*(ps+1)<<ps[2]<<endl;     //mnp

    cout<<&ps<<endl;   //指针变量的地址;  0012FF74

}2.  输入3个字符串,按由小到大顺序输出。

(1)用字符数组方法

程序代码:

#include <iostream>

#include <string>

using namespace std;

void swap(char* pointer_1 ,char* pointer_2)

{

       char p[20];

       strcpy(p,pointer_1);

       strcpy(pointer_1,pointer_2);

       strcpy(pointer_2,p);

}

void compare(char p1[],char p2[],char p3[])

{

       if (strcmp(p1,p2)>0)

       {

             swap(p1,p2);

       }

       if (strcmp(p1,p3)>0)

       {

             swap(p1,p3);

       }

       if (strcmp(p2,p3)>0)

       {

             swap(p2,p3);

       }

}

int main(int argc,char *argv[])

{

       char ch1[20],ch2[20],ch3[20];

       cout << "请输入三个字符串:" << endl;

       cin >> ch1 >> ch2>> ch3;

       compare(ch1,ch2,ch3);

       cout << ch1 << endl;

       cout << ch2 << endl;

       cout << ch3 << endl;

       return 0;

}

(2)用string方法

非指针形式:

#include <iostream>

#include <string>

using namespace std;

int main(int argc,char *argv[])

{

       string str1,str2,str3;

       cout << "请输入三个字符串:" << endl;

       cin >> str1 >> str2>> str3 ;

       if (str2 > str3)

       {

             string temp;

             temp = str2;

             str2 = str3;

             str3 = temp;

       }

       if (str1>str3)

       {

             cout << str2 << endl;

             cout << str3 << endl;

             cout << str1 << endl;

       }

       else if (str1< str2)

       {

             cout << str1 << endl;

             cout << str2 << endl;

             cout << str3 << endl;

       }

       else

       {

             cout << str2 << endl;

             cout << str1 << endl;

             cout << str3 << endl;

       }

       return 0;

}

指针形式:

#include <iostream>

#include <string>

using namespace std;

void change(string &str1,string &str2)

{

       string st;

       st = str1;

       str1 = str2;

       str2 = st;

}

int main(int argc,char *argv[])

{

       string str1 = " ";

       string str2 = " ";

       string str3 = " ";

       char* p1 = &str1[0],*p2 =&str2[0],*p3 = &str3[0];

       cout << "请输入三个字符串: "<< endl;

       cin >> str1 >> str2>> str3;

       if (str1>str2)

       {

             change(str1,str2);

       }

       if (str2>str3)

       {

             change(str2,str3);

       }

       if (str1>str3)

       {

             change(str1,str3);

       }

       cout << endl;

       cout << "Now,the order is:" << endl;

       cout << str1 << endl<< str2 << endl << str3 << endl;

       return 0;

}

3. 编写一程序,输入月份,输出该月的英文名。例如,输入“3”,则输出March,要求用指针数组实现。

#include <iostream>

#include <string>

using namespace std;

int main(int argc,char *argv[])

{

       int iMonth = 0;

       char* monthname[13]   = {"wrongmonth","January","February","March","April","May","June","July","August","September","October",

             "November","December"};

       cout << "请输入一个月份(1-12):" <<endl;

       while(iMonth!=100)

       {

             cin >> iMonth;

       if((iMonth>0)&&(iMonth<13))

       {

             cout << *(monthname+iMonth);

             cout << endl;

       }

       else

       {

             cout << "你的输入有误!!"<<endl;

       }

       }

       return 0;

}4. 有n个人围成一圈,顺序排号。从第1个人开始报数(从1~3),凡报到3的人退出圈子,问最后留下的原来排在第几号。

编程挑战:

#include <iostream>

using namespace std;

int main()

{

       int i = 0;

       int n = 0;

       int out = 0; //退出的人数

       int num = 0; //报数

       int a[1024] = {0}; //0表示退出圈子

       cout << "请输入一共有几个人:" << endl;

       cin >> n;

       for (i = 0; i < n; i++)

       {

             a[i] = 1;

       }

      

       i = 0;

       while (out != n-1)

       {

             if (a[i] == 1)

             {

                    num++;

             }

             if (num == 3)

             {

                    a[i] = 0;     //使得淘汰的位置变为0,之后的位置依次重新开始从1-3

                    num = 0;

                    out++;

             }

             i++;

             if (i == n)

             {

                    i = 0;

             }

       }

      

       for (i = 0; i < n; i++)

       {

             if (a[i] == 1)

             {

                    cout << "最后留下来的是:"<< i+1 << endl;

                    break;

             }

       }

       return 0;

}

5. 编写一个程序,读取支票上的一个数值金额,并输出其文字表示。例如,数字112.43应写为:

 

ONE HUNDRED TWELEVE and 43/100

 

提示:

n    只处理0到100之间的值。

 

n  创建一个字符型指针数组,包含所有的个位和十位数表示(40,50、60等),不要忘记11~19之间的数字!

 

n 使用%运算符将各个数字分离出来。

 

年对于大于100的数值,处理方式类似。

程序代码:

#include<iostream>

using namespace std;

int main(int argc,char* argv[])

{

       double iMoney = 0,leap = 0;

       cout << " 请输入一个(0~100)之间的数值:" <<endl;

       cin >> iMoney;

       char* p[30] ={"zero","one","two","three","four","five","six","seven","eight","nine","ten",

             "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",

             "eighteen","nineteen","twenty","thirty","forty","fifty","sixty","seventy",

             "eighty","ninety","hundred"};

       int iUnit = 0, iTens = 0, iHundred =0;

       int temp = 0;

       temp = iMoney;

       leap = iMoney -  temp;

       if (temp >= 20)

       {

             iUnit = temp % 10;

             iTens = (temp - iUnit) / 10;

             for (int i = 2;i < 10;i ++)

             {

                    if (iTens == i)

                    {

                           cout << *(p+i+18) << " ";

                    }

             }

             for (int j = 0;j < 10;j ++)

             {

                    if (iUnit == j)

                    {

                           cout << *(p+j) << " ";

                    }

             }

       }

       if (temp > 0 && temp <20)

       {

             for (int i = 0; i < 20;i ++)

             {

                    if (temp == i)

                    {

                           cout << *(p + i) << " " ;

                    }

             }

       }

       if (leap != 0)

       {

             cout << "and " << leap * 100 << "/100"<< endl;

       }

       return 0;

}


6.编写一个程序,把两个字符型链表对象链接成一个链表。

该程序应该包含函数concatenate,

它以对两个链表对象的引用为参数,

并将第2个链表链接到第一个链表上。

#include "stdio.h"

#include <stdlib.h>

 

enum Status{OK,ERROR};

typedef char ElemType;

 

typedef struct Lnode

{

 ElemType data;///数据域

 Lnode *next;///指针域

}LNode,*LinkList;

 

 

Status CreatLink(LinkList &L,int n)///创建链表

{

 LinkList head;

 LinkList p;

 L = (LinkList)malloc(sizeof(LNode));///生成头结点

 if (L == NULL)

 {

  return ERROR;

 }

 L->next = NULL;

 

 head = L;

 

 for (int i = 0; i < n; ++i)

 {

  p = (LinkList)malloc(sizeof(LNode));///生成新结点,尾插法插入新结点

  if (p == NULL)

  {

   return ERROR;

  }

  printf("please input a char:\n");

  scanf("%c",&p->data);

  getchar();

  p->next = NULL;

  head->next = p;

  head = head->next;

 }

 

 return OK;

}

 

Status DestroyLink(LinkList &L)///销毁链表

{

 LinkList p = L->next;

 LinkList q;

 if (L == NULL)

 {

  return ERROR;

 }

 

 while (p)

 {

  q = p->next;

  free(p);

  p = q;

 }

 

 free(L);

 L = NULL;

 

 return OK;

}

 

 

Status concatenate(LinkList &La,LinkList &Lb,LinkList &Lc)///将La和Lb连接到La上并释放Lb的头结点

{

 LinkList Pc = Lc = La;

 LinkList Pa = La->next;

 LinkList Pb = Lb->next;

 

 while (Pc->next)

 {

  //Pc->next = Pa;

  Pc = Pc->next;

  //Pa = Pa->next;

 

 }

 

 while (Pb)

 {

  Pc->next = Pb;

  Pb = Pb->next;

  Pc = Pc->next;

 }

 

 free(Pb);

 

 return OK;

}

 

Status PrintLink(LinkList L)///输出链表

{

 if (L == NULL)

 {

  return ERROR;

 }

 LinkList p = L->next;

 while (p)

 {

  printf("%c ",p->data);

  p = p->next;

 }

 

 printf("\n");

 

 return OK;

}

 

 

int main()

{

 LinkList la;

 LinkList lb;

 LinkList lc;

 CreatLink(la,3);///创建链表1

 PrintLink(la);

 printf("------------------------------------\n");

 CreatLink(lb,2);///创建链表2

 PrintLink(lb);

 printf("------------------------------------\n");

 concatenate(la,lb,lc);///连接后的链表

 PrintLink(lc);

 DestroyLink(lc);///销毁

 return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值