char big1[1000], big2[1000],big3[1000]
用scanf("%s")或者gets读入数据,按照按右对齐进行加运算结果存入big3,最简单的办法是逆向big1、big2然后按位加结果保存到big3,最后逆向big后就是结果,形象点举例:
如果输入的big1是:123456789999999999
程序逆向排序为:999999999987654321
对big2同样的处理,然后就可以按位计算合计存入big3
整个程序,关键的就是两个函数,第一个是计算求和的:
void big_sum(char a[], char b[], char c[])
函数的功能是计算超大整数的加法:c=a+b
第二个是逆向字符串的函数:
void str_rev(char s[])
功能是把字符串s收尾颠倒,下面以C语言的语法描述两个函数算法,如果看不懂就代码就看注释。
void str_rev(char s[]){
int n=0,i=0
char t
while(s[n]) n++/*使得指针移动到字符串末尾的结束符*/
n--
while(i>n){t=s[i]s[i]=s[n]s[n]=ti++n--}/*首尾交换*/
}
加法函数的算法逻辑如下:
void big_sum(char a[], char b[], char c[]){
int ia=0,ib=0,ic=0,s=0
str_rev(a)/*逆向a*/
str_rev(b)/*逆向b*/
while(a[ia] &&b[ib]){
if (a[ia]) {s+=a[ia]-'0'ia++}/*如果a没有完,那么取出一位累加到s,移动到下一位*/
if (b[ib]) {s+=b[ib]-'0'ib++}/*如果b没有完,那么取出一位累加到s,移动到下一位*/
c[ic]='0' + s%10/*一位结果,转换为字符*/
s/=10/*保留进位*/
ic++
}
c[ic]='\0'/*给结果添加结束符*/
str_rev(a)/*还原a*/
str_rev(b)/*还原b*/
str_rev(c)/*还原c*/
}
最后就是主程序了:
main(){
char a[1000],b[1000],c[1000]
printf("a=")gets(a)
printf("b=")gets(b)
big_sum(a,b,c)
printf("c=")pets(c)
}
#include<stdio.h>#include<string.h>
#include<conio.h>
#include
"malloc.h"//添加这个,因为要用到malloc函数
typedef
struct
Lnode
{
int
cttr
struct
Lnode
*next
}Lnode
typedef
struct
Lqueue
{
struct
Lnode
*front
struct
Lnode
*rear
}Lqueue,*LinkedQ
LinkedQ
init()
{
LinkedQ
Q=(LinkedQ)malloc(sizeof(Lqueue))
struct
Lnode
*head=(Lnode*)malloc(sizeof(Lnode))
head->next=NULL
Q->front=head
Q->rear=head
return
Q
}
int
empty(LinkedQ
Q)
{
if(Q->front==Q->rear)
{
return
1
}
else
{
return
0
}
}
void
push(LinkedQ
Q,int
ttr)
{
Lnode
*node
node=(Lnode*)malloc(sizeof(Lnode))
if(node==NULL)
{
printf("error!\n")
}
else
{
node->cttr=ttr
node->next=NULL
Q->rear->next=node
Q->rear=node
}
}
void
pop(LinkedQ
Q)
{
Lnode
*node
if(Q->front==Q->rear)
{
printf("error!\n")
}
else
{
node=Q->front->next
Q->front->next=node->next
if(node==Q->rear)
{
Q->rear=Q->front
}
free(node)
}
}
void
Qprint(LinkedQ
Q)
{
Lnode
*node=Q->front->next//-------------这里改过了
printf("data:
")
while(node!=NULL)
{
printf("%d
",node->cttr)
node=node->next
}
}
void
main()
{
LinkedQ
Q
int
ic//去掉没用的ia
scanf("%d",&ic)
while(ic!=0)
{
if(ic==-1)
Q=init()
else
if(ic%2==0)
{
push(Q,ic)
Qprint(Q)
}
else
if(ic%2!=0)
{
pop(Q)
Qprint(Q)
}
scanf("%d",&ic)
}
}
如上面是一个队列的问题,这是一个具体应用。
c语言可以用在很多方面,总的来时就是编写代码,写软件!